how to change RGB value of one pixel?

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
sunng1

how to change RGB value of one pixel?

Post 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!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to change RGB value of one pixel?

Post 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.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: how to change RGB value of one pixel?

Post 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.
sunng1

Re: how to change RGB value of one pixel?

Post 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;
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: how to change RGB value of one pixel?

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply