A simple question on displaying an image in grey scale

I have one image displayed in grey scale with pixel values range from (0 to 255). How to display the image in grey scale? I use glColor3f( r, g, b, 1.0) for diplaying colored image. How to use it for displaying grey scale image. thanks in advance.

With grey scale, just make sure r = g = b. If r,g,b all have the same value, you will have some shade of grey.

Thank for the reply. Suppose, I would like to convert grey values to rgb, i,e, ‘0’ meaning pure blue and ‘255’ pure red with green in between, then how to write in glColor3f(r,g,b)?

I really didn’t understand what you mean by converting grey values to rgb.

If you mean, you want to assign color to grey scale image, I dont think you can as there are many possibilities for a given grey scale value.

If you mean you want to interpolate btw two colors, you can do so by simply assigning your two colors to two vertices. The rasterizer will then generate the in-between colour using linear interpolation.

You could encode a palette in a 1 dimensional texture and use the gray value to look up a color. Or you could procedurally define a palette using math functions like smoothstep or something.

Thanks for the reply. Could you explain the process a bit more? It would be good if you can provide some link to some examples.

You can use something like explained at http://www.gamedev.net/topic/627574-solved-applying-color-palette-to-texture/

Note that I think that the 2D palette used in this link can to be replaced by a more logical 1D palette and use only a grey/colorindex texture that you want to “re-colormap” :

Cf. load the grey texture and a colormap of 256 colors with something like this :


texture = glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, your_texture_data);

colormap = glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256 , 0, GL_RGBA, GL_UNSIGNED_BYTE, your_colormap_data);

And use a fragment shader like this :


uniform sampler2D texture; 
uniform sampler1D palette; 

void main() 
{ 
    float  textureGrey = texture2D(texture, gl_TexCoord[0].xy).r; 
    vec4 paletteColor  = texture1D(palette, textureGrey ).rgba; 

    gl_FragColor = paletteColor; 
}