|
| 1 | +/*************************************************************************** |
| 2 | + qgssinglebandpseudocolorrenderer.cpp |
| 3 | + ------------------------------------ |
| 4 | + begin : January 2012 |
| 5 | + copyright : (C) 2012 by Marco Hugentobler |
| 6 | + email : marco at sourcepole dot ch |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +#include "qgssinglebandpseudocolorrenderer.h" |
| 19 | +#include "qgsrastershader.h" |
| 20 | +#include "qgsrastertransparency.h" |
| 21 | +#include "qgsrasterviewport.h" |
| 22 | +#include <QImage> |
| 23 | + |
| 24 | +QgsSingleBandPseudoColorRenderer::QgsSingleBandPseudoColorRenderer( QgsRasterDataProvider* provider, int band, QgsRasterShader* shader, QgsRasterResampler* resampler ): |
| 25 | + QgsRasterRenderer( provider, resampler ), mShader( shader ), mBand( band ) |
| 26 | +{ |
| 27 | +} |
| 28 | + |
| 29 | +QgsSingleBandPseudoColorRenderer::~QgsSingleBandPseudoColorRenderer() |
| 30 | +{ |
| 31 | +} |
| 32 | + |
| 33 | +void QgsSingleBandPseudoColorRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel ) |
| 34 | +{ |
| 35 | + if ( !p || !mProvider || !viewPort || !theQgsMapToPixel || !mShader ) |
| 36 | + { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + double oversamplingX, oversamplingY; |
| 41 | + QgsRasterDataProvider::DataType transparencyType = QgsRasterDataProvider::UnknownDataType; |
| 42 | + if ( mAlphaBand > 0 ) |
| 43 | + { |
| 44 | + transparencyType = ( QgsRasterDataProvider::DataType )mProvider->dataType( mAlphaBand ); |
| 45 | + } |
| 46 | + startRasterRead( mBand, viewPort, theQgsMapToPixel, oversamplingX, oversamplingY ); |
| 47 | + //Read alpha band if necessary |
| 48 | + if ( mAlphaBand > 0 && mAlphaBand != mBand ) |
| 49 | + { |
| 50 | + startRasterRead( mAlphaBand, viewPort, theQgsMapToPixel, oversamplingX, oversamplingY ); |
| 51 | + } |
| 52 | + |
| 53 | + int nCols = 0; |
| 54 | + int nRows = 0; |
| 55 | + int topLeftCol = 0; |
| 56 | + int topLeftRow = 0; |
| 57 | + void* rasterData; |
| 58 | + void* transparencyData; |
| 59 | + double currentOpacity = mOpacity; |
| 60 | + QgsRasterDataProvider::DataType rasterType = ( QgsRasterDataProvider::DataType )mProvider->dataType( mBand ); |
| 61 | + int red, green, blue; |
| 62 | + QRgb myDefaultColor = qRgba( 255, 255, 255, 0 ); |
| 63 | + |
| 64 | + //rendering is faster without considering user-defined transparency |
| 65 | + bool hasTransparency = usesTransparency( viewPort->mSrcCRS, viewPort->mDestCRS ); |
| 66 | + |
| 67 | + while ( readNextRasterPart( mBand, viewPort, nCols, nRows, &rasterData, topLeftCol, topLeftRow ) ) |
| 68 | + { |
| 69 | + if ( mAlphaBand > 0 && mAlphaBand != mBand ) |
| 70 | + { |
| 71 | + readNextRasterPart( mAlphaBand, viewPort, nCols, nRows, &transparencyData, topLeftCol, topLeftRow ); |
| 72 | + } |
| 73 | + else if ( mAlphaBand == mBand ) |
| 74 | + { |
| 75 | + transparencyData = rasterData; |
| 76 | + } |
| 77 | + |
| 78 | + //create image |
| 79 | + QImage img( nCols, nRows, QImage::Format_ARGB32_Premultiplied ); |
| 80 | + QRgb* imageScanLine = 0; |
| 81 | + double val = 0; |
| 82 | + |
| 83 | + int currentRasterPos = 0; |
| 84 | + for ( int i = 0; i < nRows; ++i ) |
| 85 | + { |
| 86 | + imageScanLine = ( QRgb* )( img.scanLine( i ) ); |
| 87 | + for ( int j = 0; j < nCols; ++j ) |
| 88 | + { |
| 89 | + val = readValue( rasterData, rasterType, currentRasterPos ); |
| 90 | + if ( !mShader->shade( val, &red, &green, &blue ) ) |
| 91 | + { |
| 92 | + imageScanLine[j] = myDefaultColor; |
| 93 | + ++currentRasterPos; |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + if ( !hasTransparency ) |
| 98 | + { |
| 99 | + imageScanLine[j] = qRgba( red, green, blue, 255 ); |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + //opacity |
| 104 | + currentOpacity = mOpacity; |
| 105 | + if ( mRasterTransparency ) |
| 106 | + { |
| 107 | + currentOpacity = mRasterTransparency->alphaValue( val, mOpacity * 255 ) / 255.0; |
| 108 | + } |
| 109 | + if ( mAlphaBand > 0 ) |
| 110 | + { |
| 111 | + currentOpacity *= ( readValue( transparencyData, transparencyType, currentRasterPos ) / 255.0 ); |
| 112 | + } |
| 113 | + |
| 114 | + imageScanLine[j] = qRgba( currentOpacity * red, currentOpacity * green, currentOpacity * blue, currentOpacity * 255 ); |
| 115 | + } |
| 116 | + ++currentRasterPos; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + drawImage( p, viewPort, img, topLeftCol, topLeftRow, nCols, nRows, oversamplingX, oversamplingY ); |
| 121 | + } |
| 122 | + |
| 123 | + stopRasterRead( mBand ); |
| 124 | + if ( mAlphaBand > 0 && mAlphaBand != mBand ) |
| 125 | + { |
| 126 | + stopRasterRead( mAlphaBand ); |
| 127 | + } |
| 128 | +} |
0 commit comments