Skip to main content
GameDev.net gamedev.net
🔒 Locked

glPixelMap()

Started by k_ounu_eddy Jun 17, 2007 at 10:11 AM 11 replies 4.5k views
Original Post
k_ounu_eddy
k_ounu_eddy
I don't understand what does this function.For example we have: GLfloat Map[256]; Map[0]=1; Map[1]=0.234; Map[2]=0.4; //etc glPixelTransferi(GL_MAP_COLOR,TRUE); //we enable MAP_COLOR glPixelMapfv(GL_PIXEL_MAP_R_TO_R,255,invertMap); glDrawPixels(64, 64, GL_RGB,GL_UNSIGNED_BYTE,Image); //we draw an image(doesn't matter the content of the Image) What does glPixelMap?It converts the first pixel red value to 1,the second to 0.234,the third 0.4,and so on?
MARS_999
MARS_999
Here is a link that may help you

http://pyopengl.sourceforge.net/documentation/manual/glPixelMap.3G.html

from what I can tell it can do that...
k_ounu_eddy
k_ounu_eddy
I knew that link too.So ,it doesn't help me.Anyway,thanks.
Let's say I have the code:
#define checkImageWidth 64
#define checkImageHeight 64
GLubyte checkImage[checkImageHeight][checkImageWidth][3];
const size=2;
float a[2]={0,1};
void makeCheckImage(void)
{
int i, j, c;

for (i = 0; i < checkImageHeight; i++) {
for (j = 0; j < checkImageWidth; j++) {
c = ((((i&0x8)==0)^((j&0x8))==0))*255;
checkImage[j][0] = (GLubyte) c;
checkImage[j][1] = (GLubyte) c;
checkImage[j][2] = (GLubyte) c;
}
}
}

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
makeCheckImage();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelTransferi(GL_MAP_COLOR,TRUE);
glPixelMapfv(GL_PIXEL_MAP_R_TO_R, 2, a); //
glPixelMapfv(GL_PIXEL_MAP_G_TO_G, 2, a); //
glPixelMapfv(GL_PIXEL_MAP_B_TO_B, 2, a); //
//without these 3 lines the computer draws a chess table
}

//in display function
glRasterPos2i(0, 0);
glDrawPixels(checkImageWidth, checkImageHeight, GL_RGB, GL_UNSIGNED_BYTE, checkImage);

But,with the 3 lines that I marked,I was thinking that a pixel will be black,the next one white,the next black,then white,and so on(the pixels which are divided by 2 will be black,the others white).But this isn't true.Can someone explain me?
Brother Bob
Brother Bob
It's just a color lookup table. It's like flexible variant of the old 8-bit indexed colors; clicky. When drawing the image, each color is modified by the lookup map, and replaced by a new value from the map.

The color value is converted into an index (color value 0 is index 0, maximum color intensity indexes the last element in the map, and colors between gets an index somewhere inside the map according to a simple linear mapping), and the new color value is the value at that position in the map. This lookup is performed per channel.

In your first example, it means that color value 0 is mapped to full intensity (pitch black to full white), color value 1 is mapped to 23.4% gray (almost pitch black to dark gray), and so on.
k_ounu_eddy
k_ounu_eddy
Thanks for explication.But,when I use RGB mode,how can I convert the RGB to colors indices?
For example,in my first example:
Map[0]=1;
Map[1]=0.234;
Map[2]=0.4;
2 correspond to which colour in RGB mode?
Brother Bob
Brother Bob
Ok, let's say the map is the one you posted, with the addition of a fourth element since the size of the map must be a power of two.
Map[0]=1;Map[1]=0.234;Map[2]=0.4;Map[3]=0.75;

That map is loaded into the R to R, B to B and G to G maps. Now you want to draw an image, and one particular pixel in this image happens to have the value
(R,G,B) = (0, 100, 230).

That is, a blue color with a bit of green in it.

Now, convert the color values from the range [0, 255] (the color range) to [0, 3] (the map size) by dividing the color components by 255 and miltiply by 3, and then round to nearest integer. That gives us our index values.

The index values are
(Ri,Gi,Bi) = (round(0/255*3), round(100/255*3), round(230/255*3)) =           = (round(0), round(1.18), round(2.70)) =           = (0, 1, 3)

Use those indices to look up the new color in the color map.
(Rm, Gm, Bm) = (Map[Ri], Map[Gi], Map[Bi]) =              = (1, 0.234, 0.75)

Keep in mind that the color range is now [0, 1] instead of [0, 255]. The output color from the mapping stage is therefore a somewhat light magenta color. This mapping is performed for each pixel in the image being drawn.
k_ounu_eddy
k_ounu_eddy
One more question :D.
Everytime the color range will be [0,1],no matter what map I use?
k_ounu_eddy
k_ounu_eddy
One more question :D.
Everytime the color range will be [0,1],no matter what map I use?
k_ounu_eddy
k_ounu_eddy
One more question :D.
Everytime the color range will be [0,1],no matter what map I use?
GreyHound
GreyHound
for what cases would that be useful? Sounds like something that would be used if you wanted to realize mayb an infrared view of a texture, but without shaders but rather the normal render pipeline?
Brother Bob
Brother Bob
Quote:
Original post by k_ounu_eddy
One more question :D.
Everytime the color range will be [0,1],no matter what map I use?

The color range is always [0, 1] for floartingpoint colors, and [0, max integer value] for integer colors. Converting integer colors to floatingpoint colors is just a matter of dividing the value by max integer value.

Math operations on colors are often specified in the [0, 1] range, and the conversion from integer to floatingpoint is assumed.
Quote:
Original post by GreyHound
for what cases would that be useful? Sounds like something that would be used if you wanted to realize mayb an infrared view of a texture, but without shaders but rather the normal render pipeline?

First, this mapping is for pixel transfers only. For drawing related functions, this means glTexImage-type calls, and glDrawPixels-type calls. It doesn't affect the rendering of primitives at all.

So what's it for? Arbitrary modification/mapping of colors (altough static mapping in the way that a specific color will always map to the same color). The color matrix can only do a linear conversion, but the mapping can to a per-channel arbitrary mapping. Gamma correction, histogram modification, color quantization simulation, indexed images using palettes are posible.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.