diff --git a/include/osgTerrain/DataSet b/include/osgTerrain/DataSet index 551fa55a3..24a1a2dab 100644 --- a/include/osgTerrain/DataSet +++ b/include/osgTerrain/DataSet @@ -760,6 +760,7 @@ class OSGTERRAIN_EXPORT DataSet : public osg::Referenced unsigned int _level; unsigned int _tileX; unsigned int _tileY; + GLenum _pixelFormat; struct ImageData { diff --git a/src/osgTerrain/DataSet.cpp b/src/osgTerrain/DataSet.cpp index 21ab9b384..b6285fa47 100644 --- a/src/osgTerrain/DataSet.cpp +++ b/src/osgTerrain/DataSet.cpp @@ -488,8 +488,10 @@ void DataSet::SourceData::readImage(DestinationData& destination) } bool hasRGB = _gdalDataSet->GetRasterCount() >= 3; + bool hasAlpha = _gdalDataSet->GetRasterCount() >= 4; bool hasColorTable = _gdalDataSet->GetRasterCount() >= 1 && _gdalDataSet->GetRasterBand(1)->GetColorTable(); bool hasGreyScale = _gdalDataSet->GetRasterCount() == 1; + unsigned int numSourceComponents = hasAlpha?4:3; if (hasRGB || hasColorTable || hasGreyScale) { @@ -498,7 +500,7 @@ void DataSet::SourceData::readImage(DestinationData& destination) unsigned int numBytesPerPixel = 1; GDALDataType targetGDALType = GDT_Byte; - int pixelSpace=3*numBytesPerPixel; + int pixelSpace=numSourceComponents*numBytesPerPixel; my_notify(osg::INFO) << "reading RGB"<GetRasterBand(1); - bandGreen = _gdalDataSet->GetRasterBand(2); - bandBlue = _gdalDataSet->GetRasterBand(3); - + GDALRasterBand* bandRed = bandRed = _gdalDataSet->GetRasterBand(1); + GDALRasterBand* bandGreen = _gdalDataSet->GetRasterBand(2); + GDALRasterBand* bandBlue = _gdalDataSet->GetRasterBand(3); + GDALRasterBand* bandAlpha = hasAlpha ? _gdalDataSet->GetRasterBand(4) : 0; bandRed->RasterIO(GF_Read, windowX,_numValuesY-(windowY+windowHeight), @@ -534,6 +533,15 @@ void DataSet::SourceData::readImage(DestinationData& destination) windowWidth,windowHeight, (void*)(tempImage+2),readWidth,readHeight, targetGDALType,pixelSpace,pixelSpace*readWidth); + + if (bandAlpha) + { + bandAlpha->RasterIO(GF_Read, + windowX,_numValuesY-(windowY+windowHeight), + windowWidth,windowHeight, + (void*)(tempImage+3),readWidth,readHeight, + targetGDALType,pixelSpace,pixelSpace*readWidth); + } } else if( hasColorTable ) @@ -610,24 +618,6 @@ void DataSet::SourceData::readImage(DestinationData& destination) { unsigned char* destImage = new unsigned char[destWidth*destHeight*pixelSpace]; -#if 0 - // glu scale appears buggy... - glPixelStorei(GL_PACK_ALIGNMENT,1); - glPixelStorei(GL_PACK_ROW_LENGTH,0); - glPixelStorei(GL_UNPACK_ALIGNMENT,1); - glPixelStorei(GL_UNPACK_ROW_LENGTH,0); - GLint status = gluScaleImage(GL_RGB, - readWidth, - readHeight, - GL_UNSIGNED_BYTE, - tempImage, - destWidth, - destHeight, - GL_UNSIGNED_BYTE, - destImage); - -#else - // rescale image by hand as glu seem buggy.... for(int j=0;jdata(destX,destY+destHeight-1); unsigned int destinationRowDelta = -(int)(destination._image->getRowSizeInBytes()); + unsigned int destination_pixelSpace = destination._image->getPixelSizeInBits()/8; + bool destination_hasAlpha = osg::Image::computeNumComponents(destination._image->getPixelFormat()); // copy image to destination image for(int row=0; @@ -732,28 +726,43 @@ void DataSet::SourceData::readImage(DestinationData& destination) for(int col=0; coldestinationTotal) + if (destination_hasAlpha) + destinationColumnPtr[3] = sourceColumnPtr[3]; + } + else + { + // source value isn't full on so blend it with destination + float rs = (float)sourceColumnPtr[3]/255.0f; + float rd = 1.0f-rs; + + destinationColumnPtr[0] = (int)(rd * (float)destinationColumnPtr[0] + rs * (float)sourceColumnPtr[0]); + destinationColumnPtr[1] = (int)(rd * (float)destinationColumnPtr[1] + rs * (float)sourceColumnPtr[1]); + destinationColumnPtr[2] = (int)(rd * (float)destinationColumnPtr[2] + rs * (float)sourceColumnPtr[2]); + + + } + } + } + else if (sourceColumnPtr[0]!=0 || sourceColumnPtr[1]!=0 || sourceColumnPtr[2]!=0) { - // copy pixel across destinationColumnPtr[0] = sourceColumnPtr[0]; destinationColumnPtr[1] = sourceColumnPtr[1]; destinationColumnPtr[2] = sourceColumnPtr[2]; } -#else - if (sourceColumnPtr[0]!=0 || sourceColumnPtr[1]!=0 || sourceColumnPtr[2]!=0) - { - // copy pixel across - destinationColumnPtr[0] = sourceColumnPtr[0]; - destinationColumnPtr[1] = sourceColumnPtr[1]; - destinationColumnPtr[2] = sourceColumnPtr[2]; - } -#endif } } @@ -1402,6 +1411,7 @@ DataSet::DestinationTile::DestinationTile(): _level(0), _tileX(0), _tileY(0), + _pixelFormat(GL_RGB), _maxSourceLevel(0), _imagery_maxNumColumns(4096), _imagery_maxNumRows(4096), @@ -1549,7 +1559,7 @@ void DataSet::DestinationTile::allocate() std::string imageName(_name+imageExension); imageData._imagery->_image->setFileName(imageName.c_str()); - imageData._imagery->_image->allocateImage(texture_numColumns,texture_numRows,1,GL_RGB,GL_UNSIGNED_BYTE); + imageData._imagery->_image->allocateImage(texture_numColumns,texture_numRows,1,_pixelFormat,GL_UNSIGNED_BYTE); unsigned char* data = imageData._imagery->_image->data(); unsigned int totalSize = imageData._imagery->_image->getTotalSizeInBytesIncludingMipmaps(); for(unsigned int i=0;i