21 #include <QDomDocument> 22 #include <QDomElement> 108 QgsDebugMsgLevel( QString(
"width = %1 height = %2 extent = %3" ).arg( width ).arg( height ).arg( extent.
toString() ), 4 );
110 std::unique_ptr< QgsRasterBlock > outputBlock(
new QgsRasterBlock() );
113 return outputBlock.release();
118 std::unique_ptr< QgsRasterBlock > inputBlock(
mInput->
block( bandNumber, extent, width, height, feedback ) );
119 if ( !inputBlock || inputBlock->isEmpty() )
122 return outputBlock.release();
125 if ( mBrightness == 0 && mContrast == 0 )
128 return inputBlock.release();
133 return outputBlock.release();
137 QRgb myNoDataColor = qRgba( 0, 0, 0, 0 );
141 double f = std::pow( ( mContrast + 100 ) / 100.0, 2 );
145 if ( inputBlock->color( i ) == myNoDataColor )
147 outputBlock->setColor( i, myNoDataColor );
151 myColor = inputBlock->color( i );
152 alpha = qAlpha( myColor );
154 r = adjustColorComponent( qRed( myColor ), alpha, mBrightness, f );
155 g = adjustColorComponent( qGreen( myColor ), alpha, mBrightness, f );
156 b = adjustColorComponent( qBlue( myColor ), alpha, mBrightness, f );
158 outputBlock->setColor( i, qRgba( r, g, b, alpha ) );
161 return outputBlock.release();
164 int QgsBrightnessContrastFilter::adjustColorComponent(
int colorComponent,
int alpha,
int brightness,
double contrastFactor )
const 169 return qBound( 0, (
int )( ( ( ( ( ( colorComponent / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) +
brightness ), 255 );
171 else if ( alpha == 0 )
180 double alphaFactor = alpha / 255.;
181 double adjustedColor = colorComponent / alphaFactor;
184 return alphaFactor * qBound( 0., ( ( ( ( ( ( adjustedColor / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) +
brightness ), 255. );
190 if ( parentElem.isNull() )
195 QDomElement filterElem = doc.createElement( QStringLiteral(
"brightnesscontrast" ) );
197 filterElem.setAttribute( QStringLiteral(
"brightness" ), QString::number( mBrightness ) );
198 filterElem.setAttribute( QStringLiteral(
"contrast" ), QString::number( mContrast ) );
199 parentElem.appendChild( filterElem );
204 if ( filterElem.isNull() )
209 mBrightness = filterElem.attribute( QStringLiteral(
"brightness" ), QStringLiteral(
"0" ) ).toInt();
210 mContrast = filterElem.attribute( QStringLiteral(
"contrast" ), QStringLiteral(
"0" ) ).toInt();
virtual int bandCount() const =0
Gets number of bands.
A rectangle specified with double values.
QgsBrightnessContrastFilter * clone() const override
Clone itself, create deep copy.
virtual QgsRectangle extent() const
Gets the extent of the interface.
virtual QgsRasterInterface * input() const
Current input.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
DataType
Raster data types.
int bandCount() const override
Gets number of bands.
virtual Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
void setBrightness(int brightness)
void setContrast(int contrast)
#define QgsDebugMsgLevel(str, level)
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.
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.
Base class for processing filters like renderers, reprojector, resampler etc.
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
Qgis::DataType dataType(int bandNo) const override
Returns data type for the band specified by number.
bool setInput(QgsRasterInterface *input) override
Set input.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
Brightness/contrast filter pipe for rasters.
QgsRasterInterface * mInput
Feedback object tailored for raster block reading.
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.
QgsBrightnessContrastFilter(QgsRasterInterface *input=nullptr)