-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
qgsmultibandcolorrenderer.cpp
157 lines (138 loc) · 5.3 KB
/
qgsmultibandcolorrenderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/***************************************************************************
qgsmultibandcolorrenderer.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 "qgsmultibandcolorrenderer.h"
#include "qgsmaptopixel.h"
#include "qgsrasterresampler.h"
#include "qgsrastertransparency.h"
#include "qgsrasterviewport.h"
#include <QImage>
#include <QPainter>
#include <QSet>
QgsMultiBandColorRenderer::QgsMultiBandColorRenderer( QgsRasterDataProvider* provider, int redBand, int greenBand, int blueBand, QgsRasterResampler* resampler ):
QgsRasterRenderer( provider, resampler ), mRedBand( redBand ), mGreenBand( greenBand ), mBlueBand( blueBand )
{
}
QgsMultiBandColorRenderer::~QgsMultiBandColorRenderer()
{
}
void QgsMultiBandColorRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel )
{
if ( !p || !mProvider || !viewPort || !theQgsMapToPixel )
{
return;
}
QgsRasterDataProvider::DataType redType = ( QgsRasterDataProvider::DataType )mProvider->dataType( mRedBand );
QgsRasterDataProvider::DataType greenType = ( QgsRasterDataProvider::DataType )mProvider->dataType( mGreenBand );
QgsRasterDataProvider::DataType blueType = ( QgsRasterDataProvider::DataType )mProvider->dataType( mBlueBand );
QgsRasterDataProvider::DataType transparencyType;
if ( mAlphaBand > 0 )
{
transparencyType = ( QgsRasterDataProvider::DataType )mProvider->dataType( mAlphaBand );
}
double oversamplingX, oversamplingY;
QSet<int> bands;
bands << mRedBand << mGreenBand << mBlueBand;
if ( mAlphaBand > 0 )
{
bands << mAlphaBand;
}
QMap<int, void*> bandData;
void* defaultPointer;
QSet<int>::const_iterator bandIt = bands.constBegin();
for ( ; bandIt != bands.constEnd(); ++bandIt )
{
bandData.insert( *bandIt, defaultPointer );
startRasterRead( *bandIt, viewPort, theQgsMapToPixel, oversamplingX, oversamplingY );
}
void* redData;
void* greenData;
void* blueData;
void* alphaData;
int nCols, nRows, topLeftCol, topLeftRow;
bool readSuccess;
while ( true )
{
QSet<int>::const_iterator bandIt = bands.constBegin();
for ( ; bandIt != bands.constEnd(); ++bandIt )
{
readSuccess = readNextRasterPart( *bandIt, viewPort, nCols, nRows, &bandData[*bandIt], topLeftCol, topLeftRow );
}
if ( !readSuccess )
{
break;
}
redData = bandData[mRedBand];
greenData = bandData[mGreenBand];
blueData = bandData[mBlueBand];
if ( mAlphaBand > 0 )
{
alphaData = bandData[mAlphaBand];
}
QImage img( nCols, nRows, QImage::Format_ARGB32_Premultiplied );
QRgb* imageScanLine = 0;
int currentRasterPos = 0;
int redVal, greenVal, blueVal;
double currentOpacity = mOpacity; //opacity (between 0 and 1)
for ( int i = 0; i < nRows; ++i )
{
imageScanLine = ( QRgb* )( img.scanLine( i ) );
for ( int j = 0; j < nCols; ++j )
{
redVal = readValue( redData, redType, currentRasterPos );
greenVal = readValue( greenData, greenType, currentRasterPos );
blueVal = readValue( blueData, blueType, currentRasterPos );
//opacity
currentOpacity = mOpacity;
if ( mRasterTransparency )
{
currentOpacity = mRasterTransparency->alphaValue( redVal, greenVal, blueVal, mOpacity * 255 ) / 255.0;
}
if ( mAlphaBand > 0 )
{
currentOpacity *= ( readValue( alphaData, transparencyType, currentRasterPos ) / 255.0 );
}
if ( doubleNear( currentOpacity, 255 ) )
{
imageScanLine[j] = qRgba( redVal, greenVal, blueVal, 255 );
}
else
{
imageScanLine[j] = qRgba( currentOpacity * redVal, currentOpacity * greenVal, currentOpacity * blueVal, currentOpacity * 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, nRows / oversamplingY, QImage::Format_ARGB32_Premultiplied );
mResampler->resample( img, dstImg );
p->drawImage( tlPoint, dstImg );
}
else //use original image
{
p->drawImage( tlPoint, img );
}
}
bandIt = bands.constBegin();
for ( ; bandIt != bands.constEnd(); ++bandIt )
{
stopRasterRead( *bandIt );
}
}