diff --git a/src/osgTerrain/DataSet.cpp b/src/osgTerrain/DataSet.cpp index 631817a48..5ed95223a 100644 --- a/src/osgTerrain/DataSet.cpp +++ b/src/osgTerrain/DataSet.cpp @@ -21,6 +21,8 @@ #include #include +#include + #include #include #include @@ -463,6 +465,21 @@ void DataSet::SourceData::readImage(DestinationData& destination) my_notify(osg::INFO)<<" copying from "<resizeTolerance || + destWindowHeightRatio>resizeTolerance) + { + readWidth = windowWidth; + readHeight = windowHeight; + doResample = true; + } + bool hasRGB = _gdalDataSet->GetRasterCount() >= 3; bool hasColorTable = _gdalDataSet->GetRasterCount() >= 1 && _gdalDataSet->GetRasterBand(1)->GetColorTable(); bool hasGreyScale = _gdalDataSet->GetRasterCount() == 1; @@ -475,12 +492,10 @@ void DataSet::SourceData::readImage(DestinationData& destination) GDALDataType targetGDALType = GDT_Byte; int pixelSpace=3*numBytesPerPixel; - int lineSpace=-(int)(destination._image->getRowSizeInBytes()); - unsigned char* imageData = destination._image->data(destX,destY+destHeight-1); my_notify(osg::INFO) << "reading RGB"<GetRasterBand(3); - bandRed->RasterIO(GF_Read, windowX,_numValuesY-(windowY+windowHeight), + bandRed->RasterIO(GF_Read, + windowX,_numValuesY-(windowY+windowHeight), windowWidth,windowHeight, - (void*)(tempImage+0),destWidth,destHeight, - targetGDALType,pixelSpace,pixelSpace*destWidth); + (void*)(tempImage+0),readWidth,readHeight, + targetGDALType,pixelSpace,pixelSpace*readWidth); bandGreen->RasterIO(GF_Read, windowX,_numValuesY-(windowY+windowHeight), windowWidth,windowHeight, - (void*)(tempImage+1),destWidth,destHeight, - targetGDALType,pixelSpace,pixelSpace*destWidth); + (void*)(tempImage+1),readWidth,readHeight, + targetGDALType,pixelSpace,pixelSpace*readWidth); bandBlue->RasterIO(GF_Read, windowX,_numValuesY-(windowY+windowHeight), windowWidth,windowHeight, - (void*)(tempImage+2),destWidth,destHeight, - targetGDALType,pixelSpace,pixelSpace*destWidth); + (void*)(tempImage+2),readWidth,readHeight, + targetGDALType,pixelSpace,pixelSpace*readWidth); } else if( hasColorTable ) @@ -528,14 +544,14 @@ void DataSet::SourceData::readImage(DestinationData& destination) band->RasterIO(GF_Read, windowX,_numValuesY-(windowY+windowHeight), windowWidth,windowHeight, - (void*)(tempImage+0),destWidth,destHeight, - targetGDALType,pixelSpace,pixelSpace*destWidth); + (void*)(tempImage+0),readWidth,readHeight, + targetGDALType,pixelSpace,pixelSpace*readWidth); ct = band->GetColorTable(); - for( i = 0; i < destWidth * destHeight; i++ ) + for( i = 0; i < readWidth * readHeight; i++ ) { GDALColorEntry sEntry; @@ -569,27 +585,140 @@ void DataSet::SourceData::readImage(DestinationData& destination) band->RasterIO(GF_Read, windowX,_numValuesY-(windowY+windowHeight), windowWidth,windowHeight, - (void*)(tempImage+0),destWidth,destHeight, - targetGDALType,pixelSpace,pixelSpace*destWidth); + (void*)(tempImage+0),readWidth,readHeight, + targetGDALType,pixelSpace,pixelSpace*readWidth); band->RasterIO(GF_Read, windowX,_numValuesY-(windowY+windowHeight), windowWidth,windowHeight, - (void*)(tempImage+1),destWidth,destHeight, - targetGDALType,pixelSpace,pixelSpace*destWidth); + (void*)(tempImage+1),readWidth,readHeight, + targetGDALType,pixelSpace,pixelSpace*readWidth); band->RasterIO(GF_Read, windowX,_numValuesY-(windowY+windowHeight), windowWidth,windowHeight, - (void*)(tempImage+2),destWidth,destHeight, - targetGDALType,pixelSpace,pixelSpace*destWidth); + (void*)(tempImage+2),readWidth,readHeight, + targetGDALType,pixelSpace,pixelSpace*readWidth); } + if (doResample || readWidth!=destWidth || readHeight!=destHeight) + { + 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;j=readWidth) read_i=readWidth-1; + + float flt_read_ir = flt_read_i-read_i; + if (read_i==readWidth-1) flt_read_ir=0.0f; + + int read_j = (int)flt_read_j; + if (read_j>=readHeight) read_j=readHeight-1; + + float flt_read_jr = flt_read_j-read_j; + if (read_j==readHeight-1) flt_read_jr=0.0f; + + unsigned char* dest = destImage + (j*destWidth + i) * pixelSpace; + if (flt_read_ir==0.0f) // no need to interpolate i axis. + { + if (flt_read_jr==0.0f) // no need to interpolate j axis. + { + // copy pixels + unsigned char* src = tempImage + (read_j*readWidth + read_i) * pixelSpace; + dest[0] = src[0]; + dest[1] = src[1]; + dest[2] = src[2]; + //std::cout<<"copy"<data(destX,destY+destHeight-1); + unsigned int destinationRowDelta = -(int)(destination._image->getRowSizeInBytes()); + // copy image to destination image for(int row=0; row_image = new osg::Image; + + std::string imageExension(".dds"); // ".rgb" + //std::string imageExension(".jp2"); // ".rgb" + std::string imageName(_name+imageExension); + _imagery->_image->setFileName(imageName.c_str()); + _imagery->_image->allocateImage(texture_numColumns,texture_numRows,1,GL_RGB,GL_UNSIGNED_BYTE); unsigned char* data = _imagery->_image->data(); unsigned int totalSize = _imagery->_image->getTotalSizeInBytesIncludingMipmaps();