21 #include <QDomDocument>
22 #include <QDomElement>
96 QgsDebugMsg( QStringLiteral(
"Unknown input data type" ) );
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() )
121 QgsDebugMsg( QStringLiteral(
"No raster data!" ) );
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();