Home | History | Annotate | Download | only in Magick++
      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="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="https://imagemagick.org/Magick++/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 
     51 using namespace std; 
     52 using namespace Magick; 
     53 int main(int argc,char **argv) 
     54 { 
     55   InitializeMagick(*argv);
     56 
     57   // Create base image 
     58   Image image(Geometry(254,218), "white");
     59 
     60 
     61   // Set the image type to TrueColor DirectClass representation.
     62   image.type(TrueColorType);
     63   // Ensure that there is only one reference to underlying image 
     64   // If this is not done, then image pixels will not be modified.
     65   image.modifyImage();
     66 
     67   // Allocate pixel view 
     68   Pixels view(image);
     69 
     70   // Set all pixels in region anchored at 38x36, with size 160x230 to green. 
     71   size_t columns = 196; size_t rows = 162; 
     72   Color green("green"); 
     73   Quantum *pixels = view.get(38,36,columns,rows); 
     74   for ( ssize_t row = 0; row &lt; rows ; ++row ) 
     75     for ( ssize_t column = 0; column &lt; columns ; ++column ) 
     76     {
     77       *pixels++=QuantumRange*green.quantumRed();
     78       *pixels++=QuantumRange*green.quantumGreen();
     79       *pixels++=QuantumRange*green.quantumBlue();
     80     }
     81 
     82   // Save changes to image.
     83   view.sync();
     84 
     85   // Set all pixels in region anchored at 86x72, with size 108x67 to yellow. 
     86   columns = 108; rows = 67; 
     87   Color yellow("yellow"); 
     88   pixels = view.get(86,72,columns,rows); 
     89   for ( ssize_t row = 0; row &lt; rows ; ++row ) 
     90     for ( ssize_t column = 0; column &lt; columns ; ++column ) 
     91     {
     92       *pixels++=QuantumRange*yellow.quantumRed();
     93       *pixels++=QuantumRange*yellow.quantumGreen();
     94       *pixels++=QuantumRange*yellow.quantumBlue();
     95     }
     96    view.sync();
     97 
     98   // Set pixel at position 108,94 to red 
     99   Color red("red");
    100   pixels = view.get(108,94,1,1);
    101   *pixels++=QuantumRange*red.quantumRed();
    102   *pixels++=QuantumRange*red.quantumGreen();
    103   *pixels++=QuantumRange*red.quantumBlue();
    104 
    105   // Save changes to image.
    106   view.sync();
    107   image.write( "logo.png" );
    108 }
    109 </div>
    110 <p style="margin-bottom: 0cm;"><i>Pixels</i> supports the following
    111 methods: </p>
    112 <p align="center" style="margin-bottom: 0cm;"><b>Pixel Cache Methods</b></p>
    113 <table width="100%" border="1" cellpadding="2" cellspacing="2">
    114 	<tbody>
    115     <tr>
    116 		<td> 			
    117       <p align="center"><b>Method</b></p>
    118 		</td>
    119 		<td> 			
    120       <p align="center"><b>Returns</b></p>
    121 		</td>
    122 		<td> 			
    123       <p align="center"><b>Signature</b></p>
    124 		</td>
    125 		<td> 			
    126       <p align="center"><b>Description</b></p>
    127 		</td>
    128 	</tr>
    129 	<tr>
    130 		<td> 			
    131       <p align="center"><a name="get"></a><font size="2">get</font></p>
    132 		</td>
    133 		<td> 			
    134       <p><font size="2"><a href="Quantum.html">Quantum</a>*</font></p>
    135 		</td>
    136 		<td> 			
    137       <p><font size="2">const ssize_t x_, const ssize_t y_, const size_t
    138 columns_, const size_t rows_</font></p>
    139 		</td>
    140 		<td> 			
    141       <p><font size="2">Transfers read-write pixels from the image to
    142 the 			pixel cache as defined by the specified rectangular region.
    143 			Modified pixels may be subsequently transferred back to the image
    144 			via <i>sync</i>. The value returned is intended for pixel access
    145 			only. It should never be deallocated.</font></p>
    146 		</td>
    147 	</tr>
    148 	<tr>
    149 		<td> 			
    150       <p align="center"><a name="getConst"></a><font size="2">getConst</font></p>
    151 		</td>
    152 		<td> 			
    153       <p><font size="2">const <a href="Quantum.html">Quantum</a>*</font></p>
    154 		</td>
    155 		<td> 			
    156       <p><font size="2">const ssize_t x_, const ssize_t y_, const size_t
    157 			columns_, const size_t rows_</font></p>
    158 		</td>
    159 		<td> 			
    160       <p><font size="2">Transfers read-only pixels from the image to
    161 the 			pixel cache as defined by the specified rectangular region.</font></p>
    162 		</td>
    163 	</tr>
    164 	<tr>
    165 		<td> 			
    166       <p align="center"><a name="set"></a><font size="2">set</font></p>
    167 		</td>
    168 		<td> 			
    169       <p><font size="2"><a href="Quantum.html">Quantum</a>*</font></p>
    170 		</td>
    171 		<td> 			
    172       <p><font size="2">const ssize_t x_, const ssize_t y_, const size_t
    173 			columns_, const size_t rows_</font></p>
    174 		</td>
    175 		<td> 			
    176       <p><font size="2">Allocates a pixel cache region to store image
    177 			pixels as defined by the region rectangle. This area is
    178 			subsequently transferred from the pixel cache to the image via 			<i>sync</i>.
    179 The value returned is intended for pixel access only. 			It should
    180 never be deallocated.</font></p>
    181 		</td>
    182 	</tr>
    183 	<tr>
    184 		<td> 			
    185       <p align="center"><a name="sync"></a><font size="2">sync</font></p>
    186 		</td>
    187 		<td> 			
    188       <p><font size="2">void</font></p>
    189 		</td>
    190 		<td> 			
    191       <p><font size="2">void</font></p>
    192 		</td>
    193 		<td> 			
    194       <p><font size="2">Transfers the image cache pixels to the image.</font></p>
    195 		</td>
    196 	</tr>
    197 	<tr>
    198 		<td> 			
    199       <p align="center"><a name="indexes"></a><font size="2">indexes</font></p>
    200 		</td>
    201 		<td> 			
    202       <p><font size="2">void*</font></p>
    203 		</td>
    204 		<td> 			
    205       <p><font size="2">void</font></p>
    206 		</td>
    207 		<td> 			
    208       <p><font size="2">Returns the PsuedoColor pixel indexes
    209 			corresponding to the pixel region defined by the last <a href="Pixels.html#get">get</a>
    210 			, <a href="Pixels.html#getConst">getConst</a>, or <a href="Pixels.html#set">set</a>
    211 			call. Only valid for PseudoColor and CMYKA images. The pixel
    212 			indexes (an array of type <i>void</i>, which is typedef 			<i>Quantum</i>,
    213 which is itself typedef <i>unsigned char</i>, or 			<i>unsigned short</i>,
    214 depending on the value of the <i>QuantumDepth 			</i>define) provide
    215 the <span lang="en-US">colormap</span> index 			(see <a
    216  href="https://imagemagick.org/Magick++/Image++.html#colorMap">colorMap</a>) for each pixel in 			the
    217 image. For CMYKA images, the indexes represent the black 			
    218 channel. The value returned is intended for pixel access 			only. It
    219 should never be deallocated.</font></p>
    220 		</td>
    221 	</tr>
    222 	<tr>
    223 		<td> 			
    224       <p align="center"><a name="x"></a><font size="2">x</font></p>
    225 		</td>
    226 		<td> 			
    227       <p><font size="2">int</font></p>
    228 		</td>
    229 		<td> 			
    230       <p><font size="2">void</font></p>
    231 		</td>
    232 		<td> 			
    233       <p><font size="2">Left ordinate of view</font></p>
    234 		</td>
    235 	</tr>
    236 	<tr>
    237 		<td> 			
    238       <p align="center"><a name="y"></a><font size="2">y</font></p>
    239 		</td>
    240 		<td> 			
    241       <p><font size="2">int</font></p>
    242 		</td>
    243 		<td> 			
    244       <p><font size="2">void</font></p>
    245 		</td>
    246 		<td> 			
    247       <p><font size="2">Top ordinate of view</font></p>
    248 		</td>
    249 	</tr>
    250 	<tr>
    251 		<td> 			
    252       <p align="center"><a name="columns"></a><font size="2">columns</font></p>
    253 		</td>
    254 		<td> 			
    255       <p><font size="2">size_t</font></p>
    256 		</td>
    257 		<td> 			
    258       <p><font size="2">void</font></p>
    259 		</td>
    260 		<td> 			
    261       <p><font size="2">Width of view</font></p>
    262 		</td>
    263 	</tr>
    264 	<tr>
    265 		<td> 			
    266       <p align="center"><a name="rows"></a><font size="2">rows</font></p>
    267 		</td>
    268 		<td> 			
    269       <p><font size="2">size_t</font></p>
    270 		</td>
    271 		<td> 			
    272       <p><font size="2">void</font></p>
    273 		</td>
    274 		<td> 			
    275       <p><font size="2">Height of view</font></p>
    276 		</td>
    277 	</tr>
    278   </tbody>
    279 </table>
    280 </div>
    281 </body>
    282 </html>
    283