[osg-users] To Convert QImage to OSG::Image

246 views
Skip to first unread message

manish Choudhary

unread,
Dec 4, 2014, 5:12:53 AM12/4/14
to osg-...@lists.openscenegraph.org
Hi,

In my application I required to convert QImage to osg::Image to use that as a texture to wrap it on model.
What is the best way to convert QImage to Osg::Image ?


Thank you!

Cheers,
manish

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





_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Gianni Ambrosio

unread,
Dec 4, 2014, 5:38:53 AM12/4/14
to osg-...@lists.openscenegraph.org
Hi Manish,
I'm just struggling with the same problem. I found on internet (maybe in this forum) a code example and I implemented the following method:


Code:
osg::Image* convertImage(const QImage& iImage)
{
osg::Image* osgImage = new osg::Image();
if (false == iImage.isNull()) {
QImage glImage = QGLWidget::convertToGLFormat(iImage);
if (false == glImage.isNull()) {
unsigned char* data = new unsigned char[glImage.byteCount()];
for(int i=0; i < glImage.byteCount(); ++i) {
data[i] = glImage.bits()[i];
}
osgImage->setImage(glImage.width(), glImage.height(), 1, 4, GL_RGBA, GL_UNSIGNED_BYTE, data, osg::Image::USE_NEW_DELETE, 1);
}
}
return osgImage;
}



Anyway, in my case this is not working well. A part from that, I'm not sure about "data" lifecycle.

Moreover, looking at

<OpenSceneGraph>\src\osgQt\QGraphicsViewAdapter.cpp

you can find around informations on how to implement it. I'm currently investigating.

Cheers,
Gianni

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

Julien Valentin

unread,
Dec 4, 2014, 6:19:21 AM12/4/14
to osg-...@lists.openscenegraph.org
Hi,
Here's some old code i used..
Ther Problem is that it only work for RGBA16F image..I recommend to make usage specific code...unless you're ready to map all osg pixelformats to Qt's;

Code:
void ManagedImage::load(const QImage&im){

for( int row=0;( int )row<im.width();row++)
for( int col=0;( int )col<im.height();col++){


setData(row,col,0,QColor(im.pixel(row,col)).redF());

setData(row,col,1,QColor(im.pixel(row,col)).greenF());
setData(row,col,2,QColor(im.pixel(row,col)).blueF());
setData(row,col,3,QColor(im.pixel(row,col)).alphaF());

}
}
QImage & ManagedImage::toQImage(){
QColor color;
QImage im(QSize(Width(),Height()),QImage::Format_RGBA32);
float pix;
for( int row=0;( int )row<Width();row++)
for( int col=0;( int )col<Height();col++){
color.setRgbF(0,pix,0);
im.setPixel(row,col,(int )(color.rgb()));

}
return im;
}


...

Thank you!

Cheers,
Julien

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

Jason MacDonald

unread,
Dec 4, 2014, 8:24:34 AM12/4/14
to osg-...@lists.openscenegraph.org
Here is my method:

QImage glimg(QGLWidget::convertToGLFormat(img->image())); //image() is the QImage
osg::Image* osgImg = new osg::Image;
unsigned char* bits = new unsigned char[glimg.byteCount()];
memcpy(bits, glimg.bits(),glimg.byteCount());
osgImg->setImage(img->pixelWidth(), img->pixelHeight(), 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, bits, osg::Image::USE_NEW_DELETE);
This email and any files transmitted with it from The Charles Machine Works, Inc. are confidential and intended solely for the use of the individual or entity to which they are addressed. If you have received this email in error please notify the sender. Our company accepts no liability for the contents of this email, or for the consequences of any actions taken on the basis of the information provided, unless that information is subsequently confirmed in writing. Please note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the company. Finally, the recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.

manish Choudhary

unread,
Dec 4, 2014, 8:52:21 AM12/4/14
to osg-...@lists.openscenegraph.org
Hi,

Thanks a lot.. all of the method suggested is working in my application.

Thank you!

Cheers,
manish

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61980#61980
Reply all
Reply to author
Forward
0 new messages