Magick::Pixels

The Pixels class provides efficient access to raw image pixels. Image pixels (of type PixelPacket) may be accessed directly via the Image Pixel Cache.  The image pixel cache is a rectangular window (a view) into the actual image pixels (which may be in memory, memory-mapped from a disk file, or entirely on disk). Obtain existing image pixels via get(). Create a new pixel region using set().

Depending on the capabilities of the operating system, and the relationship of the window to the image, the pixel cache may be a copy of the pixels in the selected window, or it may be the actual image pixels. In any case calling sync() insures that the base image is updated with the contents of the modified pixel cache. The method decode() supports copying foreign pixel data formats into the pixel cache according to the QuantumTypes. The method encode() supports copying the pixels in the cache to a foreign pixel representation according to the format specified by QuantumTypes.

The pixel view is a small image in which the pixels may be accessed, addressed, and updated, as shown in the following example, which produces an image similar to the one on the right (minus lines and text):
 
   // Create base image
    Image image(Geometry(254,218), "white");

   // Set image pixels to DirectClass representation
    image.classType( DirectClass );

   // Allocate pixel view
    Pixels view(image);

   // Set all pixels in region anchored at 38x36, with size 160x230 to green.
    unsigned int columns = 196; unsigned int rows = 162;
    Color green("green");
    PixelPacket *pixels = view.get(38,36,columns,rows);
    for ( unsigned int row = 0; row < rows ; ++row )
      for ( unsigned int column = 0; column < columns ; ++column )
        *pixels++=green;
    view.sync();

   // Set all pixels in region anchored at 86x72, with size 108x67 to yellow.
    columns = 108; rows = 67;
    Color yellow("yellow");
    pixels = view.get(86,72,columns,rows);
    for ( unsigned int row = 0; row < rows ; ++row )
      for ( unsigned int column = 0; column < columns ; ++column )
        *pixels++=yellow;
    view.sync();

   // Set pixel at position 108,94 to red
    *(view.get(108,94,1,1)) = Color("red");
    view.sync();

Pixels supports the following methods:
 
Pixel Cache Methods
Method
Returns
Signature
Description
get
PixelPacket* int x_, int y_, unsigned int columns_, unsigned int rows_ Transfers pixels from the image to the pixel cache as defined by the specified rectangular region. Modified pixels may be subsequently transferred back to the image via sync.
set
PixelPacket* int x_, int y_, unsigned int columns_, unsigned int rows_ Allocates a pixel cache region to store image pixels as defined by the region rectangle.  This area is subsequently transferred from the pixel cache to the image via sync.
sync
void void Transfers the image cache pixels to the image.
indexes
IndexPacket* void Returns the PsuedoColor pixel indexes. Only valid for PseudoColor images. The pixel indexes (typically an array of type unsigned short) provide the colormap index (see colorMap) for each pixel in the image.
x
unsigned int void Left ordinate of view
y
unsigned int void Top ordinate of view
columns
unsigned int void Width of view
rows
unsigned int void Height of view
decode
void QuantumTypes quantum_, unsigned char *source_, Transfers one or more pixel components from a buffer or file into the image pixel cache of an image. Decode is typically used to support image decoders.
encode
void QuantumTypes quantum_, unsigned char *destination_ Transfers one or more pixel components from the image pixel cache to a buffer or file. Encode is typically used to support image encoders.