QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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
33 : mIterator( iterator )
34 , mDpiTarget( dpiTarget )
35{
36}
37
39 : mIterator( iterator )
40{
41}
42
44{
45 mDpiScaleFactor = context.dpiTarget() >= 0.0 ? context.dpiTarget() / ( context.scaleFactor() * 25.4 ) : 1.0;
46 mDevicePixelRatio = context.devicePixelRatio();
47
48 draw( context.painter(), viewPort, &context.mapToPixel(), feedback );
49}
50
51void QgsRasterDrawer::draw( QPainter *p, QgsRasterViewPort *viewPort, const QgsMapToPixel *qgsMapToPixel, QgsRasterBlockFeedback *feedback )
52{
53 QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
54 if ( !p || !mIterator || !viewPort || !qgsMapToPixel )
55 {
56 return;
57 }
58
59 if ( mDpiTarget >= 0 )
60 {
61 mDpiScaleFactor = mDpiTarget / p->device()->logicalDpiX();
62 }
63
64 // last pipe filter has only 1 band
65 const int bandNumber = 1;
66 mIterator->startRasterRead( bandNumber, std::floor( static_cast<double>( viewPort->mWidth ) * mDevicePixelRatio ), std::floor( static_cast<double>( viewPort->mHeight ) * mDevicePixelRatio ), viewPort->mDrawnExtent, feedback );
67
68 //number of cols/rows in output pixels
69 int nCols = 0;
70 int nRows = 0;
71 //shift to top left point for the raster part
72 int topLeftCol = 0;
73 int topLeftRow = 0;
74
75 // We know that the output data type of last pipe filter is QImage data
76
77 std::unique_ptr< QgsRasterBlock > block;
78
79 // readNextRasterPart calcs and resets nCols, nRows, topLeftCol, topLeftRow
80 while ( mIterator->readNextRasterPart( bandNumber, nCols, nRows,
81 block, topLeftCol, topLeftRow ) )
82 {
83 if ( !block )
84 {
85 QgsDebugError( QStringLiteral( "Cannot get block" ) );
86 continue;
87 }
88
89 QImage img = block->image();
90
91 // Because of bug in Acrobat Reader we must use "white" transparent color instead
92 // of "black" for PDF. See #9101.
93 QPdfWriter *pdfWriter = dynamic_cast<QPdfWriter *>( p->device() );
94 if ( pdfWriter )
95 {
96 QgsDebugMsgLevel( QStringLiteral( "PdfFormat" ), 4 );
97
98 img = img.convertToFormat( QImage::Format_ARGB32 );
99 const QRgb transparentBlack = qRgba( 0, 0, 0, 0 );
100 const QRgb transparentWhite = qRgba( 255, 255, 255, 0 );
101 for ( int x = 0; x < img.width(); x++ )
102 {
103 for ( int y = 0; y < img.height(); y++ )
104 {
105 if ( img.pixel( x, y ) == transparentBlack )
106 {
107 img.setPixel( x, y, transparentWhite );
108 }
109 }
110 }
111 }
112
113 if ( feedback && feedback->renderPartialOutput() )
114 {
115 // there could have been partial preview written before
116 // so overwrite anything with the resulting image.
117 // (we are guaranteed to have a temporary image for this layer, see QgsMapRendererJob::needTemporaryImage)
118 p->setCompositionMode( QPainter::CompositionMode_Source );
119 }
120
121 drawImage( p, viewPort, img, topLeftCol, topLeftRow, qgsMapToPixel );
122
123 if ( feedback && feedback->renderPartialOutput() )
124 {
125 // go back to the default composition mode
126 p->setCompositionMode( QPainter::CompositionMode_SourceOver );
127 }
128
129 // OK this does not matter much anyway as the tile size quite big so most of the time
130 // there would be just one tile for the whole display area, but it won't hurt...
131 if ( feedback && feedback->isCanceled() )
132 break;
133 }
134}
135
136void QgsRasterDrawer::drawImage( QPainter *p, QgsRasterViewPort *viewPort, const QImage &img, int topLeftCol, int topLeftRow, const QgsMapToPixel *qgsMapToPixel ) const
137{
138 if ( !p || !viewPort )
139 {
140 return;
141 }
142
143 // top left position in device coords
144 const QPoint tlPoint = QPoint( std::floor( viewPort->mTopLeftPoint.x() + topLeftCol / mDpiScaleFactor / mDevicePixelRatio ),
145 std::floor( viewPort->mTopLeftPoint.y() + topLeftRow / mDpiScaleFactor / mDevicePixelRatio ) );
146 const QgsScopedQPainterState painterState( p );
147
148 // Improve rendering of rasters on high DPI screens with Qt's auto scaling enabled
149 if ( !qgsDoubleNear( mDevicePixelRatio, 1.0 ) || !qgsDoubleNear( mDpiScaleFactor, 1.0 ) )
150 {
151 p->setRenderHint( QPainter::SmoothPixmapTransform, true );
152 }
153
154 // Blending problem was reported with PDF output if background color has alpha < 255
155 // in #7766, it seems to be a bug in Qt, setting a brush with alpha 255 is a workaround
156 // which should not harm anything
157 p->setBrush( QBrush( QColor( Qt::white ), Qt::NoBrush ) );
158 if ( qgsMapToPixel )
159 {
160 const int w = qgsMapToPixel->mapWidth();
161 const int h = qgsMapToPixel->mapHeight();
162 const double rotation = qgsMapToPixel->mapRotation();
163 if ( rotation )
164 {
165 // both viewPort and image sizes are dependent on scale
166 const double cx = w / 2.0;
167 const double cy = h / 2.0;
168 p->translate( cx, cy );
169 p->rotate( rotation );
170 p->translate( -cx, -cy );
171 }
172 }
173
174 p->drawImage( QRect( tlPoint.x(), tlPoint.y(),
175 std::ceil( img.width() / mDpiScaleFactor / mDevicePixelRatio ),
176 std::ceil( img.height() / mDpiScaleFactor / mDevicePixelRatio ) ),
177 img );
178
179#if 0
180 // For debugging:
181 QRectF br = QRectF( tlPoint, img.size() / mDpiScaleFactor / devicePixelRatio );
182 QPointF c = br.center();
183 double rad = std::max( br.width(), br.height() ) / 10;
184 p->drawRoundedRect( br, rad, rad );
185 p->drawLine( QLineF( br.x(), br.y(), br.x() + br.width(), br.y() + br.height() ) );
186 p->drawLine( QLineF( br.x() + br.width(), br.y(), br.x(), br.y() + br.height() ) );
187
188 double nw = br.width() * 0.5;
189 double nh = br.height() * 0.5;
190 br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
191 p->drawRoundedRect( br, rad, rad );
192
193 nw = br.width() * 0.5;
194 nh = br.height() * 0.5;
195 br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
196 p->drawRoundedRect( br, rad, rad );
197#endif
198}
199
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
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:64
double x
Definition qgspointxy.h:63
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:6607
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:61
#define QgsDebugError(str)
Definition qgslogger.h:57
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.