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