Skip to content

Commit

Permalink
Merged from svn/trunk, r12574.
Browse files Browse the repository at this point in the history
From Farshid Lashkari, BGR write support for BMP, PNG and TGA
  • Loading branch information
robertosfield committed Jun 20, 2011
1 parent 4e660db commit f5cb210
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
17 changes: 13 additions & 4 deletions src/osgPlugins/bmp/ReaderWriterBMP.cpp
Expand Up @@ -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<unsigned char> rowBuffer(bytesPerRowAlign);
for (int y = 0; y < img.t(); ++y)
Expand All @@ -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());
}
Expand Down
2 changes: 2 additions & 0 deletions src/osgPlugins/png/ReaderWriterPNG.cpp
Expand Up @@ -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;
}

Expand Down
19 changes: 14 additions & 5 deletions src/osgPlugins/tga/ReaderWriterTGA.cpp
Expand Up @@ -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;
Expand All @@ -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<height; ++y)
{
Expand All @@ -579,12 +588,12 @@ class ReaderWriterTGA : public osgDB::ReaderWriter
switch ( numPerPixel )
{
case 3: // BGR
fout.put(ptr[off+2] * pixelMultiplier); fout.put(ptr[off+1] * pixelMultiplier);
fout.put(ptr[off+0] * pixelMultiplier);
fout.put(ptr[off+b] * pixelMultiplier); fout.put(ptr[off+g] * pixelMultiplier);
fout.put(ptr[off+r] * pixelMultiplier);
break;
case 4: // BGRA
fout.put(ptr[off+2] * pixelMultiplier); fout.put(ptr[off+1] * pixelMultiplier);
fout.put(ptr[off+0] * pixelMultiplier); fout.put(ptr[off+3] * pixelMultiplier);
fout.put(ptr[off+b] * pixelMultiplier); fout.put(ptr[off+g] * pixelMultiplier);
fout.put(ptr[off+r] * pixelMultiplier); fout.put(ptr[off+3] * pixelMultiplier);
break;
default:
return false;
Expand Down

0 comments on commit f5cb210

Please sign in to comment.