Skip to content

Commit 8990b66

Browse files
committed
WMS server: even more performant png8 conversion
1 parent f59f077 commit 8990b66

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/mapserver/qgshttprequesthandler.cpp

+32-6
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,33 @@ void QgsHttpRequestHandler::sendGetMapResponse( const QString& service, QImage*
115115
if ( png8Bit )
116116
{
117117
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+
121145
palettedImg.save( &buffer, "PNG", -1 );
122146
}
123147
else if ( png16Bit )
@@ -502,7 +526,7 @@ QString QgsHttpRequestHandler::readPostBody() const
502526
return inputString;
503527
}
504528

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 )
506530
{
507531
QHash<QRgb, int> inputColors;
508532
imageColors( inputColors, inputImage );
@@ -571,7 +595,7 @@ void QgsHttpRequestHandler::medianCut( QVector<QRgb>& colorTable, int nColors, c
571595
QgsColorBoxMap::const_iterator colorBoxIt = colorBoxMap.constBegin();
572596
for ( ; colorBoxIt != colorBoxMap.constEnd(); ++colorBoxIt )
573597
{
574-
colorTable[index] = boxColor( colorBoxIt.value(), colorBoxIt.key() );
598+
colorTable[index] = boxColor( colorBoxIt.value(), colorBoxIt.key(), index, colorIndexHash );
575599
++index;
576600
}
577601
}
@@ -765,7 +789,7 @@ bool QgsHttpRequestHandler::alphaCompare( const QPair<QRgb, int>& c1, const QPai
765789
return qAlpha( c1.first ) < qAlpha( c2.first );
766790
}
767791

768-
QRgb QgsHttpRequestHandler::boxColor( const QgsColorBox& box, int boxPixels )
792+
QRgb QgsHttpRequestHandler::boxColor( const QgsColorBox& box, int boxPixels, int colorMapIndex, QHash<QRgb, int>& colorIndexHash )
769793
{
770794
double avRed = 0;
771795
double avGreen = 0;
@@ -786,6 +810,8 @@ QRgb QgsHttpRequestHandler::boxColor( const QgsColorBox& box, int boxPixels )
786810
avGreen += ( qGreen( currentColor ) * weight );
787811
avBlue += ( qBlue( currentColor ) * weight );
788812
avAlpha += ( qAlpha( currentColor ) * weight );
813+
//allow faster lookup in image conversion
814+
colorIndexHash.insert( currentColor, colorMapIndex );
789815
}
790816

791817
return qRgba( avRed, avGreen, avBlue, avAlpha );

src/mapserver/qgshttprequesthandler.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class QgsHttpRequestHandler: public QgsRequestHandler
5555
QString readPostBody() const;
5656

5757
private:
58-
static void medianCut( QVector<QRgb>& colorTable, int nColors, const QImage& inputImage );
58+
static void medianCut( QVector<QRgb>& colorTable, QHash<QRgb, int>& colorIndexHash, int nColors, const QImage& inputImage );
5959
static void imageColors( QHash<QRgb, int>& colors, const QImage& image );
6060
static void splitColorBox( QgsColorBox& colorBox, QgsColorBoxMap& colorBoxMap,
6161
QMap<int, QgsColorBox>::iterator colorBoxMapIt );
@@ -65,7 +65,7 @@ class QgsHttpRequestHandler: public QgsRequestHandler
6565
static bool blueCompare( const QPair<QRgb, int>& c1, const QPair<QRgb, int>& c2 );
6666
static bool alphaCompare( const QPair<QRgb, int>& c1, const QPair<QRgb, int>& c2 );
6767
/**Calculates a representative color for a box (pixel weighted average)*/
68-
static QRgb boxColor( const QgsColorBox& box, int boxPixels );
68+
static QRgb boxColor( const QgsColorBox& box, int boxPixels, int colorMapIndex, QHash<QRgb, int>& colorIndexHash );
6969
};
7070

7171
#endif

0 commit comments

Comments
 (0)