17
17
18
18
#include " qgsrasterrenderer.h"
19
19
#include " qgsrasterresampler.h"
20
+ #include " qgsrasterprojector.h"
20
21
#include " qgsrastertransparency.h"
21
22
#include " qgsrasterviewport.h"
22
23
#include " qgsmaptopixel.h"
@@ -101,6 +102,7 @@ void QgsRasterRenderer::startRasterRead( int bandNumber, QgsRasterViewPort* view
101
102
pInfo.currentCol = 0 ;
102
103
pInfo.currentRow = 0 ;
103
104
pInfo.data = 0 ;
105
+ pInfo.prj = 0 ;
104
106
mRasterPartInfos .insert ( bandNumber, pInfo );
105
107
}
106
108
@@ -124,6 +126,8 @@ bool QgsRasterRenderer::readNextRasterPart( int bandNumber, double oversamplingX
124
126
// remove last data block
125
127
CPLFree ( pInfo.data );
126
128
pInfo.data = 0 ;
129
+ delete pInfo.prj ;
130
+ pInfo.prj = 0 ;
127
131
128
132
// already at end
129
133
if ( pInfo.currentCol == pInfo.nCols && pInfo.currentRow == pInfo.nRows )
@@ -144,10 +148,25 @@ bool QgsRasterRenderer::readNextRasterPart( int bandNumber, double oversamplingX
144
148
double ymax = viewPortExtent.yMaximum () - pInfo.currentRow / ( double )pInfo.nRows * viewPortExtent.height ();
145
149
QgsRectangle blockRect ( xmin, ymin, xmax, ymax );
146
150
147
- nColsRaster = nCols * oversamplingX;
148
- nRowsRaster = nRows * oversamplingY;
151
+ if ( viewPort->mSrcCRS .isValid () && viewPort->mDestCRS .isValid () && viewPort->mSrcCRS != viewPort->mDestCRS )
152
+ {
153
+ pInfo.prj = new QgsRasterProjector ( viewPort->mSrcCRS ,
154
+ viewPort->mDestCRS , blockRect, nRows, nCols, 0 , 0 , mProvider ->extent () );
155
+ blockRect = pInfo.prj ->srcExtent ();
156
+ }
157
+
158
+ if ( pInfo.prj )
159
+ {
160
+ nColsRaster = pInfo.prj ->srcCols () * oversamplingX;
161
+ nRowsRaster = pInfo.prj ->srcRows () * oversamplingY;
162
+ }
163
+ else
164
+ {
165
+ nColsRaster = nCols * oversamplingX;
166
+ nRowsRaster = nRows * oversamplingY;
167
+ }
149
168
pInfo.data = VSIMalloc ( typeSize * nColsRaster * nRowsRaster );
150
- mProvider ->readBlock ( bandNumber, blockRect, nColsRaster, nRowsRaster, viewPort-> mSrcCRS , viewPort-> mDestCRS , pInfo.data );
169
+ mProvider ->readBlock ( bandNumber, blockRect, nColsRaster, nRowsRaster, pInfo.data );
151
170
*rasterData = pInfo.data ;
152
171
topLeftCol = pInfo.currentCol ;
153
172
topLeftRow = pInfo.currentRow ;
@@ -178,6 +197,7 @@ void QgsRasterRenderer::removePartInfo( int bandNumber )
178
197
{
179
198
RasterPartInfo& pInfo = partIt.value ();
180
199
CPLFree ( pInfo.data );
200
+ delete pInfo.prj ;
181
201
mRasterPartInfos .remove ( bandNumber );
182
202
}
183
203
}
@@ -201,13 +221,29 @@ void QgsRasterRenderer::drawImage( QPainter* p, QgsRasterViewPort* viewPort, con
201
221
return ;
202
222
}
203
223
224
+ // get QgsRasterProjector
225
+ QgsRasterProjector* prj;
226
+ QMap<int , RasterPartInfo>::const_iterator partInfoIt = mRasterPartInfos .constBegin ();
227
+ if ( partInfoIt != mRasterPartInfos .constEnd () )
228
+ {
229
+ prj = partInfoIt->prj ;
230
+ }
231
+
204
232
// top left position in device coords
205
233
QPoint tlPoint = QPoint ( viewPort->topLeftPoint .x () + topLeftCol, viewPort->topLeftPoint .y () + topLeftRow );
206
234
207
235
// resample and draw image
208
236
if (( mZoomedInResampler || mZoomedOutResampler ) && !doubleNear ( oversamplingX, 1.0 ) && !doubleNear ( oversamplingY, 1.0 ) )
209
237
{
210
- QImage dstImg ( nCols, nRows, QImage::Format_ARGB32_Premultiplied );
238
+ QImage dstImg;
239
+ if ( prj )
240
+ {
241
+ dstImg = QImage ( prj->srcCols (), prj->srcRows (), QImage::Format_ARGB32_Premultiplied );
242
+ }
243
+ else
244
+ {
245
+ dstImg = QImage ( nCols, nRows, QImage::Format_ARGB32_Premultiplied );
246
+ }
211
247
if ( mZoomedInResampler && oversamplingX < 1.0 )
212
248
{
213
249
mZoomedInResampler ->resample ( img, dstImg );
@@ -217,10 +253,43 @@ void QgsRasterRenderer::drawImage( QPainter* p, QgsRasterViewPort* viewPort, con
217
253
mZoomedOutResampler ->resample ( img, dstImg );
218
254
}
219
255
220
- p->drawImage ( tlPoint, dstImg );
256
+ if ( prj )
257
+ {
258
+ QImage projectedImg ( nCols, nRows, QImage::Format_ARGB32_Premultiplied );
259
+ projectImage ( dstImg, projectedImg, prj );
260
+ p->drawImage ( tlPoint, projectedImg );
261
+ }
262
+ else
263
+ {
264
+ p->drawImage ( tlPoint, dstImg );
265
+ }
221
266
}
222
267
else // use original image
223
268
{
224
- p->drawImage ( tlPoint, img );
269
+ if ( prj )
270
+ {
271
+ QImage projectedImg ( nCols, nRows, QImage::Format_ARGB32_Premultiplied );
272
+ projectImage ( img, projectedImg, prj );
273
+ p->drawImage ( tlPoint, projectedImg );
274
+ }
275
+ else
276
+ {
277
+ p->drawImage ( tlPoint, img );
278
+ }
279
+ }
280
+ }
281
+
282
+ void QgsRasterRenderer::projectImage ( const QImage& srcImg, QImage& dstImage, QgsRasterProjector* prj ) const
283
+ {
284
+ int nRows = dstImage.height ();
285
+ int nCols = dstImage.width ();
286
+ int srcRow, srcCol;
287
+ for ( int i = 0 ; i < nRows; ++i )
288
+ {
289
+ for ( int j = 0 ; j < nCols; ++j )
290
+ {
291
+ prj->srcRowCol ( i, j, &srcRow, &srcCol );
292
+ dstImage.setPixel ( j, i, srcImg.pixel ( srcCol, srcRow ) );
293
+ }
225
294
}
226
295
}
0 commit comments