Page 1 of 1

how to change RGB value of one pixel?

Posted: 2008-07-31T00:45:23-07:00
by sunng1
hi there

I'm using MagickCore, and I have a problem with changing RGB value of pixel.

what I am doing is:

1.read a gif picture
2.change some pixels's RGB value of each frame
3.save the picture as gif

my code looks like this:

Code: Select all

int main(int argc, char* argv[])
{
    ExceptionInfo
      *exception;

    Image *images;

    ImageInfo *image_info;

	FILE * f = fopen("/home/somedir/1.gif", "rb");
	fseek(f, 0, SEEK_END);
	long fileLength = ftell(f);
	fseek(f,0, SEEK_SET);

	unsigned char * fileData = (unsigned char *)malloc(fileLength);
	fread(fileData, 1, fileLength, f);

    exception=AcquireExceptionInfo();
    image_info=CloneImageInfo((ImageInfo *) NULL);

	unsigned int length;
	char * base64data = Base64Encode(fileData, fileLength, &length);
	length = 0;
    images = ReadInlineImage(image_info, base64data, exception);

    if (exception->severity != UndefinedException)
      CatchException(exception);
    if (images == (Image *) NULL)
    {
    	printf("Read image fail!\n");
        return 1;
    }

    int imageCount = 0;
    Image * currentImage = images;
    while(currentImage != (Image *) NULL)
    {
    	imageCount++;

        /***************************************************************************/
        //I want to change some piexls here ,how can I do ?
        // for example, change point(10,10)'s RGB value to (255,255,255)
        /***************************************************************************/

    	currentImage = currentImage->next;
    }
    if(imageCount == 0)
    {
    	printf("Image error!\n");
    	return 1;
    }

	 WriteImages(image_info, images, "/home/somedir/2.gif", exception);


	printf("OK!\n");
	return 0;
}

please help me ,thanks very much!

Re: how to change RGB value of one pixel?

Posted: 2008-07-31T02:28:11-07:00
by fmw42
In command line you can do it very easily by:

convert rose: -fx "i==10&&j=10?white:u" rose_changed.png

This will change pixel 10,10 to white and leave the remaining pixels unchanged.

Re: how to change RGB value of one pixel?

Posted: 2008-07-31T06:10:08-07:00
by magick
For a single pixel, use p=GetOnePixel(image,x,y), set the color with p->red=QuantumRange, and then sync the image pixels with SyncImagePixels(). If you are updating lots of pixels, use GetImagePixels() instead. We recommend the MagickWand API which makes this stuff a bit easier to work with.

Re: how to change RGB value of one pixel?

Posted: 2008-08-06T01:45:10-07:00
by sunng1
thank you so much!

and still one thing to say:

PixelPacket p=GetOnePixel(image,x,y);
change p's value doesn't works;

PixelPacket * q = GetImagePixels(Result, x, y, 1, 1);
change q's value does works;

Re: how to change RGB value of one pixel?

Posted: 2008-08-14T19:12:38-07:00
by anthony
You need to SyncImagePixels() after the change so 'p' and 'q' changes gets written back into the actual image. This is to allow correct multi-threaded procedure handling