How to access/change index of pixels in palette type image
Posted: 2009-07-06T13:45:25-07:00
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 !
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 !