@@ -115,9 +115,33 @@ void QgsHttpRequestHandler::sendGetMapResponse( const QString& service, QImage*
115
115
if ( png8Bit )
116
116
{
117
117
QVector<QRgb> colorTable;
118
- medianCut ( colorTable, 256 , *img );
119
- QImage palettedImg = img->convertToFormat ( QImage::Format_Indexed8, colorTable, Qt::ColorOnly | Qt::ThresholdDither |
120
- Qt::ThresholdAlphaDither | Qt::NoOpaqueDetection );
118
+ QHash<QRgb, int > colorIndexHash;
119
+ medianCut ( colorTable, colorIndexHash, 256 , *img );
120
+
121
+
122
+ QImage palettedImg ( img->size (), QImage::Format_Indexed8 );
123
+ palettedImg.setColorTable ( colorTable );
124
+
125
+ int h = img->height ();
126
+ int w = img->width ();
127
+
128
+ for ( int y = 0 ; y < h; ++y )
129
+ {
130
+ QRgb* src_pixels = ( QRgb * ) img->scanLine ( y );
131
+ uchar* dest_pixels = ( uchar * ) palettedImg.scanLine ( y );
132
+
133
+ for ( int x = 0 ; x < w; ++x )
134
+ {
135
+ int src_pixel = src_pixels[x];
136
+ int value = colorIndexHash.value ( src_pixel, -1 );
137
+ if ( value == -1 )
138
+ {
139
+ continue ;
140
+ }
141
+ dest_pixels[x] = ( uchar ) value;
142
+ }
143
+ }
144
+
121
145
palettedImg.save ( &buffer, " PNG" , -1 );
122
146
}
123
147
else if ( png16Bit )
@@ -502,7 +526,7 @@ QString QgsHttpRequestHandler::readPostBody() const
502
526
return inputString;
503
527
}
504
528
505
- void QgsHttpRequestHandler::medianCut ( QVector<QRgb>& colorTable, int nColors, const QImage& inputImage )
529
+ void QgsHttpRequestHandler::medianCut ( QVector<QRgb>& colorTable, QHash<QRgb, int >& colorIndexHash, int nColors, const QImage& inputImage )
506
530
{
507
531
QHash<QRgb, int > inputColors;
508
532
imageColors ( inputColors, inputImage );
@@ -571,7 +595,7 @@ void QgsHttpRequestHandler::medianCut( QVector<QRgb>& colorTable, int nColors, c
571
595
QgsColorBoxMap::const_iterator colorBoxIt = colorBoxMap.constBegin ();
572
596
for ( ; colorBoxIt != colorBoxMap.constEnd (); ++colorBoxIt )
573
597
{
574
- colorTable[index ] = boxColor ( colorBoxIt.value (), colorBoxIt.key () );
598
+ colorTable[index ] = boxColor ( colorBoxIt.value (), colorBoxIt.key (), index , colorIndexHash );
575
599
++index ;
576
600
}
577
601
}
@@ -765,7 +789,7 @@ bool QgsHttpRequestHandler::alphaCompare( const QPair<QRgb, int>& c1, const QPai
765
789
return qAlpha ( c1.first ) < qAlpha ( c2.first );
766
790
}
767
791
768
- QRgb QgsHttpRequestHandler::boxColor ( const QgsColorBox& box, int boxPixels )
792
+ QRgb QgsHttpRequestHandler::boxColor ( const QgsColorBox& box, int boxPixels, int colorMapIndex, QHash<QRgb, int >& colorIndexHash )
769
793
{
770
794
double avRed = 0 ;
771
795
double avGreen = 0 ;
@@ -786,6 +810,8 @@ QRgb QgsHttpRequestHandler::boxColor( const QgsColorBox& box, int boxPixels )
786
810
avGreen += ( qGreen ( currentColor ) * weight );
787
811
avBlue += ( qBlue ( currentColor ) * weight );
788
812
avAlpha += ( qAlpha ( currentColor ) * weight );
813
+ // allow faster lookup in image conversion
814
+ colorIndexHash.insert ( currentColor, colorMapIndex );
789
815
}
790
816
791
817
return qRgba ( avRed, avGreen, avBlue, avAlpha );
0 commit comments