@@ -521,9 +521,9 @@ bool QgsRasterLayer::readFile( QString const & fileName )
521
521
myRasterBandStats.bandNo = i;
522
522
myRasterBandStats.statsGatheredFlag = false ;
523
523
myRasterBandStats.histogramVector = new QgsRasterBandStats::HistogramVector ();
524
- // Read color table
525
- readColorTable ( myGdalBand , &( myRasterBandStats.colorTable ) );
526
-
524
+ // Store the default color table
525
+ readColorTable (i , &myRasterBandStats.colorTable );
526
+
527
527
mRasterStatsList .push_back ( myRasterBandStats );
528
528
529
529
// Build a new contrast enhancement for the band and store in list
@@ -562,14 +562,11 @@ bool QgsRasterLayer::readFile( QString const & fileName )
562
562
563
563
drawingStyle = PALETTED_COLOR; // sensible default
564
564
565
- // Load the color table from the band
566
- QList<QgsColorRampShader::ColorRampItem> myColorRampList;
567
- readColorTable ( 1 , &myColorRampList );
568
565
// Set up a new color ramp shader
569
566
setColorShadingAlgorithm ( COLOR_RAMP );
570
567
QgsColorRampShader* myColorRampShader = ( QgsColorRampShader* ) mRasterShader ->getRasterShaderFunction ();
571
568
myColorRampShader->setColorRampType ( QgsColorRampShader::EXACT );
572
- myColorRampShader->setColorRampItemList ( myColorRampList );
569
+ myColorRampShader->setColorRampItemList ( * getColorTable ( 1 ) );
573
570
}
574
571
else if ( rasterLayerType == MULTIBAND )
575
572
{
@@ -886,27 +883,23 @@ void QgsRasterLayer::drawThumbnail( QPixmap * theQPixmap )
886
883
887
884
888
885
889
- QPixmap QgsRasterLayer::getPaletteAsPixmap ()
886
+ QPixmap QgsRasterLayer::getPaletteAsPixmap (int theBandNumber )
890
887
{
891
888
QgsDebugMsg ( " entered." );
892
889
893
890
// Only do this for the non-provider (hard-coded GDAL) scenario...
894
891
// Maybe WMS can do this differently using QImage::numColors and QImage::color()
895
- if (
896
- ( mProviderKey .isEmpty () ) &&
897
- ( hasBand ( " Palette" ) ) // dont tr() this its a gdal word!
898
- )
892
+ if (mProviderKey .isEmpty () && hasBand ( " Palette" ) && theBandNumber > 0 ) // dont tr() this its a gdal word!
899
893
{
900
894
QgsDebugMsg ( " ....found paletted image" );
901
- QgsColorTable *myColorTable = colorTable ( 1 );
902
- GDALRasterBandH myGdalBand = GDALGetRasterBand ( mGdalDataset , 1 );
903
- if ( GDALGetRasterColorInterpretation ( myGdalBand ) == GCI_PaletteIndex && myColorTable->defined () )
895
+ QgsColorRampShader myShader;
896
+ QList<QgsColorRampShader::ColorRampItem> myColorRampItemList = myShader.getColorRampItemList ();
897
+
898
+ if (readColorTable (1 , &myColorRampItemList))
904
899
{
905
- QgsDebugMsg ( " ....found GCI_PaletteIndex" );
906
- double myMin = myColorTable->rmin ();
907
- double myMax = myColorTable->rmax ();
908
- QgsDebugMsg ( " myMin = " + QString::number ( myMin ) + " myMax = " + QString::number ( myMax ) );
909
-
900
+ QgsDebugMsg ( " ....got color ramp item list" );
901
+ myShader.setColorRampItemList (myColorRampItemList);
902
+ myShader.setColorRampType (QgsColorRampShader::DISCRETE);
910
903
// Draw image
911
904
int mySize = 100 ;
912
905
QPixmap myPalettePixmap ( mySize, mySize );
@@ -916,20 +909,17 @@ QPixmap QgsRasterLayer::getPaletteAsPixmap()
916
909
myQImage.fill ( 0 );
917
910
myPalettePixmap.fill ();
918
911
919
- double myStep = ( myMax - myMin ) / ( mySize * mySize );
920
-
912
+ double myStep = ( ( double )myColorRampItemList. size () - 1 ) / ( double ) ( mySize * mySize );
913
+ double myValue = 0.0 ;
921
914
for ( int myRow = 0 ; myRow < mySize; myRow++ )
922
915
{
923
916
for ( int myCol = 0 ; myCol < mySize; myCol++ )
924
917
{
925
918
926
- double myValue = myMin + myStep * ( myCol + myRow * mySize );
927
-
919
+ myValue = myStep * (double )( myCol + myRow * mySize );
928
920
int c1, c2, c3;
929
- bool found = myColorTable->color ( myValue, &c1, &c2, &c3 );
930
-
931
- if ( found )
932
- myQImage.setPixel ( myCol, myRow, qRgb ( c1, c2, c3 ) );
921
+ myShader.generateShadedValue ( myValue, &c1, &c2, &c3 );
922
+ myQImage.setPixel ( myCol, myRow, qRgb ( c1, c2, c3 ) );
933
923
}
934
924
}
935
925
@@ -1257,7 +1247,7 @@ void QgsRasterLayer::draw( QPainter * theQPainter,
1257
1247
1258
1248
int myBandNo = 1 ;
1259
1249
drawPalettedSingleBandGray ( theQPainter, theRasterViewPort,
1260
- theQgsMapToPixel, myBandNo, mGrayBandName );
1250
+ theQgsMapToPixel, myBandNo);
1261
1251
1262
1252
break ;
1263
1253
}
@@ -1498,6 +1488,7 @@ void QgsRasterLayer::drawSingleBandPseudoColor( QPainter * theQPainter,
1498
1488
1499
1489
double myPixelValue = 0.0 ;
1500
1490
int myAlphaValue = 0 ;
1491
+ QgsDebugMsg ( " Starting main render loop" );
1501
1492
for ( int myColumn = 0 ; myColumn < theRasterViewPort->drawableAreaYDim ; ++myColumn )
1502
1493
{
1503
1494
for ( int myRow = 0 ; myRow < theRasterViewPort->drawableAreaXDim ; ++myRow )
@@ -1581,7 +1572,8 @@ void QgsRasterLayer::drawPalettedSingleBandColor( QPainter * theQPainter, QgsRas
1581
1572
int myGreenValue = 0 ;
1582
1573
int myBlueValue = 0 ;
1583
1574
int myAlphaValue = 0 ;
1584
-
1575
+
1576
+ QgsDebugMsg ( " Starting main render loop" );
1585
1577
for ( int myColumn = 0 ; myColumn < theRasterViewPort->drawableAreaYDim ; ++myColumn )
1586
1578
{
1587
1579
for ( int myRow = 0 ; myRow < theRasterViewPort->drawableAreaXDim ; ++myRow )
@@ -1637,8 +1629,7 @@ void QgsRasterLayer::drawPalettedSingleBandColor( QPainter * theQPainter, QgsRas
1637
1629
* @param theColorQString - QString containing either 'Red' 'Green' or 'Blue' indicating which part of the rgb triplet will be used to render gray.
1638
1630
*/
1639
1631
void QgsRasterLayer::drawPalettedSingleBandGray ( QPainter * theQPainter, QgsRasterViewPort * theRasterViewPort,
1640
- const QgsMapToPixel* theQgsMapToPixel, int theBandNo,
1641
- QString const & theColorQString )
1632
+ const QgsMapToPixel* theQgsMapToPixel, int theBandNo)
1642
1633
{
1643
1634
QgsDebugMsg ( " entered." );
1644
1635
// Invalid band number, segfault prevention
@@ -1647,7 +1638,11 @@ void QgsRasterLayer::drawPalettedSingleBandGray( QPainter * theQPainter, QgsRast
1647
1638
return ;
1648
1639
}
1649
1640
1650
- QgsRasterBandStats myRasterBandStats = getRasterBandStats ( theBandNo );
1641
+ if ( NULL == mRasterShader )
1642
+ {
1643
+ return ;
1644
+ }
1645
+
1651
1646
GDALRasterBandH myGdalBand = GDALGetRasterBand ( mGdalDataset , theBandNo );
1652
1647
GDALDataType myDataType = GDALGetRasterDataType ( myGdalBand );
1653
1648
void *myGdalScanData = readData ( myGdalBand, theRasterViewPort );
@@ -1658,41 +1653,23 @@ void QgsRasterLayer::drawPalettedSingleBandGray( QPainter * theQPainter, QgsRast
1658
1653
return ;
1659
1654
}
1660
1655
1661
- QgsColorTable *myColorTable = &( myRasterBandStats.colorTable );
1662
-
1663
1656
QImage myQImage = QImage ( theRasterViewPort->drawableAreaXDim , theRasterViewPort->drawableAreaYDim , QImage::Format_ARGB32 );
1664
1657
myQImage.fill ( qRgba ( 255 , 255 , 255 , 0 ) ); // fill transparent
1665
1658
1666
- bool found = false ;
1667
1659
double myPixelValue = 0.0 ;
1668
- int myRedLUTValue = 0 ;
1669
- int myGreenLUTValue = 0 ;
1670
- int myBlueLUTValue = 0 ;
1660
+ int myRedValue = 0 ;
1661
+ int myGreenValue = 0 ;
1662
+ int myBlueValue = 0 ;
1671
1663
int myAlphaValue = 0 ;
1672
1664
1673
- // Set a pointer to the LUT color channel
1674
- int * myGrayValue;
1675
- if ( theColorQString == mRedBandName )
1676
- {
1677
- myGrayValue = &myRedLUTValue;
1678
- }
1679
- else if ( theColorQString == mGreenBandName )
1680
- {
1681
- myGrayValue = &myGreenLUTValue;
1682
- }
1683
- else
1684
- {
1685
- myGrayValue = &myBlueLUTValue;
1686
- }
1687
-
1665
+ QgsDebugMsg ( " Starting main render loop" );
1688
1666
for ( int myColumn = 0 ; myColumn < theRasterViewPort->drawableAreaYDim ; ++myColumn )
1689
1667
{
1690
1668
for ( int myRow = 0 ; myRow < theRasterViewPort->drawableAreaXDim ; ++myRow )
1691
1669
{
1692
- myRedLUTValue = 0 ;
1693
- myGreenLUTValue = 0 ;
1694
- myBlueLUTValue = 0 ;
1695
- found = false ;
1670
+ myRedValue = 0 ;
1671
+ myGreenValue = 0 ;
1672
+ myBlueValue = 0 ;
1696
1673
myPixelValue = readValue ( myGdalScanData, ( GDALDataType )myDataType,
1697
1674
myColumn * theRasterViewPort->drawableAreaXDim + myRow );
1698
1675
@@ -1707,15 +1684,23 @@ void QgsRasterLayer::drawPalettedSingleBandGray( QPainter * theQPainter, QgsRast
1707
1684
continue ;
1708
1685
}
1709
1686
1710
- found = myColorTable->color ( myPixelValue, &myRedLUTValue, &myGreenLUTValue, &myBlueLUTValue );
1711
- if ( !found ) continue ;
1687
+ if ( !mRasterShader ->generateShadedValue ( myPixelValue, &myRedValue, &myGreenValue, &myBlueValue ) )
1688
+ {
1689
+ continue ;
1690
+ }
1712
1691
1713
1692
if ( mInvertPixelsFlag )
1714
1693
{
1715
- *myGrayValue = 255 - *myGrayValue;
1694
+ // Invert flag, flip blue and read
1695
+ double myGrayValue = (0.3 * (double )myRedValue) + (0.59 * (double )myGreenValue) + (0.11 * (double )myBlueValue);
1696
+ myQImage.setPixel ( myRow, myColumn, qRgba ( (int )myGrayValue, (int )myGrayValue, (int )myGrayValue, myAlphaValue ) );
1697
+ }
1698
+ else
1699
+ {
1700
+ // Normal
1701
+ double myGrayValue = (0.3 * (double )myBlueValue) + (0.59 * (double )myGreenValue) + (0.11 * (double )myRedValue);
1702
+ myQImage.setPixel ( myRow, myColumn, qRgba ( (int )myGrayValue, (int )myGrayValue, (int )myGrayValue, myAlphaValue ) );
1716
1703
}
1717
-
1718
- myQImage.setPixel ( myRow, myColumn, qRgba ( *myGrayValue, *myGrayValue, *myGrayValue, myAlphaValue ) );
1719
1704
}
1720
1705
}
1721
1706
CPLFree ( myGdalScanData );
@@ -1787,6 +1772,7 @@ void QgsRasterLayer::drawPalettedSingleBandPseudoColor( QPainter * theQPainter,
1787
1772
int myBlueValue = 0 ;
1788
1773
int myAlphaValue = 0 ;
1789
1774
1775
+ QgsDebugMsg ( " Starting main render loop" );
1790
1776
for ( int myColumn = 0 ; myColumn < theRasterViewPort->drawableAreaYDim ; ++myColumn )
1791
1777
{
1792
1778
for ( int myRow = 0 ; myRow < theRasterViewPort->drawableAreaXDim ; ++myRow )
@@ -1964,6 +1950,8 @@ void QgsRasterLayer::drawMultiBandColor( QPainter * theQPainter, QgsRasterViewPo
1964
1950
QgsContrastEnhancement* myRedContrastEnhancement = getContrastEnhancement ( myRedBandNo );
1965
1951
QgsContrastEnhancement* myGreenContrastEnhancement = getContrastEnhancement ( myGreenBandNo );
1966
1952
QgsContrastEnhancement* myBlueContrastEnhancement = getContrastEnhancement ( myBlueBandNo );
1953
+
1954
+ QgsDebugMsg ( " Starting main render loop" );
1967
1955
for ( int myColumn = 0 ; myColumn < theRasterViewPort->drawableAreaYDim ; ++myColumn )
1968
1956
{
1969
1957
for ( int myRow = 0 ; myRow < theRasterViewPort->drawableAreaXDim ; ++myRow )
@@ -2209,10 +2197,6 @@ const QgsRasterBandStats QgsRasterLayer::getRasterBandStats( int theBandNo )
2209
2197
2210
2198
QString myColorerpretation = GDALGetColorInterpretationName ( GDALGetRasterColorInterpretation ( myGdalBand ) );
2211
2199
2212
- // declare a colorTable to hold a palette - will only be used if the layer color interp is palette ???
2213
- // get the palette colour table
2214
- QgsColorTable *myColorTable = &( myRasterBandStats.colorTable );
2215
-
2216
2200
// XXX this sets the element count to a sensible value; but then you ADD to
2217
2201
// XXX it later while iterating through all the pixels?
2218
2202
// myRasterBandStats.elementCount = mRasterXDim * mRasterYDim;
@@ -2395,32 +2379,6 @@ const QgsRasterBandStats QgsRasterLayer::getRasterBandStats( int theBandNo )
2395
2379
continue ; // NULL
2396
2380
}
2397
2381
2398
- // get the nth element from the current row
2399
- if ( myColorerpretation == " Palette" ) // dont translate this its a gdal string
2400
- {
2401
- // this is a palette layer so red / green / blue 'layers are 'virtual'
2402
- // in that we need to obtain the palette entry and then get the r,g or g
2403
- // component from that palette entry
2404
-
2405
- int c1, c2, c3;
2406
- bool found = myColorTable->color ( my, &c1, &c2, &c3 );
2407
- if ( !found ) continue ;
2408
-
2409
- // check for alternate color mappings
2410
- switch ( theBandNo )
2411
- {
2412
- case 1 :
2413
- my = c1;
2414
- break ;
2415
- case 2 :
2416
- my = c2;
2417
- break ;
2418
- case 3 :
2419
- my = c3;
2420
- break ;
2421
- }
2422
- }
2423
-
2424
2382
myRasterBandStats.sumSqrDev += static_cast < double >
2425
2383
( pow ( my - myRasterBandStats.mean , 2 ) );
2426
2384
}
@@ -3770,67 +3728,7 @@ bool QgsRasterLayer::readColorTable( int theBandNumber, QList<QgsColorRampShader
3770
3728
return true ;
3771
3729
}
3772
3730
3773
- void QgsRasterLayer::readColorTable ( GDALRasterBandH gdalBand, QgsColorTable *theColorTable )
3774
- {
3775
- QgsDebugMsg ( " entered." );
3776
-
3777
- // First try to read color table from metadata
3778
- char **metadata = GDALGetMetadata ( gdalBand, NULL );
3779
- theColorTable->clear ();
3780
- bool found = false ;
3781
- while ( metadata && metadata[0 ] )
3782
- {
3783
- QStringList metadataTokens = QString ( *metadata ).split ( " =" , QString::SkipEmptyParts );
3784
-
3785
- if ( metadataTokens.count () < 2 ) continue ;
3786
-
3787
- if ( metadataTokens[0 ].contains ( " COLOR_TABLE_RULE_RGB_" ) )
3788
- {
3789
- double min, max;
3790
- int min_c1, min_c2, min_c3, max_c1, max_c2, max_c3;
3791
-
3792
- if ( sscanf ( metadataTokens[1 ].toLocal8Bit ().data (), " %lf %lf %d %d %d %d %d %d" ,
3793
- &min, &max, &min_c1, &min_c2, &min_c3, &max_c1, &max_c2, &max_c3 ) != 8 )
3794
- {
3795
- continue ;
3796
- }
3797
- theColorTable->add ( min, max,
3798
- ( unsigned char )min_c1, ( unsigned char )min_c2, ( unsigned char )min_c3, 0 ,
3799
- ( unsigned char )max_c1, ( unsigned char )max_c2, ( unsigned char )max_c3, 0 );
3800
- found = true ;
3801
- }
3802
- ++metadata;
3803
- }
3804
- theColorTable->sort ();
3805
-
3806
- // If no color table was found, try to read it from GDALColorTable
3807
- if ( !found )
3808
- {
3809
- GDALColorTableH gdalColorTable = GDALGetRasterColorTable ( gdalBand );
3810
-
3811
- if ( gdalColorTable )
3812
- {
3813
- int count = GDALGetColorEntryCount ( gdalColorTable );
3814
-
3815
- for ( int i = 0 ; i < count; i++ )
3816
- {
3817
- const GDALColorEntry *colorEntry = GDALGetColorEntry ( gdalColorTable, i );
3818
-
3819
- if ( !colorEntry ) continue ;
3820
-
3821
- theColorTable->add ( i, ( unsigned char ) colorEntry->c1 , ( unsigned char ) colorEntry->c2 ,
3822
- ( unsigned char ) colorEntry->c3 );
3823
- }
3824
- }
3825
- }
3826
-
3827
- #ifdef QGISDEBUG
3828
- theColorTable->print ();
3829
- #endif
3830
- }
3831
-
3832
-
3833
- QgsColorTable *QgsRasterLayer::colorTable ( int theBandNo )
3731
+ QList<QgsColorRampShader::ColorRampItem>* QgsRasterLayer::getColorTable ( int theBandNo )
3834
3732
{
3835
3733
return &( mRasterStatsList [theBandNo-1 ].colorTable );
3836
3734
}
0 commit comments