# Thread: A simple question on displaying an image in grey scale

1. ## 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.

2. 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.

3. 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)?

4. 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.

5. 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.

6. 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.

7. You can use something like explained at http://www.gamedev.net/topic/627574-...te-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 :
Code :
```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 :
Code :
```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;
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•