can display indexed color GeoTIFF with GDAL

git-svn-id: http://svn.osgeo.org/qgis/trunk@311 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
stevehalasz 2003-11-20 21:16:42 +00:00
parent 9ef6f338d2
commit 7edefe64ab

View File

@ -22,61 +22,75 @@
#include "qgsrect.h"
#include "qgsrasterlayer.h"
#include "gdal_priv.h"
#include "qtiffio.h"
//#include "qtiffio.h"
QgsRasterLayer::QgsRasterLayer(QString path, QString baseName)
:QgsMapLayer(RASTER, baseName, path)
{
std::cout << "QgsRasterLayer::QgsRasterLayer()" << std::endl;
// test image handling using only Qt classes (don't use gdal)
//~ GDALAllRegister();
//~ gdalDataset = (GDALDataset *) GDALOpen( path, GA_ReadOnly );
//~ std::cout << "Raster Count: " << gdalDataset->GetRasterCount() << std::endl;
GDALAllRegister();
gdalDataset = (GDALDataset *) GDALOpen( path, GA_ReadOnly );
std::cout << "Raster Count: " << gdalDataset->GetRasterCount() << std::endl;
}
QgsRasterLayer::~QgsRasterLayer()
{
GDALClose(gdalDataset);
}
void QgsRasterLayer::draw(QPainter * p, QgsRect * viewExtent, QgsCoordinateTransform * cXf)
{
std::cout << "QgsRasterLayer::draw()" << std::endl;
/*
// init the tiff handler
qInitTiffIO();
std::cout << "Image source is " << source() << std::endl;
QImage *image = new QImage(source());
if(!image){
qWarning("Failed to load the image using path stored in source()");
}else{
qWarning("Loaded image");
}
//~ for (int i = 1; i <= gdalDataset->GetRasterCount(); i++) {
//~ GDALRasterBand *gdalBand = gdalDataset->GetRasterBand( i );
//~ int xBlk;
//~ int yBlk;
//~ gdalBand->GetBlockSize(&xBlk, &yBlk);
//~ std::cout << "Block Size: " << xBlk << ", " << yBlk << std::endl;
//~ std::cout << "Raster Data Type: " << gdalBand->GetRasterDataType() << std::endl
//~ QImage image;
//~ bool good = image.
//~ /*
//~ std::cout << "Raster X, Y: : " << gdalBand->GetXSize() << ", "
//~ << gdalBand->GetYSize() << std::endl;
//~ int *pafScanline = (int *) CPLMalloc(nXSize);
//~ gdalBand->RasterIO( GF_Read, 0, 0, nXSize, 1, pafScanline, nXSize, 1, GDT_UInt32, 0, 0 );
//qInitTiffIO();
//std::cout << "Image source is " << source() << std::endl;
//QImage *image = new QImage(source());
//if(!image){
// qWarning("Failed to load the image using path stored in source()");
//}else{
// qWarning("Loaded image");
//}
//
//qWarning("Attempting to draw the image");
//p->drawImage(0, 0, *image);
//delete image;
*/
//~ QImage image;
//~ bool good = image.loadFromData(reinterpret_cast <const uchar*> (pafScanline), nXSize);
//~ if(!good){
//~ qWarning("Unable to load image data from GDAL scan line");
//~ }
//~ CPLFree(pafScanline);
//~ */
qWarning("Attempting to draw the image");
p->drawImage(0, 0, *image);
delete image;
// }
std::cout << "QgsRasterLayer::draw()" << std::endl;
std::cout << "gdalDataset->GetRasterCount(): " << gdalDataset->GetRasterCount() << std::endl;
// if there is more than one raster band they can be for red, green, blue, etc.
// so this loop doesn't make much sense right now
// only handling the case of 1 raster band that uses a palette
// to index the rgb color values
// this works for GeoTIFFs from:
// http://cugir.mannlib.cornell.edu/browse_map/quad_map.html
for (int i = 1; i <= gdalDataset->GetRasterCount(); i++) {
GDALRasterBand *gdalBand = gdalDataset->GetRasterBand( i );
int nXSize = gdalBand->GetXSize();
int nYSize = gdalBand->GetYSize();
// read raster 1 line at a time
// display image at 0,0
for (int nYOff = 0; nYOff < nYSize; nYOff++) {
uint *scanline = (uint*) CPLMalloc(sizeof(uint)*nXSize);
CPLErr result = gdalBand->RasterIO(
GF_Read, 0, nYOff, nXSize, 1, scanline, nXSize, 1, GDT_UInt32, 0, 0 );
GDALColorTable *colorTable = gdalBand->GetColorTable();
// print each point in scanline using color looked up in color table
for (int i = 0; i < nXSize; i++) {
const GDALColorEntry *colorEntry = GDALGetColorEntry (colorTable, scanline[i]);
p->setPen(QColor(colorEntry->c1, colorEntry->c2, colorEntry->c3));
p->drawPoint(i, nYOff);
}
CPLFree(scanline);
}
}
}
//void QgsRasterLayer::identify(QgsRect * r)