From 9781fe55ea606cb7d2c23bd0d191ca7d18c31bf4 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 20 Jun 2011 12:36:53 +0000 Subject: [PATCH] From Farshid Lashkari, BGR write support for BMP, PNG and TGA --- src/osgPlugins/bmp/ReaderWriterBMP.cpp | 17 +++++++++++++---- src/osgPlugins/png/ReaderWriterPNG.cpp | 2 ++ src/osgPlugins/tga/ReaderWriterTGA.cpp | 19 ++++++++++++++----- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/osgPlugins/bmp/ReaderWriterBMP.cpp b/src/osgPlugins/bmp/ReaderWriterBMP.cpp index 97f34f94d..934d57c12 100644 --- a/src/osgPlugins/bmp/ReaderWriterBMP.cpp +++ b/src/osgPlugins/bmp/ReaderWriterBMP.cpp @@ -513,7 +513,16 @@ static bool bmp_save(const osg::Image& img, std::ostream& fout) fout.write((char*) &dib, sizeof(dib)); } - const unsigned int channelsPerPixel = img.computeNumComponents(img.getPixelFormat()); + unsigned int pixelFormat = img.getPixelFormat(); + + unsigned int r = 0, g = 1, b = 2; + if ( pixelFormat == GL_BGR || pixelFormat == GL_BGRA ) + { + r = 2; + b = 0; + } + + const unsigned int channelsPerPixel = img.computeNumComponents(pixelFormat); std::vector rowBuffer(bytesPerRowAlign); for (int y = 0; y < img.t(); ++y) @@ -523,9 +532,9 @@ static bool bmp_save(const osg::Image& img, std::ostream& fout) { // RGB -> BGR unsigned int rowOffs = x * 3, imgOffs = x * channelsPerPixel; - rowBuffer[rowOffs + 2] = imgp[imgOffs + 0]; - rowBuffer[rowOffs + 1] = imgp[imgOffs + 1]; - rowBuffer[rowOffs + 0] = imgp[imgOffs + 2]; + rowBuffer[rowOffs + 2] = imgp[imgOffs + r]; + rowBuffer[rowOffs + 1] = imgp[imgOffs + g]; + rowBuffer[rowOffs + 0] = imgp[imgOffs + b]; } fout.write((char*) &*rowBuffer.begin(), rowBuffer.size()); } diff --git a/src/osgPlugins/png/ReaderWriterPNG.cpp b/src/osgPlugins/png/ReaderWriterPNG.cpp index 48de02cf6..8ab3da3d4 100644 --- a/src/osgPlugins/png/ReaderWriterPNG.cpp +++ b/src/osgPlugins/png/ReaderWriterPNG.cpp @@ -118,6 +118,8 @@ class ReaderWriterPNG : public osgDB::ReaderWriter case(GL_LUMINANCE_ALPHA): color = PNG_COLOR_TYPE_GRAY_ALPHA ; break; case(GL_RGB): color = PNG_COLOR_TYPE_RGB; break; case(GL_RGBA): color = PNG_COLOR_TYPE_RGB_ALPHA; break; + case(GL_BGR): color = PNG_COLOR_TYPE_RGB; png_set_bgr(png); break; + case(GL_BGRA): color = PNG_COLOR_TYPE_RGB_ALPHA; png_set_bgr(png); break; default: return WriteResult::ERROR_IN_WRITING_FILE; break; } diff --git a/src/osgPlugins/tga/ReaderWriterTGA.cpp b/src/osgPlugins/tga/ReaderWriterTGA.cpp index e56995cad..57fc5d7b1 100644 --- a/src/osgPlugins/tga/ReaderWriterTGA.cpp +++ b/src/osgPlugins/tga/ReaderWriterTGA.cpp @@ -549,8 +549,9 @@ class ReaderWriterTGA : public osgDB::ReaderWriter // Other data types can be added soon with different options // The format description can be found at: // http://local.wasp.uwa.edu.au/~pbourke/dataformats/tga/ + unsigned int pixelFormat = image.getPixelFormat(); int width = image.s(), height = image.t(); - int numPerPixel = image.computeNumComponents(image.getPixelFormat()); + int numPerPixel = image.computeNumComponents(pixelFormat); int pixelMultiplier = (image.getDataType()==GL_FLOAT ? 255 : 1); const unsigned char* data = image.data(); if ( !data ) return false; @@ -569,6 +570,14 @@ class ReaderWriterTGA : public osgDB::ReaderWriter fout.put(numPerPixel * 8); // Image pixel size fout.put(0); // Image descriptor + // Swap red/blue channels for BGR images + int r = 0, g = 1, b = 2; + if( pixelFormat == GL_BGR || pixelFormat == GL_BGRA ) + { + r = 2; + b = 0; + } + // Data for (int y=0; y