30 #include <QDomDocument>
31 #include <QDomElement>
102 QgsDebugMsg( QStringLiteral(
"Unknown input data type" ) );
126 const int bandNumber = 1;
129 std::unique_ptr< QgsRasterBlock > outputBlock(
new QgsRasterBlock() );
131 return outputBlock.release();
133 double oversampling = 1.0;
135 double providerXRes = 0;
143 const double pixelRatio = outputXRes / providerXRes;
145 QgsDebugMsgLevel( QStringLiteral(
"xRes = %1 providerXRes = %2 pixelRatio = %3 oversampling = %4" ).arg( outputXRes ).arg( providerXRes ).arg( pixelRatio ).arg( oversampling ), 4 );
156 QgsDebugMsgLevel( QStringLiteral(
"oversampling %1" ).arg( oversampling ), 4 );
168 const double oversamplingX = (
static_cast< double >( width ) * oversampling ) / width;
169 const double oversamplingY = (
static_cast< double >( height ) * oversampling ) / height;
172 int tileBufferPixels = 0;
173 if ( providerXRes != 0 )
177 tileBufferPixels =
static_cast< int >( std::ceil(
mZoomedInResampler->tileBufferPixels() * oversampling ) );
181 tileBufferPixels =
static_cast< int >( std::ceil(
mZoomedOutResampler->tileBufferPixels() * oversampling ) );
184 const double sourceTileBufferSize = providerXRes * tileBufferPixels;
192 const int resWidth =
static_cast< int >( std::round( width * oversamplingX ) ) + 2 * tileBufferPixels;
193 const int resHeight =
static_cast< int >( std::round( height * oversamplingY ) ) + 2 * tileBufferPixels;
195 std::unique_ptr< QgsRasterBlock > inputBlock(
mInput->
block( bandNumber, bufferedExtent, resWidth, resHeight, feedback ) );
196 if ( !inputBlock || inputBlock->isEmpty() )
198 QgsDebugMsg( QStringLiteral(
"No raster data!" ) );
199 return outputBlock.release();
204 return outputBlock.release();
208 const QImage img = inputBlock->image();
210 const int resampleWidth =
static_cast< int >( std::round( width * ( bufferedExtent.
width() /
extent.
width() ) ) );
211 const int resampleHeight =
static_cast< int >( std::round( height * ( bufferedExtent.
height() /
extent.
height() ) ) );
220 dstImg = resamplerV2->resampleV2( img, QSize( resampleWidth, resampleHeight ) );
226 QImage dstImg = QImage( resampleWidth, resampleHeight, QImage::Format_ARGB32_Premultiplied );
237 dstImg = resamplerV2->resampleV2( img, QSize( resampleWidth, resampleHeight ) );
243 QImage dstImg = QImage( resampleWidth, resampleHeight, QImage::Format_ARGB32_Premultiplied );
251 QgsDebugMsg( QStringLiteral(
"Unexpected resampling" ) );
252 dstImg = img.scaled( width, height );
256 const QImage cropped = tileBufferPixels > 0 ? dstImg.copy( ( resampleWidth - width ) / 2, ( resampleHeight - height ) / 2, width, height )
258 outputBlock->setImage( &cropped );
260 return outputBlock.release();
265 if ( parentElem.isNull() )
270 QDomElement rasterRendererElem = doc.createElement( QStringLiteral(
"rasterresampler" ) );
272 rasterRendererElem.setAttribute( QStringLiteral(
"maxOversampling" ), QString::number(
mMaxOversampling ) );
275 rasterRendererElem.setAttribute( QStringLiteral(
"zoomedInResampler" ),
mZoomedInResampler->type() );
279 rasterRendererElem.setAttribute( QStringLiteral(
"zoomedOutResampler" ),
mZoomedOutResampler->type() );
281 parentElem.appendChild( rasterRendererElem );
286 if ( filterElem.isNull() )
291 mMaxOversampling = filterElem.attribute( QStringLiteral(
"maxOversampling" ), QStringLiteral(
"2.0" ) ).toDouble();
293 const QString zoomedInResamplerType = filterElem.attribute( QStringLiteral(
"zoomedInResampler" ) );
294 if ( zoomedInResamplerType == QLatin1String(
"bilinear" ) )
298 else if ( zoomedInResamplerType == QLatin1String(
"cubic" ) )
303 const QString zoomedOutResamplerType = filterElem.attribute( QStringLiteral(
"zoomedOutResampler" ) );
304 if ( zoomedOutResamplerType == QLatin1String(
"bilinear" ) )
308 else if ( zoomedOutResamplerType == QLatin1String(
"cubic" ) )
DataType
Raster data types.
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
@ UnknownDataType
Unknown or unspecified type.
@ ARGB32
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
Bilinear Raster Resampler.
Feedback object tailored for raster block reading.
Base class for raster data providers.
QgsRectangle extent() const override=0
Returns the extent of the layer.
Base class for processing filters like renderers, reprojector, resampler etc.
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 Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
@ Size
Original data source size (and thus resolution) is known, it is not always available,...
virtual QgsRasterInterface * input() const
Current input.
virtual int xSize() const
Gets raster size.
virtual const QgsRasterInterface * sourceInput() const
Gets source / raw input, the first in pipe, usually provider.
virtual int bandCount() const =0
Gets number of bands.
QgsRasterInterface * mInput
virtual int capabilities() const
Returns a bitmask containing the supported capabilities.
virtual QgsRectangle extent() const
Gets the extent of the interface.
Resample filter pipe for rasters.
double mMaxOversampling
Maximum boundary for oversampling (to avoid too much data traffic). Default: 2.0.
bool setInput(QgsRasterInterface *input) override
Set input.
void setZoomedOutResampler(QgsRasterResampler *r)
Sets resampler for zoomed out scales. Takes ownership of the object.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
void setMaxOversampling(double os)
Qgis::DataType dataType(int bandNo) const override
Returns data type for the band specified by number.
std::unique_ptr< QgsRasterResampler > mZoomedInResampler
Resampler used if screen resolution is higher than raster resolution (zoomed in). 0 means no resampli...
void readXml(const QDomElement &filterElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
std::unique_ptr< QgsRasterResampler > mZoomedOutResampler
Resampler used if raster resolution is higher than raster resolution (zoomed out)....
QgsRasterResampleFilter(QgsRasterInterface *input=nullptr)
int bandCount() const override
Gets number of bands.
QgsRasterResampleFilter * clone() const override
Clone itself, create deep copy.
void setZoomedInResampler(QgsRasterResampler *r)
Sets resampler for zoomed in scales. Takes ownership of the object.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
Interface for resampling rasters (V2) (e.g.
Interface for resampling rasters (e.g.
A rectangle specified with double values.
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
double height() const SIP_HOLDGIL
Returns the height of the rectangle.
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
#define QgsDebugMsgLevel(str, level)