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