QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsrasterdrawer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterdrawer.cpp
3  ---------------------
4  begin : June 2012
5  copyright : (C) 2012 by Radim Blazek
6  email : radim dot blazek at gmail.com
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 "qgslogger.h"
19 #include "qgsrasterblock.h"
20 #include "qgsrasterdrawer.h"
21 #include "qgsrasterinterface.h"
22 #include "qgsrasteriterator.h"
23 #include "qgsrasterviewport.h"
24 #include "qgsmaptopixel.h"
25 #include "qgsrendercontext.h"
26 #include <QImage>
27 #include <QPainter>
28 #include <QPrinter>
29 
30 QgsRasterDrawer::QgsRasterDrawer( QgsRasterIterator *iterator ): mIterator( iterator )
31 {
32 }
33 
34 void QgsRasterDrawer::draw( QPainter *p, QgsRasterViewPort *viewPort, const QgsMapToPixel *qgsMapToPixel, QgsRasterBlockFeedback *feedback )
35 {
36  QgsDebugMsgLevel( "Entered", 4 );
37  if ( !p || !mIterator || !viewPort || !qgsMapToPixel )
38  {
39  return;
40  }
41 
42  // last pipe filter has only 1 band
43  int bandNumber = 1;
44  mIterator->startRasterRead( bandNumber, viewPort->mWidth, viewPort->mHeight, viewPort->mDrawnExtent, feedback );
45 
46  //number of cols/rows in output pixels
47  int nCols = 0;
48  int nRows = 0;
49  //shift to top left point for the raster part
50  int topLeftCol = 0;
51  int topLeftRow = 0;
52 
53  // We know that the output data type of last pipe filter is QImage data
54 
55  std::unique_ptr< QgsRasterBlock > block;
56 
57  // readNextRasterPart calcs and resets nCols, nRows, topLeftCol, topLeftRow
58  while ( mIterator->readNextRasterPart( bandNumber, nCols, nRows,
59  block, topLeftCol, topLeftRow ) )
60  {
61  if ( !block )
62  {
63  QgsDebugMsg( "Cannot get block" );
64  continue;
65  }
66 
67  QImage img = block->image();
68 
69 #ifndef QT_NO_PRINTER
70  // Because of bug in Acrobat Reader we must use "white" transparent color instead
71  // of "black" for PDF. See #9101.
72  QPrinter *printer = dynamic_cast<QPrinter *>( p->device() );
73  if ( printer && printer->outputFormat() == QPrinter::PdfFormat )
74  {
75  QgsDebugMsgLevel( "PdfFormat", 4 );
76 
77  img = img.convertToFormat( QImage::Format_ARGB32 );
78  QRgb transparentBlack = qRgba( 0, 0, 0, 0 );
79  QRgb transparentWhite = qRgba( 255, 255, 255, 0 );
80  for ( int x = 0; x < img.width(); x++ )
81  {
82  for ( int y = 0; y < img.height(); y++ )
83  {
84  if ( img.pixel( x, y ) == transparentBlack )
85  {
86  img.setPixel( x, y, transparentWhite );
87  }
88  }
89  }
90  }
91 #endif
92 
93  if ( feedback && feedback->renderPartialOutput() )
94  {
95  // there could have been partial preview written before
96  // so overwrite anything with the resulting image.
97  // (we are guaranteed to have a temporary image for this layer, see QgsMapRendererJob::needTemporaryImage)
98  p->setCompositionMode( QPainter::CompositionMode_Source );
99  }
100 
101  drawImage( p, viewPort, img, topLeftCol, topLeftRow, qgsMapToPixel );
102 
103  if ( feedback && feedback->renderPartialOutput() )
104  {
105  // go back to the default composition mode
106  p->setCompositionMode( QPainter::CompositionMode_SourceOver );
107  }
108 
109  // OK this does not matter much anyway as the tile size quite big so most of the time
110  // there would be just one tile for the whole display area, but it won't hurt...
111  if ( feedback && feedback->isCanceled() )
112  break;
113  }
114 }
115 
116 void QgsRasterDrawer::drawImage( QPainter *p, QgsRasterViewPort *viewPort, const QImage &img, int topLeftCol, int topLeftRow, const QgsMapToPixel *qgsMapToPixel ) const
117 {
118  if ( !p || !viewPort )
119  {
120  return;
121  }
122 
123  //top left position in device coords
124  QPoint tlPoint = QPoint( viewPort->mTopLeftPoint.x() + topLeftCol, viewPort->mTopLeftPoint.y() + topLeftRow );
125  p->save();
126  p->setRenderHint( QPainter::Antialiasing, false );
127 
128  // Blending problem was reported with PDF output if background color has alpha < 255
129  // in #7766, it seems to be a bug in Qt, setting a brush with alpha 255 is a workaround
130  // which should not harm anything
131  p->setBrush( QBrush( QColor( Qt::white ), Qt::NoBrush ) );
132 
133  if ( qgsMapToPixel )
134  {
135  int w = qgsMapToPixel->mapWidth();
136  int h = qgsMapToPixel->mapHeight();
137 
138  double rotation = qgsMapToPixel->mapRotation();
139  if ( rotation )
140  {
141  // both viewPort and image sizes are dependent on scale
142  double cx = w / 2.0;
143  double cy = h / 2.0;
144  p->translate( cx, cy );
145  p->rotate( rotation );
146  p->translate( -cx, -cy );
147  }
148  }
149 
150  p->drawImage( tlPoint, img );
151 
152 #if 0
153  // For debugging:
154  QRectF br = QRectF( tlPoint, img.size() );
155  QPointF c = br.center();
156  double rad = std::max( br.width(), br.height() ) / 10;
157  p->drawRoundedRect( br, rad, rad );
158  p->drawLine( QLineF( br.x(), br.y(), br.x() + br.width(), br.y() + br.height() ) );
159  p->drawLine( QLineF( br.x() + br.width(), br.y(), br.x(), br.y() + br.height() ) );
160 
161  double nw = br.width() * 0.5;
162  double nh = br.height() * 0.5;
163  br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
164  p->drawRoundedRect( br, rad, rad );
165 
166  nw = br.width() * 0.5;
167  nh = br.height() * 0.5;
168  br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
169  p->drawRoundedRect( br, rad, rad );
170 #endif
171 
172  p->restore();
173 }
174 
void drawImage(QPainter *p, QgsRasterViewPort *viewPort, const QImage &img, int topLeftCol, int topLeftRow, const QgsMapToPixel *mapToPixel=nullptr) const
Draws raster part.
bool renderPartialOutput() const
Whether our painter is drawing to a temporary image used just by this layer.
Iterator for sequentially processing raster cells.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
double y
Definition: qgspointxy.h:48
int mWidth
Width, number of columns to be rendered.
double mapRotation() const
Returns current map rotation in degrees.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:36
int mapWidth() const
Returns current map width in pixels The information is only known if setRotation was used...
int mapHeight() const
Returns current map height in pixels.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QgsRasterDrawer(QgsRasterIterator *iterator)
bool readNextRasterPart(int bandNumber, int &nCols, int &nRows, QgsRasterBlock **block, int &topLeftCol, int &topLeftRow)
Fetches next part of raster data, caller takes ownership of the block and caller should delete the bl...
double x
Definition: qgspointxy.h:47
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
int mHeight
Distance in map units from bottom edge to top edge for the part of the raster that is to be rendered...
This class provides details of the viewable area that a raster will be rendered into.
Feedback object tailored for raster block reading.
void draw(QPainter *p, QgsRasterViewPort *viewPort, const QgsMapToPixel *qgsMapToPixel, QgsRasterBlockFeedback *feedback=nullptr)
Draws raster data.
void startRasterRead(int bandNumber, int nCols, int nRows, const QgsRectangle &extent, QgsRasterBlockFeedback *feedback=nullptr)
Start reading of raster band.
QgsRectangle mDrawnExtent
Intersection of current map extent and layer extent.
QgsPointXY mTopLeftPoint
Coordinate (in output device coordinate system) of top left corner of the part of the raster that is ...