QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
qgsrasterresamplefilter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterresamplefilter.cpp
3  ---------------------
4  begin : December 2011
5  copyright : (C) 2011 by Marco Hugentobler
6  email : marco at sourcepole dot ch
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 "qgsrasterdataprovider.h"
20 #include "qgsrasterresampler.h"
21 #include "qgsrasterprojector.h"
22 #include "qgsrastertransparency.h"
23 #include "qgsrasterviewport.h"
24 #include "qgsmaptopixel.h"
25 
26 //resamplers
29 
30 #include <QDomDocument>
31 #include <QDomElement>
32 #include <QImage>
33 #include <QPainter>
34 
36  : QgsRasterInterface( input )
37 {
38 }
39 
41 {
42  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
43  QgsRasterResampleFilter *resampler = new QgsRasterResampleFilter( nullptr );
44  if ( mZoomedInResampler )
45  {
46  resampler->setZoomedInResampler( mZoomedInResampler->clone() );
47  }
48  if ( mZoomedOutResampler )
49  {
50  resampler->setZoomedOutResampler( mZoomedOutResampler->clone() );
51  }
53  return resampler;
54 }
55 
57 {
58  if ( mOn ) return 1;
59 
60  if ( mInput ) return mInput->bandCount();
61 
62  return 0;
63 }
64 
66 {
67  if ( mOn ) return Qgis::ARGB32_Premultiplied;
68 
69  if ( mInput ) return mInput->dataType( bandNo );
70 
71  return Qgis::UnknownDataType;
72 }
73 
75 {
76  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
77 
78  // Resampler can only work with single band ARGB32_Premultiplied
79  if ( !input )
80  {
81  QgsDebugMsg( QStringLiteral( "No input" ) );
82  return false;
83  }
84 
85  if ( !mOn )
86  {
87  // In off mode we can connect to anything
88  QgsDebugMsgLevel( QStringLiteral( "OK" ), 4 );
89  mInput = input;
90  return true;
91  }
92 
93  if ( input->bandCount() < 1 )
94  {
95  QgsDebugMsg( QStringLiteral( "No input band" ) );
96  return false;
97  }
98 
99  if ( input->dataType( 1 ) != Qgis::ARGB32_Premultiplied &&
100  input->dataType( 1 ) != Qgis::ARGB32 )
101  {
102  QgsDebugMsg( QStringLiteral( "Unknown input data type" ) );
103  return false;
104  }
105 
106  mInput = input;
107  QgsDebugMsgLevel( QStringLiteral( "OK" ), 4 );
108  return true;
109 }
110 
112 {
113  mZoomedInResampler.reset( r );
114 }
115 
117 {
118  mZoomedOutResampler.reset( r );
119 }
120 
121 QgsRasterBlock *QgsRasterResampleFilter::block( int bandNo, QgsRectangle const &extent, int width, int height, QgsRasterBlockFeedback *feedback )
122 {
123  Q_UNUSED( bandNo )
124  QgsDebugMsgLevel( QStringLiteral( "width = %1 height = %2 extent = %3" ).arg( width ).arg( height ).arg( extent.toString() ), 4 );
125  std::unique_ptr< QgsRasterBlock > outputBlock( new QgsRasterBlock() );
126  if ( !mInput )
127  return outputBlock.release();
128 
129  double oversampling = 1.0; // approximate global oversampling factor
130  double outputXRes;
131  double providerXRes = 0;
133  {
134  QgsRasterDataProvider *provider = dynamic_cast<QgsRasterDataProvider *>( mInput->sourceInput() );
135  if ( provider && ( provider->capabilities() & QgsRasterDataProvider::Size ) )
136  {
137  outputXRes = extent.width() / width;
138  providerXRes = provider->extent().width() / provider->xSize();
139  double pixelRatio = outputXRes / providerXRes;
140  oversampling = ( pixelRatio > mMaxOversampling ) ? mMaxOversampling : pixelRatio;
141  QgsDebugMsgLevel( QStringLiteral( "xRes = %1 providerXRes = %2 pixelRatio = %3 oversampling = %4" ).arg( outputXRes ).arg( providerXRes ).arg( pixelRatio ).arg( oversampling ), 4 );
142  }
143  else
144  {
145  // We don't know exact data source resolution (WMS) so we expect that
146  // server data have higher resolution (which is not always true) and use
147  // mMaxOversampling
148  oversampling = mMaxOversampling;
149  }
150  }
151 
152  QgsDebugMsgLevel( QStringLiteral( "oversampling %1" ).arg( oversampling ), 4 );
153 
154  int bandNumber = 1;
155 
156  // Do no oversampling if no resampler for zoomed in / zoomed out (nearest neighbour)
157  // We do mZoomedInResampler if oversampling == 1 (otherwise for example reprojected
158  // zoom in rasters are never resampled because projector limits resolution.
159  if ( ( ( oversampling < 1.0 || qgsDoubleNear( oversampling, 1.0 ) ) && !mZoomedInResampler ) || ( oversampling > 1.0 && !mZoomedOutResampler ) )
160  {
161  QgsDebugMsgLevel( QStringLiteral( "No oversampling." ), 4 );
162  return mInput->block( bandNumber, extent, width, height, feedback );
163  }
164 
165  //effective oversampling factors are different to global one because of rounding
166  double oversamplingX = ( static_cast< double >( width ) * oversampling ) / width;
167  double oversamplingY = ( static_cast< double >( height ) * oversampling ) / height;
168 
169  // we must also increase the extent to get correct result on borders of parts
170  int tileBufferPixels = 0;
171  if ( providerXRes != 0 )
172  {
173  if ( mZoomedInResampler && ( oversamplingX < 1.0 || qgsDoubleNear( oversampling, 1.0 ) ) )
174  {
175  tileBufferPixels = static_cast< int >( std::ceil( mZoomedInResampler->tileBufferPixels() * oversampling ) );
176  }
177  else if ( mZoomedOutResampler && oversamplingX > 1.0 )
178  {
179  tileBufferPixels = static_cast< int >( std::ceil( mZoomedOutResampler->tileBufferPixels() * oversampling ) );
180  }
181  }
182  const double sourceTileBufferSize = providerXRes * tileBufferPixels;
183 
184  QgsRectangle bufferedExtent( extent.xMinimum() - sourceTileBufferSize,
185  extent.yMinimum() - sourceTileBufferSize,
186  extent.xMaximum() + sourceTileBufferSize,
187  extent.yMaximum() + sourceTileBufferSize
188  );
189 
190  int resWidth = static_cast< int >( std::round( width * oversamplingX ) ) + 2 * tileBufferPixels;
191  int resHeight = static_cast< int >( std::round( height * oversamplingY ) ) + 2 * tileBufferPixels;
192 
193  std::unique_ptr< QgsRasterBlock > inputBlock( mInput->block( bandNumber, bufferedExtent, resWidth, resHeight, feedback ) );
194  if ( !inputBlock || inputBlock->isEmpty() )
195  {
196  QgsDebugMsg( QStringLiteral( "No raster data!" ) );
197  return outputBlock.release();
198  }
199 
200  if ( !outputBlock->reset( Qgis::ARGB32_Premultiplied, width, height ) )
201  {
202  return outputBlock.release();
203  }
204 
205  //resample image
206  QImage img = inputBlock->image();
207 
208  int resampleWidth = static_cast< int >( std::round( width * ( bufferedExtent.width() / extent.width() ) ) );
209  int resampleHeight = static_cast< int >( std::round( height * ( bufferedExtent.height() / extent.height() ) ) );
210 
211  QImage dstImg;
212  if ( mZoomedInResampler && ( oversamplingX < 1.0 || qgsDoubleNear( oversampling, 1.0 ) ) )
213  {
214  QgsDebugMsgLevel( QStringLiteral( "zoomed in resampling" ), 4 );
215 
216  if ( QgsRasterResamplerV2 *resamplerV2 = dynamic_cast< QgsRasterResamplerV2 * >( mZoomedInResampler.get( ) ) )
217  {
218  dstImg = resamplerV2->resampleV2( img, QSize( resampleWidth, resampleHeight ) );
219  }
220  else
221  {
222  // old inefficient interface
224  QImage dstImg = QImage( resampleWidth, resampleHeight, QImage::Format_ARGB32_Premultiplied );
225  mZoomedInResampler->resample( img, dstImg );
227  }
228  }
229  else if ( mZoomedOutResampler && oversamplingX > 1.0 )
230  {
231  QgsDebugMsgLevel( QStringLiteral( "zoomed out resampling" ), 4 );
232 
233  if ( QgsRasterResamplerV2 *resamplerV2 = dynamic_cast< QgsRasterResamplerV2 * >( mZoomedOutResampler.get( ) ) )
234  {
235  dstImg = resamplerV2->resampleV2( img, QSize( resampleWidth, resampleHeight ) );
236  }
237  else
238  {
239  // old inefficient interface
241  QImage dstImg = QImage( resampleWidth, resampleHeight, QImage::Format_ARGB32_Premultiplied );
242  mZoomedOutResampler->resample( img, dstImg );
244  }
245  }
246  else
247  {
248  // Should not happen
249  QgsDebugMsg( QStringLiteral( "Unexpected resampling" ) );
250  dstImg = img.scaled( width, height );
251  }
252 
253  // extract desired part of dstImage
254  QImage cropped = tileBufferPixels > 0 ? dstImg.copy( ( resampleWidth - width ) / 2, ( resampleHeight - height ) / 2, width, height )
255  : dstImg; // otherwise implicit copy, nice and cheap
256  outputBlock->setImage( &cropped );
257 
258  return outputBlock.release(); // No resampling
259 }
260 
261 void QgsRasterResampleFilter::writeXml( QDomDocument &doc, QDomElement &parentElem ) const
262 {
263  if ( parentElem.isNull() )
264  {
265  return;
266  }
267 
268  QDomElement rasterRendererElem = doc.createElement( QStringLiteral( "rasterresampler" ) );
269 
270  rasterRendererElem.setAttribute( QStringLiteral( "maxOversampling" ), QString::number( mMaxOversampling ) );
271  if ( mZoomedInResampler )
272  {
273  rasterRendererElem.setAttribute( QStringLiteral( "zoomedInResampler" ), mZoomedInResampler->type() );
274  }
275  if ( mZoomedOutResampler )
276  {
277  rasterRendererElem.setAttribute( QStringLiteral( "zoomedOutResampler" ), mZoomedOutResampler->type() );
278  }
279  parentElem.appendChild( rasterRendererElem );
280 }
281 
282 void QgsRasterResampleFilter::readXml( const QDomElement &filterElem )
283 {
284  if ( filterElem.isNull() )
285  {
286  return;
287  }
288 
289  mMaxOversampling = filterElem.attribute( QStringLiteral( "maxOversampling" ), QStringLiteral( "2.0" ) ).toDouble();
290 
291  QString zoomedInResamplerType = filterElem.attribute( QStringLiteral( "zoomedInResampler" ) );
292  if ( zoomedInResamplerType == QLatin1String( "bilinear" ) )
293  {
295  }
296  else if ( zoomedInResamplerType == QLatin1String( "cubic" ) )
297  {
299  }
300 
301  QString zoomedOutResamplerType = filterElem.attribute( QStringLiteral( "zoomedOutResampler" ) );
302  if ( zoomedOutResamplerType == QLatin1String( "bilinear" ) )
303  {
305  }
306 }
virtual int bandCount() const =0
Gets number of bands.
A rectangle specified with double values.
Definition: qgsrectangle.h:41
Cubic Raster Resampler.
Interface for resampling rasters (e.g.
virtual QgsRectangle extent() const
Gets the extent of the interface.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
virtual QgsRasterInterface * input() const
Current input.
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:731
DataType
Raster data types.
Definition: qgis.h:101
Resample filter pipe for rasters.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
virtual Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
void readXml(const QDomElement &filterElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition: qgis.h:116
Raster data container.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
virtual QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr)=0
Read block of data using given extent and size.
virtual int capabilities() const
Returns a bitmask containing the supported capabilities.
QgsRectangle extent() const override=0
Returns the extent of the layer.
double width() const
Returns the width of the rectangle.
Definition: qgsrectangle.h:202
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
Unknown or unspecified type.
Definition: qgis.h:103
QgsRasterResampleFilter(QgsRasterInterface *input=nullptr)
Base class for processing filters like renderers, reprojector, resampler etc.
double mMaxOversampling
Maximum boundary for oversampling (to avoid too much data traffic). Default: 2.0. ...
bool setInput(QgsRasterInterface *input) override
Set input.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:177
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:162
std::unique_ptr< QgsRasterResampler > mZoomedOutResampler
Resampler used if raster resolution is higher than raster resolution (zoomed out). 0 mean no resampling (nearest neighbour)
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:732
Qgis::DataType dataType(int bandNo) const override
Returns data type for the band specified by number.
void setZoomedInResampler(QgsRasterResampler *r)
Sets resampler for zoomed in scales. Takes ownership of the object.
QgsRasterResampleFilter * clone() const override
Clone itself, create deep copy.
Bilinear Raster Resampler.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
void setZoomedOutResampler(QgsRasterResampler *r)
Sets resampler for zoomed out scales. Takes ownership of the object.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:167
std::unique_ptr< QgsRasterResampler > mZoomedInResampler
Resampler used if screen resolution is higher than raster resolution (zoomed in). 0 means no resampli...
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:172
Interface for resampling rasters (V2) (e.g.
QgsRasterInterface * mInput
Feedback object tailored for raster block reading.
virtual int xSize() const
Gets raster size.
int bandCount() const override
Gets number of bands.
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
Definition: qgis.h:115
virtual const QgsRasterInterface * sourceInput() const
Gets source / raw input, the first in pipe, usually provider.
double height() const
Returns the height of the rectangle.
Definition: qgsrectangle.h:209
Base class for raster data providers.