From 7edefe64ab75102ce137ab5266232a12462b5e87 Mon Sep 17 00:00:00 2001 From: stevehalasz Date: Thu, 20 Nov 2003 21:16:42 +0000 Subject: [PATCH] can display indexed color GeoTIFF with GDAL git-svn-id: http://svn.osgeo.org/qgis/trunk@311 c8812cc2-4d05-0410-92ff-de0c093fc19c --- qgis/src/qgsrasterlayer.cpp | 92 +++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 39 deletions(-) diff --git a/qgis/src/qgsrasterlayer.cpp b/qgis/src/qgsrasterlayer.cpp index 81dc5d693a6..c65f2917020 100644 --- a/qgis/src/qgsrasterlayer.cpp +++ b/qgis/src/qgsrasterlayer.cpp @@ -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"); + //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; + */ + + 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); + } } - //~ 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 ); - - //~ QImage image; - //~ bool good = image.loadFromData(reinterpret_cast (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; -// } } //void QgsRasterLayer::identify(QgsRect * r)