#include #include #include #include #include #include #include #include class ReaderWriterGDAL : public osgDB::ReaderWriter { public: virtual const char* className() { return "GDAL Image Reader"; } virtual bool acceptsExtension(const std::string& extension) { return osgDB::equalCaseInsensitive(extension,"gdal") || osgDB::equalCaseInsensitive(extension,"gdal"); } virtual ReadResult readImage(const std::string& fileName, const osgDB::ReaderWriter::Options* options) { //std::string ext = osgDB::getFileExtension(fileName); //if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; initGDAL(); std::auto_ptr dataset((GDALDataset*)GDALOpen(fileName.c_str(),GA_ReadOnly)); if (!dataset.get()) return ReadResult::FILE_NOT_HANDLED; int dataWidth = dataset->GetRasterXSize(); int dataHeight = dataset->GetRasterYSize(); int windowX = 0; int windowY = 0; int windowWidth = dataWidth; int windowHeight = dataHeight; int destX = 0; int destY = 0; int destWidth = osg::minimum(dataWidth,1024); int destHeight = osg::minimum(dataHeight,1024); const osgDB::ImageOptions* imageOptions = dynamic_cast(options); if (imageOptions) { std::cout<<"Got ImageOptions"<_sourceImageWindowMode) { case(osgDB::ImageOptions::RATIO_WINDOW): windowX = osg::maximum((int)(floor((double)dataWidth * imageOptions->_sourceRatioWindow.windowX)),0); windowY = osg::maximum((int)(floor((double)dataHeight * imageOptions->_sourceRatioWindow.windowY)),0); windowWidth = osg::minimum((int)(ceil((double)dataWidth * (imageOptions->_sourceRatioWindow.windowX + imageOptions->_sourceRatioWindow.windowWidth))),dataWidth)-windowX; windowHeight = osg::minimum((int)(ceil((double)dataHeight * (imageOptions->_sourceRatioWindow.windowY + imageOptions->_sourceRatioWindow.windowHeight))),dataHeight)-windowY; break; case(osgDB::ImageOptions::PIXEL_WINDOW): windowX = imageOptions->_sourcePixelWindow.windowX; windowY = imageOptions->_sourcePixelWindow.windowY; windowWidth = imageOptions->_sourcePixelWindow.windowWidth; windowHeight = imageOptions->_sourcePixelWindow.windowHeight; break; default: // leave source window dimensions as whole image. break; } switch(imageOptions->_destinationImageWindowMode) { case(osgDB::ImageOptions::RATIO_WINDOW): destX = (unsigned int)(floor((double)dataWidth * imageOptions->_destinationRatioWindow.windowX)); destY = (unsigned int)(floor((double)dataHeight * imageOptions->_destinationRatioWindow.windowY)); destWidth = (unsigned int)(ceil((double)dataWidth * (imageOptions->_destinationRatioWindow.windowX + imageOptions->_destinationRatioWindow.windowWidth)))-windowX; destHeight = (unsigned int)(ceil((double)dataHeight * (imageOptions->_destinationRatioWindow.windowY + imageOptions->_destinationRatioWindow.windowHeight)))-windowY; break; case(osgDB::ImageOptions::PIXEL_WINDOW): destX = imageOptions->_destinationPixelWindow.windowX; destY = imageOptions->_destinationPixelWindow.windowY; destWidth = imageOptions->_destinationPixelWindow.windowWidth; destHeight = imageOptions->_destinationPixelWindow.windowHeight; break; default: // leave source window dimensions as whole image. break; } } // windowX = 0; // windowY = 0; // windowWidth = destWidth; // windowHeight = destHeight; std::cout << " windowX = "<GetRasterCount()<GetProjectionRef()<GetGeoTransform(geoTransform)!=CE_None) { std::cout << " GetGeoTransform "<< std::endl; std::cout << " Origin = "<GetRasterCount(); GDALRasterBand* bandGray = 0; GDALRasterBand* bandRed = 0; GDALRasterBand* bandGreen = 0; GDALRasterBand* bandBlue = 0; GDALRasterBand* bandAlpha = 0; for(int b=1;b<=numBands;++b) { GDALRasterBand* band = dataset->GetRasterBand(b); std::cout << " Band "<RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(imageData+0),destWidth,destHeight,GDT_Byte,pixelSpace,lineSpace); bandGreen->RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(imageData+1),destWidth,destHeight,GDT_Byte,pixelSpace,lineSpace); bandBlue->RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(imageData+2),destWidth,destHeight,GDT_Byte,pixelSpace,lineSpace); bandAlpha->RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(imageData+3),destWidth,destHeight,GDT_Byte,pixelSpace,lineSpace); } else { // RGB int pixelSpace=3; int lineSpace=destWidth * pixelSpace; imageData = new unsigned char[destWidth * destHeight * pixelSpace]; pixelFormat = GL_RGB; internalFormat = GL_RGB; dataType = GL_UNSIGNED_BYTE; std::cout << "reading RGB"<RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(imageData+0),destWidth,destHeight,GDT_Byte,pixelSpace,lineSpace); bandGreen->RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(imageData+1),destWidth,destHeight,GDT_Byte,pixelSpace,lineSpace); bandBlue->RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(imageData+2),destWidth,destHeight,GDT_Byte,pixelSpace,lineSpace); } } else if (bandGray) { if (bandAlpha) { // Luminance alpha int pixelSpace=2; int lineSpace=destWidth * pixelSpace; imageData = new unsigned char[destWidth * destHeight * pixelSpace]; pixelFormat = GL_LUMINANCE_ALPHA; internalFormat = GL_LUMINANCE_ALPHA; dataType = GL_UNSIGNED_BYTE; std::cout << "reading grey + alpha"<RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(imageData+0),destWidth,destHeight,GDT_Byte,pixelSpace,lineSpace); bandAlpha->RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(imageData+1),destWidth,destHeight,GDT_Byte,pixelSpace,lineSpace); } else { // Luminance map int pixelSpace=1; int lineSpace=destWidth * pixelSpace; imageData = new unsigned char[destWidth * destHeight * pixelSpace]; pixelFormat = GL_LUMINANCE; internalFormat = GL_LUMINANCE; dataType = GL_UNSIGNED_BYTE; std::cout << "reading grey"<RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(imageData+0),destWidth,destHeight,GDT_Byte,pixelSpace,lineSpace); } } else if (bandAlpha) { // alpha map (treat as a Luminance map) int pixelSpace=1; int lineSpace=destWidth * pixelSpace; imageData = new unsigned char[destWidth * destHeight * pixelSpace]; pixelFormat = GL_LUMINANCE; internalFormat = GL_LUMINANCE; dataType = GL_UNSIGNED_BYTE; std::cout << "reading alpha"<RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(imageData+0),destWidth,destHeight,GDT_Byte,pixelSpace,lineSpace); } else { std::cout << "not found any usable bands in file."<setFileName(fileName.c_str()); image->setImage(s,t,r, internalFormat, pixelFormat, dataType, (unsigned char *)imageData, osg::Image::USE_NEW_DELETE); image->flipVertical(); return image; } return 0; } void initGDAL() { static bool s_initialized = false; if (!s_initialized) { s_initialized = true; GDALAllRegister(); } } }; // now register with Registry to instantiate the above // reader/writer. osgDB::RegisterReaderWriterProxy g_readerWriter_GDAL_Proxy;