How to access/change index of pixels in palette type image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
andigum79

How to access/change index of pixels in palette type image

Post by andigum79 »

Hi,

after two days of reading and searching, I'm somehow lost and hoping for help.

My problem is the following. I would like to create a palette type png using the C++ API. My final goal is to create many different images with a constant colormap, independent of the actual colors in the respective images.

If I understand correctly, I should proceed in the following way:
// constructor for image with dimensions N,M
Image image( Geometry(N,M), Color(0,0,0));
// set type to Palette
image.type(ImageType(PaletteType));
// set up the colormap, with e.g. 4 colors
image.colorMapSize(4);
// entries of the color map, values for rr/gg/bb=0...1
image.colorMap(0,Color(0.5,0,0));
image.colorMap(1,Color(0,0.5,0));
image.colorMap(2,Color(0,0,0.5));
image.colorMap(3,Color(0.5,0.5,0.5));
But now, how do I set the index of a certain pixel to one of the four colors of my colormap ?
For example, I tried something like
Pixels res_im(image);
PixelPacket *pixels = res_im.get(0,0,dr,dc);
IndexPacket *index = res_im.indexes();
for(unsigned int i=0;i<N;i++)
for(unsigned int j=0;j<M;j++)
{
index = 2;
*index++;
}
in order to set all pixels to the third color in the colormap, being Color(0,0,0.5).
But this did not work out...
How should I do this ?

Of course I could possibly first create the pngs with non-Palette-type, and then convert on the command-line using convert. But then, with different images, I finally have different colormaps, depending on the actual content of the image. For my purpose, I should rather have a constant colormap for many different images.
Is there any other way to achieve this ?

Thanks in advance. Any hint or help is highly appreciated !
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to access/change index of pixels in palette type image

Post by fmw42 »

can't help with the API, but you need to create an image with all the colors you want in it to use for the common colormap. This can be applied with -map. see http://www.imagemagick.org/Usage/quantize/#map
andigum79

Re: How to access/change index of pixels in palette type image

Post by andigum79 »

Thanks for this hint, should work. But still I would prefer to create a fixed colormap using the API, and circumvent any further command line conversions...
Thanks again anyway !
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to access/change index of pixels in palette type image

Post by fmw42 »

There should be a way to use the equivalent to -map in an API.
andigum79

Re: How to access/change index of pixels in palette type image

Post by andigum79 »

Thanks fmw42, that's exactly what I was looking for - but without any success...
I thought that it should be possible to first create the complete colormap using the API, and then just say for each pixel which indes from the colormap to use...
But how to do this ?!?!
andigum79

Re: How to access/change index of pixels in palette type image

Post by andigum79 »

Is there any way to access the colormap-index of the single pixels in a palette-type image ?
Or am I completely on the wrong track ?
Thanks for your help !
andigum79

Re: How to access/change index of pixels in palette type image

Post by andigum79 »

I had to kill the beast myself. Here is the solution.

// constructor for image with dimensions N,M
Image image( Geometry(N,M), Color(0,0,0));
// set type to Palette
image.type(PaletteType);
// set up the colormap, with e.g. 4 colors
image.colorMapSize(4);
// entries of the color map
image.colorMap(0,Color(0,0,0));
image.colorMap(1,Color(10,0,0));
image.colorMap(2,Color(0,10,0));
image.colorMap(2,Color(0,0,10));
// get pixel cache
Pixels res_im(image);
PixelPacket *pixels = res_im.get(0,0,dr,dc);
// get index array corresponding to previous "get" command
IndexPacket *index = res_im.indexes();
// set index array with offset into colormap
for(unsigned int i=0;i<M;i++)
for(unsigned int j=0;j<N;j++)
*(index+j+i*N) = //put index here, in the present case 0/1/2/3
image.write("png8:outputimage.png");

For images with less than 256, it's fast as hell, and file size is very small. Additionally, colormap is always the same, no matter whether the respective color is in the image or not.

But one more question: how to apply binary transparency after the above ?!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to access/change index of pixels in palette type image

Post by anthony »

At this time a fixed color map is not guranteed in IM.

Mind you I have not really looked into this either, but it would probably require a new option to specify an output color map for pallette image file formats.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply