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