float QgsHillshadeFilter::processNineCellWindow( float *x11, float *x21, float *x31, float *x12, float *x22, float *x32, float *x13, float *x23, float *x33 ) { float derX = calcFirstDerX( x11, x21, x31, x12, x22, x32, x13, x23, x33 ); float derY = calcFirstDerY( x11, x21, x31, x12, x22, x32, x13, x23, x33 ); if ( derX == mOutputNodataValue || derY == mOutputNodataValue ) { return mOutputNodataValue; } float zenith_rad = mLightAngle * M_PI / 180.0; float slope_rad = atan( sqrt( derX * derX + derY * derY ) ); float azimuth_rad = mLightAzimuth * M_PI / 180.0; float aspect_rad = 0; if ( derX == 0 && derY == 0 ) //aspect undefined, take a neutral value. Better solutions? { aspect_rad = azimuth_rad / 2.0; } else { aspect_rad = M_PI + atan2( derX, derY ); } return qMax( 0.0, 255.0 * ( ( cos( zenith_rad ) * cos( slope_rad ) ) + ( sin( zenith_rad ) * sin( slope_rad ) * cos( azimuth_rad - aspect_rad ) ) ) ); }
float QgsHillshadeFilter::processNineCellWindow( float *x11, float *x21, float *x31, float *x12, float *x22, float *x32, float *x13, float *x23, float *x33 ) { float derX = calcFirstDerX( x11, x21, x31, x12, x22, x32, x13, x23, x33 ); float derY = calcFirstDerY( x11, x21, x31, x12, x22, x32, x13, x23, x33 ); if ( derX == mOutputNodataValue || derY == mOutputNodataValue ) { return mOutputNodataValue; } float slope_rad = std::atan( std::sqrt( derX * derX + derY * derY ) ); float aspect_rad = 0; if ( derX == 0 && derY == 0 ) //aspect undefined, take a neutral value. Better solutions? { aspect_rad = mAzimuthRad / 2.0f; } else { aspect_rad = M_PI + std::atan2( derX, derY ); } return std::max( 0.0f, 255.0f * ( ( mCosZenithRad * std::cos( slope_rad ) ) + ( mSinZenithRad * std::sin( slope_rad ) * std::cos( mAzimuthRad - aspect_rad ) ) ) ); }
float QgsAspectFilter::processNineCellWindow( float *x11, float *x21, float *x31, float *x12, float *x22, float *x32, float *x13, float *x23, float *x33 ) { float derX = calcFirstDerX( x11, x21, x31, x12, x22, x32, x13, x23, x33 ); float derY = calcFirstDerY( x11, x21, x31, x12, x22, x32, x13, x23, x33 ); if ( derX == mOutputNodataValue || derY == mOutputNodataValue || ( derX == 0.0 && derY == 0.0 ) ) { return mOutputNodataValue; } else { return 180.0 + std::atan2( derX, derY ) * 180.0 / M_PI; } }
QgsRasterBlock *QgsHillshadeRenderer::block( int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback* feedback ) { Q_UNUSED( bandNo ); QgsRasterBlock *outputBlock = new QgsRasterBlock(); if ( !mInput ) { QgsDebugMsg( "No input raster!" ); return outputBlock; } QgsRasterBlock *inputBlock = mInput->block( mBand, extent, width, height, feedback ); if ( !inputBlock || inputBlock->isEmpty() ) { QgsDebugMsg( "No raster data!" ); delete inputBlock; return outputBlock; } QgsRasterBlock *alphaBlock = nullptr; if ( mAlphaBand > 0 && mBand != mAlphaBand ) { alphaBlock = mInput->block( mAlphaBand, extent, width, height, feedback ); if ( !alphaBlock || alphaBlock->isEmpty() ) { // TODO: better to render without alpha delete inputBlock; delete alphaBlock; return outputBlock; } } else if ( mAlphaBand > 0 ) { alphaBlock = inputBlock; } if ( !outputBlock->reset( Qgis::ARGB32_Premultiplied, width, height ) ) { delete inputBlock; delete alphaBlock; return outputBlock; } double cellXSize = extent.width() / double( width ); double cellYSize = extent.height() / double( height ); double zenithRad = qMax( 0.0, 90 - mLightAngle ) * M_PI / 180.0; double azimuthRad = -1 * mLightAzimuth * M_PI / 180.0; double cosZenithRad = cos( zenithRad ); double sinZenithRad = sin( zenithRad ); // Multi direction hillshade: http://pubs.usgs.gov/of/1992/of92-422/of92-422.pdf double angle0Rad = ( -1 * mLightAzimuth - 45 - 45 * 0.5 ) * M_PI / 180.0; double angle1Rad = ( -1 * mLightAzimuth - 45 * 0.5 ) * M_PI / 180.0; double angle2Rad = ( -1 * mLightAzimuth + 45 * 0.5 ) * M_PI / 180.0; double angle3Rad = ( -1 * mLightAzimuth + 45 + 45 * 0.5 ) * M_PI / 180.0; QRgb myDefaultColor = NODATA_COLOR; for ( qgssize i = 0; i < ( qgssize )height; i++ ) { for ( qgssize j = 0; j < ( qgssize )width; j++ ) { if ( inputBlock->isNoData( i, j ) ) { outputBlock->setColor( i, j, myDefaultColor ); continue; } qgssize iUp, iDown, jLeft, jRight; if ( i == 0 ) { iUp = i; iDown = i + 1; } else if ( i < ( qgssize )height - 1 ) { iUp = i - 1; iDown = i + 1; } else { iUp = i - 1; iDown = i; } if ( j == 0 ) { jLeft = j; jRight = j + 1; } else if ( j < ( qgssize )width - 1 ) { jLeft = j - 1; jRight = j + 1; } else { jLeft = j - 1; jRight = j; } double x11; double x21; double x31; double x12; double x22; // Working cell double x32; double x13; double x23; double x33; // This is center cell. It is not nodata. Use this in place of nodata neighbors x22 = inputBlock->value( i, j ); x11 = inputBlock->isNoData( iUp, jLeft ) ? x22 : inputBlock->value( iUp, jLeft ); x21 = inputBlock->isNoData( i, jLeft ) ? x22 : inputBlock->value( i, jLeft ); x31 = inputBlock->isNoData( iDown, jLeft ) ? x22 : inputBlock->value( iDown, jLeft ); x12 = inputBlock->isNoData( iUp, j ) ? x22 : inputBlock->value( iUp, j ); // x22 x32 = inputBlock->isNoData( iDown, j ) ? x22 : inputBlock->value( iDown, j ); x13 = inputBlock->isNoData( iUp, jRight ) ? x22 : inputBlock->value( iUp, jRight ); x23 = inputBlock->isNoData( i, jRight ) ? x22 : inputBlock->value( i, jRight ); x33 = inputBlock->isNoData( iDown, jRight ) ? x22 : inputBlock->value( iDown, jRight ); double derX = calcFirstDerX( x11, x21, x31, x12, x22, x32, x13, x23, x33, cellXSize ); double derY = calcFirstDerY( x11, x21, x31, x12, x22, x32, x13, x23, x33, cellYSize ); double slopeRad = atan( mZFactor * sqrt( derX * derX + derY * derY ) ); double aspectRad = atan2( derX, -derY ); double grayValue; if ( !mMultiDirectional ) { // Standard single direction hillshade grayValue = qBound( 0.0, 255.0 * ( cosZenithRad * cos( slopeRad ) + sinZenithRad * sin( slopeRad ) * cos( azimuthRad - aspectRad ) ) , 255.0 ); } else { // Weighted multi direction as in http://pubs.usgs.gov/of/1992/of92-422/of92-422.pdf double weight0 = sin( aspectRad - angle0Rad ); double weight1 = sin( aspectRad - angle1Rad ); double weight2 = sin( aspectRad - angle2Rad ); double weight3 = sin( aspectRad - angle3Rad ); weight0 = weight0 * weight0; weight1 = weight1 * weight1; weight2 = weight2 * weight2; weight3 = weight3 * weight3; double cosSlope = cosZenithRad * cos( slopeRad ); double sinSlope = sinZenithRad * sin( slopeRad ); double color0 = cosSlope + sinSlope * cos( angle0Rad - aspectRad ) ; double color1 = cosSlope + sinSlope * cos( angle1Rad - aspectRad ) ; double color2 = cosSlope + sinSlope * cos( angle2Rad - aspectRad ) ; double color3 = cosSlope + sinSlope * cos( angle3Rad - aspectRad ) ; grayValue = qBound( 0.0, 255 * ( weight0 * color0 + weight1 * color1 + weight2 * color2 + weight3 * color3 ) * 0.5, 255.0 ); } double currentAlpha = mOpacity; if ( mRasterTransparency ) { currentAlpha = mRasterTransparency->alphaValue( x22, mOpacity * 255 ) / 255.0; } if ( mAlphaBand > 0 ) { currentAlpha *= alphaBlock->value( i ) / 255.0; } if ( qgsDoubleNear( currentAlpha, 1.0 ) ) { outputBlock->setColor( i, j, qRgba( grayValue, grayValue, grayValue, 255 ) ); } else { outputBlock->setColor( i, j, qRgba( currentAlpha * grayValue, currentAlpha * grayValue, currentAlpha * grayValue, currentAlpha * 255 ) ); } } } delete inputBlock; if ( mAlphaBand > 0 && mBand != mAlphaBand ) { delete alphaBlock; } return outputBlock; }
QgsRasterBlock *QgsHillshadeRenderer::block( int bandNo, const QgsRectangle &extent, int width, int height ) { Q_UNUSED( bandNo ); QgsRasterBlock *outputBlock = new QgsRasterBlock(); if ( !mInput ) { QgsDebugMsg( "No input raster!" ); return outputBlock; } QgsRasterBlock *inputBlock = mInput->block( mBand, extent, width, height ); if ( !inputBlock || inputBlock->isEmpty() ) { QgsDebugMsg( "No raster data!" ); delete inputBlock; return outputBlock; } if ( !outputBlock->reset( QGis::ARGB32_Premultiplied, width, height ) ) { delete inputBlock; return outputBlock; } double cellXSize = extent.width() / double( width ); double cellYSize = extent.height() / double( height ); double zenithRad = qMax( 0.0, 90 - mLightAngle ) * M_PI / 180.0; double azimuthRad = -1 * mLightAzimuth * M_PI / 180.0; double cosZenithRad = cos( zenithRad ); double sinZenithRad = sin( zenithRad ); QRgb myDefaultColor = NODATA_COLOR; for ( qgssize i = 0; i < ( qgssize )height; i++ ) { for ( qgssize j = 0; j < ( qgssize )width; j++ ) { if ( inputBlock->isNoData( i, j ) ) { outputBlock->setColor( i, j, myDefaultColor ); continue; } qgssize iUp, iDown, jLeft, jRight; if ( i == 0 ) { iUp = i; iDown = i + 1; } else if ( i < ( qgssize )height - 1 ) { iUp = i - 1; iDown = i + 1; } else { iUp = i - 1; iDown = i; } if ( j == 0 ) { jLeft = j; jRight = j + 1; } else if ( j < ( qgssize )width - 1 ) { jLeft = j - 1; jRight = j + 1; } else { jLeft = j - 1; jRight = j; } double x11; double x21; double x31; double x12; double x22; // Working cell double x32; double x13; double x23; double x33; // This is center cell. It is not nodata. Use this in place of nodata neighbors x22 = inputBlock->value( i, j ); x11 = inputBlock->isNoData( iUp, jLeft ) ? x22 : inputBlock->value( iUp, jLeft ); x21 = inputBlock->isNoData( i, jLeft ) ? x22 : inputBlock->value( i, jLeft ); x31 = inputBlock->isNoData( iDown, jLeft ) ? x22 : inputBlock->value( iDown, jLeft ); x12 = inputBlock->isNoData( iUp, j ) ? x22 : inputBlock->value( iUp, j ); // x22 x32 = inputBlock->isNoData( iDown, j ) ? x22 : inputBlock->value( iDown, j ); x13 = inputBlock->isNoData( iUp, jRight ) ? x22 : inputBlock->value( iUp, jRight ); x23 = inputBlock->isNoData( i, jRight ) ? x22 : inputBlock->value( i, jRight ); x33 = inputBlock->isNoData( iDown, jRight ) ? x22 : inputBlock->value( iDown, jRight ); double derX = calcFirstDerX( x11, x21, x31, x12, x22, x32, x13, x23, x33, cellXSize ); double derY = calcFirstDerY( x11, x21, x31, x12, x22, x32, x13, x23, x33, cellYSize ); double slope_rad = atan( mZFactor * sqrt( derX * derX + derY * derY ) ); double aspectRad = atan2( derX, -derY ); double colorvalue = qBound( 0.0, 255.0 * (( cosZenithRad * cos( slope_rad ) ) + ( sinZenithRad * sin( slope_rad ) * cos( azimuthRad - aspectRad ) ) ), 255.0 ); outputBlock->setColor( i, j, qRgb( colorvalue, colorvalue, colorvalue ) ); } } return outputBlock; }