QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsmediancut.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmediancut.cpp
3 -------------------------
4 begin : December 20 , 2016
5 copyright : (C) 2007 by Marco Hugentobler ( parts from qgswmshandler)
6 (C) 2014 by Alessandro Pasotti ( parts from qgswmshandler)
7 (C) 2016 by David Marteau
8 email : marco dot hugentobler at karto dot baug dot ethz dot ch
9 a dot pasotti at itopen dot it
10 david dot marteau at 3liz dot com
11 ***************************************************************************/
12
13/***************************************************************************
14 * *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
19 * *
20 ***************************************************************************/
21
22#include "qgsmediancut.h"
23
24#include <QList>
25#include <QMultiMap>
26#include <QHash>
27
28namespace QgsWms
29{
30
31 typedef QList<QPair<QRgb, int>> QgsColorBox; //Color / number of pixels
32 typedef QMultiMap<int, QgsColorBox> QgsColorBoxMap; // sum of pixels / color box
33
34 namespace
35 {
36
37 void imageColors( QHash<QRgb, int> &colors, const QImage &image )
38 {
39 colors.clear();
40 const int width = image.width();
41 const int height = image.height();
42
43 const QRgb *currentScanLine = nullptr;
44 QHash<QRgb, int>::iterator colorIt;
45 for ( int i = 0; i < height; ++i )
46 {
47 currentScanLine = ( const QRgb * ) ( image.scanLine( i ) );
48 for ( int j = 0; j < width; ++j )
49 {
50 colorIt = colors.find( currentScanLine[j] );
51 if ( colorIt == colors.end() )
52 {
53 colors.insert( currentScanLine[j], 1 );
54 }
55 else
56 {
57 colorIt.value()++;
58 }
59 }
60 }
61 }
62
63 bool minMaxRange( const QgsColorBox &colorBox, int &redRange, int &greenRange, int &blueRange, int &alphaRange )
64 {
65 if ( colorBox.size() < 1 )
66 {
67 return false;
68 }
69
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();
78
79 int currentRed = 0;
80 int currentGreen = 0;
81 int currentBlue = 0;
82 int currentAlpha = 0;
83
84 for ( auto colorBoxIt = colorBox.constBegin(); colorBoxIt != colorBox.constEnd(); ++colorBoxIt )
85 {
86 currentRed = qRed( colorBoxIt->first );
87 if ( currentRed > rMax )
88 {
89 rMax = currentRed;
90 }
91 if ( currentRed < rMin )
92 {
93 rMin = currentRed;
94 }
95
96 currentGreen = qGreen( colorBoxIt->first );
97 if ( currentGreen > gMax )
98 {
99 gMax = currentGreen;
100 }
101 if ( currentGreen < gMin )
102 {
103 gMin = currentGreen;
104 }
105
106 currentBlue = qBlue( colorBoxIt->first );
107 if ( currentBlue > bMax )
108 {
109 bMax = currentBlue;
110 }
111 if ( currentBlue < bMin )
112 {
113 bMin = currentBlue;
114 }
115
116 currentAlpha = qAlpha( colorBoxIt->first );
117 if ( currentAlpha > aMax )
118 {
119 aMax = currentAlpha;
120 }
121 if ( currentAlpha < aMin )
122 {
123 aMin = currentAlpha;
124 }
125 }
126
127 redRange = rMax - rMin;
128 greenRange = gMax - gMin;
129 blueRange = bMax - bMin;
130 alphaRange = aMax - aMin;
131 return true;
132 }
133
134 bool redCompare( QPair<QRgb, int> c1, QPair<QRgb, int> c2 )
135 {
136 return qRed( c1.first ) < qRed( c2.first );
137 }
138
139 bool greenCompare( QPair<QRgb, int> c1, QPair<QRgb, int> c2 )
140 {
141 return qGreen( c1.first ) < qGreen( c2.first );
142 }
143
144 bool blueCompare( QPair<QRgb, int> c1, QPair<QRgb, int> c2 )
145 {
146 return qBlue( c1.first ) < qBlue( c2.first );
147 }
148
149 bool alphaCompare( QPair<QRgb, int> c1, QPair<QRgb, int> c2 )
150 {
151 return qAlpha( c1.first ) < qAlpha( c2.first );
152 }
153
154 QRgb boxColor( const QgsColorBox &box, int boxPixels )
155 {
156 double avRed = 0;
157 double avGreen = 0;
158 double avBlue = 0;
159 double avAlpha = 0;
160 QRgb currentColor;
161 int currentPixel;
162
163 double weight;
164
165 for ( auto colorBoxIt = box.constBegin(); colorBoxIt != box.constEnd(); ++colorBoxIt )
166 {
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 );
174 }
175
176 return qRgba( avRed, avGreen, avBlue, avAlpha );
177 }
178
179
180 void splitColorBox( QgsColorBox &colorBox, QgsColorBoxMap &colorBoxMap, QMultiMap<int, QgsColorBox>::iterator colorBoxMapIt )
181 {
182 if ( colorBox.size() < 2 )
183 {
184 return; //need at least two colors for a split
185 }
186
187 //a,r,g,b ranges
188 int redRange = 0;
189 int greenRange = 0;
190 int blueRange = 0;
191 int alphaRange = 0;
192
193 if ( !minMaxRange( colorBox, redRange, greenRange, blueRange, alphaRange ) )
194 {
195 return;
196 }
197
198 //sort color box for a/r/g/b
199 if ( redRange >= greenRange && redRange >= blueRange && redRange >= alphaRange )
200 {
201 std::sort( colorBox.begin(), colorBox.end(), redCompare );
202 }
203 else if ( greenRange >= redRange && greenRange >= blueRange && greenRange >= alphaRange )
204 {
205 std::sort( colorBox.begin(), colorBox.end(), greenCompare );
206 }
207 else if ( blueRange >= redRange && blueRange >= greenRange && blueRange >= alphaRange )
208 {
209 std::sort( colorBox.begin(), colorBox.end(), blueCompare );
210 }
211 else
212 {
213 std::sort( colorBox.begin(), colorBox.end(), alphaCompare );
214 }
215
216 //get median
217 const double halfSum = colorBoxMapIt.key() / 2.0;
218 int currentSum = 0;
219 int currentListIndex = 0;
220
221 auto colorBoxIt = colorBox.begin();
222 for ( ; colorBoxIt != colorBox.end(); ++colorBoxIt )
223 {
224 currentSum += colorBoxIt->second;
225 if ( currentSum >= halfSum )
226 {
227 break;
228 }
229 ++currentListIndex;
230 }
231
232 if ( currentListIndex > ( colorBox.size() - 2 ) ) //if the median is contained in the last color, split one item before that
233 {
234 --currentListIndex;
235 currentSum -= colorBoxIt->second;
236 }
237 else
238 {
239 ++colorBoxIt; //the iterator needs to point behind the last item to remove
240 }
241
242 //do split: replace old color box, insert new one
243 const QgsColorBox newColorBox1 = colorBox.mid( 0, currentListIndex + 1 );
244 colorBoxMap.insert( currentSum, newColorBox1 );
245
246 colorBox.erase( colorBox.begin(), colorBoxIt );
247 const QgsColorBox newColorBox2 = colorBox;
248 colorBoxMap.erase( colorBoxMapIt );
249 colorBoxMap.insert( halfSum * 2.0 - currentSum, newColorBox2 );
250 }
251
252 } // namespace
253
254 void medianCut( QVector<QRgb> &colorTable, int nColors, const QImage &inputImage )
255 {
256 QHash<QRgb, int> inputColors;
257 imageColors( inputColors, inputImage );
258
259 if ( inputColors.size() <= nColors ) //all the colors in the image can be mapped to one palette color
260 {
261 colorTable.resize( inputColors.size() );
262 int index = 0;
263 for ( auto inputColorIt = inputColors.constBegin(); inputColorIt != inputColors.constEnd(); ++inputColorIt )
264 {
265 colorTable[index] = inputColorIt.key();
266 ++index;
267 }
268 return;
269 }
270
271 //create first box
272 QgsColorBox firstBox; //QList< QPair<QRgb, int> >
273 int firstBoxPixelSum = 0;
274 for ( auto inputColorIt = inputColors.constBegin(); inputColorIt != inputColors.constEnd(); ++inputColorIt )
275 {
276 firstBox.push_back( qMakePair( inputColorIt.key(), inputColorIt.value() ) );
277 firstBoxPixelSum += inputColorIt.value();
278 }
279
280 QgsColorBoxMap colorBoxMap; //QMultiMap< int, ColorBox >
281 colorBoxMap.insert( firstBoxPixelSum, firstBox );
282 QMultiMap<int, QgsColorBox>::iterator colorBoxMapIt = colorBoxMap.end();
283
284 //split boxes until number of boxes == nColors or all the boxes have color count 1
285 bool allColorsMapped = false;
286 while ( colorBoxMap.size() < nColors )
287 {
288 //start at the end of colorBoxMap and pick the first entry with number of colors < 1
289 colorBoxMapIt = colorBoxMap.end();
290 while ( true )
291 {
292 --colorBoxMapIt;
293 if ( colorBoxMapIt.value().size() > 1 )
294 {
295 splitColorBox( colorBoxMapIt.value(), colorBoxMap, colorBoxMapIt );
296 break;
297 }
298 if ( colorBoxMapIt == colorBoxMap.begin() )
299 {
300 allColorsMapped = true;
301 break;
302 }
303 }
304
305 if ( allColorsMapped )
306 {
307 break;
308 }
309 }
310
311 //get representative colors for the boxes
312 int index = 0;
313 colorTable.resize( colorBoxMap.size() );
314 for ( auto colorBoxIt = colorBoxMap.constBegin(); colorBoxIt != colorBoxMap.constEnd(); ++colorBoxIt )
315 {
316 colorTable[index] = boxColor( colorBoxIt.value(), colorBoxIt.key() );
317 ++index;
318 }
319 }
320
321} // namespace QgsWms
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.