WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Thread Tools Rate Thread Display Modes
Old 05-03-2005, 06:51 AM
vmerget vmerget is offline
Join Date: Mar 2005
Posts: 3
Only 16 grays in imagegen

I have a weird problem with an imagegen plug-in I wrote.
I'm using it to display grayscale images (though the plug-in
technically accepts 32-bit RGBA) on a TEXQUAD.

The plug-in works fine *except* on one machine. On that
one machine, the plug-in only displays 16 levels of gray.
A smooth gradient from black to white turns into a
series of stripes.

Some experiments imply that the lower 4 bits of each byte
are being discarded (e.g. when I set all R and G components
to 0, I got 16 shades of blue). The bytes are fine when I
write them into the osg::Image, so it's happening after that.

It doesn't seem to be a hardware limitation of the
machine: it displays textures read from a grayscale JPEG
just fine.

Is there something in the imagegen support code that
would cause something like this?
-- Dan Merget
Reply With Quote
Old 05-03-2005, 10:34 AM
farshizzo farshizzo is offline
WorldViz Team Member
Join Date: Mar 2003
Posts: 2,849

Have you tried creating the image as a true grayscale image? To do that you would create the image like so:
image->setImage(size,size,1,1,GL_LUMINANCE,GL_UNSIGNED_BYTE,new unsigned char[size*size],osg::Image::USE_NEW_DELETE);
Then you would set each pixel value using a single byte:
unsigned char *pixel = image->data(x,y);
*pixel = 255;
If you need the image to be created as RGBA then you can either post the code you are using or email it to me at

Also, make sure that the computer which is experiencing this problem is set to 32-bit color mode in the graphics card settings. It might be set to 16-bit.
Reply With Quote
Old 05-03-2005, 12:53 PM
vmerget vmerget is offline
Join Date: Mar 2005
Posts: 3
Checking the settings of the graphics card was the first thing
I checked. Unless I missed something, it's 32 bits. I also tried
a JPEG that I created in Photoshop -- an RGB image that
"just happened" to have R == G == B for all pixels --- and it
displayed a full range of greys. I looked very closely at the
screen, just to see if it were dithering 16 grays to create the
illusion of ~256 grays, and I don't believe it was.

I'll probably try creating the image as a true grayscale as an
experiment later, just to see if the result changes. I didn't
do so originally because I wanted the flexibility of being able
to write a colored image, even if the image was currently
greyscale. In particular, I might want to have a key that
adds some colored dots or squares at key points on the
mage. (Basically, the plugin enables you to dynamically
update the texture from a Python string containing a grayscale
or RGBA image in SGI format.

Here's the code that creates the image:

void InitImageGen(void *imagegen)
VizImageGenObj *self =

strcpy(self->version, "strtexture v1.0");
self->user[0] = 0;
self->dataSize = 0;

int imageWidth = nearbyPowerOf2(self->x);
int imageHeight = nearbyPowerOf2(self->y);
if (imageWidth <= 0 || imageHeight <= 0) {
imageWidth = 256;
imageHeight = 256;

osg::Image *image = new osg::Image;
image->setImage(imageWidth, imageHeight, 1, 4,
new uchar[imageWidth * imageHeight * 4],
self->image = image;
self->texture = new osg::Texture2D(image);

self->status = 1;

And here's the shortest routine I use to write fill the image:

void clearImage(osg::Image *image, uchar red = 0,
uchar green = 0, uchar blue = 0,
uchar alpha = 255)
for (int ii = 0; ii < image->s(); ii++) {
for(int jj = 0; jj < image->t(); jj++) {
uchar *pixel = image->data(ii, jj);
pixel[0] = red;
pixel[1] = green;
pixel[2] = blue;
pixel[3] = alpha;
-- Dan Merget
Reply With Quote
Old 05-03-2005, 01:31 PM
farshizzo farshizzo is offline
WorldViz Team Member
Join Date: Mar 2003
Posts: 2,849

Is the computer that is experiencing this problem using a different graphics card/driver than the other computers?
Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -7. The time now is 06:58 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Copyright 2002-2018 WorldViz LLC