Skip to content

Commit

Permalink
First working version of color quantization. Not optimized yet
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent authored and Marco Hugentobler committed Oct 2, 2012
1 parent 52d053d commit e0ab410
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/mapserver/qgshttprequesthandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );
}
2 changes: 2 additions & 0 deletions src/mapserver/qgshttprequesthandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.