Pixels.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Magick++ API: Working with Pixels </title>
  7. <link rel="stylesheet" href="magick.css" type="text/css" />
  8. </head>
  9. <body>
  10. <div class="doc-section">
  11. <h1 align="center">Magick::Pixels</h1>
  12. <p>The <i>Pixels</i> class provides efficient access to raw image
  13. pixels. Image pixels (of type <a href="https://imagemagick.org/Magick++/Quantum.html"><i>Quantum</i></a>)
  14. may be accessed directly via the <i>Image Pixel Cache</i>. The image
  15. pixel cache is a rectangular window (a view) into the actual image
  16. pixels (which may be in memory, memory-mapped from a disk file, or
  17. entirely on disk). Obtain existing image pixels via <i>get()</i>.
  18. Create a new pixel region using <i>set().</i> </p>
  19. <p>Depending on the capabilities of the operating system, and the
  20. relationship of the window to the image, the pixel cache may be a copy
  21. of the pixels in the selected window, or it may be the actual image
  22. pixels. In any case calling <i>sync()</i> insures that the base image
  23. is updated with the contents of the modified pixel cache. The method <i>decode()</i>supports
  24. copying foreign pixel data formats into the pixel cache according to
  25. the <i>QuantumTypes</i>. The method <i>encode()</i> supports copying
  26. the pixels in the cache to a foreign pixel representation according to
  27. the format specified by <i>QuantumTypes</i>. </p>
  28. <p>Setting a view using the Pixels class does not cause the number of
  29. references to the underlying image to be reduced to one. Therefore, in
  30. order to ensure that only the current generation of the image is
  31. modified, the Image's <a href="Image++.html#modifyImage">modifyImage()</a>
  32. method should be invoked to reduce the reference count on the underlying
  33. image to one. If this is not done, then it is possible for a previous
  34. generation of the image to be modified due to the use of reference
  35. counting when copying or constructing an Image. </p>
  36. <p>The <i>Quantum</i>* returned by the <i>set</i> and <i>get</i>
  37. methods, and the <i>void</i>* returned by the <i>indexes</i>
  38. method point to pixel data managed by the <i>Pixels</i> class. The <i>Pixels</i>
  39. class is responsible for releasing resources associated with the pixel
  40. view. This means that the pointer should never be passed to delete() or
  41. free(). </p>
  42. <p style="margin-bottom: 0cm;">The pixel view is a small image in which
  43. the pixels may be accessed, addressed, and updated, as shown in the
  44. following example, which produces an image similar to the one on the
  45. right (minus lines and text): </p>
  46. <p class="image"><img class="icon" src="Cache.png" name="Graphic1" align="bottom" width="254" border="0" /></p>
  47. <div class="viewport">
  48. #include &lt;Magick++.h>
  49. #include &lt;iostream>
  50. using namespace std;
  51. using namespace Magick;
  52. int main(int argc,char **argv)
  53. {
  54. InitializeMagick(*argv);
  55. // Create base image
  56. Image image(Geometry(254,218), "white");
  57. // Set the image type to TrueColor DirectClass representation.
  58. image.type(TrueColorType);
  59. // Ensure that there is only one reference to underlying image
  60. // If this is not done, then image pixels will not be modified.
  61. image.modifyImage();
  62. // Allocate pixel view
  63. Pixels view(image);
  64. // Set all pixels in region anchored at 38x36, with size 160x230 to green.
  65. size_t columns = 196; size_t rows = 162;
  66. Color green("green");
  67. Quantum *pixels = view.get(38,36,columns,rows);
  68. for ( ssize_t row = 0; row &lt; rows ; ++row )
  69. for ( ssize_t column = 0; column &lt; columns ; ++column )
  70. {
  71. *pixels++=QuantumRange*green.quantumRed();
  72. *pixels++=QuantumRange*green.quantumGreen();
  73. *pixels++=QuantumRange*green.quantumBlue();
  74. }
  75. // Save changes to image.
  76. view.sync();
  77. // Set all pixels in region anchored at 86x72, with size 108x67 to yellow.
  78. columns = 108; rows = 67;
  79. Color yellow("yellow");
  80. pixels = view.get(86,72,columns,rows);
  81. for ( ssize_t row = 0; row &lt; rows ; ++row )
  82. for ( ssize_t column = 0; column &lt; columns ; ++column )
  83. {
  84. *pixels++=QuantumRange*yellow.quantumRed();
  85. *pixels++=QuantumRange*yellow.quantumGreen();
  86. *pixels++=QuantumRange*yellow.quantumBlue();
  87. }
  88. view.sync();
  89. // Set pixel at position 108,94 to red
  90. Color red("red");
  91. pixels = view.get(108,94,1,1);
  92. *pixels++=QuantumRange*red.quantumRed();
  93. *pixels++=QuantumRange*red.quantumGreen();
  94. *pixels++=QuantumRange*red.quantumBlue();
  95. // Save changes to image.
  96. view.sync();
  97. image.write( "logo.png" );
  98. }
  99. </div>
  100. <p style="margin-bottom: 0cm;"><i>Pixels</i> supports the following
  101. methods: </p>
  102. <p align="center" style="margin-bottom: 0cm;"><b>Pixel Cache Methods</b></p>
  103. <table width="100%" border="1" cellpadding="2" cellspacing="2">
  104. <tbody>
  105. <tr>
  106. <td>
  107. <p align="center"><b>Method</b></p>
  108. </td>
  109. <td>
  110. <p align="center"><b>Returns</b></p>
  111. </td>
  112. <td>
  113. <p align="center"><b>Signature</b></p>
  114. </td>
  115. <td>
  116. <p align="center"><b>Description</b></p>
  117. </td>
  118. </tr>
  119. <tr>
  120. <td>
  121. <p align="center"><a name="get"></a><font size="2">get</font></p>
  122. </td>
  123. <td>
  124. <p><font size="2"><a href="https://imagemagick.org/Magick++/Quantum.html">Quantum</a>*</font></p>
  125. </td>
  126. <td>
  127. <p><font size="2">const ssize_t x_, const ssize_t y_, const size_t
  128. columns_, const size_t rows_</font></p>
  129. </td>
  130. <td>
  131. <p><font size="2">Transfers read-write pixels from the image to
  132. the pixel cache as defined by the specified rectangular region.
  133. Modified pixels may be subsequently transferred back to the image
  134. via <i>sync</i>. The value returned is intended for pixel access
  135. only. It should never be deallocated.</font></p>
  136. </td>
  137. </tr>
  138. <tr>
  139. <td>
  140. <p align="center"><a name="getConst"></a><font size="2">getConst</font></p>
  141. </td>
  142. <td>
  143. <p><font size="2">const <a href="https://imagemagick.org/Magick++/Quantum.html">Quantum</a>*</font></p>
  144. </td>
  145. <td>
  146. <p><font size="2">const ssize_t x_, const ssize_t y_, const size_t
  147. columns_, const size_t rows_</font></p>
  148. </td>
  149. <td>
  150. <p><font size="2">Transfers read-only pixels from the image to
  151. the pixel cache as defined by the specified rectangular region.</font></p>
  152. </td>
  153. </tr>
  154. <tr>
  155. <td>
  156. <p align="center"><a name="set"></a><font size="2">set</font></p>
  157. </td>
  158. <td>
  159. <p><font size="2"><a href="https://imagemagick.org/Magick++/Quantum.html">Quantum</a>*</font></p>
  160. </td>
  161. <td>
  162. <p><font size="2">const ssize_t x_, const ssize_t y_, const size_t
  163. columns_, const size_t rows_</font></p>
  164. </td>
  165. <td>
  166. <p><font size="2">Allocates a pixel cache region to store image
  167. pixels as defined by the region rectangle.  This area is
  168. subsequently transferred from the pixel cache to the image via <i>sync</i>.
  169. The value returned is intended for pixel access only. It should
  170. never be deallocated.</font></p>
  171. </td>
  172. </tr>
  173. <tr>
  174. <td>
  175. <p align="center"><a name="sync"></a><font size="2">sync</font></p>
  176. </td>
  177. <td>
  178. <p><font size="2">void</font></p>
  179. </td>
  180. <td>
  181. <p><font size="2">void</font></p>
  182. </td>
  183. <td>
  184. <p><font size="2">Transfers the image cache pixels to the image.</font></p>
  185. </td>
  186. </tr>
  187. <tr>
  188. <td>
  189. <p align="center"><a name="indexes"></a><font size="2">indexes</font></p>
  190. </td>
  191. <td>
  192. <p><font size="2">void*</font></p>
  193. </td>
  194. <td>
  195. <p><font size="2">void</font></p>
  196. </td>
  197. <td>
  198. <p><font size="2">Returns the PsuedoColor pixel indexes
  199. corresponding to the pixel region defined by the last <a href="Pixels.html#get">get</a>
  200. , <a href="Pixels.html#getConst">getConst</a>, or <a href="Pixels.html#set">set</a>
  201. call. Only valid for PseudoColor and CMYKA images. The pixel
  202. indexes (an array of type <i>void</i>, which is typedef <i>Quantum</i>,
  203. which is itself typedef <i>unsigned char</i>, or <i>unsigned short</i>,
  204. depending on the value of the <i>QuantumDepth </i>define) provide
  205. the <span lang="en-US">colormap</span> index (see <a
  206. href="Image++.html#colorMap">colorMap</a>) for each pixel in the
  207. image. For CMYKA images, the indexes represent the black
  208. channel. The value returned is intended for pixel access only. It
  209. should never be deallocated.</font></p>
  210. </td>
  211. </tr>
  212. <tr>
  213. <td>
  214. <p align="center"><a name="x"></a><font size="2">x</font></p>
  215. </td>
  216. <td>
  217. <p><font size="2">int</font></p>
  218. </td>
  219. <td>
  220. <p><font size="2">void</font></p>
  221. </td>
  222. <td>
  223. <p><font size="2">Left ordinate of view</font></p>
  224. </td>
  225. </tr>
  226. <tr>
  227. <td>
  228. <p align="center"><a name="y"></a><font size="2">y</font></p>
  229. </td>
  230. <td>
  231. <p><font size="2">int</font></p>
  232. </td>
  233. <td>
  234. <p><font size="2">void</font></p>
  235. </td>
  236. <td>
  237. <p><font size="2">Top ordinate of view</font></p>
  238. </td>
  239. </tr>
  240. <tr>
  241. <td>
  242. <p align="center"><a name="columns"></a><font size="2">columns</font></p>
  243. </td>
  244. <td>
  245. <p><font size="2">size_t</font></p>
  246. </td>
  247. <td>
  248. <p><font size="2">void</font></p>
  249. </td>
  250. <td>
  251. <p><font size="2">Width of view</font></p>
  252. </td>
  253. </tr>
  254. <tr>
  255. <td>
  256. <p align="center"><a name="rows"></a><font size="2">rows</font></p>
  257. </td>
  258. <td>
  259. <p><font size="2">size_t</font></p>
  260. </td>
  261. <td>
  262. <p><font size="2">void</font></p>
  263. </td>
  264. <td>
  265. <p><font size="2">Height of view</font></p>
  266. </td>
  267. </tr>
  268. </tbody>
  269. </table>
  270. </div>
  271. </body>
  272. </html>