Skip to content
Permalink
Browse files

First working version of color quantization. Not optimized yet

  • Loading branch information
mhugent authored and Marco Hugentobler committed Oct 2, 2012
1 parent 52d053d commit e0ab4104d8533c85c1ef8114af0616d62d817711
Showing with 37 additions and 3 deletions.
  1. +35 −3 src/mapserver/qgshttprequesthandler.cpp
  2. +2 −0 src/mapserver/qgshttprequesthandler.h
@@ -522,9 +522,15 @@ void QgsHttpRequestHandler::medianCut( QVector<QRgb>& colorTable, int nColors, c
}
}

bool debug = true; //only for breakpoint

//todo: evaluate the colors of the final boxes
//get representative colors for the boxes
int index = 0;
colorTable.resize( colorBoxMap.size() );
QgsColorBoxMap::const_iterator colorBoxIt = colorBoxMap.constBegin();
for ( ; colorBoxIt != colorBoxMap.constEnd(); ++colorBoxIt )
{
colorTable[index] = boxColor( colorBoxIt.value(), colorBoxIt.key() );
++index;
}
}

void QgsHttpRequestHandler::imageColors( QHash<QRgb, int>& colors, const QImage& image )
@@ -715,3 +721,29 @@ 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 )
{
double avRed = 0;
double avGreen = 0;
double avBlue = 0;
double avAlpha = 0;
QRgb currentColor;
int currentPixel;

double weight;

QgsColorBox::const_iterator colorBoxIt = box.constBegin();
for ( ; colorBoxIt != box.constEnd(); ++colorBoxIt )
{
currentColor = colorBoxIt->first;
currentPixel = colorBoxIt->second;
weight = currentPixel / boxPixels;
avRed += ( qRed( currentColor ) * weight );
avGreen += ( qGreen( currentColor ) * weight );
avBlue += ( qBlue( currentColor ) * weight );
avAlpha += ( qAlpha( currentColor ) * weight );
}

return qRgba( avRed, avGreen, avBlue, avAlpha );
}
@@ -63,6 +63,8 @@ class QgsHttpRequestHandler: public QgsRequestHandler
static bool greenCompare( const QPair<QRgb, int>& c1, const QPair<QRgb, int>& c2 );
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 );
};

#endif

0 comments on commit e0ab410

Please sign in to comment.
You can’t perform that action at this time.