21 #include <QDomDocument>
22 #include <QDomElement>
97 QgsDebugMsg( QStringLiteral(
"Unknown input data type" ) );
111 std::unique_ptr< QgsRasterBlock > outputBlock(
new QgsRasterBlock() );
114 return outputBlock.release();
119 std::unique_ptr< QgsRasterBlock > inputBlock(
mInput->
block( bandNumber,
extent, width, height, feedback ) );
120 if ( !inputBlock || inputBlock->isEmpty() )
122 QgsDebugMsg( QStringLiteral(
"No raster data!" ) );
123 return outputBlock.release();
126 if ( mBrightness == 0 && mContrast == 0 && mGamma == 1.0 )
128 QgsDebugMsgLevel( QStringLiteral(
"No brightness/contrast/gamma changes." ), 4 );
129 return inputBlock.release();
134 return outputBlock.release();
138 QRgb myNoDataColor = qRgba( 0, 0, 0, 0 );
142 double f = std::pow( ( mContrast + 100 ) / 100.0, 2 );
143 double gammaCorrection = 1.0 / mGamma;
147 if ( inputBlock->color( i ) == myNoDataColor )
149 outputBlock->setColor( i, myNoDataColor );
153 myColor = inputBlock->color( i );
154 alpha = qAlpha( myColor );
156 r = adjustColorComponent( qRed( myColor ), alpha, mBrightness, f, gammaCorrection );
157 g = adjustColorComponent( qGreen( myColor ), alpha, mBrightness, f, gammaCorrection );
158 b = adjustColorComponent( qBlue( myColor ), alpha, mBrightness, f, gammaCorrection );
160 outputBlock->setColor( i, qRgba( r, g, b, alpha ) );
163 return outputBlock.release();
166 int QgsBrightnessContrastFilter::adjustColorComponent(
int colorComponent,
int alpha,
int brightness,
double contrastFactor,
double gammaCorrection )
const
171 return qBound( 0, (
int )( 255 * std::pow( ( ( ( ( ( ( colorComponent / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) +
brightness ) / 255.0, gammaCorrection ) ), 255 );
173 else if ( alpha == 0 )
182 double alphaFactor = alpha / 255.;
183 double adjustedColor = colorComponent / alphaFactor;
186 return alphaFactor * qBound( 0., 255 * std::pow( ( ( ( ( ( ( adjustedColor / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) +
brightness ) / 255, gammaCorrection ), 255. );
192 if ( parentElem.isNull() )
197 QDomElement filterElem = doc.createElement( QStringLiteral(
"brightnesscontrast" ) );
199 filterElem.setAttribute( QStringLiteral(
"brightness" ), QString::number( mBrightness ) );
200 filterElem.setAttribute( QStringLiteral(
"contrast" ), QString::number( mContrast ) );
201 filterElem.setAttribute( QStringLiteral(
"gamma" ), QString::number( mGamma ) );
202 parentElem.appendChild( filterElem );
207 if ( filterElem.isNull() )
212 mBrightness = filterElem.attribute( QStringLiteral(
"brightness" ), QStringLiteral(
"0" ) ).toInt();
213 mContrast = filterElem.attribute( QStringLiteral(
"contrast" ), QStringLiteral(
"0" ) ).toInt();
214 mGamma = filterElem.attribute( QStringLiteral(
"gamma" ), QStringLiteral(
"1" ) ).toDouble();