Setting the color of one pixel

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
alsbraga

Setting the color of one pixel

Post 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é
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Setting the color of one pixel

Post 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();

}
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
alsbraga

Re: Setting the color of one pixel

Post 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é
Post Reply