Skip to content

Commit

Permalink
Added single band gray renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Dec 29, 2011
1 parent 2e371eb commit 46aa6c6
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -169,6 +169,7 @@ SET(QGIS_CORE_SRCS
raster/qgscubicrasterresampler.cpp
raster/qgspalettedrasterrenderer.cpp
raster/qgsmultibandcolorrenderer.cpp
raster/qgssinglebandgrayrenderer.cpp

renderer/qgscontinuouscolorrenderer.cpp
renderer/qgsgraduatedsymbolrenderer.cpp
Expand Down
17 changes: 17 additions & 0 deletions src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -41,6 +41,7 @@ email : tim at linfiniti.com
#include "qgsbilinearrasterresampler.h"
#include "qgscubicrasterresampler.h"
#include "qgsmultibandcolorrenderer.h"
#include "qgssinglebandgrayrenderer.h"

#include <cstdio>
#include <cmath>
Expand Down Expand Up @@ -815,8 +816,24 @@ void QgsRasterLayer::draw( QPainter * theQPainter,
}
else
{
QgsSingleBandGrayRenderer r( mDataProvider, bandNumber( mGrayBandName ), mResampler );
r.setOpacity( mTransparencyLevel / 255.0 );
r.setRasterTransparency( &mRasterTransparency );
if ( mTransparencyBandName != TRSTRING_NOT_SET )
{
int tBandNr = bandNumber( mTransparencyBandName );
if ( tBandNr > 0 )
{
r.setAlphaBand( tBandNr );
}
}
r.setInvertColor( mInvertColor );
r.draw( theQPainter, theRasterViewPort, theQgsMapToPixel );

#if 0
drawSingleBandGray( theQPainter, theRasterViewPort,
theQgsMapToPixel, bandNumber( mGrayBandName ) );
#endif //0
break;
}
// a "Gray" or "Undefined" layer drawn using a pseudocolor algorithm
Expand Down
99 changes: 99 additions & 0 deletions src/core/raster/qgssinglebandgrayrenderer.cpp
@@ -0,0 +1,99 @@
/***************************************************************************
qgssinglebandgrayrenderer.cpp
-----------------------------
begin : December 2011
copyright : (C) 2011 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgssinglebandgrayrenderer.h"
#include "qgscontrastenhancement.h"
#include "qgsrasterresampler.h"
#include "qgsrasterviewport.h"
#include <QImage>
#include <QPainter>

QgsSingleBandGrayRenderer::QgsSingleBandGrayRenderer( QgsRasterDataProvider* provider, int grayBand, QgsRasterResampler* resampler ):
QgsRasterRenderer( provider, resampler ), mGrayBand( grayBand ), mContrastEnhancement( 0 )
{
}

QgsSingleBandGrayRenderer::~QgsSingleBandGrayRenderer()
{
}

void QgsSingleBandGrayRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel )
{
if ( !p || !mProvider || !viewPort || !theQgsMapToPixel )
{
return;
}

double oversamplingX, oversamplingY;
startRasterRead( mGrayBand, viewPort, theQgsMapToPixel, oversamplingX, oversamplingY );

int nCols = 0;
int nRows = 0;
int topLeftCol = 0;
int topLeftRow = 0;
QgsRasterDataProvider::DataType rasterType = ( QgsRasterDataProvider::DataType )mProvider->dataType( mGrayBand );
void* rasterData;
//double currentOpacity = mOpacity;
int grayVal;

while ( readNextRasterPart( mGrayBand, viewPort, nCols, nRows, &rasterData, topLeftCol, topLeftRow ) )
{
//create image
QImage img( nCols, nRows, QImage::Format_ARGB32_Premultiplied );
QRgb* imageScanLine = 0;
int currentRasterPos = 0;

for ( int i = 0; i < nRows; ++i )
{
imageScanLine = ( QRgb* )( img.scanLine( i ) );
for ( int j = 0; j < nCols; ++j )
{
grayVal = readValue( rasterData, rasterType, currentRasterPos );
if ( mContrastEnhancement )
{
grayVal = mContrastEnhancement->enhanceContrast( grayVal );
}

if ( mInvertColor )
{
grayVal = 255 - grayVal;
}

imageScanLine[j] = qRgba( grayVal, grayVal, grayVal, 255 );
++currentRasterPos;
}
}

//draw image
//top left position in device coords
QPointF tlPoint = QPointF( viewPort->topLeftPoint.x(), viewPort->topLeftPoint.y() );
tlPoint += QPointF( topLeftCol / oversamplingX, topLeftRow / oversamplingY );

if ( mResampler ) //resample to output resolution
{
QImage dstImg( nCols / oversamplingX + 1.0, nRows / oversamplingY + 1.0, QImage::Format_ARGB32_Premultiplied );
mResampler->resample( img, dstImg );
p->drawImage( tlPoint, dstImg );
}
else //use original image
{
p->drawImage( tlPoint, img );
}
}

stopRasterRead( mGrayBand );
}
41 changes: 41 additions & 0 deletions src/core/raster/qgssinglebandgrayrenderer.h
@@ -0,0 +1,41 @@
/***************************************************************************
qgssinglebandgrayrenderer.h
---------------------------
begin : December 2011
copyright : (C) 2011 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSSINGLEBANDGRAYRENDERER_H
#define QGSSINGLEBANDGRAYRENDERER_H

#include "qgsrasterrenderer.h"

class QgsContrastEnhancement;

class QgsSingleBandGrayRenderer: public QgsRasterRenderer
{
public:
QgsSingleBandGrayRenderer( QgsRasterDataProvider* provider, int grayBand, QgsRasterResampler* resampler = 0 );
~QgsSingleBandGrayRenderer();

virtual void draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel );

const QgsContrastEnhancement* contrastEnhancement() const { return mContrastEnhancement; }
void setContrastEnhancement( QgsContrastEnhancement* ce ) { mContrastEnhancement = ce; }

private:
int mGrayBand;
QgsContrastEnhancement* mContrastEnhancement;
};

#endif // QGSSINGLEBANDGRAYRENDERER_H

0 comments on commit 46aa6c6

Please sign in to comment.