Skip to content

Commit

Permalink
WMS server: even more performant png8 conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jan 12, 2014
1 parent f59f077 commit 8990b66
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
38 changes: 32 additions & 6 deletions src/mapserver/qgshttprequesthandler.cpp
Expand Up @@ -115,9 +115,33 @@ void QgsHttpRequestHandler::sendGetMapResponse( const QString& service, QImage*
if ( png8Bit )
{
QVector<QRgb> colorTable;
medianCut( colorTable, 256, *img );
QImage palettedImg = img->convertToFormat( QImage::Format_Indexed8, colorTable, Qt::ColorOnly | Qt::ThresholdDither |
Qt::ThresholdAlphaDither | Qt::NoOpaqueDetection );
QHash<QRgb, int> colorIndexHash;
medianCut( colorTable, colorIndexHash, 256, *img );


QImage palettedImg( img->size(), QImage::Format_Indexed8 );
palettedImg.setColorTable( colorTable );

int h = img->height();
int w = img->width();

for ( int y = 0; y < h; ++y )
{
QRgb* src_pixels = ( QRgb * ) img->scanLine( y );
uchar* dest_pixels = ( uchar * ) palettedImg.scanLine( y );

for ( int x = 0; x < w; ++x )
{
int src_pixel = src_pixels[x];
int value = colorIndexHash.value( src_pixel, -1 );
if ( value == -1 )
{
continue;
}
dest_pixels[x] = ( uchar ) value;
}
}

palettedImg.save( &buffer, "PNG", -1 );
}
else if ( png16Bit )
Expand Down Expand Up @@ -502,7 +526,7 @@ QString QgsHttpRequestHandler::readPostBody() const
return inputString;
}

void QgsHttpRequestHandler::medianCut( QVector<QRgb>& colorTable, int nColors, const QImage& inputImage )
void QgsHttpRequestHandler::medianCut( QVector<QRgb>& colorTable, QHash<QRgb, int>& colorIndexHash, int nColors, const QImage& inputImage )
{
QHash<QRgb, int> inputColors;
imageColors( inputColors, inputImage );
Expand Down Expand Up @@ -571,7 +595,7 @@ void QgsHttpRequestHandler::medianCut( QVector<QRgb>& colorTable, int nColors, c
QgsColorBoxMap::const_iterator colorBoxIt = colorBoxMap.constBegin();
for ( ; colorBoxIt != colorBoxMap.constEnd(); ++colorBoxIt )
{
colorTable[index] = boxColor( colorBoxIt.value(), colorBoxIt.key() );
colorTable[index] = boxColor( colorBoxIt.value(), colorBoxIt.key(), index, colorIndexHash );
++index;
}
}
Expand Down Expand Up @@ -765,7 +789,7 @@ bool QgsHttpRequestHandler::alphaCompare( const QPair<QRgb, int>& c1, const QPai
return qAlpha( c1.first ) < qAlpha( c2.first );
}

QRgb QgsHttpRequestHandler::boxColor( const QgsColorBox& box, int boxPixels )
QRgb QgsHttpRequestHandler::boxColor( const QgsColorBox& box, int boxPixels, int colorMapIndex, QHash<QRgb, int>& colorIndexHash )
{
double avRed = 0;
double avGreen = 0;
Expand All @@ -786,6 +810,8 @@ QRgb QgsHttpRequestHandler::boxColor( const QgsColorBox& box, int boxPixels )
avGreen += ( qGreen( currentColor ) * weight );
avBlue += ( qBlue( currentColor ) * weight );
avAlpha += ( qAlpha( currentColor ) * weight );
//allow faster lookup in image conversion
colorIndexHash.insert( currentColor, colorMapIndex );
}

return qRgba( avRed, avGreen, avBlue, avAlpha );
Expand Down
4 changes: 2 additions & 2 deletions src/mapserver/qgshttprequesthandler.h
Expand Up @@ -55,7 +55,7 @@ class QgsHttpRequestHandler: public QgsRequestHandler
QString readPostBody() const;

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

#endif

0 comments on commit 8990b66

Please sign in to comment.