37 void imageColors( QHash<QRgb, int> &colors,
const QImage &image )
40 const int width = image.width();
41 const int height = image.height();
43 const QRgb *currentScanLine =
nullptr;
44 QHash<QRgb, int>::iterator colorIt;
45 for (
int i = 0; i < height; ++i )
47 currentScanLine = (
const QRgb * ) ( image.scanLine( i ) );
48 for (
int j = 0; j < width; ++j )
50 colorIt = colors.find( currentScanLine[j] );
51 if ( colorIt == colors.end() )
53 colors.insert( currentScanLine[j], 1 );
63 bool minMaxRange(
const QgsColorBox &colorBox,
int &redRange,
int &greenRange,
int &blueRange,
int &alphaRange )
65 if ( colorBox.size() < 1 )
70 int rMin = std::numeric_limits<int>::max();
71 int gMin = std::numeric_limits<int>::max();
72 int bMin = std::numeric_limits<int>::max();
73 int aMin = std::numeric_limits<int>::max();
74 int rMax = std::numeric_limits<int>::min();
75 int gMax = std::numeric_limits<int>::min();
76 int bMax = std::numeric_limits<int>::min();
77 int aMax = std::numeric_limits<int>::min();
84 for (
auto colorBoxIt = colorBox.constBegin(); colorBoxIt != colorBox.constEnd(); ++colorBoxIt )
86 currentRed = qRed( colorBoxIt->first );
87 if ( currentRed > rMax )
91 if ( currentRed < rMin )
96 currentGreen = qGreen( colorBoxIt->first );
97 if ( currentGreen > gMax )
101 if ( currentGreen < gMin )
106 currentBlue = qBlue( colorBoxIt->first );
107 if ( currentBlue > bMax )
111 if ( currentBlue < bMin )
116 currentAlpha = qAlpha( colorBoxIt->first );
117 if ( currentAlpha > aMax )
121 if ( currentAlpha < aMin )
127 redRange = rMax - rMin;
128 greenRange = gMax - gMin;
129 blueRange = bMax - bMin;
130 alphaRange = aMax - aMin;
134 bool redCompare( QPair<QRgb, int> c1, QPair<QRgb, int> c2 )
136 return qRed( c1.first ) < qRed( c2.first );
139 bool greenCompare( QPair<QRgb, int> c1, QPair<QRgb, int> c2 )
141 return qGreen( c1.first ) < qGreen( c2.first );
144 bool blueCompare( QPair<QRgb, int> c1, QPair<QRgb, int> c2 )
146 return qBlue( c1.first ) < qBlue( c2.first );
149 bool alphaCompare( QPair<QRgb, int> c1, QPair<QRgb, int> c2 )
151 return qAlpha( c1.first ) < qAlpha( c2.first );
154 QRgb boxColor(
const QgsColorBox &box,
int boxPixels )
165 for (
auto colorBoxIt = box.constBegin(); colorBoxIt != box.constEnd(); ++colorBoxIt )
167 currentColor = colorBoxIt->first;
168 currentPixel = colorBoxIt->second;
169 weight =
static_cast<double>( currentPixel ) / boxPixels;
170 avRed += ( qRed( currentColor ) * weight );
171 avGreen += ( qGreen( currentColor ) * weight );
172 avBlue += ( qBlue( currentColor ) * weight );
173 avAlpha += ( qAlpha( currentColor ) * weight );
176 return qRgba( avRed, avGreen, avBlue, avAlpha );
180 void splitColorBox(
QgsColorBox &colorBox,
QgsColorBoxMap &colorBoxMap, QMultiMap<int, QgsColorBox>::iterator colorBoxMapIt )
182 if ( colorBox.size() < 2 )
193 if ( !minMaxRange( colorBox, redRange, greenRange, blueRange, alphaRange ) )
199 if ( redRange >= greenRange && redRange >= blueRange && redRange >= alphaRange )
201 std::sort( colorBox.begin(), colorBox.end(), redCompare );
203 else if ( greenRange >= redRange && greenRange >= blueRange && greenRange >= alphaRange )
205 std::sort( colorBox.begin(), colorBox.end(), greenCompare );
207 else if ( blueRange >= redRange && blueRange >= greenRange && blueRange >= alphaRange )
209 std::sort( colorBox.begin(), colorBox.end(), blueCompare );
213 std::sort( colorBox.begin(), colorBox.end(), alphaCompare );
217 const double halfSum = colorBoxMapIt.key() / 2.0;
219 int currentListIndex = 0;
221 auto colorBoxIt = colorBox.begin();
222 for ( ; colorBoxIt != colorBox.end(); ++colorBoxIt )
224 currentSum += colorBoxIt->second;
225 if ( currentSum >= halfSum )
232 if ( currentListIndex > ( colorBox.size() - 2 ) )
235 currentSum -= colorBoxIt->second;
243 const QgsColorBox newColorBox1 = colorBox.mid( 0, currentListIndex + 1 );
244 colorBoxMap.insert( currentSum, newColorBox1 );
246 colorBox.erase( colorBox.begin(), colorBoxIt );
248 colorBoxMap.erase( colorBoxMapIt );
249 colorBoxMap.insert( halfSum * 2.0 - currentSum, newColorBox2 );
254 void medianCut( QVector<QRgb> &colorTable,
int nColors,
const QImage &inputImage )
256 QHash<QRgb, int> inputColors;
257 imageColors( inputColors, inputImage );
259 if ( inputColors.size() <= nColors )
261 colorTable.resize( inputColors.size() );
263 for (
auto inputColorIt = inputColors.constBegin(); inputColorIt != inputColors.constEnd(); ++inputColorIt )
265 colorTable[index] = inputColorIt.key();
273 int firstBoxPixelSum = 0;
274 for (
auto inputColorIt = inputColors.constBegin(); inputColorIt != inputColors.constEnd(); ++inputColorIt )
276 firstBox.push_back( qMakePair( inputColorIt.key(), inputColorIt.value() ) );
277 firstBoxPixelSum += inputColorIt.value();
281 colorBoxMap.insert( firstBoxPixelSum, firstBox );
282 QMultiMap<int, QgsColorBox>::iterator colorBoxMapIt = colorBoxMap.end();
285 bool allColorsMapped =
false;
286 while ( colorBoxMap.size() < nColors )
289 colorBoxMapIt = colorBoxMap.end();
293 if ( colorBoxMapIt.value().size() > 1 )
295 splitColorBox( colorBoxMapIt.value(), colorBoxMap, colorBoxMapIt );
298 if ( colorBoxMapIt == colorBoxMap.begin() )
300 allColorsMapped =
true;
305 if ( allColorsMapped )
313 colorTable.resize( colorBoxMap.size() );
314 for (
auto colorBoxIt = colorBoxMap.constBegin(); colorBoxIt != colorBoxMap.constEnd(); ++colorBoxIt )
316 colorTable[index] = boxColor( colorBoxIt.value(), colorBoxIt.key() );
Median cut implementation.
QList< QPair< QRgb, int > > QgsColorBox
QMultiMap< int, QgsColorBox > QgsColorBoxMap
void medianCut(QVector< QRgb > &colorTable, int nColors, const QImage &inputImage)
Median cut implementation used when reducing RGB colors to palletized colors.