- Timestamp:
- 03/21/12 18:36:20 (14 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgPlugins/gdal/ReaderWriterGDAL.cpp
r12292 r13041 1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2007 Robert Osfield 1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2007 Robert Osfield 2 2 * 3 * This library is open source and may be redistributed and/or modified under 4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 3 * This library is open source and may be redistributed and/or modified under 4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 5 5 * (at your option) any later version. The full license is in LICENSE file 6 6 * included with this distribution, and on the openscenegraph.org website. 7 * 7 * 8 8 * This library is distributed in the hope that it will be useful, 9 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 11 * OpenSceneGraph Public License for more details. 12 12 */ … … 33 33 #include "DataSetLayer.h" 34 34 35 #define SERIALIZER() OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_serializerMutex) 35 #define SERIALIZER() OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_serializerMutex) 36 36 37 37 // From easyrgb.com … … 49 49 { 50 50 public: 51 51 52 52 ReaderWriterGDAL() 53 53 { 54 54 supportsExtension("gdal","GDAL Image reader"); 55 55 } 56 56 57 57 virtual const char* className() const { return "GDAL Image Reader"; } 58 58 … … 60 60 { 61 61 if (file.empty()) return ReadResult::FILE_NOT_FOUND; 62 62 63 63 if (osgDB::equalCaseInsensitive(osgDB::getFileExtension(file),"gdal")) 64 64 { … … 76 76 osg::ref_ptr<GDALPlugin::DataSetLayer> dataset = new GDALPlugin::DataSetLayer(fileName); 77 77 dataset->setGdalReader(this); 78 78 79 79 if (dataset->isOpen()) return dataset.release(); 80 80 81 81 return ReadResult::FILE_NOT_HANDLED; 82 82 } … … 85 85 { 86 86 if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; 87 87 88 88 if (osgDB::equalCaseInsensitive(osgDB::getFileExtension(fileName),"gdal")) 89 89 { … … 94 94 return const_cast<ReaderWriterGDAL*>(this)->local_readImage(fileName, options); 95 95 } 96 96 97 97 virtual ReadResult readHeightField(const std::string& fileName, const osgDB::ReaderWriter::Options* options) const 98 98 { 99 99 if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; 100 100 101 101 if (osgDB::equalCaseInsensitive(osgDB::getFileExtension(fileName),"gdal")) 102 102 { … … 126 126 std::auto_ptr<GDALDataset> dataset((GDALDataset*)GDALOpen(fileName.c_str(),GA_ReadOnly)); 127 127 if (!dataset.get()) return ReadResult::FILE_NOT_HANDLED; 128 128 129 129 int dataWidth = dataset->GetRasterXSize(); 130 130 int dataHeight = dataset->GetRasterYSize(); 131 131 132 132 int windowX = 0; 133 133 int windowY = 0; … … 149 149 { 150 150 OSG_INFO<<"Got ImageOptions"<<std::endl; 151 151 152 152 int margin = 0; 153 153 switch(imageOptions->_sourceImageWindowMode) … … 159 159 double desiredWidth = (double)dataWidth * imageOptions->_sourceRatioWindow.windowWidth; 160 160 double desiredHeight = (double)dataHeight * imageOptions->_sourceRatioWindow.windowHeight; 161 161 162 162 windowX = osg::maximum((int)(floor(desiredX))-margin,0); 163 163 windowY = osg::maximum((int)(floor(desiredY))-margin,0); 164 164 windowWidth = osg::minimum((int)(ceil(desiredX + desiredWidth))+margin,dataWidth)-windowX; 165 165 windowHeight = osg::minimum((int)(ceil(desiredY + desiredHeight))+margin,dataHeight)-windowY; 166 166 167 167 texCoordRange = new osgDB::ImageOptions::TexCoordRange; 168 168 texCoordRange->set((desiredX-(double)windowX)/(double)windowWidth, … … 183 183 break; 184 184 } 185 186 // reapply the window coords to the pixel window so that calling code 185 186 // reapply the window coords to the pixel window so that calling code 187 187 // knows the original pixel size 188 188 imageOptions->_sourcePixelWindow.windowX = windowX; … … 210 210 } 211 211 212 } 213 214 // windowX = 0; 215 // windowY = 0; 212 } 213 214 // windowX = 0; 215 // windowY = 0; 216 216 // windowWidth = destWidth; 217 217 // windowHeight = destHeight; … … 230 230 231 231 OSG_INFO << std::endl; 232 232 233 233 OSG_INFO << " GetRaterCount() "<< dataset->GetRasterCount()<<std::endl; 234 234 OSG_INFO << " GetProjectionRef() "<< dataset->GetProjectionRef()<<std::endl; 235 236 235 236 237 237 double geoTransform[6]; 238 238 if (dataset->GetGeoTransform(geoTransform)==CE_None) … … 245 245 246 246 int numBands = dataset->GetRasterCount(); 247 248 247 248 249 249 GDALRasterBand* bandGray = 0; 250 250 GDALRasterBand* bandRed = 0; … … 253 253 GDALRasterBand* bandAlpha = 0; 254 254 GDALRasterBand* bandPalette = 0; 255 255 256 256 int internalFormat = GL_LUMINANCE; 257 257 unsigned int pixelFormat = GL_LUMINANCE; 258 258 unsigned int dataType = 0; 259 259 unsigned int numBytesPerPixel = 0; 260 260 261 261 GDALDataType targetGDALType = GDT_Byte; 262 262 263 263 for(int b=1;b<=numBands;++b) 264 264 { 265 265 266 266 GDALRasterBand* band = dataset->GetRasterBand(b); 267 267 268 268 OSG_INFO << " Band "<<b<<std::endl; 269 269 … … 272 272 OSG_INFO << " DataTypeName() = "<< GDALGetDataTypeName(band->GetRasterDataType())<<std::endl; 273 273 OSG_INFO << " ColorIntepretationName() = "<< GDALGetColorInterpretationName(band->GetColorInterpretation())<<std::endl; 274 274 275 275 bool bandNotHandled = true; 276 276 if (ecwLoad) … … 308 308 OSG_INFO << " Palette Interpretation: " << GDALGetPaletteInterpretationName(band->GetColorTable()->GetPaletteInterpretation()) << std::endl; 309 309 } 310 310 311 311 // int gotMin,gotMax; 312 312 // double minmax[2]; 313 // 313 // 314 314 // minmax[0] = band->GetMinimum(&gotMin); 315 315 // minmax[1] = band->GetMaximum(&gotMax); … … 319 319 // GDALComputeRasterMinMax(band,TRUE,minmax); 320 320 // } 321 // 321 // 322 322 // OSG_INFO << " min "<<minmax[0]<<std::endl; 323 323 // OSG_INFO << " max "<<minmax[1]<<std::endl; … … 339 339 } 340 340 } 341 342 341 342 343 343 int s = destWidth; 344 344 int t = destHeight; 345 345 int r = 1; 346 346 347 347 348 348 if (dataType==0) 349 349 { … … 374 374 bandBlue->RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(imageData+2),destWidth,destHeight,targetGDALType,pixelSpace,lineSpace); 375 375 bandAlpha->RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(imageData+3),destWidth,destHeight,targetGDALType,pixelSpace,lineSpace); 376 376 377 377 } 378 378 else … … 392 392 bandGreen->RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(imageData+1),destWidth,destHeight,targetGDALType,pixelSpace,lineSpace); 393 393 bandBlue->RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(imageData+2),destWidth,destHeight,targetGDALType,pixelSpace,lineSpace); 394 394 395 395 } 396 396 } … … 458 458 459 459 bandPalette->RasterIO(GF_Read,windowX,windowY,windowWidth,windowHeight,(void*)(rawImageData),destWidth,destHeight,targetGDALType,pixelSpace,lineSpace); 460 460 461 461 // Map the indexes to an actual RGBA Value. 462 462 for (int i = 0; i < destWidth * destHeight; i++) … … 505 505 if ( S == 0 ) //HSL values = 0 - 1 506 506 { 507 R = L; //RGB results = 0 - 1 507 R = L; //RGB results = 0 - 1 508 508 G = L; 509 509 B = L; … … 521 521 R = Hue_2_RGB( var_1, var_2, H + ( 1 / 3 ) ); 522 522 G = Hue_2_RGB( var_1, var_2, H ); 523 B = Hue_2_RGB( var_1, var_2, H - ( 1 / 3 ) ); 524 } 523 B = Hue_2_RGB( var_1, var_2, H - ( 1 / 3 ) ); 524 } 525 525 imageData[4*i+0] = static_cast<unsigned char>(R*255.0f); 526 526 imageData[4*i+1] = static_cast<unsigned char>(G*255.0f); … … 543 543 OSG_INFO << "not found any usable bands in file."<<std::endl; 544 544 } 545 545 546 546 547 547 //GDALOpen(dataset); 548 548 549 549 if (imageData) 550 550 { … … 557 557 (unsigned char *)imageData, 558 558 osg::Image::USE_NEW_DELETE); 559 559 560 560 if (texCoordRange) image->setUserData(texCoordRange); 561 561 562 562 image->flipVertical(); 563 563 564 564 return image; 565 565 566 566 } 567 568 return 0; 567 568 return 0; 569 569 570 570 } 571 571 572 572 573 573 ReadResult local_readHeightField(const std::string& fileName, const osgDB::ReaderWriter::Options* options) … … 580 580 std::auto_ptr<GDALDataset> dataset((GDALDataset*)GDALOpen(fileName.c_str(),GA_ReadOnly)); 581 581 if (!dataset.get()) return ReadResult::FILE_NOT_HANDLED; 582 582 583 583 int dataWidth = dataset->GetRasterXSize(); 584 584 int dataHeight = dataset->GetRasterYSize(); 585 585 586 586 int windowX = 0; 587 587 int windowY = 0; … … 600 600 { 601 601 OSG_INFO<<"Got ImageOptions"<<std::endl; 602 602 603 603 int margin = 0; 604 604 switch(imageOptions->_sourceImageWindowMode) … … 610 610 double desiredWidth = (double)dataWidth * imageOptions->_sourceRatioWindow.windowWidth; 611 611 double desiredHeight = (double)dataHeight * imageOptions->_sourceRatioWindow.windowHeight; 612 612 613 613 windowX = osg::maximum((int)(floor(desiredX))-margin,0); 614 614 windowY = osg::maximum((int)(floor(desiredY))-margin,0); 615 615 windowWidth = osg::minimum((int)(ceil(desiredX + desiredWidth))+margin,dataWidth)-windowX; 616 616 windowHeight = osg::minimum((int)(ceil(desiredY + desiredHeight))+margin,dataHeight)-windowY; 617 617 618 618 texCoordRange = new osgDB::ImageOptions::TexCoordRange; 619 619 texCoordRange->set((desiredX-(double)windowX)/(double)windowWidth, … … 634 634 break; 635 635 } 636 636 637 637 switch(imageOptions->_destinationImageWindowMode) 638 638 { … … 654 654 } 655 655 656 } 657 658 // windowX = 0; 659 // windowY = 0; 656 } 657 658 // windowX = 0; 659 // windowY = 0; 660 660 // windowWidth = destWidth; 661 661 // windowHeight = destHeight; … … 674 674 675 675 OSG_INFO << std::endl; 676 676 677 677 OSG_INFO << " GetRaterCount() "<< dataset->GetRasterCount()<<std::endl; 678 678 OSG_INFO << " GetProjectionRef() "<< dataset->GetProjectionRef()<<std::endl; 679 680 679 680 681 681 double geoTransform[6]; 682 682 CPLErr err = dataset->GetGeoTransform(geoTransform); … … 686 686 OSG_INFO << " Pixel X = "<<geoTransform[1]<<" "<<geoTransform[4]<<std::endl; 687 687 OSG_INFO << " Pixel Y = "<<geoTransform[2]<<" "<<geoTransform[5]<<std::endl; 688 688 689 689 690 690 double TopLeft[2],BottomLeft[2],BottomRight[2],TopRight[2]; … … 697 697 TopRight[0] = TopLeft[0]+geoTransform[1]*(dataWidth-1); 698 698 TopRight[1] = TopLeft[1]+geoTransform[4]*(dataWidth-1); 699 699 700 700 701 701 double rotation = atan2(geoTransform[2], geoTransform[1]); 702 702 OSG_INFO<<"GDAL rotation = "<<rotation<<std::endl; 703 703 704 704 OSG_INFO << "TopLeft "<<TopLeft[0]<<"\t"<<TopLeft[1]<<std::endl; 705 705 OSG_INFO << "BottomLeft "<<BottomLeft[0]<<"\t"<<BottomLeft[1]<<std::endl; … … 710 710 711 711 int numBands = dataset->GetRasterCount(); 712 713 712 713 714 714 GDALRasterBand* bandGray = 0; 715 715 GDALRasterBand* bandRed = 0; … … 717 717 GDALRasterBand* bandBlue = 0; 718 718 GDALRasterBand* bandAlpha = 0; 719 719 720 720 for(int b=1;b<=numBands;++b) 721 721 { 722 722 723 723 GDALRasterBand* band = dataset->GetRasterBand(b); 724 724 725 725 OSG_INFO << " Band "<<b<<std::endl; 726 726 … … 729 729 OSG_INFO << " DataTypeName() = "<< GDALGetDataTypeName(band->GetRasterDataType())<<std::endl; 730 730 OSG_INFO << " ColorIntepretationName() = "<< GDALGetColorInterpretationName(band->GetColorInterpretation())<<std::endl; 731 732 731 732 733 733 OSG_INFO << std::endl; 734 734 OSG_INFO << " GetNoDataValue() = "<< band->GetNoDataValue()<<std::endl; … … 738 738 OSG_INFO << " GetScale() = "<< band->GetScale()<<std::endl; 739 739 OSG_INFO << " GetUnitType() = '"<< band->GetUnitType()<<"'"<<std::endl; 740 741 740 741 742 742 if (band->GetColorInterpretation()==GCI_GrayIndex) bandGray = band; 743 743 else if (band->GetColorInterpretation()==GCI_RedBand) bandRed = band; … … 746 746 else if (band->GetColorInterpretation()==GCI_AlphaBand) bandAlpha = band; 747 747 else bandGray = band; 748 749 } 750 751 748 749 } 750 751 752 752 GDALRasterBand* bandSelected = 0; 753 753 if (!bandSelected && bandGray) bandSelected = bandGray; … … 777 777 } 778 778 hf->setOrigin(osg::Vec3(BottomLeft[0],BottomLeft[1],0)); 779 779 780 780 hf->setXInterval(sqrt(geoTransform[1]*geoTransform[1] + geoTransform[2]*geoTransform[2])); 781 781 hf->setYInterval(sqrt(geoTransform[4]*geoTransform[4] + geoTransform[5]*geoTransform[5])); 782 782 783 783 hf->setRotation(osg::Quat(rotation, osg::Vec3d(0.0, 0.0, 1.0))); 784 784 785 785 return hf; 786 786 } 787 787 788 788 return ReadResult::FILE_NOT_HANDLED; 789 789 … … 799 799 } 800 800 } 801 801 802 802 mutable OpenThreads::ReentrantMutex _serializerMutex; 803 803 804 804 }; 805 805
