Newbie question on reading pixel color

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
efo

Newbie question on reading pixel color

Post by efo »

Hi guys,

I am a newbie and I am trying to figure out a a simple program using a c interface and imagemagick. First I have to load a picture and reduce the number of colors to a given number (e.g. 8). Next, I have to read the pixel colors one by one.
I got across and example to read pixel color and I stripped it down to:

MagickWandGenesis();
wand=NewMagickWand();
MagickReadImage(wand,argv[1]);
PixelWand **pixels;
iterator=NewPixelIterator(wand);
unsigned long width;
MagickPixelPacket pixel;

wpxl=MagickGetImageWidth(wand);
hpxl=MagickGetImageHeight(wand);

for (y=0; y < hpxl; y++)
{
pixels=PixelGetNextIteratorRow(iterator,&width);
for (x=0; x < wpxl; x++)
{
PixelGetMagickColor(pixels[x],&pixel);
printf("%d %d => %f %f %f \n",x,y,pixel.red,pixel.green,pixel.blue);
}
}

Something I dont fully understand is why pixels is ** and not * if only one row at a time is read into it. Aslo, do I really need an iterator? Isnt there a function that allows me to access pixels directly (e.g. color= function(wand,x,y) or pixels=function(wand,x1,y1,x2,y2) and PixelGetMagickVolor(pixels[x,y],&pixel))?

Sorry for the dumb questions but I am having hard time finding detailed documentation on the matter.

Thanks.

Efo
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Newbie question on reading pixel color

Post by magick »

MagickWand applies object-oriented principles to a procedural language, C. If you would like direct access to the image pixels, use MagickCore. In MagickCore, use this code snippet to access pixel values:

Code: Select all

  for (y=0; y < (long) image->rows; y++)
  {
    p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
    if (p == (const Image *) NULL)
      break;
    for (x=0; x < (long) image->columns; x++)
    {
      (void) printf("%d %d: %d %d %d\n",y,x,p->red,p->green,p->blue);
      p++
    }
efo

Re: Newbie question on reading pixel color

Post by efo »

Thank you Magick,

That's what I was looking for!!! (or better AcquireOneMagickPixel).
Can you suggest any good readings for MagickCore?

Thanks again!

Efo
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Newbie question on reading pixel color

Post by magick »

We aspire to write a book about MagickWand and MagickCore but so far we can't find the time. In the mean-time all we have is the ImageMagick source code which is written in MagickCore and the API details at http://www.imagemagick.org/script/magick-core.php.
Post Reply