From f2b2f00ea1c28ac3d2a541ed3de82ae9f3cc871c Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 29 Nov 2006 20:10:08 +0000 Subject: [PATCH] From Per Fahlberg, added support for writing 16-bit rgb images --- src/osgPlugins/rgb/ReaderWriterRGB.cpp | 46 ++++++++++++++++++-------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/src/osgPlugins/rgb/ReaderWriterRGB.cpp b/src/osgPlugins/rgb/ReaderWriterRGB.cpp index 95c8d6554..c1f2b89bb 100644 --- a/src/osgPlugins/rgb/ReaderWriterRGB.cpp +++ b/src/osgPlugins/rgb/ReaderWriterRGB.cpp @@ -547,7 +547,10 @@ class ReaderWriterRGB : public osgDB::ReaderWriter GLenum pixelFormat = img.getPixelFormat(); - raw.dim = + raw.dim = 3; + raw.sizeX = img.s(); + raw.sizeY = img.t(); + raw.sizeZ = pixelFormat == GL_COLOR_INDEX? 1 : pixelFormat == GL_RED? 1 : pixelFormat == GL_GREEN? 1 : @@ -559,31 +562,48 @@ class ReaderWriterRGB : public osgDB::ReaderWriter pixelFormat == GL_BGRA? 4 : pixelFormat == GL_LUMINANCE? 1 : pixelFormat == GL_LUMINANCE_ALPHA ? 2 : 1; - - raw.sizeX = img.s(); - raw.sizeY = img.t(); - raw.sizeZ = raw.dim; raw.min = 0; raw.max = 0xFF; raw.wasteBytes = 0; strncpy( raw.name, name.c_str(), 80); raw.colorMap = 0; + raw.bpc = (img.getPixelSizeInBits()/raw.sizeZ)/8; int isize = img.getImageSizeInBytes(); unsigned char *buffer = new unsigned char[isize]; - unsigned char *dptr = buffer; - int i, j; - for( i = 0; i < raw.sizeZ; i++ ) + if(raw.bpc == 1) { - const unsigned char *ptr = img.data(); - ptr += i; - for( j = 0; j < isize/raw.sizeZ; j++ ) + unsigned char *dptr = buffer; + int i, j; + for( i = 0; i < raw.sizeZ; ++i ) { - *(dptr++) = *ptr; - ptr += raw.sizeZ; + const unsigned char *ptr = img.data(); + ptr += i; + for( j = 0; j < isize/raw.sizeZ; ++j ) + { + *(dptr++) = *ptr; + ptr += raw.sizeZ; + } + } + } + else + { // bpc == 2 + unsigned short *dptr = reinterpret_cast(buffer); + int i, j; + for( i = 0; i < raw.sizeZ; ++i ) + { + const unsigned short *ptr = reinterpret_cast(img.data()); + ptr += i; + for( j = 0; j < isize/(raw.sizeZ*2); ++j ) + { + *dptr = *ptr; + ConvertShort(dptr++, 1); + ptr += raw.sizeZ; + } } } + if( raw.needsBytesSwapped() ) raw.swapBytes();