How to correctly use syncPixels

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
CHRISTOPHERHENRY

How to correctly use syncPixels

Post by CHRISTOPHERHENRY »

I am having trouble getting syncPixels to work. Here is my simple example:

Code: Select all

//Create empty image
Image image1 = Image(Geometry(w,h),Color(0,0,0));//w and h contain the width and height of the image stored in chData
PixelPacket *iPtr1 = image1.setPixels(0,0,image1.columns(), image1.rows());

image1.readPixels(RGBQuantum, chData); //unsigned char *chData containing image data
image1.syncPixels();

Color clrTemp = image1.pixelColor(0,0);
Quantum tR,tG,tB;
tR = clrTemp.redQuantum();
tG = clrTemp.greenQuantum();
tB = clrTemp.blueQuantum();

My problem is that tR = tG = tB = 0 when in fact they should (if I understand correctly) contain the RGB values of the first pixel contained in chData. What am I doing wrong?

Note, the data pointed to by iPtr1 contains the correct values, I just can't get these values "copied" back to the variable image1.
CHRISTOPHERHENRY

Re: How to correctly use syncPixels

Post by CHRISTOPHERHENRY »

Okay, I have solved my problem:

Code: Select all

//Copy image data from wxImage to ImageMagick image
chData = wImage.GetData();

//Get image width and height
w = wImage.GetWidth();
h = wImage.GetHeight();

//Create a blank image the same size as wImage
image1 = Image::Image(Geometry(w,h),Color(0,0,0));

//Set the iamge type to TrueColor DirectClass respresentation
image1.type(TrueColorType);

//Ensure that there is only one reference to underlying image
image1.modifyImage();

//Allocate a pixel cache region to store wImage data
iPtr1 = image1.setPixels(0,0,image1.columns(), image1.rows());

//Transfer one or more pixel components from wImage into the image pixel cache
image1.readPixels(RGBQuantum, chData);

//Transfer the image cache pixels to the image
image1.syncPixels();
Post Reply