QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
132  {
133  QgsRasterDataProvider *provider = dynamic_cast<QgsRasterDataProvider *>( mInput->sourceInput() );
134  if ( provider && ( provider->capabilities() & QgsRasterDataProvider::Size ) )
135  {
136  double xRes = extent.width() / width;
137  double providerXRes = provider->extent().width() / provider->xSize();
138  double pixelRatio = xRes / providerXRes;
139  oversampling = ( pixelRatio > mMaxOversampling ) ? mMaxOversampling : pixelRatio;
140  QgsDebugMsgLevel( QStringLiteral( "xRes = %1 providerXRes = %2 pixelRatio = %3 oversampling = %4" ).arg( xRes ).arg( providerXRes ).arg( pixelRatio ).arg( oversampling ), 4 );
141  }
142  else
143  {
144  // We don't know exact data source resolution (WMS) so we expect that
145  // server data have higher resolution (which is not always true) and use
146  // mMaxOversampling
147  oversampling = mMaxOversampling;
148  }
149  }
150 
151  QgsDebugMsgLevel( QStringLiteral( "oversampling %1" ).arg( oversampling ), 4 );
152 
153  int bandNumber = 1;
154 
155  // Do no oversampling if no resampler for zoomed in / zoomed out (nearest neighbour)
156  // We do mZoomedInResampler if oversampling == 1 (otherwise for example reprojected
157  // zoom in rasters are never resampled because projector limits resolution.
158  if ( ( ( oversampling < 1.0 || qgsDoubleNear( oversampling, 1.0 ) ) && !mZoomedInResampler ) || ( oversampling > 1.0 && !mZoomedOutResampler ) )
159  {
160  QgsDebugMsgLevel( QStringLiteral( "No oversampling." ), 4 );
161  return mInput->block( bandNumber, extent, width, height, feedback );
162  }
163 
164  //effective oversampling factors are different to global one because of rounding
165  double oversamplingX = ( static_cast< double >( width ) * oversampling ) / width;
166  double oversamplingY = ( static_cast< double >( height ) * oversampling ) / height;
167 
168  // TODO: we must also increase the extent to get correct result on borders of parts
169 
170  int resWidth = width * oversamplingX;
171  int resHeight = height * oversamplingY;
172 
173  std::unique_ptr< QgsRasterBlock > inputBlock( mInput->block( bandNumber, extent, resWidth, resHeight, feedback ) );
174  if ( !inputBlock || inputBlock->isEmpty() )
175  {
176  QgsDebugMsg( QStringLiteral( "No raster data!" ) );
177  return outputBlock.release();
178  }
179 
180  if ( !outputBlock->reset( Qgis::ARGB32_Premultiplied, width, height ) )
181  {
182  return outputBlock.release();
183  }
184 
185  //resample image
186  QImage img = inputBlock->image();
187 
188  QImage dstImg = QImage( width, height, QImage::Format_ARGB32_Premultiplied );
189 
190  if ( mZoomedInResampler && ( oversamplingX < 1.0 || qgsDoubleNear( oversampling, 1.0 ) ) )
191  {
192  QgsDebugMsgLevel( QStringLiteral( "zoomed in resampling" ), 4 );
193  mZoomedInResampler->resample( img, dstImg );
194  }
195  else if ( mZoomedOutResampler && oversamplingX > 1.0 )
196  {
197  QgsDebugMsgLevel( QStringLiteral( "zoomed out resampling" ), 4 );
198  mZoomedOutResampler->resample( img, dstImg );
199  }
200  else
201  {
202  // Should not happen
203  QgsDebugMsg( QStringLiteral( "Unexpected resampling" ) );
204  dstImg = img.scaled( width, height );
205  }
206 
207  outputBlock->setImage( &dstImg );
208 
209  return outputBlock.release(); // No resampling
210 }
211 
212 void QgsRasterResampleFilter::writeXml( QDomDocument &doc, QDomElement &parentElem ) const
213 {
214  if ( parentElem.isNull() )
215  {
216  return;
217  }
218 
219  QDomElement rasterRendererElem = doc.createElement( QStringLiteral( "rasterresampler" ) );
220 
221  rasterRendererElem.setAttribute( QStringLiteral( "maxOversampling" ), QString::number( mMaxOversampling ) );
222  if ( mZoomedInResampler )
223  {
224  rasterRendererElem.setAttribute( QStringLiteral( "zoomedInResampler" ), mZoomedInResampler->type() );
225  }
226  if ( mZoomedOutResampler )
227  {
228  rasterRendererElem.setAttribute( QStringLiteral( "zoomedOutResampler" ), mZoomedOutResampler->type() );
229  }
230  parentElem.appendChild( rasterRendererElem );
231 }
232 
233 void QgsRasterResampleFilter::readXml( const QDomElement &filterElem )
234 {
235  if ( filterElem.isNull() )
236  {
237  return;
238  }
239 
240  mMaxOversampling = filterElem.attribute( QStringLiteral( "maxOversampling" ), QStringLiteral( "2.0" ) ).toDouble();
241 
242  QString zoomedInResamplerType = filterElem.attribute( QStringLiteral( "zoomedInResampler" ) );
243  if ( zoomedInResamplerType == QLatin1String( "bilinear" ) )
244  {
246  }
247  else if ( zoomedInResamplerType == QLatin1String( "cubic" ) )
248  {
250  }
251 
252  QString zoomedOutResamplerType = filterElem.attribute( QStringLiteral( "zoomedOutResampler" ) );
253  if ( zoomedOutResamplerType == QLatin1String( "bilinear" ) )
254  {
256  }
257 }
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:280
virtual QgsRasterInterface * input() const
Current input.
DataType
Raster data types.
Definition: qgis.h:80
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:95
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:82
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.
std::unique_ptr< QgsRasterResampler > mZoomedOutResampler
Resampler used if raster resolution is higher than raster resolution (zoomed out). 0 mean no resampling (nearest neighbour)
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.
std::unique_ptr< QgsRasterResampler > mZoomedInResampler
Resampler used if screen resolution is higher than raster resolution (zoomed in). 0 means no resampli...
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:94
virtual const QgsRasterInterface * sourceInput() const
Gets source / raw input, the first in pipe, usually provider.
Base class for raster data providers.