22#include <QDomDocument>
26using namespace Qt::StringLiterals;
52 return mInput->bandCount();
67 return mInput->dataType( bandNo );
92 if (
input->bandCount() < 1 )
113 QgsDebugMsgLevel( u
"width = %1 height = %2 extent = %3"_s.arg( width ).arg( height ).arg(
extent.toString() ), 4 );
115 auto outputBlock = std::make_unique<QgsRasterBlock>();
118 return outputBlock.release();
123 std::unique_ptr< QgsRasterBlock > inputBlock(
mInput->block( bandNumber,
extent, width, height, feedback ) );
124 if ( !inputBlock || inputBlock->isEmpty() )
127 return outputBlock.release();
130 if ( mBrightness == 0 && mContrast == 0 && mGamma == 1.0 )
133 return inputBlock.release();
138 return outputBlock.release();
142 QRgb myNoDataColor = qRgba( 0, 0, 0, 0 );
146 double f = std::pow( ( mContrast + 100 ) / 100.0, 2 );
147 double gammaCorrection = 1.0 / mGamma;
151 if ( inputBlock->color( i ) == myNoDataColor )
153 outputBlock->setColor( i, myNoDataColor );
157 myColor = inputBlock->color( i );
158 alpha = qAlpha( myColor );
160 r = adjustColorComponent( qRed( myColor ), alpha, mBrightness, f, gammaCorrection );
161 g = adjustColorComponent( qGreen( myColor ), alpha, mBrightness, f, gammaCorrection );
162 b = adjustColorComponent( qBlue( myColor ), alpha, mBrightness, f, gammaCorrection );
164 outputBlock->setColor( i, qRgba( r, g, b, alpha ) );
167 return outputBlock.release();
172 mBrightness = std::clamp(
brightness, -255, 255 );
177 mContrast = std::clamp(
contrast, -100, 100 );
182 mGamma = std::clamp(
gamma, 0.1, 10.0 );
185int QgsBrightnessContrastFilter::adjustColorComponent(
int colorComponent,
int alpha,
int brightness,
double contrastFactor,
double gammaCorrection )
const
190 return std::clamp( (
int )( 255 * std::pow( ( ( ( ( ( ( colorComponent / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) +
brightness ) / 255.0, gammaCorrection ) ), 0, 255 );
192 else if ( alpha == 0 )
201 double alphaFactor = alpha / 255.;
202 double adjustedColor = colorComponent / alphaFactor;
205 return alphaFactor * std::clamp( 255 * std::pow( ( ( ( ( ( ( adjustedColor / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) +
brightness ) / 255, gammaCorrection ), 0., 255. );
211 if ( parentElem.isNull() )
216 QDomElement filterElem = doc.createElement( u
"brightnesscontrast"_s );
218 filterElem.setAttribute( u
"brightness"_s, QString::number( mBrightness ) );
219 filterElem.setAttribute( u
"contrast"_s, QString::number( mContrast ) );
220 filterElem.setAttribute( u
"gamma"_s, QString::number( mGamma ) );
221 parentElem.appendChild( filterElem );
226 if ( filterElem.isNull() )
231 mBrightness = filterElem.attribute( u
"brightness"_s, u
"0"_s ).toInt();
232 mContrast = filterElem.attribute( u
"contrast"_s, u
"0"_s ).toInt();
233 mGamma = filterElem.attribute( u
"gamma"_s, u
"1"_s ).toDouble();
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.
int contrast() const
Returns current contrast level.
int bandCount() const override
Gets number of bands.
QgsBrightnessContrastFilter * clone() const override
Clone itself, create deep copy.
bool setInput(QgsRasterInterface *input) override
Set input.
QgsBrightnessContrastFilter(QgsRasterInterface *input=nullptr)
int brightness() const
Returns current brightness level.
double gamma() const
Returns current gamma value.
void readXml(const QDomElement &filterElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
void setGamma(double gamma)
Set gamma value.
void setContrast(int contrast)
Set contrast level.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
void setBrightness(int brightness)
Set brightness level.
Qgis::DataType dataType(int bandNo) const override
Returns data type for the band specified by number.
Feedback object tailored for raster block reading.
QgsRasterInterface(QgsRasterInterface *input=nullptr)
QgsRasterInterface * mInput
virtual QgsRectangle extent() const
Gets the extent of the interface.
virtual QgsRasterInterface * input() const
Current input.
A rectangle specified with double values.
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...
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)