/src/gdal/alg/gdalmediancut.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: CIETMap Phase 2 |
4 | | * Purpose: Use median cut algorithm to generate an near-optimal PCT for a |
5 | | * given RGB image. Implemented as function GDALComputeMedianCutPCT. |
6 | | * Author: Frank Warmerdam, warmerdam@pobox.com |
7 | | * |
8 | | ****************************************************************************** |
9 | | * Copyright (c) 2001, Frank Warmerdam |
10 | | * Copyright (c) 2007-2010, Even Rouault <even dot rouault at spatialys.com> |
11 | | * |
12 | | * SPDX-License-Identifier: MIT |
13 | | ****************************************************************************** |
14 | | * |
15 | | * This code was based on the tiffmedian.c code from libtiff (www.libtiff.org) |
16 | | * which was based on a paper by Paul Heckbert: |
17 | | * |
18 | | * "Color Image Quantization for Frame Buffer Display", Paul |
19 | | * Heckbert, SIGGRAPH proceedings, 1982, pp. 297-307. |
20 | | * |
21 | | */ |
22 | | |
23 | | #include "cpl_port.h" |
24 | | #include "gdal_alg.h" |
25 | | #include "gdal_alg_priv.h" |
26 | | |
27 | | #include <climits> |
28 | | #include <cstring> |
29 | | |
30 | | #include <algorithm> |
31 | | #include <limits> |
32 | | |
33 | | #include "cpl_conv.h" |
34 | | #include "cpl_error.h" |
35 | | #include "cpl_float.h" |
36 | | #include "cpl_progress.h" |
37 | | #include "cpl_vsi.h" |
38 | | #include "gdal.h" |
39 | | #include "gdal_priv.h" |
40 | | |
41 | | template <typename T> static T *HISTOGRAM(T *h, int n, int r, int g, int b) |
42 | 0 | { |
43 | 0 | const int index = (r * n + g) * n + b; |
44 | 0 | return &h[index]; |
45 | 0 | } Unexecuted instantiation: gdalmediancut.cpp:unsigned int* HISTOGRAM<unsigned int>(unsigned int*, int, int, int, int) Unexecuted instantiation: gdalmediancut.cpp:unsigned int const* HISTOGRAM<unsigned int const>(unsigned int const*, int, int, int, int) Unexecuted instantiation: gdalmediancut.cpp:unsigned long long* HISTOGRAM<unsigned long long>(unsigned long long*, int, int, int, int) Unexecuted instantiation: gdalmediancut.cpp:unsigned long long const* HISTOGRAM<unsigned long long const>(unsigned long long const*, int, int, int, int) |
46 | | |
47 | | #ifndef MAKE_COLOR_CODE_defined |
48 | | #define MAKE_COLOR_CODE_defined |
49 | | |
50 | | static int MAKE_COLOR_CODE(int r, int g, int b) |
51 | 0 | { |
52 | 0 | return r | (g << 8) | (b << 16); |
53 | 0 | } |
54 | | #endif |
55 | | |
56 | | // NOTE: If changing the size of this structure, edit |
57 | | // MEDIAN_CUT_AND_DITHER_BUFFER_SIZE_65536 in gdal_alg_priv.h and take into |
58 | | // account ColorIndex in gdaldither.cpp. |
59 | | typedef struct |
60 | | { |
61 | | GUInt32 nColorCode; |
62 | | int nCount; |
63 | | GUInt32 nColorCode2; |
64 | | int nCount2; |
65 | | GUInt32 nColorCode3; |
66 | | int nCount3; |
67 | | } HashHistogram; |
68 | | |
69 | | typedef struct colorbox |
70 | | { |
71 | | struct colorbox *next, *prev; |
72 | | int rmin, rmax; |
73 | | int gmin, gmax; |
74 | | int bmin, bmax; |
75 | | GUIntBig total; |
76 | | } Colorbox; |
77 | | |
78 | | template <class T> |
79 | | static void splitbox(Colorbox *ptr, const T *histogram, |
80 | | const HashHistogram *psHashHistogram, int nCLevels, |
81 | | Colorbox **pfreeboxes, Colorbox **pusedboxes, |
82 | | GByte *pabyRedBand, GByte *pabyGreenBand, |
83 | | GByte *pabyBlueBand, T nPixels); |
84 | | |
85 | | template <class T> |
86 | | static void shrinkbox(Colorbox *box, const T *histogram, int nCLevels); |
87 | | |
88 | | static Colorbox *largest_box(Colorbox *usedboxes); |
89 | | |
90 | | /************************************************************************/ |
91 | | /* GDALComputeMedianCutPCT() */ |
92 | | /************************************************************************/ |
93 | | |
94 | | /** |
95 | | * Compute optimal PCT for RGB image. |
96 | | * |
97 | | * This function implements a median cut algorithm to compute an "optimal" |
98 | | * pseudocolor table for representing an input RGB image. This PCT could |
99 | | * then be used with GDALDitherRGB2PCT() to convert a 24bit RGB image into |
100 | | * an eightbit pseudo-colored image. |
101 | | * |
102 | | * This code was based on the tiffmedian.c code from libtiff (www.libtiff.org) |
103 | | * which was based on a paper by Paul Heckbert: |
104 | | * |
105 | | * \verbatim |
106 | | * "Color Image Quantization for Frame Buffer Display", Paul |
107 | | * Heckbert, SIGGRAPH proceedings, 1982, pp. 297-307. |
108 | | * \endverbatim |
109 | | * |
110 | | * The red, green and blue input bands do not necessarily need to come |
111 | | * from the same file, but they must be the same width and height. They will |
112 | | * be clipped to 8bit during reading, so non-eight bit bands are generally |
113 | | * inappropriate. |
114 | | * |
115 | | * Since GDAL 3.13, source nodata values or mask band will be taken into account |
116 | | * to determine which pixels are valid. |
117 | | * |
118 | | * @param hRed Red input band. |
119 | | * @param hGreen Green input band. |
120 | | * @param hBlue Blue input band. |
121 | | * @param pfnIncludePixel function used to test which pixels should be included |
122 | | * in the analysis. At this time this argument is ignored. |
123 | | * This should normally be NULL. |
124 | | * @param nColors the desired number of colors to be returned (2-256). |
125 | | * @param hColorTable the colors will be returned in this color table object. |
126 | | * @param pfnProgress callback for reporting algorithm progress matching the |
127 | | * GDALProgressFunc() semantics. May be NULL. |
128 | | * @param pProgressArg callback argument passed to pfnProgress. |
129 | | * |
130 | | * @return returns CE_None on success or CE_Failure if an error occurs. |
131 | | */ |
132 | | |
133 | | extern "C" int CPL_STDCALL GDALComputeMedianCutPCT( |
134 | | GDALRasterBandH hRed, GDALRasterBandH hGreen, GDALRasterBandH hBlue, |
135 | | int (*pfnIncludePixel)(int, int, void *), int nColors, |
136 | | GDALColorTableH hColorTable, GDALProgressFunc pfnProgress, |
137 | | void *pProgressArg) |
138 | | |
139 | 0 | { |
140 | 0 | VALIDATE_POINTER1(hRed, "GDALComputeMedianCutPCT", CE_Failure); |
141 | 0 | const int nXSize = GDALGetRasterBandXSize(hRed); |
142 | 0 | const int nYSize = GDALGetRasterBandYSize(hRed); |
143 | 0 | if (nYSize == 0) |
144 | 0 | return CE_Failure; |
145 | 0 | if (static_cast<GUInt32>(nXSize) < |
146 | 0 | std::numeric_limits<GUInt32>::max() / static_cast<GUInt32>(nYSize)) |
147 | 0 | { |
148 | 0 | return GDALComputeMedianCutPCTInternal( |
149 | 0 | hRed, hGreen, hBlue, nullptr, nullptr, nullptr, pfnIncludePixel, |
150 | 0 | nColors, 5, static_cast<GUInt32 *>(nullptr), hColorTable, |
151 | 0 | pfnProgress, pProgressArg); |
152 | 0 | } |
153 | 0 | else |
154 | 0 | { |
155 | 0 | return GDALComputeMedianCutPCTInternal( |
156 | 0 | hRed, hGreen, hBlue, nullptr, nullptr, nullptr, pfnIncludePixel, |
157 | 0 | nColors, 5, static_cast<GUIntBig *>(nullptr), hColorTable, |
158 | 0 | pfnProgress, pProgressArg); |
159 | 0 | } |
160 | 0 | } |
161 | | |
162 | | #ifndef IsColorCodeSet_defined |
163 | | #define IsColorCodeSet_defined |
164 | | |
165 | | static inline bool IsColorCodeSet(GUInt32 nColorCode) |
166 | 0 | { |
167 | 0 | return (nColorCode >> 31) == 0; |
168 | 0 | } |
169 | | #endif |
170 | | |
171 | | static inline int FindColorCount(const HashHistogram *psHashHistogram, |
172 | | GUInt32 nColorCode) |
173 | 0 | { |
174 | |
|
175 | 0 | GUInt32 nIdx = nColorCode % PRIME_FOR_65536; |
176 | 0 | while (true) |
177 | 0 | { |
178 | 0 | if (!IsColorCodeSet(psHashHistogram[nIdx].nColorCode)) |
179 | 0 | { |
180 | 0 | return 0; |
181 | 0 | } |
182 | 0 | if (psHashHistogram[nIdx].nColorCode == nColorCode) |
183 | 0 | { |
184 | 0 | return psHashHistogram[nIdx].nCount; |
185 | 0 | } |
186 | 0 | if (!IsColorCodeSet(psHashHistogram[nIdx].nColorCode2)) |
187 | 0 | { |
188 | 0 | return 0; |
189 | 0 | } |
190 | 0 | if (psHashHistogram[nIdx].nColorCode2 == nColorCode) |
191 | 0 | { |
192 | 0 | return psHashHistogram[nIdx].nCount2; |
193 | 0 | } |
194 | 0 | if (!IsColorCodeSet(psHashHistogram[nIdx].nColorCode3)) |
195 | 0 | { |
196 | 0 | return 0; |
197 | 0 | } |
198 | 0 | if (psHashHistogram[nIdx].nColorCode3 == nColorCode) |
199 | 0 | { |
200 | 0 | return psHashHistogram[nIdx].nCount3; |
201 | 0 | } |
202 | | |
203 | 0 | do |
204 | 0 | { |
205 | 0 | nIdx += 257; |
206 | 0 | if (nIdx >= PRIME_FOR_65536) |
207 | 0 | nIdx -= PRIME_FOR_65536; |
208 | 0 | } while (IsColorCodeSet(psHashHistogram[nIdx].nColorCode) && |
209 | 0 | psHashHistogram[nIdx].nColorCode != nColorCode && |
210 | 0 | IsColorCodeSet(psHashHistogram[nIdx].nColorCode2) && |
211 | 0 | psHashHistogram[nIdx].nColorCode2 != nColorCode && |
212 | 0 | IsColorCodeSet(psHashHistogram[nIdx].nColorCode3) && |
213 | 0 | psHashHistogram[nIdx].nColorCode3 != nColorCode); |
214 | 0 | } |
215 | 0 | } |
216 | | |
217 | | static inline int *FindAndInsertColorCount(HashHistogram *psHashHistogram, |
218 | | GUInt32 nColorCode) |
219 | 0 | { |
220 | 0 | GUInt32 nIdx = nColorCode % PRIME_FOR_65536; |
221 | 0 | while (true) |
222 | 0 | { |
223 | 0 | if (psHashHistogram[nIdx].nColorCode == nColorCode) |
224 | 0 | { |
225 | 0 | return &(psHashHistogram[nIdx].nCount); |
226 | 0 | } |
227 | 0 | if (!IsColorCodeSet(psHashHistogram[nIdx].nColorCode)) |
228 | 0 | { |
229 | 0 | psHashHistogram[nIdx].nColorCode = nColorCode; |
230 | 0 | psHashHistogram[nIdx].nCount = 0; |
231 | 0 | return &(psHashHistogram[nIdx].nCount); |
232 | 0 | } |
233 | 0 | if (psHashHistogram[nIdx].nColorCode2 == nColorCode) |
234 | 0 | { |
235 | 0 | return &(psHashHistogram[nIdx].nCount2); |
236 | 0 | } |
237 | 0 | if (!IsColorCodeSet(psHashHistogram[nIdx].nColorCode2)) |
238 | 0 | { |
239 | 0 | psHashHistogram[nIdx].nColorCode2 = nColorCode; |
240 | 0 | psHashHistogram[nIdx].nCount2 = 0; |
241 | 0 | return &(psHashHistogram[nIdx].nCount2); |
242 | 0 | } |
243 | 0 | if (psHashHistogram[nIdx].nColorCode3 == nColorCode) |
244 | 0 | { |
245 | 0 | return &(psHashHistogram[nIdx].nCount3); |
246 | 0 | } |
247 | 0 | if (!IsColorCodeSet(psHashHistogram[nIdx].nColorCode3)) |
248 | 0 | { |
249 | 0 | psHashHistogram[nIdx].nColorCode3 = nColorCode; |
250 | 0 | psHashHistogram[nIdx].nCount3 = 0; |
251 | 0 | return &(psHashHistogram[nIdx].nCount3); |
252 | 0 | } |
253 | | |
254 | 0 | do |
255 | 0 | { |
256 | 0 | nIdx += 257; |
257 | 0 | if (nIdx >= PRIME_FOR_65536) |
258 | 0 | nIdx -= PRIME_FOR_65536; |
259 | 0 | } while (IsColorCodeSet(psHashHistogram[nIdx].nColorCode) && |
260 | 0 | psHashHistogram[nIdx].nColorCode != nColorCode && |
261 | 0 | IsColorCodeSet(psHashHistogram[nIdx].nColorCode2) && |
262 | 0 | psHashHistogram[nIdx].nColorCode2 != nColorCode && |
263 | 0 | IsColorCodeSet(psHashHistogram[nIdx].nColorCode3) && |
264 | 0 | psHashHistogram[nIdx].nColorCode3 != nColorCode); |
265 | 0 | } |
266 | 0 | } |
267 | | |
268 | | template <class T> |
269 | | int GDALComputeMedianCutPCTInternal( |
270 | | GDALRasterBandH hRed, GDALRasterBandH hGreen, GDALRasterBandH hBlue, |
271 | | GByte *pabyRedBand, GByte *pabyGreenBand, GByte *pabyBlueBand, |
272 | | int (*pfnIncludePixel)(int, int, void *), int nColors, int nBits, |
273 | | T *panHistogram, // NULL, or >= size (1<<nBits)^3 * sizeof(T) bytes. |
274 | | GDALColorTableH hColorTable, GDALProgressFunc pfnProgress, |
275 | | void *pProgressArg) |
276 | | |
277 | 0 | { |
278 | 0 | VALIDATE_POINTER1(hRed, "GDALComputeMedianCutPCT", CE_Failure); |
279 | 0 | VALIDATE_POINTER1(hGreen, "GDALComputeMedianCutPCT", CE_Failure); |
280 | 0 | VALIDATE_POINTER1(hBlue, "GDALComputeMedianCutPCT", CE_Failure); |
281 | | |
282 | 0 | CPLErr err = CE_None; |
283 | | |
284 | | /* -------------------------------------------------------------------- */ |
285 | | /* Validate parameters. */ |
286 | | /* -------------------------------------------------------------------- */ |
287 | 0 | const int nXSize = GDALGetRasterBandXSize(hRed); |
288 | 0 | const int nYSize = GDALGetRasterBandYSize(hRed); |
289 | |
|
290 | 0 | if (GDALGetRasterBandXSize(hGreen) != nXSize || |
291 | 0 | GDALGetRasterBandYSize(hGreen) != nYSize || |
292 | 0 | GDALGetRasterBandXSize(hBlue) != nXSize || |
293 | 0 | GDALGetRasterBandYSize(hBlue) != nYSize) |
294 | 0 | { |
295 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
296 | 0 | "Green or blue band doesn't match size of red band."); |
297 | |
|
298 | 0 | return CE_Failure; |
299 | 0 | } |
300 | | |
301 | 0 | if (pfnIncludePixel != nullptr) |
302 | 0 | { |
303 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
304 | 0 | "GDALComputeMedianCutPCT() doesn't currently support " |
305 | 0 | "pfnIncludePixel function."); |
306 | |
|
307 | 0 | return CE_Failure; |
308 | 0 | } |
309 | | |
310 | 0 | if (nColors <= 0) |
311 | 0 | { |
312 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
313 | 0 | "GDALComputeMedianCutPCT(): " |
314 | 0 | "nColors must be strictly greater than 1."); |
315 | |
|
316 | 0 | return CE_Failure; |
317 | 0 | } |
318 | | |
319 | 0 | if (nColors > 256) |
320 | 0 | { |
321 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
322 | 0 | "GDALComputeMedianCutPCT(): " |
323 | 0 | "nColors must be lesser than or equal to 256."); |
324 | |
|
325 | 0 | return CE_Failure; |
326 | 0 | } |
327 | | |
328 | 0 | if (pfnProgress == nullptr) |
329 | 0 | pfnProgress = GDALDummyProgress; |
330 | |
|
331 | 0 | int nSrcNoData = -1; |
332 | 0 | GDALRasterBandH hMaskBand = nullptr; |
333 | 0 | int bSrcHasNoDataR = FALSE; |
334 | 0 | const double dfSrcNoDataR = GDALGetRasterNoDataValue(hRed, &bSrcHasNoDataR); |
335 | 0 | int bSrcHasNoDataG = FALSE; |
336 | 0 | const double dfSrcNoDataG = |
337 | 0 | GDALGetRasterNoDataValue(hGreen, &bSrcHasNoDataG); |
338 | 0 | int bSrcHasNoDataB = FALSE; |
339 | 0 | const double dfSrcNoDataB = |
340 | 0 | GDALGetRasterNoDataValue(hBlue, &bSrcHasNoDataB); |
341 | 0 | if (bSrcHasNoDataR && bSrcHasNoDataG && bSrcHasNoDataB && |
342 | 0 | dfSrcNoDataR == dfSrcNoDataG && dfSrcNoDataR == dfSrcNoDataB && |
343 | 0 | dfSrcNoDataR >= 0 && dfSrcNoDataR <= 255 && |
344 | 0 | std::round(dfSrcNoDataR) == dfSrcNoDataR) |
345 | 0 | { |
346 | 0 | nSrcNoData = static_cast<int>(dfSrcNoDataR); |
347 | 0 | } |
348 | 0 | else |
349 | 0 | { |
350 | 0 | const int nMaskFlags = GDALGetMaskFlags(hRed); |
351 | 0 | if ((nMaskFlags & GMF_PER_DATASET)) |
352 | 0 | hMaskBand = GDALGetMaskBand(hRed); |
353 | 0 | } |
354 | | |
355 | | /* ==================================================================== */ |
356 | | /* STEP 1: create empty boxes. */ |
357 | | /* ==================================================================== */ |
358 | 0 | if (static_cast<GUInt32>(nXSize) > |
359 | 0 | cpl::NumericLimits<T>::max() / static_cast<GUInt32>(nYSize)) |
360 | 0 | { |
361 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
362 | 0 | "GDALComputeMedianCutPCTInternal() not called " |
363 | 0 | "with large enough type"); |
364 | 0 | } |
365 | |
|
366 | 0 | T nPixels = 0; |
367 | 0 | if (nBits == 8 && pabyRedBand != nullptr && pabyGreenBand != nullptr && |
368 | 0 | pabyBlueBand != nullptr && |
369 | 0 | static_cast<GUInt32>(nXSize) <= |
370 | 0 | cpl::NumericLimits<T>::max() / static_cast<GUInt32>(nYSize)) |
371 | 0 | { |
372 | 0 | nPixels = static_cast<T>(nXSize) * static_cast<T>(nYSize); |
373 | 0 | } |
374 | |
|
375 | 0 | const int nCLevels = 1 << nBits; |
376 | 0 | const int nCLevelsCube = nCLevels * nCLevels * nCLevels; |
377 | 0 | T *histogram = nullptr; |
378 | 0 | HashHistogram *psHashHistogram = nullptr; |
379 | 0 | if (panHistogram) |
380 | 0 | { |
381 | 0 | if (nBits == 8 && static_cast<GUIntBig>(nXSize) * nYSize <= 65536) |
382 | 0 | { |
383 | | // If the image is small enough, then the number of colors |
384 | | // will be limited and using a hashmap, rather than a full table |
385 | | // will be more efficient. |
386 | 0 | histogram = nullptr; |
387 | 0 | psHashHistogram = reinterpret_cast<HashHistogram *>(panHistogram); |
388 | 0 | memset(psHashHistogram, 0xFF, |
389 | 0 | sizeof(HashHistogram) * PRIME_FOR_65536); |
390 | 0 | } |
391 | 0 | else |
392 | 0 | { |
393 | 0 | histogram = panHistogram; |
394 | 0 | memset(histogram, 0, nCLevelsCube * sizeof(T)); |
395 | 0 | } |
396 | 0 | } |
397 | 0 | else |
398 | 0 | { |
399 | 0 | histogram = |
400 | 0 | static_cast<T *>(VSI_CALLOC_VERBOSE(nCLevelsCube, sizeof(T))); |
401 | 0 | if (histogram == nullptr) |
402 | 0 | { |
403 | 0 | return CE_Failure; |
404 | 0 | } |
405 | 0 | } |
406 | 0 | Colorbox *box_list = |
407 | 0 | static_cast<Colorbox *>(CPLMalloc(nColors * sizeof(Colorbox))); |
408 | 0 | Colorbox *freeboxes = box_list; |
409 | 0 | freeboxes[0].next = &freeboxes[1]; |
410 | 0 | freeboxes[0].prev = nullptr; |
411 | 0 | for (int i = 1; i < nColors - 1; ++i) |
412 | 0 | { |
413 | 0 | freeboxes[i].next = &freeboxes[i + 1]; |
414 | 0 | freeboxes[i].prev = &freeboxes[i - 1]; |
415 | 0 | } |
416 | 0 | freeboxes[nColors - 1].next = nullptr; |
417 | 0 | freeboxes[nColors - 1].prev = &freeboxes[nColors - 2]; |
418 | | |
419 | | /* ==================================================================== */ |
420 | | /* Build histogram. */ |
421 | | /* ==================================================================== */ |
422 | | |
423 | | /* -------------------------------------------------------------------- */ |
424 | | /* Initialize the box datastructures. */ |
425 | | /* -------------------------------------------------------------------- */ |
426 | 0 | Colorbox *freeboxes_before = freeboxes; |
427 | 0 | freeboxes = freeboxes_before->next; |
428 | 0 | if (freeboxes) |
429 | 0 | freeboxes->prev = nullptr; |
430 | |
|
431 | 0 | Colorbox *usedboxes = freeboxes_before; |
432 | 0 | usedboxes->next = nullptr; |
433 | 0 | usedboxes->rmin = 999; |
434 | 0 | usedboxes->gmin = 999; |
435 | 0 | usedboxes->bmin = 999; |
436 | 0 | usedboxes->rmax = -1; |
437 | 0 | usedboxes->gmax = -1; |
438 | 0 | usedboxes->bmax = -1; |
439 | 0 | usedboxes->total = |
440 | 0 | static_cast<GUIntBig>(nXSize) * static_cast<GUIntBig>(nYSize); |
441 | | |
442 | | /* -------------------------------------------------------------------- */ |
443 | | /* Collect histogram. */ |
444 | | /* -------------------------------------------------------------------- */ |
445 | | |
446 | | // TODO(schwehr): Move these closer to usage after removing gotos. |
447 | 0 | const int nColorShift = 8 - nBits; |
448 | 0 | int nColorCounter = 0; |
449 | 0 | GByte anRed[256] = {}; |
450 | 0 | GByte anGreen[256] = {}; |
451 | 0 | GByte anBlue[256] = {}; |
452 | |
|
453 | 0 | GByte *pabyRedLine = static_cast<GByte *>(VSI_MALLOC_VERBOSE(nXSize)); |
454 | 0 | GByte *pabyGreenLine = static_cast<GByte *>(VSI_MALLOC_VERBOSE(nXSize)); |
455 | 0 | GByte *pabyBlueLine = static_cast<GByte *>(VSI_MALLOC_VERBOSE(nXSize)); |
456 | 0 | std::unique_ptr<GByte, VSIFreeReleaser> pabyMask; |
457 | 0 | if (hMaskBand) |
458 | 0 | pabyMask.reset(static_cast<GByte *>(VSI_MALLOC_VERBOSE(nXSize))); |
459 | |
|
460 | 0 | if (pabyRedLine == nullptr || pabyGreenLine == nullptr || |
461 | 0 | pabyBlueLine == nullptr || (hMaskBand && !pabyMask)) |
462 | 0 | { |
463 | 0 | err = CE_Failure; |
464 | 0 | goto end_and_cleanup; |
465 | 0 | } |
466 | | |
467 | 0 | for (int iLine = 0; iLine < nYSize; iLine++) |
468 | 0 | { |
469 | 0 | if (!pfnProgress(iLine / static_cast<double>(nYSize), |
470 | 0 | "Generating Histogram", pProgressArg)) |
471 | 0 | { |
472 | 0 | CPLError(CE_Failure, CPLE_UserInterrupt, "User Terminated"); |
473 | 0 | err = CE_Failure; |
474 | 0 | goto end_and_cleanup; |
475 | 0 | } |
476 | | |
477 | 0 | err = GDALRasterIO(hRed, GF_Read, 0, iLine, nXSize, 1, pabyRedLine, |
478 | 0 | nXSize, 1, GDT_UInt8, 0, 0); |
479 | 0 | if (err == CE_None) |
480 | 0 | err = GDALRasterIO(hGreen, GF_Read, 0, iLine, nXSize, 1, |
481 | 0 | pabyGreenLine, nXSize, 1, GDT_UInt8, 0, 0); |
482 | 0 | if (err == CE_None) |
483 | 0 | err = GDALRasterIO(hBlue, GF_Read, 0, iLine, nXSize, 1, |
484 | 0 | pabyBlueLine, nXSize, 1, GDT_UInt8, 0, 0); |
485 | 0 | if (err == CE_None && hMaskBand) |
486 | 0 | err = GDALRasterIO(hMaskBand, GF_Read, 0, iLine, nXSize, 1, |
487 | 0 | pabyMask.get(), nXSize, 1, GDT_UInt8, 0, 0); |
488 | 0 | if (err != CE_None) |
489 | 0 | goto end_and_cleanup; |
490 | | |
491 | 0 | for (int iPixel = 0; iPixel < nXSize; iPixel++) |
492 | 0 | { |
493 | 0 | if ((pabyRedLine[iPixel] == nSrcNoData && |
494 | 0 | pabyGreenLine[iPixel] == nSrcNoData && |
495 | 0 | pabyBlueLine[iPixel] == nSrcNoData) || |
496 | 0 | (pabyMask && (pabyMask.get())[iPixel] == 0)) |
497 | 0 | { |
498 | 0 | continue; |
499 | 0 | } |
500 | | |
501 | 0 | const int nRed = pabyRedLine[iPixel] >> nColorShift; |
502 | 0 | const int nGreen = pabyGreenLine[iPixel] >> nColorShift; |
503 | 0 | const int nBlue = pabyBlueLine[iPixel] >> nColorShift; |
504 | |
|
505 | 0 | usedboxes->rmin = std::min(usedboxes->rmin, nRed); |
506 | 0 | usedboxes->gmin = std::min(usedboxes->gmin, nGreen); |
507 | 0 | usedboxes->bmin = std::min(usedboxes->bmin, nBlue); |
508 | 0 | usedboxes->rmax = std::max(usedboxes->rmax, nRed); |
509 | 0 | usedboxes->gmax = std::max(usedboxes->gmax, nGreen); |
510 | 0 | usedboxes->bmax = std::max(usedboxes->bmax, nBlue); |
511 | |
|
512 | 0 | bool bFirstOccurrence; |
513 | 0 | if (psHashHistogram) |
514 | 0 | { |
515 | 0 | int *pnColor = FindAndInsertColorCount( |
516 | 0 | psHashHistogram, MAKE_COLOR_CODE(nRed, nGreen, nBlue)); |
517 | 0 | bFirstOccurrence = (*pnColor == 0); |
518 | 0 | (*pnColor)++; |
519 | 0 | } |
520 | 0 | else |
521 | 0 | { |
522 | 0 | T *pnColor = |
523 | 0 | HISTOGRAM(histogram, nCLevels, nRed, nGreen, nBlue); |
524 | 0 | bFirstOccurrence = (*pnColor == 0); |
525 | 0 | (*pnColor)++; |
526 | 0 | } |
527 | 0 | if (bFirstOccurrence) |
528 | 0 | { |
529 | 0 | if (nColorShift == 0 && nColorCounter < nColors) |
530 | 0 | { |
531 | 0 | anRed[nColorCounter] = static_cast<GByte>(nRed); |
532 | 0 | anGreen[nColorCounter] = static_cast<GByte>(nGreen); |
533 | 0 | anBlue[nColorCounter] = static_cast<GByte>(nBlue); |
534 | 0 | } |
535 | 0 | nColorCounter++; |
536 | 0 | } |
537 | 0 | } |
538 | 0 | } |
539 | | |
540 | 0 | if (!pfnProgress(1.0, "Generating Histogram", pProgressArg)) |
541 | 0 | { |
542 | 0 | CPLError(CE_Failure, CPLE_UserInterrupt, "User Terminated"); |
543 | 0 | err = CE_Failure; |
544 | 0 | goto end_and_cleanup; |
545 | 0 | } |
546 | | |
547 | 0 | if (nColorShift == 0 && nColorCounter <= nColors) |
548 | 0 | { |
549 | | #if DEBUG_VERBOSE |
550 | | CPLDebug("MEDIAN_CUT", "%d colors found <= %d", nColorCounter, nColors); |
551 | | #endif |
552 | 0 | for (int iColor = 0; iColor < nColorCounter; iColor++) |
553 | 0 | { |
554 | 0 | const GDALColorEntry sEntry = {static_cast<GByte>(anRed[iColor]), |
555 | 0 | static_cast<GByte>(anGreen[iColor]), |
556 | 0 | static_cast<GByte>(anBlue[iColor]), |
557 | 0 | 255}; |
558 | 0 | GDALSetColorEntry(hColorTable, iColor, &sEntry); |
559 | 0 | } |
560 | 0 | goto end_and_cleanup; |
561 | 0 | } |
562 | | |
563 | | /* ==================================================================== */ |
564 | | /* STEP 3: continually subdivide boxes until no more free */ |
565 | | /* boxes remain or until all colors assigned. */ |
566 | | /* ==================================================================== */ |
567 | 0 | while (freeboxes != nullptr) |
568 | 0 | { |
569 | 0 | auto ptr = largest_box(usedboxes); |
570 | 0 | if (ptr != nullptr) |
571 | 0 | splitbox(ptr, histogram, psHashHistogram, nCLevels, &freeboxes, |
572 | 0 | &usedboxes, pabyRedBand, pabyGreenBand, pabyBlueBand, |
573 | 0 | nPixels); |
574 | 0 | else |
575 | 0 | freeboxes = nullptr; |
576 | 0 | } |
577 | | |
578 | | /* ==================================================================== */ |
579 | | /* STEP 4: assign colors to all boxes */ |
580 | | /* ==================================================================== */ |
581 | 0 | { |
582 | 0 | Colorbox *ptr = usedboxes; |
583 | 0 | for (int i = 0; ptr != nullptr; ++i, ptr = ptr->next) |
584 | 0 | { |
585 | 0 | const GDALColorEntry sEntry = { |
586 | 0 | static_cast<GByte>(((ptr->rmin + ptr->rmax) << nColorShift) / |
587 | 0 | 2), |
588 | 0 | static_cast<GByte>(((ptr->gmin + ptr->gmax) << nColorShift) / |
589 | 0 | 2), |
590 | 0 | static_cast<GByte>(((ptr->bmin + ptr->bmax) << nColorShift) / |
591 | 0 | 2), |
592 | 0 | 255}; |
593 | 0 | GDALSetColorEntry(hColorTable, i, &sEntry); |
594 | 0 | } |
595 | 0 | } |
596 | |
|
597 | 0 | end_and_cleanup: |
598 | 0 | CPLFree(pabyRedLine); |
599 | 0 | CPLFree(pabyGreenLine); |
600 | 0 | CPLFree(pabyBlueLine); |
601 | | |
602 | | // We're done with the boxes now. |
603 | 0 | CPLFree(box_list); |
604 | 0 | freeboxes = nullptr; |
605 | 0 | usedboxes = nullptr; |
606 | |
|
607 | 0 | if (panHistogram == nullptr) |
608 | 0 | CPLFree(histogram); |
609 | |
|
610 | 0 | return err; |
611 | 0 | } Unexecuted instantiation: int GDALComputeMedianCutPCTInternal<unsigned int>(void*, void*, void*, unsigned char*, unsigned char*, unsigned char*, int (*)(int, int, void*), int, int, unsigned int*, void*, int (*)(double, char const*, void*), void*) Unexecuted instantiation: int GDALComputeMedianCutPCTInternal<unsigned long long>(void*, void*, void*, unsigned char*, unsigned char*, unsigned char*, int (*)(int, int, void*), int, int, unsigned long long*, void*, int (*)(double, char const*, void*), void*) |
612 | | |
613 | | /************************************************************************/ |
614 | | /* largest_box() */ |
615 | | /************************************************************************/ |
616 | | |
617 | | static Colorbox *largest_box(Colorbox *usedboxes) |
618 | 0 | { |
619 | 0 | Colorbox *b = nullptr; |
620 | |
|
621 | 0 | for (Colorbox *p = usedboxes; p != nullptr; p = p->next) |
622 | 0 | { |
623 | 0 | if ((p->rmax > p->rmin || p->gmax > p->gmin || p->bmax > p->bmin) && |
624 | 0 | (b == nullptr || p->total > b->total)) |
625 | 0 | { |
626 | 0 | b = p; |
627 | 0 | } |
628 | 0 | } |
629 | 0 | return b; |
630 | 0 | } |
631 | | |
632 | | static void shrinkboxFromBand(Colorbox *ptr, const GByte *pabyRedBand, |
633 | | const GByte *pabyGreenBand, |
634 | | const GByte *pabyBlueBand, GUIntBig nPixels) |
635 | 0 | { |
636 | 0 | int rmin_new = 255; |
637 | 0 | int rmax_new = 0; |
638 | 0 | int gmin_new = 255; |
639 | 0 | int gmax_new = 0; |
640 | 0 | int bmin_new = 255; |
641 | 0 | int bmax_new = 0; |
642 | 0 | for (GUIntBig i = 0; i < nPixels; i++) |
643 | 0 | { |
644 | 0 | const int iR = pabyRedBand[i]; |
645 | 0 | const int iG = pabyGreenBand[i]; |
646 | 0 | const int iB = pabyBlueBand[i]; |
647 | 0 | if (iR >= ptr->rmin && iR <= ptr->rmax && iG >= ptr->gmin && |
648 | 0 | iG <= ptr->gmax && iB >= ptr->bmin && iB <= ptr->bmax) |
649 | 0 | { |
650 | 0 | if (iR < rmin_new) |
651 | 0 | rmin_new = iR; |
652 | 0 | if (iR > rmax_new) |
653 | 0 | rmax_new = iR; |
654 | 0 | if (iG < gmin_new) |
655 | 0 | gmin_new = iG; |
656 | 0 | if (iG > gmax_new) |
657 | 0 | gmax_new = iG; |
658 | 0 | if (iB < bmin_new) |
659 | 0 | bmin_new = iB; |
660 | 0 | if (iB > bmax_new) |
661 | 0 | bmax_new = iB; |
662 | 0 | } |
663 | 0 | } |
664 | |
|
665 | 0 | CPLAssert(rmin_new >= ptr->rmin && rmin_new <= rmax_new && |
666 | 0 | rmax_new <= ptr->rmax); |
667 | 0 | CPLAssert(gmin_new >= ptr->gmin && gmin_new <= gmax_new && |
668 | 0 | gmax_new <= ptr->gmax); |
669 | 0 | CPLAssert(bmin_new >= ptr->bmin && bmin_new <= bmax_new && |
670 | 0 | bmax_new <= ptr->bmax); |
671 | 0 | ptr->rmin = rmin_new; |
672 | 0 | ptr->rmax = rmax_new; |
673 | 0 | ptr->gmin = gmin_new; |
674 | 0 | ptr->gmax = gmax_new; |
675 | 0 | ptr->bmin = bmin_new; |
676 | 0 | ptr->bmax = bmax_new; |
677 | 0 | } |
678 | | |
679 | | static void shrinkboxFromHashHistogram(Colorbox *box, |
680 | | const HashHistogram *psHashHistogram) |
681 | 0 | { |
682 | 0 | if (box->rmax > box->rmin) |
683 | 0 | { |
684 | 0 | for (int ir = box->rmin; ir <= box->rmax; ++ir) |
685 | 0 | { |
686 | 0 | for (int ig = box->gmin; ig <= box->gmax; ++ig) |
687 | 0 | { |
688 | 0 | for (int ib = box->bmin; ib <= box->bmax; ++ib) |
689 | 0 | { |
690 | 0 | if (FindColorCount(psHashHistogram, |
691 | 0 | MAKE_COLOR_CODE(ir, ig, ib)) != 0) |
692 | 0 | { |
693 | 0 | box->rmin = ir; |
694 | 0 | goto have_rmin; |
695 | 0 | } |
696 | 0 | } |
697 | 0 | } |
698 | 0 | } |
699 | 0 | } |
700 | 0 | have_rmin: |
701 | 0 | if (box->rmax > box->rmin) |
702 | 0 | { |
703 | 0 | for (int ir = box->rmax; ir >= box->rmin; --ir) |
704 | 0 | { |
705 | 0 | for (int ig = box->gmin; ig <= box->gmax; ++ig) |
706 | 0 | { |
707 | 0 | for (int ib = box->bmin; ib <= box->bmax; ++ib) |
708 | 0 | { |
709 | 0 | if (FindColorCount(psHashHistogram, |
710 | 0 | MAKE_COLOR_CODE(ir, ig, ib)) != 0) |
711 | 0 | { |
712 | 0 | box->rmax = ir; |
713 | 0 | goto have_rmax; |
714 | 0 | } |
715 | 0 | } |
716 | 0 | } |
717 | 0 | } |
718 | 0 | } |
719 | | |
720 | 0 | have_rmax: |
721 | 0 | if (box->gmax > box->gmin) |
722 | 0 | { |
723 | 0 | for (int ig = box->gmin; ig <= box->gmax; ++ig) |
724 | 0 | { |
725 | 0 | for (int ir = box->rmin; ir <= box->rmax; ++ir) |
726 | 0 | { |
727 | 0 | for (int ib = box->bmin; ib <= box->bmax; ++ib) |
728 | 0 | { |
729 | 0 | if (FindColorCount(psHashHistogram, |
730 | 0 | MAKE_COLOR_CODE(ir, ig, ib)) != 0) |
731 | 0 | { |
732 | 0 | box->gmin = ig; |
733 | 0 | goto have_gmin; |
734 | 0 | } |
735 | 0 | } |
736 | 0 | } |
737 | 0 | } |
738 | 0 | } |
739 | | |
740 | 0 | have_gmin: |
741 | 0 | if (box->gmax > box->gmin) |
742 | 0 | { |
743 | 0 | for (int ig = box->gmax; ig >= box->gmin; --ig) |
744 | 0 | { |
745 | 0 | for (int ir = box->rmin; ir <= box->rmax; ++ir) |
746 | 0 | { |
747 | 0 | int ib = box->bmin; |
748 | 0 | for (; ib <= box->bmax; ++ib) |
749 | 0 | { |
750 | 0 | if (FindColorCount(psHashHistogram, |
751 | 0 | MAKE_COLOR_CODE(ir, ig, ib)) != 0) |
752 | 0 | { |
753 | 0 | box->gmax = ig; |
754 | 0 | goto have_gmax; |
755 | 0 | } |
756 | 0 | } |
757 | 0 | } |
758 | 0 | } |
759 | 0 | } |
760 | | |
761 | 0 | have_gmax: |
762 | 0 | if (box->bmax > box->bmin) |
763 | 0 | { |
764 | 0 | for (int ib = box->bmin; ib <= box->bmax; ++ib) |
765 | 0 | { |
766 | 0 | for (int ir = box->rmin; ir <= box->rmax; ++ir) |
767 | 0 | { |
768 | 0 | for (int ig = box->gmin; ig <= box->gmax; ++ig) |
769 | 0 | { |
770 | 0 | if (FindColorCount(psHashHistogram, |
771 | 0 | MAKE_COLOR_CODE(ir, ig, ib)) != 0) |
772 | 0 | { |
773 | 0 | box->bmin = ib; |
774 | 0 | goto have_bmin; |
775 | 0 | } |
776 | 0 | } |
777 | 0 | } |
778 | 0 | } |
779 | 0 | } |
780 | | |
781 | 0 | have_bmin: |
782 | 0 | if (box->bmax > box->bmin) |
783 | 0 | { |
784 | 0 | for (int ib = box->bmax; ib >= box->bmin; --ib) |
785 | 0 | { |
786 | 0 | for (int ir = box->rmin; ir <= box->rmax; ++ir) |
787 | 0 | { |
788 | 0 | for (int ig = box->gmin; ig <= box->gmax; ++ig) |
789 | 0 | { |
790 | 0 | if (FindColorCount(psHashHistogram, |
791 | 0 | MAKE_COLOR_CODE(ir, ig, ib)) != 0) |
792 | 0 | { |
793 | 0 | box->bmax = ib; |
794 | 0 | goto have_bmax; |
795 | 0 | } |
796 | 0 | } |
797 | 0 | } |
798 | 0 | } |
799 | 0 | } |
800 | | |
801 | 0 | have_bmax:; |
802 | 0 | } |
803 | | |
804 | | /************************************************************************/ |
805 | | /* splitbox() */ |
806 | | /************************************************************************/ |
807 | | template <class T> |
808 | | static void splitbox(Colorbox *ptr, const T *histogram, |
809 | | const HashHistogram *psHashHistogram, int nCLevels, |
810 | | Colorbox **pfreeboxes, Colorbox **pusedboxes, |
811 | | GByte *pabyRedBand, GByte *pabyGreenBand, |
812 | | GByte *pabyBlueBand, T nPixels) |
813 | 0 | { |
814 | 0 | T hist2[256] = {}; |
815 | 0 | int first = 0; |
816 | 0 | int last = 0; |
817 | |
|
818 | 0 | enum |
819 | 0 | { |
820 | 0 | RED, |
821 | 0 | GREEN, |
822 | 0 | BLUE |
823 | 0 | } axis; |
824 | | |
825 | | // See which axis is the largest, do a histogram along that axis. Split at |
826 | | // median point. Contract both new boxes to fit points and return. |
827 | 0 | { |
828 | 0 | int i = ptr->rmax - ptr->rmin; |
829 | 0 | if (i >= ptr->gmax - ptr->gmin && i >= ptr->bmax - ptr->bmin) |
830 | 0 | axis = RED; |
831 | 0 | else if (ptr->gmax - ptr->gmin >= ptr->bmax - ptr->bmin) |
832 | 0 | axis = GREEN; |
833 | 0 | else |
834 | 0 | axis = BLUE; |
835 | 0 | } |
836 | | // Get histogram along longest axis. |
837 | 0 | const GUInt32 nIters = (ptr->rmax - ptr->rmin + 1) * |
838 | 0 | (ptr->gmax - ptr->gmin + 1) * |
839 | 0 | (ptr->bmax - ptr->bmin + 1); |
840 | |
|
841 | 0 | switch (axis) |
842 | 0 | { |
843 | 0 | case RED: |
844 | 0 | { |
845 | 0 | if (nPixels != 0 && nIters > nPixels) |
846 | 0 | { |
847 | 0 | const int rmin = ptr->rmin; |
848 | 0 | const int rmax = ptr->rmax; |
849 | 0 | const int gmin = ptr->gmin; |
850 | 0 | const int gmax = ptr->gmax; |
851 | 0 | const int bmin = ptr->bmin; |
852 | 0 | const int bmax = ptr->bmax; |
853 | 0 | for (T iPixel = 0; iPixel < nPixels; iPixel++) |
854 | 0 | { |
855 | 0 | int iR = pabyRedBand[iPixel]; |
856 | 0 | int iG = pabyGreenBand[iPixel]; |
857 | 0 | int iB = pabyBlueBand[iPixel]; |
858 | 0 | if (iR >= rmin && iR <= rmax && iG >= gmin && iG <= gmax && |
859 | 0 | iB >= bmin && iB <= bmax) |
860 | 0 | { |
861 | 0 | hist2[iR]++; |
862 | 0 | } |
863 | 0 | } |
864 | 0 | } |
865 | 0 | else if (psHashHistogram) |
866 | 0 | { |
867 | 0 | T *histp = &hist2[ptr->rmin]; |
868 | 0 | for (int ir = ptr->rmin; ir <= ptr->rmax; ++ir) |
869 | 0 | { |
870 | 0 | *histp = 0; |
871 | 0 | for (int ig = ptr->gmin; ig <= ptr->gmax; ++ig) |
872 | 0 | { |
873 | 0 | for (int ib = ptr->bmin; ib <= ptr->bmax; ++ib) |
874 | 0 | { |
875 | 0 | *histp += FindColorCount( |
876 | 0 | psHashHistogram, MAKE_COLOR_CODE(ir, ig, ib)); |
877 | 0 | } |
878 | 0 | } |
879 | 0 | histp++; |
880 | 0 | } |
881 | 0 | } |
882 | 0 | else |
883 | 0 | { |
884 | 0 | T *histp = &hist2[ptr->rmin]; |
885 | 0 | for (int ir = ptr->rmin; ir <= ptr->rmax; ++ir) |
886 | 0 | { |
887 | 0 | *histp = 0; |
888 | 0 | for (int ig = ptr->gmin; ig <= ptr->gmax; ++ig) |
889 | 0 | { |
890 | 0 | const T *iptr = |
891 | 0 | HISTOGRAM(histogram, nCLevels, ir, ig, ptr->bmin); |
892 | 0 | for (int ib = ptr->bmin; ib <= ptr->bmax; ++ib) |
893 | 0 | *histp += *iptr++; |
894 | 0 | } |
895 | 0 | histp++; |
896 | 0 | } |
897 | 0 | } |
898 | 0 | first = ptr->rmin; |
899 | 0 | last = ptr->rmax; |
900 | 0 | break; |
901 | 0 | } |
902 | 0 | case GREEN: |
903 | 0 | { |
904 | 0 | if (nPixels != 0 && nIters > nPixels) |
905 | 0 | { |
906 | 0 | const int rmin = ptr->rmin; |
907 | 0 | const int rmax = ptr->rmax; |
908 | 0 | const int gmin = ptr->gmin; |
909 | 0 | const int gmax = ptr->gmax; |
910 | 0 | const int bmin = ptr->bmin; |
911 | 0 | const int bmax = ptr->bmax; |
912 | 0 | for (T iPixel = 0; iPixel < nPixels; iPixel++) |
913 | 0 | { |
914 | 0 | const int iR = pabyRedBand[iPixel]; |
915 | 0 | const int iG = pabyGreenBand[iPixel]; |
916 | 0 | const int iB = pabyBlueBand[iPixel]; |
917 | 0 | if (iR >= rmin && iR <= rmax && iG >= gmin && iG <= gmax && |
918 | 0 | iB >= bmin && iB <= bmax) |
919 | 0 | { |
920 | 0 | hist2[iG]++; |
921 | 0 | } |
922 | 0 | } |
923 | 0 | } |
924 | 0 | else if (psHashHistogram) |
925 | 0 | { |
926 | 0 | T *histp = &hist2[ptr->gmin]; |
927 | 0 | for (int ig = ptr->gmin; ig <= ptr->gmax; ++ig) |
928 | 0 | { |
929 | 0 | *histp = 0; |
930 | 0 | for (int ir = ptr->rmin; ir <= ptr->rmax; ++ir) |
931 | 0 | { |
932 | 0 | for (int ib = ptr->bmin; ib <= ptr->bmax; ++ib) |
933 | 0 | { |
934 | 0 | *histp += FindColorCount( |
935 | 0 | psHashHistogram, MAKE_COLOR_CODE(ir, ig, ib)); |
936 | 0 | } |
937 | 0 | } |
938 | 0 | histp++; |
939 | 0 | } |
940 | 0 | } |
941 | 0 | else |
942 | 0 | { |
943 | 0 | T *histp = &hist2[ptr->gmin]; |
944 | 0 | for (int ig = ptr->gmin; ig <= ptr->gmax; ++ig) |
945 | 0 | { |
946 | 0 | *histp = 0; |
947 | 0 | for (int ir = ptr->rmin; ir <= ptr->rmax; ++ir) |
948 | 0 | { |
949 | 0 | const T *iptr = |
950 | 0 | HISTOGRAM(histogram, nCLevels, ir, ig, ptr->bmin); |
951 | 0 | for (int ib = ptr->bmin; ib <= ptr->bmax; ++ib) |
952 | 0 | *histp += *iptr++; |
953 | 0 | } |
954 | 0 | histp++; |
955 | 0 | } |
956 | 0 | } |
957 | 0 | first = ptr->gmin; |
958 | 0 | last = ptr->gmax; |
959 | 0 | break; |
960 | 0 | } |
961 | 0 | case BLUE: |
962 | 0 | { |
963 | 0 | if (nPixels != 0 && nIters > nPixels) |
964 | 0 | { |
965 | 0 | const int rmin = ptr->rmin; |
966 | 0 | const int rmax = ptr->rmax; |
967 | 0 | const int gmin = ptr->gmin; |
968 | 0 | const int gmax = ptr->gmax; |
969 | 0 | const int bmin = ptr->bmin; |
970 | 0 | const int bmax = ptr->bmax; |
971 | 0 | for (T iPixel = 0; iPixel < nPixels; iPixel++) |
972 | 0 | { |
973 | 0 | const int iR = pabyRedBand[iPixel]; |
974 | 0 | const int iG = pabyGreenBand[iPixel]; |
975 | 0 | const int iB = pabyBlueBand[iPixel]; |
976 | 0 | if (iR >= rmin && iR <= rmax && iG >= gmin && iG <= gmax && |
977 | 0 | iB >= bmin && iB <= bmax) |
978 | 0 | { |
979 | 0 | hist2[iB]++; |
980 | 0 | } |
981 | 0 | } |
982 | 0 | } |
983 | 0 | else if (psHashHistogram) |
984 | 0 | { |
985 | 0 | T *histp = &hist2[ptr->bmin]; |
986 | 0 | for (int ib = ptr->bmin; ib <= ptr->bmax; ++ib) |
987 | 0 | { |
988 | 0 | *histp = 0; |
989 | 0 | for (int ir = ptr->rmin; ir <= ptr->rmax; ++ir) |
990 | 0 | { |
991 | 0 | for (int ig = ptr->gmin; ig <= ptr->gmax; ++ig) |
992 | 0 | { |
993 | 0 | *histp += FindColorCount( |
994 | 0 | psHashHistogram, MAKE_COLOR_CODE(ir, ig, ib)); |
995 | 0 | } |
996 | 0 | } |
997 | 0 | histp++; |
998 | 0 | } |
999 | 0 | } |
1000 | 0 | else |
1001 | 0 | { |
1002 | 0 | T *histp = &hist2[ptr->bmin]; |
1003 | 0 | for (int ib = ptr->bmin; ib <= ptr->bmax; ++ib) |
1004 | 0 | { |
1005 | 0 | *histp = 0; |
1006 | 0 | for (int ir = ptr->rmin; ir <= ptr->rmax; ++ir) |
1007 | 0 | { |
1008 | 0 | const T *iptr = |
1009 | 0 | HISTOGRAM(histogram, nCLevels, ir, ptr->gmin, ib); |
1010 | 0 | for (int ig = ptr->gmin; ig <= ptr->gmax; ++ig) |
1011 | 0 | { |
1012 | 0 | *histp += *iptr; |
1013 | 0 | iptr += nCLevels; |
1014 | 0 | } |
1015 | 0 | } |
1016 | 0 | histp++; |
1017 | 0 | } |
1018 | 0 | } |
1019 | 0 | first = ptr->bmin; |
1020 | 0 | last = ptr->bmax; |
1021 | 0 | break; |
1022 | 0 | } |
1023 | 0 | } |
1024 | | // Find median point. |
1025 | 0 | T *histp = &hist2[first]; |
1026 | 0 | int i = first; // TODO(schwehr): Rename i. |
1027 | 0 | { |
1028 | 0 | T sum = 0; |
1029 | 0 | T sum2 = static_cast<T>(ptr->total / 2); |
1030 | 0 | for (; i <= last && (sum += *histp++) < sum2; ++i) |
1031 | 0 | { |
1032 | 0 | } |
1033 | 0 | } |
1034 | 0 | if (i == first) |
1035 | 0 | i++; |
1036 | | |
1037 | | // Create new box, re-allocate points. |
1038 | 0 | Colorbox *new_cb = *pfreeboxes; |
1039 | 0 | *pfreeboxes = new_cb->next; |
1040 | 0 | if (*pfreeboxes) |
1041 | 0 | (*pfreeboxes)->prev = nullptr; |
1042 | 0 | if (*pusedboxes) |
1043 | 0 | (*pusedboxes)->prev = new_cb; |
1044 | 0 | new_cb->next = *pusedboxes; |
1045 | 0 | *pusedboxes = new_cb; |
1046 | |
|
1047 | 0 | histp = &hist2[first]; |
1048 | 0 | { |
1049 | 0 | T sum1 = 0; |
1050 | 0 | for (int j = first; j < i; j++) |
1051 | 0 | sum1 += *histp++; |
1052 | 0 | T sum2 = 0; |
1053 | 0 | for (int j = i; j <= last; j++) |
1054 | 0 | sum2 += *histp++; |
1055 | 0 | new_cb->total = sum1; |
1056 | 0 | ptr->total = sum2; |
1057 | 0 | } |
1058 | 0 | new_cb->rmin = ptr->rmin; |
1059 | 0 | new_cb->rmax = ptr->rmax; |
1060 | 0 | new_cb->gmin = ptr->gmin; |
1061 | 0 | new_cb->gmax = ptr->gmax; |
1062 | 0 | new_cb->bmin = ptr->bmin; |
1063 | 0 | new_cb->bmax = ptr->bmax; |
1064 | 0 | switch (axis) |
1065 | 0 | { |
1066 | 0 | case RED: |
1067 | 0 | new_cb->rmax = i - 1; |
1068 | 0 | ptr->rmin = i; |
1069 | 0 | break; |
1070 | 0 | case GREEN: |
1071 | 0 | new_cb->gmax = i - 1; |
1072 | 0 | ptr->gmin = i; |
1073 | 0 | break; |
1074 | 0 | case BLUE: |
1075 | 0 | new_cb->bmax = i - 1; |
1076 | 0 | ptr->bmin = i; |
1077 | 0 | break; |
1078 | 0 | } |
1079 | | |
1080 | 0 | if (nPixels != 0 && |
1081 | 0 | static_cast<T>(new_cb->rmax - new_cb->rmin + 1) * |
1082 | 0 | static_cast<T>(new_cb->gmax - new_cb->gmin + 1) * |
1083 | 0 | static_cast<T>(new_cb->bmax - new_cb->bmin + 1) > |
1084 | 0 | nPixels) |
1085 | 0 | { |
1086 | 0 | shrinkboxFromBand(new_cb, pabyRedBand, pabyGreenBand, pabyBlueBand, |
1087 | 0 | nPixels); |
1088 | 0 | } |
1089 | 0 | else if (psHashHistogram != nullptr) |
1090 | 0 | { |
1091 | 0 | shrinkboxFromHashHistogram(new_cb, psHashHistogram); |
1092 | 0 | } |
1093 | 0 | else |
1094 | 0 | { |
1095 | 0 | shrinkbox(new_cb, histogram, nCLevels); |
1096 | 0 | } |
1097 | |
|
1098 | 0 | if (nPixels != 0 && static_cast<T>(ptr->rmax - ptr->rmin + 1) * |
1099 | 0 | static_cast<T>(ptr->gmax - ptr->gmin + 1) * |
1100 | 0 | static_cast<T>(ptr->bmax - ptr->bmin + 1) > |
1101 | 0 | nPixels) |
1102 | 0 | { |
1103 | 0 | shrinkboxFromBand(ptr, pabyRedBand, pabyGreenBand, pabyBlueBand, |
1104 | 0 | nPixels); |
1105 | 0 | } |
1106 | 0 | else if (psHashHistogram != nullptr) |
1107 | 0 | { |
1108 | 0 | shrinkboxFromHashHistogram(ptr, psHashHistogram); |
1109 | 0 | } |
1110 | 0 | else |
1111 | 0 | { |
1112 | 0 | shrinkbox(ptr, histogram, nCLevels); |
1113 | 0 | } |
1114 | 0 | } Unexecuted instantiation: gdalmediancut.cpp:void splitbox<unsigned int>(colorbox*, unsigned int const*, HashHistogram const*, int, colorbox**, colorbox**, unsigned char*, unsigned char*, unsigned char*, unsigned int) Unexecuted instantiation: gdalmediancut.cpp:void splitbox<unsigned long long>(colorbox*, unsigned long long const*, HashHistogram const*, int, colorbox**, colorbox**, unsigned char*, unsigned char*, unsigned char*, unsigned long long) |
1115 | | |
1116 | | /************************************************************************/ |
1117 | | /* shrinkbox() */ |
1118 | | /************************************************************************/ |
1119 | | template <class T> |
1120 | | static void shrinkbox(Colorbox *box, const T *histogram, int nCLevels) |
1121 | 0 | { |
1122 | 0 | if (box->rmax > box->rmin) |
1123 | 0 | { |
1124 | 0 | for (int ir = box->rmin; ir <= box->rmax; ++ir) |
1125 | 0 | { |
1126 | 0 | for (int ig = box->gmin; ig <= box->gmax; ++ig) |
1127 | 0 | { |
1128 | 0 | const T *histp = |
1129 | 0 | HISTOGRAM(histogram, nCLevels, ir, ig, box->bmin); |
1130 | 0 | for (int ib = box->bmin; ib <= box->bmax; ++ib) |
1131 | 0 | { |
1132 | 0 | if (*histp++ != 0) |
1133 | 0 | { |
1134 | 0 | box->rmin = ir; |
1135 | 0 | goto have_rmin; |
1136 | 0 | } |
1137 | 0 | } |
1138 | 0 | } |
1139 | 0 | } |
1140 | 0 | } |
1141 | 0 | have_rmin: |
1142 | 0 | if (box->rmax > box->rmin) |
1143 | 0 | { |
1144 | 0 | for (int ir = box->rmax; ir >= box->rmin; --ir) |
1145 | 0 | { |
1146 | 0 | for (int ig = box->gmin; ig <= box->gmax; ++ig) |
1147 | 0 | { |
1148 | 0 | const T *histp = |
1149 | 0 | HISTOGRAM(histogram, nCLevels, ir, ig, box->bmin); |
1150 | 0 | for (int ib = box->bmin; ib <= box->bmax; ++ib) |
1151 | 0 | { |
1152 | 0 | if (*histp++ != 0) |
1153 | 0 | { |
1154 | 0 | box->rmax = ir; |
1155 | 0 | goto have_rmax; |
1156 | 0 | } |
1157 | 0 | } |
1158 | 0 | } |
1159 | 0 | } |
1160 | 0 | } |
1161 | | |
1162 | 0 | have_rmax: |
1163 | 0 | if (box->gmax > box->gmin) |
1164 | 0 | { |
1165 | 0 | for (int ig = box->gmin; ig <= box->gmax; ++ig) |
1166 | 0 | { |
1167 | 0 | for (int ir = box->rmin; ir <= box->rmax; ++ir) |
1168 | 0 | { |
1169 | 0 | const T *histp = |
1170 | 0 | HISTOGRAM(histogram, nCLevels, ir, ig, box->bmin); |
1171 | 0 | for (int ib = box->bmin; ib <= box->bmax; ++ib) |
1172 | 0 | { |
1173 | 0 | if (*histp++ != 0) |
1174 | 0 | { |
1175 | 0 | box->gmin = ig; |
1176 | 0 | goto have_gmin; |
1177 | 0 | } |
1178 | 0 | } |
1179 | 0 | } |
1180 | 0 | } |
1181 | 0 | } |
1182 | | |
1183 | 0 | have_gmin: |
1184 | 0 | if (box->gmax > box->gmin) |
1185 | 0 | { |
1186 | 0 | for (int ig = box->gmax; ig >= box->gmin; --ig) |
1187 | 0 | { |
1188 | 0 | for (int ir = box->rmin; ir <= box->rmax; ++ir) |
1189 | 0 | { |
1190 | 0 | const T *histp = |
1191 | 0 | HISTOGRAM(histogram, nCLevels, ir, ig, box->bmin); |
1192 | 0 | for (int ib = box->bmin; ib <= box->bmax; ++ib) |
1193 | 0 | { |
1194 | 0 | if (*histp++ != 0) |
1195 | 0 | { |
1196 | 0 | box->gmax = ig; |
1197 | 0 | goto have_gmax; |
1198 | 0 | } |
1199 | 0 | } |
1200 | 0 | } |
1201 | 0 | } |
1202 | 0 | } |
1203 | | |
1204 | 0 | have_gmax: |
1205 | 0 | if (box->bmax > box->bmin) |
1206 | 0 | { |
1207 | 0 | for (int ib = box->bmin; ib <= box->bmax; ++ib) |
1208 | 0 | { |
1209 | 0 | for (int ir = box->rmin; ir <= box->rmax; ++ir) |
1210 | 0 | { |
1211 | 0 | const T *histp = |
1212 | 0 | HISTOGRAM(histogram, nCLevels, ir, box->gmin, ib); |
1213 | 0 | for (int ig = box->gmin; ig <= box->gmax; ++ig) |
1214 | 0 | { |
1215 | 0 | if (*histp != 0) |
1216 | 0 | { |
1217 | 0 | box->bmin = ib; |
1218 | 0 | goto have_bmin; |
1219 | 0 | } |
1220 | 0 | histp += nCLevels; |
1221 | 0 | } |
1222 | 0 | } |
1223 | 0 | } |
1224 | 0 | } |
1225 | | |
1226 | 0 | have_bmin: |
1227 | 0 | if (box->bmax > box->bmin) |
1228 | 0 | { |
1229 | 0 | for (int ib = box->bmax; ib >= box->bmin; --ib) |
1230 | 0 | { |
1231 | 0 | for (int ir = box->rmin; ir <= box->rmax; ++ir) |
1232 | 0 | { |
1233 | 0 | const T *histp = |
1234 | 0 | HISTOGRAM(histogram, nCLevels, ir, box->gmin, ib); |
1235 | 0 | for (int ig = box->gmin; ig <= box->gmax; ++ig) |
1236 | 0 | { |
1237 | 0 | if (*histp != 0) |
1238 | 0 | { |
1239 | 0 | box->bmax = ib; |
1240 | 0 | goto have_bmax; |
1241 | 0 | } |
1242 | 0 | histp += nCLevels; |
1243 | 0 | } |
1244 | 0 | } |
1245 | 0 | } |
1246 | 0 | } |
1247 | | |
1248 | 0 | have_bmax:; |
1249 | 0 | } Unexecuted instantiation: gdalmediancut.cpp:void shrinkbox<unsigned int>(colorbox*, unsigned int const*, int) Unexecuted instantiation: gdalmediancut.cpp:void shrinkbox<unsigned long long>(colorbox*, unsigned long long const*, int) |
1250 | | |
1251 | | // Explicitly instantiate template functions. |
1252 | | template int GDALComputeMedianCutPCTInternal<GUInt32>( |
1253 | | GDALRasterBandH hRed, GDALRasterBandH hGreen, GDALRasterBandH hBlue, |
1254 | | GByte *pabyRedBand, GByte *pabyGreenBand, GByte *pabyBlueBand, |
1255 | | int (*pfnIncludePixel)(int, int, void *), int nColors, int nBits, |
1256 | | GUInt32 *panHistogram, GDALColorTableH hColorTable, |
1257 | | GDALProgressFunc pfnProgress, void *pProgressArg); |
1258 | | |
1259 | | template int GDALComputeMedianCutPCTInternal<GUIntBig>( |
1260 | | GDALRasterBandH hRed, GDALRasterBandH hGreen, GDALRasterBandH hBlue, |
1261 | | GByte *pabyRedBand, GByte *pabyGreenBand, GByte *pabyBlueBand, |
1262 | | int (*pfnIncludePixel)(int, int, void *), int nColors, int nBits, |
1263 | | GUIntBig *panHistogram, GDALColorTableH hColorTable, |
1264 | | GDALProgressFunc pfnProgress, void *pProgressArg); |