QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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 "qgsrasterdrawer.h"
19
20#include "qgslogger.h"
21#include "qgsmaptopixel.h"
22#include "qgsrasterblock.h"
23#include "qgsrasterinterface.h"
24#include "qgsrasteriterator.h"
25#include "qgsrasterviewport.h"
26#include "qgsrendercontext.h"
27
28#include <QImage>
29#include <QPainter>
30#include <QPdfWriter>
31#include <QString>
32
33using namespace Qt::StringLiterals;
34
36 : mIterator( iterator )
37 , mDpiTarget( dpiTarget )
38{
39}
40
42 : mIterator( iterator )
43{
44}
45
47{
48 mDpiScaleFactor = context.dpiTarget() >= 0.0 ? context.dpiTarget() / ( context.scaleFactor() * 25.4 ) : 1.0;
49 mDevicePixelRatio = context.devicePixelRatio();
50
51 draw( context.painter(), viewPort, &context.mapToPixel(), feedback );
52}
53
54void QgsRasterDrawer::draw( QPainter *p, QgsRasterViewPort *viewPort, const QgsMapToPixel *qgsMapToPixel, QgsRasterBlockFeedback *feedback )
55{
56 QgsDebugMsgLevel( u"Entered"_s, 4 );
57 if ( !p || !mIterator || !viewPort || !qgsMapToPixel )
58 {
59 return;
60 }
61
62 if ( mDpiTarget >= 0 )
63 {
64 mDpiScaleFactor = mDpiTarget / p->device()->logicalDpiX();
65 }
66
67 // last pipe filter has only 1 band
68 const int bandNumber = 1;
69 mIterator->startRasterRead( bandNumber, std::floor( static_cast<double>( viewPort->mWidth ) * mDevicePixelRatio ), std::floor( static_cast<double>( viewPort->mHeight ) * mDevicePixelRatio ), viewPort->mDrawnExtent, feedback );
70
71 //number of cols/rows in output pixels
72 int nCols = 0;
73 int nRows = 0;
74 //shift to top left point for the raster part
75 int topLeftCol = 0;
76 int topLeftRow = 0;
77
78 // We know that the output data type of last pipe filter is QImage data
79
80 std::unique_ptr< QgsRasterBlock > block;
81
82 // readNextRasterPart calcs and resets nCols, nRows, topLeftCol, topLeftRow
83 while ( mIterator->readNextRasterPart( bandNumber, nCols, nRows,
84 block, topLeftCol, topLeftRow ) )
85 {
86 if ( !block )
87 {
88 QgsDebugError( u"Cannot get block"_s );
89 continue;
90 }
91
92 QImage img = block->image();
93
94 // Because of bug in Acrobat Reader we must use "white" transparent color instead
95 // of "black" for PDF. See #9101.
96 QPdfWriter *pdfWriter = dynamic_cast<QPdfWriter *>( p->device() );
97 if ( pdfWriter )
98 {
99 QgsDebugMsgLevel( u"PdfFormat"_s, 4 );
100
101 img = img.convertToFormat( QImage::Format_ARGB32 );
102 const QRgb transparentBlack = qRgba( 0, 0, 0, 0 );
103 const QRgb transparentWhite = qRgba( 255, 255, 255, 0 );
104 for ( int x = 0; x < img.width(); x++ )
105 {
106 for ( int y = 0; y < img.height(); y++ )
107 {
108 if ( img.pixel( x, y ) == transparentBlack )
109 {
110 img.setPixel( x, y, transparentWhite );
111 }
112 }
113 }
114 }
115
116 if ( feedback && feedback->renderPartialOutput() )
117 {
118 // there could have been partial preview written before
119 // so overwrite anything with the resulting image.
120 // (we are guaranteed to have a temporary image for this layer, see QgsMapRendererJob::needTemporaryImage)
121 p->setCompositionMode( QPainter::CompositionMode_Source );
122 }
123
124 drawImage( p, viewPort, img, topLeftCol, topLeftRow, qgsMapToPixel );
125
126 if ( feedback && feedback->renderPartialOutput() )
127 {
128 // go back to the default composition mode
129 p->setCompositionMode( QPainter::CompositionMode_SourceOver );
130 }
131
132 // OK this does not matter much anyway as the tile size quite big so most of the time
133 // there would be just one tile for the whole display area, but it won't hurt...
134 if ( feedback && feedback->isCanceled() )
135 break;
136 }
137}
138
139void QgsRasterDrawer::drawImage( QPainter *p, QgsRasterViewPort *viewPort, const QImage &img, int topLeftCol, int topLeftRow, const QgsMapToPixel *qgsMapToPixel ) const
140{
141 if ( !p || !viewPort )
142 {
143 return;
144 }
145
146 // top left position in device coords
147 const QPoint tlPoint = QPoint( std::floor( viewPort->mTopLeftPoint.x() + topLeftCol / mDpiScaleFactor / mDevicePixelRatio ),
148 std::floor( viewPort->mTopLeftPoint.y() + topLeftRow / mDpiScaleFactor / mDevicePixelRatio ) );
149 const QgsScopedQPainterState painterState( p );
150
151 // Improve rendering of rasters on high DPI screens with Qt's auto scaling enabled
152 if ( !qgsDoubleNear( mDevicePixelRatio, 1.0 ) || !qgsDoubleNear( mDpiScaleFactor, 1.0 ) )
153 {
154 p->setRenderHint( QPainter::SmoothPixmapTransform, true );
155 }
156
157 // Blending problem was reported with PDF output if background color has alpha < 255
158 // in #7766, it seems to be a bug in Qt, setting a brush with alpha 255 is a workaround
159 // which should not harm anything
160 p->setBrush( QBrush( QColor( Qt::white ), Qt::NoBrush ) );
161 if ( qgsMapToPixel )
162 {
163 const int w = qgsMapToPixel->mapWidth();
164 const int h = qgsMapToPixel->mapHeight();
165 const double rotation = qgsMapToPixel->mapRotation();
166 if ( rotation )
167 {
168 // both viewPort and image sizes are dependent on scale
169 const double cx = w / 2.0;
170 const double cy = h / 2.0;
171 p->translate( cx, cy );
172 p->rotate( rotation );
173 p->translate( -cx, -cy );
174 }
175 }
176
177 p->drawImage( QRect( tlPoint.x(), tlPoint.y(),
178 std::ceil( img.width() / mDpiScaleFactor / mDevicePixelRatio ),
179 std::ceil( img.height() / mDpiScaleFactor / mDevicePixelRatio ) ),
180 img );
181
182#if 0
183 // For debugging:
184 QRectF br = QRectF( tlPoint, img.size() / mDpiScaleFactor / devicePixelRatio );
185 QPointF c = br.center();
186 double rad = std::max( br.width(), br.height() ) / 10;
187 p->drawRoundedRect( br, rad, rad );
188 p->drawLine( QLineF( br.x(), br.y(), br.x() + br.width(), br.y() + br.height() ) );
189 p->drawLine( QLineF( br.x() + br.width(), br.y(), br.x(), br.y() + br.height() ) );
190
191 double nw = br.width() * 0.5;
192 double nh = br.height() * 0.5;
193 br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
194 p->drawRoundedRect( br, rad, rad );
195
196 nw = br.width() * 0.5;
197 nh = br.height() * 0.5;
198 br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
199 p->drawRoundedRect( br, rad, rad );
200#endif
201}
202
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:55
Perform transforms between map coordinates and device coordinates.
int mapHeight() const
Returns current map height in pixels.
int mapWidth() const
Returns the current map width in pixels.
double mapRotation() const
Returns the current map rotation in degrees (clockwise).
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
Feedback object tailored for raster block reading.
bool renderPartialOutput() const
Whether our painter is drawing to a temporary image used just by this layer.
void drawImage(QPainter *p, QgsRasterViewPort *viewPort, const QImage &img, int topLeftCol, int topLeftRow, const QgsMapToPixel *mapToPixel=nullptr) const
Draws raster part.
Q_DECL_DEPRECATED QgsRasterDrawer(QgsRasterIterator *iterator, double dpiTarget)
The QgsRasterDrawer constructor.
void draw(QPainter *p, QgsRasterViewPort *viewPort, const QgsMapToPixel *qgsMapToPixel, QgsRasterBlockFeedback *feedback=nullptr)
Draws raster data.
Iterator for sequentially processing raster cells.
Contains information about the context of a rendering operation.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
QPainter * painter()
Returns the destination QPainter for the render operation.
float devicePixelRatio() const
Returns the device pixel ratio.
double dpiTarget() const
Returns the targeted DPI for rendering.
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
Scoped object for saving and restoring a QPainter object's state.
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
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6900
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59
This class provides details of the viewable area that a raster will be rendered into.
qgssize mHeight
Height, number of rows to be rendered.
QgsPointXY mTopLeftPoint
Coordinate (in output device coordinate system) of top left corner of the part of the raster that is ...
QgsRectangle mDrawnExtent
Intersection of current map extent and layer extent, in map (destination) CRS.
qgssize mWidth
Width, number of columns to be rendered.