Skip to content

Commit 58c5e9d

Browse files
mhugentMarco Hugentobler
authored and
Marco Hugentobler
committed
Start coding of 8bit png conversion
1 parent 1549f09 commit 58c5e9d

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/mapserver/qgshttprequesthandler.cpp

+36-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ void QgsHttpRequestHandler::sendGetMapResponse( const QString& service, QImage*
111111

112112
if ( png8Bit )
113113
{
114-
QImage palettedImg = img->convertToFormat( QImage::Format_Indexed8, Qt::ColorOnly | Qt::ThresholdDither |
114+
QVector<QRgb> colorTable;
115+
medianCut( colorTable, 256, *img );
116+
QImage palettedImg = img->convertToFormat( QImage::Format_Indexed8, colorTable, Qt::ColorOnly | Qt::ThresholdDither |
115117
Qt::ThresholdAlphaDither | Qt::NoOpaqueDetection );
116118
palettedImg.save( &buffer, "PNG", -1 );
117119
}
@@ -469,3 +471,36 @@ QString QgsHttpRequestHandler::readPostBody() const
469471
}
470472
return inputString;
471473
}
474+
475+
void QgsHttpRequestHandler::medianCut( QVector<QRgb>& colorTable, int nColors, const QImage& inputImage )
476+
{
477+
QHash<QRgb, int> inputColors;
478+
imageColors( inputColors, inputImage );
479+
//todo...
480+
}
481+
482+
void QgsHttpRequestHandler::imageColors( QHash<QRgb, int>& colors, const QImage& image )
483+
{
484+
colors.clear();
485+
int width = image.width();
486+
int height = image.height();
487+
488+
QRgb currentColor;
489+
QHash<QRgb, int>::iterator colorIt;
490+
for ( int i = 0; i < height; ++i )
491+
{
492+
for ( int j = 0; j < width; ++j )
493+
{
494+
currentColor = image.pixel( j, i );
495+
colorIt = colors.find( currentColor );
496+
if ( colorIt == colors.end() )
497+
{
498+
colors.insert( currentColor, 1 );
499+
}
500+
else
501+
{
502+
colorIt.value()++;
503+
}
504+
}
505+
}
506+
}

src/mapserver/qgshttprequesthandler.h

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define QGSHTTPREQUESTHANDLER_H
2020

2121
#include "qgsrequesthandler.h"
22+
#include <QColor>
2223

2324
/**Base class for request handler using HTTP.
2425
It provides a method to send data to the client*/
@@ -47,6 +48,10 @@ class QgsHttpRequestHandler: public QgsRequestHandler
4748
void requestStringToParameterMap( const QString& request, QMap<QString, QString>& parameters );
4849
/**Read CONTENT_LENGTH characters from stdin*/
4950
QString readPostBody() const;
51+
52+
private:
53+
static void medianCut( QVector<QRgb>& colorTable, int nColors, const QImage& inputImage );
54+
static void imageColors( QHash<QRgb, int>& colors, const QImage& image );
5055
};
5156

5257
#endif

0 commit comments

Comments
 (0)