Hi there, 

I am trying to load an osg::Image using the setImage method - inparticular I am 
copying a Managed bitmap (System.Drawing.Bitmap) to the osg::Image to use it as 
a texture map. 

As you can see I'm having a few problems. Here's the osg::Image texture mapped 
onto a quad when I load it in from file using osgDB::readImageFile

[Image: http://i137.photobucket.com/albums/q217/andyb1979/tquadreadimage.png ]

Here's the same textured quad, but the osg::Image was created using setImage 
and a copy from a managed bitmap

[Image: http://i137.photobucket.com/albums/q217/andyb1979/th_tquadsetimage.png ]

As you can see the image seems to have been inverted and flipped!

Here is my code to convert a managed bitmap to osg::Image. The input bitmap has 
been loaded from file, is in the format 24bpp RGB. 

I know I'm getting the size/bits per pixel right as the output osg::Image is 
the right shape and size, with no tearing. However I don't know why the image 
is inverted!


Code:

osg::ref_ptr<osg::Image> 
SceneUtil::ManagedBitmapToOSGImage(System::Drawing::Bitmap ^ bitmap)
{
        //
        // Check params
        //

        if (bitmap == nullptr)
        {
                throw gcnew Exception("Unable to convert 
System::Drawing::Bitmap to osg::Image as the input image is null");
        }

        if (bitmap->PixelFormat != 
System::Drawing::Imaging::PixelFormat::Format24bppRgb)
        {
                throw gcnew Exception("Unable to convert 
System::Drawing::Bitmap to osg::Image as the input image must be in the format 
Format24bppRgb");
        }

        // Create a new OSG Image
        osg::ref_ptr<osg::Image> image = new osg::Image();

        System::Drawing::Imaging::BitmapData ^ bitmapData = bitmap->LockBits(
                System::Drawing::Rectangle(0, 0, bitmap->Width, 
bitmap->Height), 
                System::Drawing::Imaging::ImageLockMode::ReadOnly, 
                System::Drawing::Imaging::PixelFormat::Format24bppRgb);

        // Create data to hold the destination image
        BYTE * ptrSrcImage = (BYTE*)bitmapData->Scan0.ToPointer();
        BYTE * ptrDestImage = new unsigned char [bitmap->Width * bitmap->Height 
* 3];           
        BYTE * ptrSourceRow = nullptr;
        BYTE * ptrDestRow = nullptr;

        int iWidth = bitmapData->Width;
        int iHeight = bitmapData->Height;
        int iStride = bitmapData->Stride;
        
        // Copy the System::Drawing::Bitmap instance over line by line - this 
gets around the 
        // lack of stride support in the osg::Image. 
        for(int i = 0; i < iHeight; i++)
        {
                // Get the source row pointer
                ptrSourceRow = ptrSrcImage + (i * iStride);

                // Get the destination row pointer
                ptrDestRow = ptrDestImage + (i * (iWidth * 3));

                // Copy the source row to the destination row
                memcpy(ptrDestRow, ptrSourceRow, iWidth * 3);
        }

        // Set the data on the osg::Image
        image->setImage(
                bitmap->Width, 
                bitmap->Height, 
                1, 
                GL_RGB,
                GL_RGB,
                GL_UNSIGNED_BYTE, 
                ptrDestImage, 
                osg::Image::USE_NEW_DELETE);    

        bitmap->UnlockBits(bitmapData);

        return image;
}




Any ideas?

Thank you!

Andrew

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





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

Reply via email to