How can I access pixel index?

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
idries
Posts: 2
Joined: 2016-09-24T01:27:56-07:00
Authentication code: 1151

How can I access pixel index?

Post by idries »

Hi,

I'm trying to read the pixel index data from a palettized image. I've tried a lot of different approaches but my naive implementation is:

Code: Select all

	int x, y;
	size_t width;
	PixelIterator* pixel_iterator = NewPixelIterator(read_wand);

	for (y = 0; y < (long)MagickGetImageHeight(read_wand); y++)
	{
		PixelInfo pixel_info;
		PixelWand** pixels = PixelGetNextIteratorRow(pixel_iterator, &width);

		if ((pixels == (PixelWand **)NULL))
			break;

		for (x = 0; x < (long)width; x++)
		{
			int index = PixelGetIndex(pixels[x]);

			// process index here
		}
	}
I would expect this to give me index values, but they are always 0. More confusingly, I've had a look inside the implementation of PixelGetIndex:

Code: Select all

WandExport Quantum PixelGetIndex(const PixelWand *wand)
{
  assert(wand != (const PixelWand *) NULL);
  assert(wand->signature == MagickWandSignature);
  if (wand->debug != MagickFalse)
    (void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",wand->name);
  return((Quantum) wand->pixel.black);
}
I would have expected this to return the 'index' field. Checking in the debugger the index field for all my pixels is 0 as well.

The image I've originally loaded is a 24 BPP png, but I've used ImageMagick to remap it to a 55 colour palette (the image only uses 29 of the colours). When I save the image:

Code: Select all

MagickWriteImage(read_wand, dest_file_name);
I get an 8 bpp png so ImageMagick clearly knows it's a palettized image.

I called the following functions to try and 'force' ImageMagick to palettize the image:

Code: Select all

	SetImageStorageClass(extent_image, PseudoClass, exception);
	SetImageType(extent_image, PaletteType, exception);
These do change the fields in the structure but don't make black or index fields get populated.

I took a look at the colormap of the image. It seems to be completely correct, so obviously I could look up each pixel's rbg values to find the palette index, but this seems silly.

Any ideas?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I access pixel index?

Post by snibgo »

You don't say what version IM you are using. V6.9.3-7 has different code for PixelGetIndex(), in pixel-wand.c, to what you quoted.

Code: Select all

WandExport IndexPacket PixelGetIndex(const PixelWand *wand)
{
  assert(wand != (const PixelWand *) NULL);
  assert(wand->signature == WandSignature);
  if (wand->debug != MagickFalse)
    (void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",wand->name);
  return((IndexPacket) wand->pixel.index);
}
snibgo's IM pages: im.snibgo.com
idries
Posts: 2
Joined: 2016-09-24T01:27:56-07:00
Authentication code: 1151

Re: How can I access pixel index?

Post by idries »

I am on 7.0.3-0.

Does the index field contain reasonable data in your version?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I access pixel index?

Post by snibgo »

I don't know why, in v7, PixelGetIndex() returns wand.pixel->black. The corresponding function PixelSetIndex() sets wand.pixel->index.

I don't use the Wand interface for pixel data, nor do I use indexes. PixelGetIndex() doesn't seem to be called anywhere within ImageMagick, so a bug in that function would go unnoticed.
snibgo's IM pages: im.snibgo.com
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: How can I access pixel index?

Post by dlemstra »

PixelGetIndex should return wand.pixel->index instead. I just pushed a fix to make sure this is resolved in the next version of ImageMagick.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Post Reply