Page 1 of 1

How to correctly use syncPixels

Posted: 2008-11-28T13:36:19-07:00
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.

Re: How to correctly use syncPixels

Posted: 2008-11-28T13:59:04-07:00
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();