From Farshid Lashkari, BGR write support for BMP, PNG and TGA
This commit is contained in:
@@ -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)
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<height; ++y)
|
||||
{
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user