Setting the color of one pixel
Posted: 2010-04-14T16:23:33-07:00
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.
All I get is a white image (or in the color passed to PixelSetColor).
I did a test substituting the PixelSetXXXXQuantum calls to this:
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é
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.
*/
I did a test substituting the PixelSetXXXXQuantum calls to this:
Code: Select all
PixelSetColor(pixelWand, "#FF0000");
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é