Hi everyone,

I am trying to dynamically convert several images from rgb to rgba however it 
has been very difficult to achieve my goal.
I have a few rgb images with black background though what I want is to convert 
the black background into transparency.

So far I haven't found any method that performs this automatically so I tried 
to create a blank image adding an alpha channel. To each pixel I stored the 
original image RGB data and set the value 255 to alpha channel, in order to 
have as output the original image but with an alpha channel.

The code bellow was developed to have as output an image similar to the 
original but with alpha channel:


Code:

osg::Image * image_rgb = osgDB::readImageFile(texture_file);
osg::Image * image_rgba = new osg::Image;

int height = image_rgb->s();
int width = image_rgb->t();
int length = image_rgb->r();
const long size = width * height * length * 4;

unsigned char* rgb_data = image_rgb->data();
unsigned char* rgba_data = (unsigned char*)calloc(size,sizeof(unsigned char));

for(long i = 0; i < size; i+= 4)
{
rgba_data[i + 0] = rgb_data[i + 0]; //red
rgba_data[i + 1] = rgb_data[i + 1]; //green
rgba_data[i + 2] = rgb_data[i + 2]; //blue
rgba_data[i + 3] = 255; //alpha
}

image_rgba->setImage(width, height, length, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 
rgba_data, osg::Image::AllocationMode::NO_DELETE);




The left image represent the original RGB image, the center image represents 
the output form the code above and the right image is the expected result.

[Image: http://imageshack.us/photo/my-images/201/osgforum.png ]

Any suggestion or correction about the code I developed will be well received!

Thank you!

Cheers,
Jorge d'Alpuim

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=48744#48744





_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to