103 auto outputBlock = std::make_unique<QgsRasterBlock>();
107 return outputBlock.release();
110 std::shared_ptr< QgsRasterBlock > inputBlock(
mInput->
block( mBand,
extent, width, height, feedback ) );
112 if ( !inputBlock || inputBlock->isEmpty() )
115 return outputBlock.release();
118 std::shared_ptr< QgsRasterBlock > alphaBlock;
123 if ( !alphaBlock || alphaBlock->isEmpty() )
126 return outputBlock.release();
131 alphaBlock = inputBlock;
136 return outputBlock.release();
139 if ( width == 0 || height == 0 )
140 return outputBlock.release();
145 float cellXSize =
static_cast<float>(
extent.
width() ) / width;
146 float cellYSize =
static_cast<float>(
extent.
height() ) / height;
147 float zenithRad =
static_cast<float>( std::max( 0.0, 90 - mLightAngle ) * M_PI / 180.0 );
148 float azimuthRad =
static_cast<float>( -1 * mLightAzimuth * M_PI / 180.0 );
149 float cosZenithRad = std::cos( zenithRad );
150 float sinZenithRad = std::sin( zenithRad );
153 float cos_alt_mul_z = cosZenithRad *
static_cast<float>( mZFactor );
154 float cos_az_mul_cos_alt_mul_z = std::cos( azimuthRad ) * cos_alt_mul_z;
155 float sin_az_mul_cos_alt_mul_z = std::sin( azimuthRad ) * cos_alt_mul_z;
156 float cos_az_mul_cos_alt_mul_z_mul_254 = 254.0f * cos_az_mul_cos_alt_mul_z;
157 float sin_az_mul_cos_alt_mul_z_mul_254 = 254.0f * sin_az_mul_cos_alt_mul_z;
158 float square_z =
static_cast<float>( mZFactor * mZFactor );
159 float sin_altRadians_mul_254 = 254.0f * sinZenithRad;
162 float sin_altRadians_mul_127 = 127.0f * sinZenithRad;
164 float cos225_az_mul_cos_alt_mul_z_mul_127 = -32.87001872802012f * cos_alt_mul_z;
165 float cos_alt_mul_z_mul_127 = 127.0f * cos_alt_mul_z;
176 && inputBlock->dataTypeSize() <= 4 );
182 if ( source.isEmpty() )
190 std::chrono::time_point<std::chrono::system_clock> startTime( std::chrono::system_clock::now() );
198 std::size_t inputDataTypeSize = inputBlock->dataTypeSize();
199 std::size_t outputDataTypeSize = outputBlock->dataTypeSize();
202 switch ( inputBlock->dataType() )
205 typeName = QStringLiteral(
"unsigned char" );
208 typeName = QStringLiteral(
"unsigned int" );
211 typeName = QStringLiteral(
"short" );
214 typeName = QStringLiteral(
"unsigned int" );
220 typeName = QStringLiteral(
"float" );
223 throw QgsException( QStringLiteral(
"Unsupported data type for OpenCL processing." ) );
228 source.replace( QLatin1String(
"__global float *scanLine" ), QStringLiteral(
"__global %1 *scanLine" ).arg(
typeName ) );
232 std::size_t scanLineWidth( inputBlock->width() + 2 );
233 std::size_t inputSize( inputDataTypeSize * inputBlock->width() );
236 std::size_t bufferWidth( width + 2 );
237 std::size_t bufferSize( inputDataTypeSize * bufferWidth );
242 auto scanLine = std::make_unique<QgsRasterBlock>( inputBlock->dataType(), scanLineWidth, 1 );
249 std::vector<float> rasterParams;
250 rasterParams.push_back( inputBlock->noDataValue() );
251 rasterParams.push_back( outputBlock->noDataValue() );
252 rasterParams.push_back( mZFactor );
253 rasterParams.push_back( cellXSize );
254 rasterParams.push_back( cellYSize );
255 rasterParams.push_back(
static_cast<float>(
mOpacity ) );
258 rasterParams.push_back( cos_az_mul_cos_alt_mul_z_mul_254 );
259 rasterParams.push_back( sin_az_mul_cos_alt_mul_z_mul_254 );
260 rasterParams.push_back( square_z );
261 rasterParams.push_back( sin_altRadians_mul_254 );
264 rasterParams.push_back( sin_altRadians_mul_127 );
265 rasterParams.push_back( cos225_az_mul_cos_alt_mul_z_mul_127 );
266 rasterParams.push_back( cos_alt_mul_z_mul_127 );
269 rasterParams.push_back(
static_cast<float>( qBlue( defaultNodataColor ) ) );
270 rasterParams.push_back(
static_cast<float>( qGreen( defaultNodataColor ) ) );
271 rasterParams.push_back(
static_cast<float>( qRed( defaultNodataColor ) ) );
272 rasterParams.push_back(
static_cast<float>( qAlpha( defaultNodataColor ) ) / 255.0f );
275 rasterParams.push_back(
static_cast<float>( mMultiDirectional ) );
277 cl::Buffer rasterParamsBuffer( queue, rasterParams.begin(), rasterParams.end(),
true,
false,
nullptr );
278 cl::Buffer scanLine1Buffer( ctx, CL_MEM_READ_ONLY, bufferSize,
nullptr,
nullptr );
279 cl::Buffer scanLine2Buffer( ctx, CL_MEM_READ_ONLY, bufferSize,
nullptr,
nullptr );
280 cl::Buffer scanLine3Buffer( ctx, CL_MEM_READ_ONLY, bufferSize,
nullptr,
nullptr );
281 cl::Buffer *scanLineBuffer[3] = {&scanLine1Buffer, &scanLine2Buffer, &scanLine3Buffer};
283 cl::Buffer resultLineBuffer( ctx, CL_MEM_WRITE_ONLY, outputDataTypeSize * width,
nullptr,
nullptr );
285 static std::map<Qgis::DataType, cl::Program> programCache;
286 cl::Program program = programCache[inputBlock->dataType()];
287 if ( ! program.get() )
291 program = programCache[inputBlock->dataType()];
298 auto kernel = cl::KernelFunctor <
304 > ( program,
"processNineCellWindow" );
308 std::vector<int> rowIndex = {0, 1, 2};
310 for (
int i = 0; i < height; i++ )
319 feedback->
setProgress( 100.0 *
static_cast< double >( i ) / height );
325 scanLine->resetNoDataValue();
326 queue.enqueueWriteBuffer( scanLine1Buffer, CL_TRUE, 0, bufferSize, scanLine->bits( ) );
328 memcpy( scanLine->bits( 0, 1 ), inputBlock->bits( i, 0 ), inputSize );
329 queue.enqueueWriteBuffer( scanLine2Buffer, CL_TRUE, 0, bufferSize, scanLine->bits( ) );
331 memcpy( scanLine->bits( 0, 1 ), inputBlock->bits( i + 1, 0 ), inputSize );
332 queue.enqueueWriteBuffer( scanLine3Buffer, CL_TRUE, 0, bufferSize, scanLine->bits( ) );
338 if ( i == inputBlock->height() - 1 )
340 scanLine->resetNoDataValue();
341 queue.enqueueWriteBuffer( *scanLineBuffer[rowIndex[2]], CL_TRUE, 0, bufferSize, scanLine->bits( ) );
345 queue.enqueueWriteBuffer( *scanLineBuffer[rowIndex[2]], CL_TRUE, inputDataTypeSize * 1 , inputSize, inputBlock->bits( i + 1, 0 ) );
349 kernel( cl::EnqueueArgs(
353 *scanLineBuffer[rowIndex[0]],
354 *scanLineBuffer[rowIndex[1]],
355 *scanLineBuffer[rowIndex[2]],
360 queue.enqueueReadBuffer( resultLineBuffer, CL_TRUE, 0, outputDataTypeSize * outputBlock->width( ), outputBlock->bits( i, 0 ) );
361 std::rotate( rowIndex.begin(), rowIndex.begin() + 1, rowIndex.end() );
364 catch ( cl::Error &e )
379 double pixelValues[9] {0, 0, 0, 0, 0, 0, 0, 0, 0};
380 bool isNoData[9] {
false,
false,
false,
false,
false,
false,
false,
false,
false};
382 for (
int row = 0; row < height; row++ )
384 for (
int col = 0; col < width; col++ )
392 else if ( row == height - 1 )
400 pixelValues[ 0 ] = inputBlock->valueAndNoData( iUp, 0, isNoData[0] );
401 pixelValues[ 1 ] = pixelValues[0];
402 isNoData[1] = isNoData[0];
403 pixelValues[ 2 ] = pixelValues[0];
404 isNoData[2] = isNoData[0];
406 pixelValues[ 3 ] = inputBlock->valueAndNoData( row, 0, isNoData[3] );
407 pixelValues[ 4 ] = pixelValues[3];
408 isNoData[4] = isNoData[3];
409 pixelValues[ 5 ] = pixelValues[3];
410 isNoData[5] = isNoData[3];
412 pixelValues[ 6 ] = inputBlock->valueAndNoData( iDown, 0, isNoData[6] );
413 pixelValues[ 7 ] = pixelValues[6];
414 isNoData[7] = isNoData[6];
415 pixelValues[ 8 ] = pixelValues[6];
416 isNoData[8] = isNoData[6];
421 pixelValues[ 0 ] = pixelValues[1];
422 pixelValues[ 1 ] = pixelValues[2];
423 pixelValues[ 3 ] = pixelValues[4];
424 pixelValues[ 4 ] = pixelValues[5];
425 pixelValues[ 6 ] = pixelValues[7];
426 pixelValues[ 7 ] = pixelValues[8];
427 isNoData[ 0 ] = isNoData[1];
428 isNoData[ 1 ] = isNoData[2];
429 isNoData[ 3 ] = isNoData[4];
430 isNoData[ 4 ] = isNoData[5];
431 isNoData[ 6 ] = isNoData[7];
432 isNoData[ 7 ] = isNoData[8];
436 if ( col < width - 1 )
438 pixelValues[2] = inputBlock->valueAndNoData( iUp, col + 1, isNoData[2] );
439 pixelValues[5] = inputBlock->valueAndNoData( row, col + 1, isNoData[5] );
440 pixelValues[8] = inputBlock->valueAndNoData( iDown, col + 1, isNoData[8] );
445 outputBlock->setColor( row, col, defaultNodataColor );
450 const double x22 = pixelValues[4];
452 const double x11 = isNoData[0] ? x22 : pixelValues[0];
453 const double x21 = isNoData[3] ? x22 : pixelValues[3];
454 const double x31 = isNoData[6] ? x22 : pixelValues[6];
455 const double x12 = isNoData[1] ? x22 : pixelValues[1];
457 const double x32 = isNoData[7] ? x22 : pixelValues[7];
458 const double x13 = isNoData[2] ? x22 : pixelValues[2];
459 const double x23 = isNoData[5] ? x22 : pixelValues[5];
460 const double x33 = isNoData[8] ? x22 : pixelValues[8];
463 const double derX = ( ( x13 + x23 + x23 + x33 ) - ( x11 + x21 + x21 + x31 ) ) / ( 8 * cellXSize );
464 const double derY = ( ( x31 + x32 + x32 + x33 ) - ( x11 + x12 + x12 + x13 ) ) / ( 8 * -cellYSize );
469 if ( !mMultiDirectional )
472 grayValue = std::clamp( ( sin_altRadians_mul_254 -
473 ( derY * cos_az_mul_cos_alt_mul_z_mul_254 -
474 derX * sin_az_mul_cos_alt_mul_z_mul_254 ) ) /
475 std::sqrt( 1 + square_z * ( derX * derX + derY * derY ) ),
482 const float xx = derX * derX;
483 const float yy = derY * derY;
484 const float xx_plus_yy = xx + yy;
486 if ( xx_plus_yy == 0.0 )
488 grayValue = std::clamp(
static_cast<float>( 1.0 + sin_altRadians_mul_254 ), 0.0f, 255.0f );
493 float val225_mul_127 = sin_altRadians_mul_127 +
494 ( derX - derY ) * cos225_az_mul_cos_alt_mul_z_mul_127;
495 val225_mul_127 = ( val225_mul_127 <= 0.0 ) ? 0.0 : val225_mul_127;
496 float val270_mul_127 = sin_altRadians_mul_127 -
497 derX * cos_alt_mul_z_mul_127;
498 val270_mul_127 = ( val270_mul_127 <= 0.0 ) ? 0.0 : val270_mul_127;
499 float val315_mul_127 = sin_altRadians_mul_127 +
500 ( derX + derY ) * cos225_az_mul_cos_alt_mul_z_mul_127;
501 val315_mul_127 = ( val315_mul_127 <= 0.0 ) ? 0.0 : val315_mul_127;
502 float val360_mul_127 = sin_altRadians_mul_127 -
503 derY * cos_alt_mul_z_mul_127;
504 val360_mul_127 = ( val360_mul_127 <= 0.0 ) ? 0.0 : val360_mul_127;
507 const float weight_225 = 0.5 * xx_plus_yy - derX * derY;
508 const float weight_270 = xx;
509 const float weight_315 = xx_plus_yy - weight_225;
510 const float weight_360 = yy;
511 const float cang_mul_127 = (
512 ( weight_225 * val225_mul_127 +
513 weight_270 * val270_mul_127 +
514 weight_315 * val315_mul_127 +
515 weight_360 * val360_mul_127 ) / xx_plus_yy ) /
516 ( 1 + square_z * xx_plus_yy );
518 grayValue = std::clamp( 1.0f + cang_mul_127, 0.0f, 255.0f );
529 currentAlpha *= alphaBlock->value( row ) / 255.0;
534 outputBlock->setColor( row, col, qRgba( grayValue, grayValue, grayValue, 255 ) );
538 outputBlock->setColor( row, col, qRgba( currentAlpha * grayValue, currentAlpha * grayValue, currentAlpha * grayValue, currentAlpha * 255 ) );
547 if (
QgsSettings().value( QStringLiteral(
"Map/logCanvasRefreshEvent" ),
false ).toBool() )
550 .arg( useOpenCL ? QStringLiteral(
"OpenCL" ) : QStringLiteral(
"CPU" ) )
553 .arg( std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::system_clock::now() - startTime ).count() ),
559 return outputBlock.release();
611 QDomNodeList elements = element.elementsByTagName( QStringLiteral(
"sld:RasterSymbolizer" ) );
612 if ( elements.size() == 0 )
616 QDomElement rasterSymbolizerElem = elements.at( 0 ).toElement();
623 QDomElement channelSelectionElem = doc.createElement( QStringLiteral(
"sld:ChannelSelection" ) );
624 elements = rasterSymbolizerElem.elementsByTagName( QStringLiteral(
"sld:Opacity" ) );
625 if ( elements.size() != 0 )
627 rasterSymbolizerElem.insertAfter( channelSelectionElem, elements.at( 0 ) );
631 elements = rasterSymbolizerElem.elementsByTagName( QStringLiteral(
"sld:Geometry" ) );
632 if ( elements.size() != 0 )
634 rasterSymbolizerElem.insertAfter( channelSelectionElem, elements.at( 0 ) );
638 rasterSymbolizerElem.insertBefore( channelSelectionElem, rasterSymbolizerElem.firstChild() );
643 QDomElement channelElem = doc.createElement( QStringLiteral(
"sld:GrayChannel" ) );
644 channelSelectionElem.appendChild( channelElem );
647 QDomElement sourceChannelNameElem = doc.createElement( QStringLiteral(
"sld:SourceChannelName" ) );
648 sourceChannelNameElem.appendChild( doc.createTextNode( QString::number( mBand ) ) );
649 channelElem.appendChild( sourceChannelNameElem );
653 QDomElement shadedReliefElem = doc.createElement( QStringLiteral(
"sld:ShadedRelief" ) );
654 rasterSymbolizerElem.appendChild( shadedReliefElem );
657 QDomElement brightnessOnlyElem = doc.createElement( QStringLiteral(
"sld:BrightnessOnly" ) );
658 brightnessOnlyElem.appendChild( doc.createTextNode( QStringLiteral(
"true" ) ) );
659 shadedReliefElem.appendChild( brightnessOnlyElem );
662 QDomElement reliefFactorElem = doc.createElement( QStringLiteral(
"sld:ReliefFactor" ) );
663 reliefFactorElem.appendChild( doc.createTextNode( QString::number(
zFactor() ) ) );
664 shadedReliefElem.appendChild( reliefFactorElem );
667 QDomElement altitudeVendorOptionElem = doc.createElement( QStringLiteral(
"sld:VendorOption" ) );
668 altitudeVendorOptionElem.setAttribute( QStringLiteral(
"name" ), QStringLiteral(
"altitude" ) );
669 altitudeVendorOptionElem.appendChild( doc.createTextNode( QString::number(
altitude() ) ) );
670 shadedReliefElem.appendChild( altitudeVendorOptionElem );
673 QDomElement azimutVendorOptionElem = doc.createElement( QStringLiteral(
"sld:VendorOption" ) );
674 azimutVendorOptionElem.setAttribute( QStringLiteral(
"name" ), QStringLiteral(
"azimuth" ) );
675 azimutVendorOptionElem.appendChild( doc.createTextNode( QString::number(
azimuth() ) ) );
676 shadedReliefElem.appendChild( azimutVendorOptionElem );
679 QDomElement multidirectionalVendorOptionElem = doc.createElement( QStringLiteral(
"sld:VendorOption" ) );
680 multidirectionalVendorOptionElem.setAttribute( QStringLiteral(
"name" ), QStringLiteral(
"multidirectional" ) );
681 multidirectionalVendorOptionElem.appendChild( doc.createTextNode( QString::number(
multiDirectional() ) ) );
682 shadedReliefElem.appendChild( multidirectionalVendorOptionElem );