getting the colormapindex for each 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
murmel
Posts: 4
Joined: 2016-10-20T07:44:39-07:00
Authentication code: 1151

getting the colormapindex for each pixel

Post by murmel »

hi,
unluckely I'm a complete newbe using the magickwand within c-lang. first of all my magickwand-version is the debian-sid (8:6.8.9.9-7.2)

I tried to generate a small test image with 3 colors in it.

Code: Select all

$ convert -size 4x4 xc:red xc:green xc:blue +append a.png
$ identify -format "%k %wx%h %f\n" a.png
3 12x4 a.png
the goal to reach was printing out the palette index for each pixel. so I wrote a small program

Code: Select all

// cc -o `pkg-config --cflags --libs MagickWand` -o c c.c

#include <wand/magick_wand.h>

int main(int argc,char **argv)
{
	MagickWand *m_wand = NULL;
	PixelWand *color = NULL;
	MagickPixelPacket pixel;
	PixelIterator* pixel_iterator = NULL;
	
	int x, y;
	size_t width, height;
        size_t indexedcolors;
	double R, G, B;
	char* file = argv[1];
	
	MagickWandGenesis();
	
	m_wand = NewMagickWand();
	color = NewPixelWand();

	MagickReadImage(m_wand, file);
	
	width = MagickGetImageWidth(m_wand);
	height = MagickGetImageHeight(m_wand);

	indexedcolors = MagickGetImageColors(m_wand);

	printf("image: %s width: %d height: %d countindex: %d\n", file, (int) width, (int) height, indexedcolors);

	for (y = 0; y <= (int)height; y++)
	{
	   printf("line: %03d pixel: ", y);
	   for (x = 0; x < (int)width; x++)
	   {
	      MagickGetImagePixelColor(m_wand, (ssize_t)x,(ssize_t)y, color);
              printf("%02x ", PixelGetColorCount(color)); 
	   }
	printf("\n");
	}

	MagickWandTerminus();

	exit(0);
}
the outcoming result is:

Code: Select all

image: a.png width: 12 height: 4 countindex: 3
line: 000 pixel: 00 00 00 00 00 00 00 00 00 00 00 00 
line: 001 pixel: 00 00 00 00 00 00 00 00 00 00 00 00 
line: 002 pixel: 00 00 00 00 00 00 00 00 00 00 00 00 
line: 003 pixel: 00 00 00 00 00 00 00 00 00 00 00 00 
line: 004 pixel: 00 00 00 00 00 00 00 00 00 00 00 00 
why does this not work?

thank you in advance for helping me
murmel
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: getting the colormapindex for each pixel

Post by snibgo »

You want an index, but asked for PixelGetColorCount().

Perhaps PixelGetIndex() would provide what you want.
snibgo's IM pages: im.snibgo.com
murmel
Posts: 4
Joined: 2016-10-20T07:44:39-07:00
Authentication code: 1151

Re: getting the colormapindex for each pixel

Post by murmel »

thank you for reply ... I didn't recognized that function so far.

it seems to work with a testimage.PNG produced by convert :D but the same testimage formated as a GIF - it doesn't (using a palette with only 7 colors).

Code: Select all

IndexPacket PixelGetIndex(const PixelWand *wand)
as i mentioned the typedef IndexPacket is equal to "unsigned char". which type is used if image ownes more than 255 colors/8bit

many thanks to the comunity
murmel :)
Post Reply