Added new GDAL plugin and osgbluemarble example to demonstrate how to create
PagedLOD'd databases using GDAL.
This commit is contained in:
@@ -10,6 +10,7 @@ CXXFILES =\
|
||||
Registry.cpp\
|
||||
Version.cpp\
|
||||
WriteFile.cpp\
|
||||
ImageOptions.cpp\
|
||||
Input.cpp\
|
||||
Output.cpp\
|
||||
Field.cpp\
|
||||
|
||||
14
src/osgPlugins/gdal/GNUmakefile
Normal file
14
src/osgPlugins/gdal/GNUmakefile
Normal file
@@ -0,0 +1,14 @@
|
||||
TOPDIR = ../../..
|
||||
include $(TOPDIR)/Make/makedefs
|
||||
|
||||
CXXFILES =\
|
||||
ReaderWriterGDAL.cpp\
|
||||
|
||||
LIBS += $(OSG_LIBS) -lgdal.1.1 $(OTHER_LIBS)
|
||||
|
||||
TARGET_BASENAME = gdal
|
||||
|
||||
include $(TOPDIR)/Make/cygwin_plugin_def
|
||||
PLUGIN = $(PLUGIN_PREFIX)$(TARGET_BASENAME).$(PLUGIN_EXT)
|
||||
|
||||
include $(TOPDIR)/Make/makerules
|
||||
310
src/osgPlugins/gdal/ReaderWriterGDAL.cpp
Normal file
310
src/osgPlugins/gdal/ReaderWriterGDAL.cpp
Normal file
@@ -0,0 +1,310 @@
|
||||
#include <osg/Image>
|
||||
#include <osg/Notify>
|
||||
#include <osg/Geode>
|
||||
#include <osg/GL>
|
||||
|
||||
#include <osgDB/Registry>
|
||||
#include <osgDB/FileNameUtils>
|
||||
#include <osgDB/ImageOptions>
|
||||
|
||||
#include <gdal_priv.h>
|
||||
|
||||
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<GDALDataset> 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<const osgDB::ImageOptions*>(options);
|
||||
if (imageOptions)
|
||||
{
|
||||
std::cout<<"Got ImageOptions"<<std::endl;
|
||||
|
||||
switch(imageOptions->_sourceImageWindowMode)
|
||||
{
|
||||
case(osgDB::ImageOptions::RATIO_WINDOW):
|
||||
windowX = (unsigned int)(floor((double)dataWidth * imageOptions->_sourceRatioWindow.windowX));
|
||||
windowY = (unsigned int)(floor((double)dataHeight * imageOptions->_sourceRatioWindow.windowY));
|
||||
windowWidth = (unsigned int)(ceil((double)dataWidth * (imageOptions->_sourceRatioWindow.windowX + imageOptions->_sourceRatioWindow.windowWidth)))-windowX;
|
||||
windowHeight = (unsigned int)(ceil((double)dataHeight * (imageOptions->_sourceRatioWindow.windowY + imageOptions->_sourceRatioWindow.windowHeight)))-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 = "<<windowX<<std::endl;
|
||||
std::cout << " windowY = "<<windowY<<std::endl;
|
||||
std::cout << " windowWidth = "<<windowWidth<<std::endl;
|
||||
std::cout << " windowHeight = "<<windowHeight<<std::endl;
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << " destX = "<<destX<<std::endl;
|
||||
std::cout << " destY = "<<destY<<std::endl;
|
||||
std::cout << " destWidth = "<<destWidth<<std::endl;
|
||||
std::cout << " destHeight = "<<destHeight<<std::endl;
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << " GetRaterCount() "<< dataset->GetRasterCount()<<std::endl;
|
||||
std::cout << " GetProjectionRef() "<< dataset->GetProjectionRef()<<std::endl;
|
||||
|
||||
|
||||
double geoTransform[6];
|
||||
if (dataset->GetGeoTransform(geoTransform)!=CE_None)
|
||||
{
|
||||
std::cout << " GetGeoTransform "<< std::endl;
|
||||
std::cout << " Origin = "<<geoTransform[0]<<" "<<geoTransform[3]<<std::endl;
|
||||
std::cout << " Pixel X = "<<geoTransform[1]<<" "<<geoTransform[4]<<std::endl;
|
||||
std::cout << " Pixel Y = "<<geoTransform[2]<<" "<<geoTransform[5]<<std::endl;
|
||||
}
|
||||
|
||||
int numBands = dataset->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 "<<b<<std::endl;
|
||||
|
||||
std::cout << " GetOverviewCount() = "<< band->GetOverviewCount()<<std::endl;
|
||||
std::cout << " GetColorTable() = "<< band->GetColorTable()<<std::endl;
|
||||
std::cout << " DataTypeName() = "<< GDALGetDataTypeName(band->GetRasterDataType())<<std::endl;
|
||||
std::cout << " ColorIntepretationName() = "<< GDALGetColorInterpretationName(band->GetColorInterpretation())<<std::endl;
|
||||
|
||||
if (band->GetColorInterpretation()==GCI_GrayIndex) bandGray = band;
|
||||
else if (band->GetColorInterpretation()==GCI_RedBand) bandRed = band;
|
||||
else if (band->GetColorInterpretation()==GCI_GreenBand) bandGreen = band;
|
||||
else if (band->GetColorInterpretation()==GCI_BlueBand) bandBlue = band;
|
||||
else if (band->GetColorInterpretation()==GCI_AlphaBand) bandAlpha = band;
|
||||
|
||||
// int gotMin,gotMax;
|
||||
// double minmax[2];
|
||||
//
|
||||
// minmax[0] = band->GetMinimum(&gotMin);
|
||||
// minmax[1] = band->GetMaximum(&gotMax);
|
||||
// if (!(gotMin && gotMax))
|
||||
// {
|
||||
// std::cout<<" computing min max"<<std::endl;
|
||||
// GDALComputeRasterMinMax(band,TRUE,minmax);
|
||||
// }
|
||||
//
|
||||
// std::cout << " min "<<minmax[0]<<std::endl;
|
||||
// std::cout << " max "<<minmax[1]<<std::endl;
|
||||
}
|
||||
|
||||
|
||||
int s = destWidth;
|
||||
int t = destHeight;
|
||||
int r = 1;
|
||||
|
||||
int internalFormat = GL_LUMINANCE;
|
||||
unsigned int pixelFormat = GL_LUMINANCE;
|
||||
unsigned int dataType = GL_FLOAT;
|
||||
|
||||
unsigned char* imageData = 0;
|
||||
|
||||
if (bandRed && bandGreen && bandBlue)
|
||||
{
|
||||
if (bandAlpha)
|
||||
{
|
||||
// RGBA
|
||||
|
||||
int pixelSpace=4;
|
||||
int lineSpace=destWidth * pixelSpace;
|
||||
|
||||
imageData = new unsigned char[destWidth * destHeight * pixelSpace];
|
||||
pixelFormat = GL_RGBA;
|
||||
internalFormat = GL_RGBA;
|
||||
dataType = GL_UNSIGNED_BYTE;
|
||||
|
||||
std::cout << "reading RGBA"<<std::endl;
|
||||
|
||||
bandRed->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"<<std::endl;
|
||||
|
||||
bandRed->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"<<std::endl;
|
||||
|
||||
bandGray->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"<<std::endl;
|
||||
|
||||
bandGray->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"<<std::endl;
|
||||
|
||||
bandAlpha->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."<<std::endl;
|
||||
}
|
||||
|
||||
|
||||
//GDALOpen(dataset);
|
||||
|
||||
if (imageData)
|
||||
{
|
||||
osg::Image* image = new osg::Image;
|
||||
image->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<ReaderWriterGDAL> g_readerWriter_GDAL_Proxy;
|
||||
@@ -94,6 +94,18 @@ char DataInputStream::readChar(){
|
||||
return c;
|
||||
}
|
||||
|
||||
unsigned char DataInputStream::readUChar(){
|
||||
unsigned char c;
|
||||
_istream->read((char*)&c, CHARSIZE);
|
||||
|
||||
if (_istream->rdstate() & _istream->failbit)
|
||||
throw Exception("DataInputStream::readUChar(): Failed to read unsigned char value.");
|
||||
|
||||
if (_verboseOutput) std::cout<<"read/writeUChar() ["<<(int)c<<"]"<<std::endl;
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
unsigned short DataInputStream::readUShort(){
|
||||
unsigned short s;
|
||||
_istream->read((char*)&s, SHORTSIZE);
|
||||
|
||||
@@ -31,6 +31,7 @@ public:
|
||||
~DataInputStream();
|
||||
bool readBool();
|
||||
char readChar();
|
||||
unsigned char readUChar();
|
||||
unsigned short readUShort();
|
||||
unsigned int readUInt();
|
||||
int readInt();
|
||||
|
||||
@@ -74,6 +74,12 @@ void DataOutputStream::writeChar(char c){
|
||||
if (_verboseOutput) std::cout<<"read/writeChar() ["<<(int)c<<"]"<<std::endl;
|
||||
}
|
||||
|
||||
void DataOutputStream::writeUChar(unsigned char c){
|
||||
_ostream->write((char*)&c, CHARSIZE);
|
||||
|
||||
if (_verboseOutput) std::cout<<"read/writeUChar() ["<<(int)c<<"]"<<std::endl;
|
||||
}
|
||||
|
||||
void DataOutputStream::writeUShort(unsigned short s){
|
||||
_ostream->write((char*)&s, SHORTSIZE);
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ public:
|
||||
~DataOutputStream();
|
||||
void writeBool(bool b);
|
||||
void writeChar(char c);
|
||||
void writeUChar(unsigned char c);
|
||||
void writeUShort(unsigned short s);
|
||||
void writeUInt(unsigned int s);
|
||||
void writeInt(int i);
|
||||
|
||||
@@ -13,6 +13,7 @@ CXXFILES =\
|
||||
DataOutputStream.cpp\
|
||||
DrawArrayLengths.cpp\
|
||||
DrawArrays.cpp\
|
||||
DrawElementsUByte.cpp\
|
||||
DrawElementsUShort.cpp\
|
||||
DrawElementsUInt.cpp\
|
||||
Drawable.cpp\
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "Drawable.h"
|
||||
#include "DrawArrays.h"
|
||||
#include "DrawArrayLengths.h"
|
||||
#include "DrawElementsUByte.h"
|
||||
#include "DrawElementsUShort.h"
|
||||
#include "DrawElementsUInt.h"
|
||||
|
||||
@@ -45,6 +46,8 @@ void Geometry::write(DataOutputStream* out){
|
||||
((ive::DrawArrays*)(getPrimitiveSet(i)))->write(out);
|
||||
else if(dynamic_cast<osg::DrawArrayLengths*>(getPrimitiveSet(i)))
|
||||
((ive::DrawArrayLengths*)(getPrimitiveSet(i)))->write(out);
|
||||
else if(dynamic_cast<osg::DrawElementsUByte*>(getPrimitiveSet(i)))
|
||||
((ive::DrawElementsUByte*)(getPrimitiveSet(i)))->write(out);
|
||||
else if(dynamic_cast<osg::DrawElementsUShort*>(getPrimitiveSet(i)))
|
||||
((ive::DrawElementsUShort*)(getPrimitiveSet(i)))->write(out);
|
||||
else if(dynamic_cast<osg::DrawElementsUInt*>(getPrimitiveSet(i)))
|
||||
@@ -180,6 +183,11 @@ void Geometry::read(DataInputStream* in){
|
||||
((ive::DrawArrayLengths*)(prim))->read(in);
|
||||
addPrimitiveSet(prim);
|
||||
}
|
||||
else if(primID==IVEDRAWELEMENTSUBYTE){
|
||||
prim = new osg::DrawElementsUByte();
|
||||
((ive::DrawElementsUByte*)(prim))->read(in);
|
||||
addPrimitiveSet(prim);
|
||||
}
|
||||
else if(primID==IVEDRAWELEMENTSUSHORT){
|
||||
prim = new osg::DrawElementsUShort();
|
||||
((ive::DrawElementsUShort*)(prim))->read(in);
|
||||
|
||||
@@ -59,8 +59,9 @@ namespace ive {
|
||||
#define IVEPRIMITIVESET 0x00010000
|
||||
#define IVEDRAWARRAYS 0x00010001
|
||||
#define IVEDRAWARRAYLENGTHS 0x00010002
|
||||
#define IVEDRAWELEMENTSUSHORT 0x00010003
|
||||
#define IVEDRAWELEMENTSUSHORT 0x00010003
|
||||
#define IVEDRAWELEMENTSUINT 0x00010004
|
||||
#define IVEDRAWELEMENTSUBYTE 0x00010005
|
||||
|
||||
// osgSim classes
|
||||
#define IVEBLINKSEQUENCE 0x00100001
|
||||
|
||||
Reference in New Issue
Block a user