Page 1 of 1

Setting the color of one pixel

Posted: 2010-04-14T16:23:33-07:00
by alsbraga
Hello to all

I'm a newbie in ImageMagick and MagickWand. (Actually I'm not very good in C too) This is a really great software suite!

By now I could successfully read images, display them and get pixel colors. Currently I'm not able to set pixels in an image.

I'm working in a project where I need to build a grayscale image. Basically I have an array of unsigned chars where each position represents one pixel. I need it converted to a displayable image. I tried to do it using the PixelSetXXXXQuantum functions but it didn't work. Below there is a code snipped in the way I tried it. It is not my actual code; it is a simplified version.

Code: Select all

    MagickWand * spaceWand;
    PixelWand * pixelWand;
    unsigned char * space;
    unsigned long row, col, rowsNo, colsNo;
    /*
     *
     * Here I initialize "space", "rowsNo" and "colsNo".
     *
     */
    MagickWandGenesis();
    spaceWand = NewMagickWand();
    PixelSetColor(pixelWand, "#FFFFFF");
    MagickNewImage(spaceWand, colsNo, rowsNo, pixelWand);
    for(row = 0; row < rowsNo; row++) {
        for(col = 0; col < colsNo; col++) {
            MagickGetImagePixelColor(space, col, row, pixelWand);
            colorQuantum = space[row*colsNo+col];
            PixelSetRedQuantum(pixelWand, colorQuantum);
            PixelSetGreenQuantum(pixelWand, colorQuantum);
            PixelSetBlueQuantum(pixelWand, colorQuantum);
        }
    }
    MagickDisplayImage(spaceWand, ":0.0");
    /*
     * Freeing resources code goes here.
     */
All I get is a white image (or in the color passed to PixelSetColor).

I did a test substituting the PixelSetXXXXQuantum calls to this:

Code: Select all

            PixelSetColor(pixelWand, "#FF0000");
but the pixels didn't change.

I imagine that the PixelWand obtained from "MagickGetImagePixelColor" is not a pointer to the actual pixel but a copy of it. Nevertheless, I didn't find some method like "MagickSetImagePixelColor".

I also tried something like the example in the magic-wand.html page where iterators are used. Surprisingly the methods "PixelGetMagickColor" and "PixelSetMagickColor" used in the example are not present in the header files nor in the documentation.

Sorry to bring this basic topic, but I searched the docs and the forum and couldn't find anything. There are nice examples of code for many interesting tasks but nothing to something that simple. Also, please forgive me for any dumb thing I may have coded. ;)

If someone can help, it will be great.

Thank's a lot.
André

Re: Setting the color of one pixel

Posted: 2010-04-14T18:31:10-07:00
by el_supremo
PixelSetColor sets the colour of the pixelwand but the pixelwand is not associated with the image.
I've attached a C MagickWand function which creates a grayscale gradient image. It should help you out.

Pete

Code: Select all

/*
	Create a simple grayscale gradient
*/
#include <windows.h>
#include <wand/magick_wand.h>

void test_wand(LPTSTR lpCmdLine)
{
	MagickWand *m_wand = NULL;
	PixelWand *p_wand = NULL;
	PixelIterator *iterator = NULL;
	PixelWand **pixels = NULL;
	int x,y,gray;
	char hex[128];

	MagickWandGenesis();

	p_wand = NewPixelWand();
	PixelSetColor(p_wand,"white");
    m_wand = NewMagickWand();
	// Create a 100x100 image with a default of white
	MagickNewImage(m_wand,100,100,p_wand);
	// Get a new pixel iterator 
	iterator=NewPixelIterator(m_wand);
	for(y=0;y<100;y++) {
		// Get the next row of the image as an array of PixelWands
		pixels=PixelGetNextIteratorRow(iterator,&x);
		// Set the row of wands to a simple gray scale gradient
		for(x=0;x<100;x++) {
			gray = x*255/100;
			sprintf(hex,"#%02x%02x%02x",gray,gray,gray);
			PixelSetColor(pixels[x],hex);
		}
		// Sync writes the pixels back to the m_wand
		PixelSyncIterator(iterator);
	}
	// Clean up
	iterator=DestroyPixelIterator(iterator);
	MagickWriteImage(m_wand,"bits_demo.gif");
	DestroyMagickWand(m_wand);
    MagickWandTerminus();

}

Re: Setting the color of one pixel

Posted: 2010-04-15T17:25:40-07:00
by alsbraga
Great!

It worked perfectly!

After posting the message, I found that the forum has more messages than I saw. There were actually some other posts which deal with the thing but I didn't check them yesterday. Today I worked with your code and it is really simple and effective.

Thank you very much!
André