Mercurial > hg > xemacs-beta
annotate src/imgproc.c @ 5670:baab2e3a4141
Prepare for release.
| author | Stephen J. Turnbull <stephen@xemacs.org> |
|---|---|
| date | Fri, 03 Aug 2012 02:00:29 +0900 |
| parents | 2aa9cd456ae7 |
| children |
| rev | line source |
|---|---|
| 428 | 1 /* Image processing functions |
| 2 Copyright (C) 1998 Jareth Hein | |
| 3 | |
|
5405
2aa9cd456ae7
Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
5169
diff
changeset
|
4 This file is part of XEmacs. |
| 428 | 5 |
|
5405
2aa9cd456ae7
Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
5169
diff
changeset
|
6 XEmacs is free software: you can redistribute it and/or modify it |
| 428 | 7 under the terms of the GNU General Public License as published by the |
|
5405
2aa9cd456ae7
Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
5169
diff
changeset
|
8 Free Software Foundation, either version 3 of the License, or (at your |
|
2aa9cd456ae7
Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
5169
diff
changeset
|
9 option) any later version. |
| 428 | 10 |
| 11 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
| 12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 for more details. | |
| 15 | |
| 16 You should have received a copy of the GNU General Public License | |
|
5405
2aa9cd456ae7
Move src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
5169
diff
changeset
|
17 along with XEmacs. If not, see <http://www.gnu.org/licenses/>. */ |
| 428 | 18 |
| 19 /* Synched up with: Not in FSF. */ | |
| 20 | |
| 21 /* Original author: Jareth Hein */ | |
| 22 | |
| 23 /* Parts of this file are based on code from Sam Leffler's tiff library, | |
| 24 with the original copyright displayed here: | |
| 25 | |
| 26 Copyright (c) 1988-1997 Sam Leffler | |
| 27 Copyright (c) 1991-1997 Silicon Graphics, Inc. | |
|
5169
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
28 Copyright (C) 2010 Ben Wing. |
| 428 | 29 |
| 30 Permission to use, copy, modify, distribute, and sell this software and | |
| 31 its documentation for any purpose is hereby granted without fee, provided | |
| 32 that (i) the above copyright notices and this permission notice appear in | |
| 33 all copies of the software and related documentation, and (ii) the names of | |
| 34 Sam Leffler and Silicon Graphics may not be used in any advertising or | |
| 35 publicity relating to the software without the specific, prior written | |
| 36 permission of Sam Leffler and Silicon Graphics. */ | |
| 37 | |
| 38 /* Quantizing code based off of the paper | |
| 39 Color Image Quantization for Frame Buffer Display, Paul Heckbert, | |
| 40 Siggraph '82 proceedings, pp. 297-307 */ | |
| 41 | |
| 42 #include <config.h> | |
| 43 #include "lisp.h" | |
| 44 #include "imgproc.h" | |
| 45 | |
| 46 static void | |
| 2367 | 47 get_histogram(quant_table *qt, Binbyte *pic, |
| 428 | 48 int width, int height, Colorbox* box) |
| 49 { | |
| 2367 | 50 register Binbyte *inptr; |
| 428 | 51 register int red, green, blue; |
| 647 | 52 register int j, i; |
| 428 | 53 |
| 54 box->rmin = box->gmin = box->bmin = 999; | |
| 55 box->rmax = box->gmax = box->bmax = -1; | |
| 56 box->total = width * height; | |
| 57 | |
| 58 inptr = pic; | |
| 59 for (i = 0; i < height; i++) | |
| 60 { | |
| 61 for (j = width; j-- > 0;) | |
| 62 { | |
| 63 red = *inptr++ >> COLOR_SHIFT; | |
| 64 green = *inptr++ >> COLOR_SHIFT; | |
| 65 blue = *inptr++ >> COLOR_SHIFT; | |
| 66 if (red < box->rmin) | |
| 67 box->rmin = red; | |
| 68 if (red > box->rmax) | |
| 69 box->rmax = red; | |
| 70 if (green < box->gmin) | |
| 71 box->gmin = green; | |
| 72 if (green > box->gmax) | |
| 73 box->gmax = green; | |
| 74 if (blue < box->bmin) | |
| 75 box->bmin = blue; | |
| 76 if (blue > box->bmax) | |
| 77 box->bmax = blue; | |
| 78 qt->histogram[red][green][blue]++; | |
| 79 } | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 static Colorbox * | |
| 84 largest_box(quant_table *qt) | |
| 85 { | |
| 86 register Colorbox *p, *b; | |
| 87 register int size; | |
| 88 | |
| 89 b = NULL; | |
| 90 size = -1; | |
| 91 for (p = qt->usedboxes; p != NULL; p = p->next) | |
| 92 if ((p->rmax > p->rmin || p->gmax > p->gmin || | |
| 93 p->bmax > p->bmin) && p->total > size) | |
| 94 size = (b = p)->total; | |
| 95 return (b); | |
| 96 } | |
| 97 | |
| 98 static void | |
| 99 shrinkbox(quant_table *qt, Colorbox* box) | |
| 100 { | |
| 101 register int *histp, ir, ig, ib; | |
| 102 | |
| 103 if (box->rmax > box->rmin) | |
| 104 { | |
| 105 for (ir = box->rmin; ir <= box->rmax; ++ir) | |
| 106 for (ig = box->gmin; ig <= box->gmax; ++ig) | |
| 107 { | |
| 108 histp = &(qt->histogram[ir][ig][box->bmin]); | |
| 109 for (ib = box->bmin; ib <= box->bmax; ++ib) | |
| 110 if (*histp++ != 0) | |
| 111 { | |
| 112 box->rmin = ir; | |
| 113 goto have_rmin; | |
| 114 } | |
| 115 } | |
| 116 have_rmin: | |
| 117 if (box->rmax > box->rmin) | |
| 118 for (ir = box->rmax; ir >= box->rmin; --ir) | |
| 119 for (ig = box->gmin; ig <= box->gmax; ++ig) | |
| 120 { | |
| 121 histp = &(qt->histogram[ir][ig][box->bmin]); | |
| 122 ib = box->bmin; | |
| 123 for (; ib <= box->bmax; ++ib) | |
| 124 if (*histp++ != 0) | |
| 125 { | |
| 126 box->rmax = ir; | |
| 127 goto have_rmax; | |
| 128 } | |
| 129 } | |
| 130 } | |
| 131 have_rmax: | |
| 132 if (box->gmax > box->gmin) | |
| 133 { | |
| 134 for (ig = box->gmin; ig <= box->gmax; ++ig) | |
| 135 for (ir = box->rmin; ir <= box->rmax; ++ir) | |
| 136 { | |
| 137 histp = &(qt->histogram[ir][ig][box->bmin]); | |
| 138 for (ib = box->bmin; ib <= box->bmax; ++ib) | |
| 139 if (*histp++ != 0) | |
| 140 { | |
| 141 box->gmin = ig; | |
| 142 goto have_gmin; | |
| 143 } | |
| 144 } | |
| 145 have_gmin: | |
| 146 if (box->gmax > box->gmin) | |
| 147 for (ig = box->gmax; ig >= box->gmin; --ig) | |
| 148 for (ir = box->rmin; ir <= box->rmax; ++ir) | |
| 149 { | |
| 150 histp = &(qt->histogram[ir][ig][box->bmin]); | |
| 151 ib = box->bmin; | |
| 152 for (; ib <= box->bmax; ++ib) | |
| 153 if (*histp++ != 0) | |
| 154 { | |
| 155 box->gmax = ig; | |
| 156 goto have_gmax; | |
| 157 } | |
| 158 } | |
| 159 } | |
| 160 have_gmax: | |
| 161 if (box->bmax > box->bmin) | |
| 162 { | |
| 163 for (ib = box->bmin; ib <= box->bmax; ++ib) | |
| 164 for (ir = box->rmin; ir <= box->rmax; ++ir) | |
| 165 { | |
| 166 histp = &(qt->histogram[ir][box->gmin][ib]); | |
| 167 for (ig = box->gmin; ig <= box->gmax; ++ig) | |
| 168 { | |
| 169 if (*histp != 0) | |
| 170 { | |
| 171 box->bmin = ib; | |
| 172 goto have_bmin; | |
| 173 } | |
| 174 histp += B_LEN; | |
| 175 } | |
| 176 } | |
| 177 have_bmin: | |
| 178 if (box->bmax > box->bmin) | |
| 179 for (ib = box->bmax; ib >= box->bmin; --ib) | |
| 180 for (ir = box->rmin; ir <= box->rmax; ++ir) | |
| 181 { | |
| 182 histp = &(qt->histogram[ir][box->gmin][ib]); | |
| 183 ig = box->gmin; | |
| 184 for (; ig <= box->gmax; ++ig) | |
| 185 { | |
| 186 if (*histp != 0) | |
| 187 { | |
| 188 box->bmax = ib; | |
| 189 goto have_bmax; | |
| 190 } | |
| 191 histp += B_LEN; | |
| 192 } | |
| 193 } | |
| 194 } | |
| 195 have_bmax: | |
| 196 ; | |
| 197 } | |
| 198 | |
| 199 static void | |
| 200 splitbox(quant_table *qt, Colorbox* ptr) | |
| 201 { | |
| 202 int hist2[B_LEN]; | |
| 203 int first = 0, last = 0; | |
| 3025 | 204 register Colorbox *new_; |
| 428 | 205 register int *iptr, *histp; |
| 206 register int i, j; | |
| 207 register int ir,ig,ib; | |
| 208 register int sum, sum1, sum2; | |
| 209 enum { RED, GREEN, BLUE } axis; | |
| 210 | |
| 211 /* | |
| 212 * See which axis is the largest, do a histogram along that | |
| 213 * axis. Split at median point. Contract both new boxes to | |
| 214 * fit points and return | |
| 215 */ | |
| 216 i = ptr->rmax - ptr->rmin; | |
| 217 if (i >= ptr->gmax - ptr->gmin && i >= ptr->bmax - ptr->bmin) | |
| 218 axis = RED; | |
| 219 else if (ptr->gmax - ptr->gmin >= ptr->bmax - ptr->bmin) | |
| 220 axis = GREEN; | |
| 221 else | |
| 222 axis = BLUE; | |
| 223 /* get histogram along longest axis */ | |
| 224 switch (axis) | |
| 225 { | |
| 226 case RED: | |
| 227 histp = &hist2[ptr->rmin]; | |
| 228 for (ir = ptr->rmin; ir <= ptr->rmax; ++ir) | |
| 229 { | |
| 230 *histp = 0; | |
| 231 for (ig = ptr->gmin; ig <= ptr->gmax; ++ig) | |
| 232 { | |
| 233 iptr = &(qt->histogram[ir][ig][ptr->bmin]); | |
| 234 for (ib = ptr->bmin; ib <= ptr->bmax; ++ib) | |
| 235 *histp += *iptr++; | |
| 236 } | |
| 237 histp++; | |
| 238 } | |
| 239 first = ptr->rmin; | |
| 240 last = ptr->rmax; | |
| 241 break; | |
| 242 case GREEN: | |
| 243 histp = &hist2[ptr->gmin]; | |
| 244 for (ig = ptr->gmin; ig <= ptr->gmax; ++ig) | |
| 245 { | |
| 246 *histp = 0; | |
| 247 for (ir = ptr->rmin; ir <= ptr->rmax; ++ir) | |
| 248 { | |
| 249 iptr = &(qt->histogram[ir][ig][ptr->bmin]); | |
| 250 for (ib = ptr->bmin; ib <= ptr->bmax; ++ib) | |
| 251 *histp += *iptr++; | |
| 252 } | |
| 253 histp++; | |
| 254 } | |
| 255 first = ptr->gmin; | |
| 256 last = ptr->gmax; | |
| 257 break; | |
| 258 case BLUE: | |
| 259 histp = &hist2[ptr->bmin]; | |
| 260 for (ib = ptr->bmin; ib <= ptr->bmax; ++ib) | |
| 261 { | |
| 262 *histp = 0; | |
| 263 for (ir = ptr->rmin; ir <= ptr->rmax; ++ir) | |
| 264 { | |
| 265 iptr = &(qt->histogram[ir][ptr->gmin][ib]); | |
| 266 for (ig = ptr->gmin; ig <= ptr->gmax; ++ig) | |
| 267 { | |
| 268 *histp += *iptr; | |
| 269 iptr += B_LEN; | |
| 270 } | |
| 271 } | |
| 272 histp++; | |
| 273 } | |
| 274 first = ptr->bmin; | |
| 275 last = ptr->bmax; | |
| 276 break; | |
| 277 } | |
| 278 /* find median point */ | |
| 279 sum2 = ptr->total / 2; | |
| 280 histp = &hist2[first]; | |
| 281 sum = 0; | |
| 282 for (i = first; i <= last && (sum += *histp++) < sum2; ++i) | |
| 283 ; | |
| 284 if (i == first) | |
| 285 i++; | |
| 286 | |
| 287 /* Create new box, re-allocate points */ | |
| 3025 | 288 new_ = qt->freeboxes; |
| 289 qt->freeboxes = new_->next; | |
| 428 | 290 if (qt->freeboxes) |
| 291 qt->freeboxes->prev = NULL; | |
| 292 if (qt->usedboxes) | |
| 3025 | 293 qt->usedboxes->prev = new_; |
| 294 new_->next = qt->usedboxes; | |
| 295 qt->usedboxes = new_; | |
| 428 | 296 |
| 297 histp = &hist2[first]; | |
| 298 for (sum1 = 0, j = first; j < i; j++) | |
| 299 sum1 += *histp++; | |
| 300 for (sum2 = 0, j = i; j <= last; j++) | |
| 301 sum2 += *histp++; | |
| 3025 | 302 new_->total = sum1; |
| 428 | 303 ptr->total = sum2; |
| 304 | |
| 3025 | 305 new_->rmin = ptr->rmin; |
| 306 new_->rmax = ptr->rmax; | |
| 307 new_->gmin = ptr->gmin; | |
| 308 new_->gmax = ptr->gmax; | |
| 309 new_->bmin = ptr->bmin; | |
| 310 new_->bmax = ptr->bmax; | |
| 428 | 311 switch (axis) |
| 312 { | |
| 313 case RED: | |
| 3025 | 314 new_->rmax = i-1; |
| 428 | 315 ptr->rmin = i; |
| 316 break; | |
| 317 case GREEN: | |
| 3025 | 318 new_->gmax = i-1; |
| 428 | 319 ptr->gmin = i; |
| 320 break; | |
| 321 case BLUE: | |
| 3025 | 322 new_->bmax = i-1; |
| 428 | 323 ptr->bmin = i; |
| 324 break; | |
| 325 } | |
| 3025 | 326 shrinkbox (qt, new_); |
| 428 | 327 shrinkbox (qt, ptr); |
| 328 } | |
| 329 | |
| 330 | |
| 331 static C_cell * | |
| 332 create_colorcell(quant_table *qt, int num_colors, int red, int green, int blue) | |
| 333 { | |
| 334 register int ir, ig, ib, i; | |
| 335 register C_cell *ptr; | |
| 336 int mindist, next_n; | |
| 337 register int tmp, dist, n; | |
| 338 | |
| 339 ir = red >> (COLOR_DEPTH-C_DEPTH); | |
| 340 ig = green >> (COLOR_DEPTH-C_DEPTH); | |
| 341 ib = blue >> (COLOR_DEPTH-C_DEPTH); | |
| 2367 | 342 ptr = xnew (C_cell); |
| 428 | 343 *(qt->ColorCells + ir*C_LEN*C_LEN + ig*C_LEN + ib) = ptr; |
| 344 ptr->num_ents = 0; | |
| 345 | |
| 346 /* | |
| 347 * Step 1: find all colors inside this cell, while we're at | |
| 348 * it, find distance of centermost point to furthest corner | |
| 349 */ | |
| 350 mindist = 99999999; | |
| 351 for (i = 0; i < num_colors; ++i) | |
| 352 { | |
| 353 if (qt->rm[i]>>(COLOR_DEPTH-C_DEPTH) != ir || | |
| 354 qt->gm[i]>>(COLOR_DEPTH-C_DEPTH) != ig || | |
| 355 qt->bm[i]>>(COLOR_DEPTH-C_DEPTH) != ib) | |
| 356 continue; | |
| 357 ptr->entries[ptr->num_ents][0] = i; | |
| 358 ptr->entries[ptr->num_ents][1] = 0; | |
| 359 ++ptr->num_ents; | |
| 360 tmp = qt->rm[i] - red; | |
| 361 if (tmp < (MAX_COLOR/C_LEN/2)) | |
| 362 tmp = MAX_COLOR/C_LEN-1 - tmp; | |
| 363 dist = tmp*tmp; | |
| 364 tmp = qt->gm[i] - green; | |
| 365 if (tmp < (MAX_COLOR/C_LEN/2)) | |
| 366 tmp = MAX_COLOR/C_LEN-1 - tmp; | |
| 367 dist += tmp*tmp; | |
| 368 tmp = qt->bm[i] - blue; | |
| 369 if (tmp < (MAX_COLOR/C_LEN/2)) | |
| 370 tmp = MAX_COLOR/C_LEN-1 - tmp; | |
| 371 dist += tmp*tmp; | |
| 372 if (dist < mindist) | |
| 373 mindist = dist; | |
| 374 } | |
| 375 | |
| 376 /* | |
| 377 * Step 3: find all points within that distance to cell. | |
| 378 */ | |
| 379 for (i = 0; i < num_colors; ++i) | |
| 380 { | |
| 381 if (qt->rm[i] >> (COLOR_DEPTH-C_DEPTH) == ir && | |
| 382 qt->gm[i] >> (COLOR_DEPTH-C_DEPTH) == ig && | |
| 383 qt->bm[i] >> (COLOR_DEPTH-C_DEPTH) == ib) | |
| 384 continue; | |
| 385 dist = 0; | |
| 386 if ((tmp = red - qt->rm[i]) > 0 || | |
| 387 (tmp = qt->rm[i] - (red + MAX_COLOR/C_LEN-1)) > 0 ) | |
| 388 dist += tmp*tmp; | |
| 389 if ((tmp = green - qt->gm[i]) > 0 || | |
| 390 (tmp = qt->gm[i] - (green + MAX_COLOR/C_LEN-1)) > 0 ) | |
| 391 dist += tmp*tmp; | |
| 392 if ((tmp = blue - qt->bm[i]) > 0 || | |
| 393 (tmp = qt->bm[i] - (blue + MAX_COLOR/C_LEN-1)) > 0 ) | |
| 394 dist += tmp*tmp; | |
| 395 if (dist < mindist) | |
| 396 { | |
| 397 ptr->entries[ptr->num_ents][0] = i; | |
| 398 ptr->entries[ptr->num_ents][1] = dist; | |
| 399 ++ptr->num_ents; | |
| 400 } | |
| 401 } | |
| 402 | |
| 403 /* | |
| 404 * Sort color cells by distance, use cheap exchange sort | |
| 405 */ | |
| 406 for (n = ptr->num_ents - 1; n > 0; n = next_n) | |
| 407 { | |
| 408 next_n = 0; | |
| 409 for (i = 0; i < n; ++i) | |
| 410 if (ptr->entries[i][1] > ptr->entries[i+1][1]) | |
| 411 { | |
| 412 tmp = ptr->entries[i][0]; | |
| 413 ptr->entries[i][0] = ptr->entries[i+1][0]; | |
| 414 ptr->entries[i+1][0] = tmp; | |
| 415 tmp = ptr->entries[i][1]; | |
| 416 ptr->entries[i][1] = ptr->entries[i+1][1]; | |
| 417 ptr->entries[i+1][1] = tmp; | |
| 418 next_n = i; | |
| 419 } | |
| 420 } | |
| 421 return (ptr); | |
| 422 } | |
| 423 | |
| 424 static int | |
| 425 map_colortable(quant_table *qt, int num_colors) | |
| 426 { | |
| 427 register int *histp = &(qt->histogram[0][0][0]); | |
| 428 register C_cell *cell; | |
| 429 register int j, tmp, d2, dist; | |
| 430 int ir, ig, ib, i; | |
| 431 | |
| 432 for (ir = 0; ir < B_LEN; ++ir) | |
| 433 for (ig = 0; ig < B_LEN; ++ig) | |
| 434 for (ib = 0; ib < B_LEN; ++ib, histp++) | |
| 435 { | |
| 436 if (*histp == 0) | |
| 437 { | |
| 438 *histp = -1; | |
| 439 continue; | |
| 440 } | |
| 441 cell = *(qt->ColorCells + | |
| 442 (((ir>>(B_DEPTH-C_DEPTH)) << C_DEPTH*2) + | |
| 443 ((ig>>(B_DEPTH-C_DEPTH)) << C_DEPTH) + | |
| 444 (ib>>(B_DEPTH-C_DEPTH)))); | |
| 445 if (cell == NULL ) | |
| 446 cell = create_colorcell (qt, num_colors, | |
| 447 ir << COLOR_SHIFT, | |
| 448 ig << COLOR_SHIFT, | |
| 449 ib << COLOR_SHIFT); | |
| 450 if (cell == NULL) /* memory exhausted! punt! */ | |
| 451 return -1; | |
| 452 dist = 9999999; | |
| 453 for (i = 0; i < cell->num_ents && | |
| 454 dist > cell->entries[i][1]; ++i) | |
| 455 { | |
| 456 j = cell->entries[i][0]; | |
| 457 d2 = qt->rm[j] - (ir << COLOR_SHIFT); | |
| 458 d2 *= d2; | |
| 459 tmp = qt->gm[j] - (ig << COLOR_SHIFT); | |
| 460 d2 += tmp*tmp; | |
| 461 tmp = qt->bm[j] - (ib << COLOR_SHIFT); | |
| 462 d2 += tmp*tmp; | |
| 463 if (d2 < dist) | |
| 464 { | |
| 465 dist = d2; | |
| 466 *histp = j; | |
| 467 } | |
| 468 } | |
| 469 } | |
| 470 return 0; | |
| 471 } | |
| 472 | |
| 473 quant_table * | |
| 2367 | 474 build_EImage_quantable(Binbyte *eimage, int width, int height, int num_colors) |
| 428 | 475 { |
| 476 quant_table *qt; | |
| 477 Colorbox *box_list, *ptr; | |
| 478 int i,res; | |
| 479 | |
| 480 qt = (quant_table*)xmalloc_and_zero (sizeof(quant_table)); | |
| 481 if (qt == NULL) return NULL; | |
| 482 | |
| 483 assert (num_colors < 257 && num_colors > 2); | |
| 484 /* | |
| 485 * STEP 1: create empty boxes | |
| 486 */ | |
| 487 qt->usedboxes = NULL; | |
| 2367 | 488 box_list = qt->freeboxes = xnew_array (Colorbox, num_colors); |
| 428 | 489 qt->freeboxes[0].next = &(qt->freeboxes[1]); |
| 490 qt->freeboxes[0].prev = NULL; | |
| 491 for (i = 1; i < num_colors-1; ++i) | |
| 492 { | |
| 493 qt->freeboxes[i].next = &(qt->freeboxes[i+1]); | |
| 494 qt->freeboxes[i].prev = &(qt->freeboxes[i-1]); | |
| 495 } | |
| 496 qt->freeboxes[num_colors-1].next = NULL; | |
| 497 qt->freeboxes[num_colors-1].prev = &(qt->freeboxes[num_colors-2]); | |
| 498 | |
| 499 /* | |
| 500 * STEP 2: get histogram, initialize first box | |
| 501 */ | |
| 502 ptr = qt->freeboxes; | |
| 503 qt->freeboxes = ptr->next; | |
| 504 if (qt->freeboxes) | |
| 505 qt->freeboxes->prev = NULL; | |
| 506 ptr->next = qt->usedboxes; | |
| 507 qt->usedboxes = ptr; | |
| 508 if (ptr->next) | |
| 509 ptr->next->prev = ptr; | |
| 510 get_histogram (qt, eimage, width, height, ptr); | |
| 511 | |
| 512 /* | |
| 513 * STEP 3: continually subdivide boxes until no more free | |
| 514 * boxes remain or until all colors assigned. | |
| 515 */ | |
| 516 while (qt->freeboxes != NULL) | |
| 517 { | |
| 518 ptr = largest_box(qt); | |
| 519 if (ptr != NULL) | |
| 520 splitbox (qt, ptr); | |
| 521 else | |
| 522 qt->freeboxes = NULL; | |
| 523 } | |
| 524 | |
| 525 /* | |
| 526 * STEP 4: assign colors to all boxes | |
| 527 */ | |
| 528 for (i = 0, ptr = qt->usedboxes; ptr != NULL; ++i, ptr = ptr->next) | |
| 529 { | |
| 530 qt->rm[i] = ((ptr->rmin + ptr->rmax) << COLOR_SHIFT) / 2; | |
| 531 qt->gm[i] = ((ptr->gmin + ptr->gmax) << COLOR_SHIFT) / 2; | |
| 532 qt->bm[i] = ((ptr->bmin + ptr->bmax) << COLOR_SHIFT) / 2; | |
| 533 qt->um[i] = ptr->total; | |
| 534 } | |
| 535 qt->num_active_colors = i; | |
| 536 | |
| 537 /* We're done with the boxes now */ | |
|
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
538 xfree (box_list); |
| 428 | 539 qt->freeboxes = qt->usedboxes = NULL; |
| 540 | |
| 541 /* | |
| 542 * STEP 5: scan histogram and map all values to closest color | |
| 543 */ | |
| 544 /* 5a: create cell list as described in Heckbert */ | |
| 545 qt->ColorCells = (C_cell **)xmalloc_and_zero (C_LEN*C_LEN*C_LEN*sizeof (C_cell*)); | |
| 546 /* 5b: create mapping from truncated pixel space to color | |
| 547 table entries */ | |
| 548 res = map_colortable (qt, num_colors); | |
| 549 | |
| 550 /* 5c: done with ColorCells */ | |
| 551 for (i = 0; i < C_LEN*C_LEN*C_LEN; i++) | |
| 1726 | 552 if (qt->ColorCells[i]) |
|
5169
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
553 { |
|
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
554 xfree (qt->ColorCells[i]); |
|
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
555 qt->ColorCells[i] = 0; |
|
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
556 } |
|
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
557 xfree (qt->ColorCells); |
|
5169
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
558 qt->ColorCells = 0; |
| 428 | 559 |
| 560 if (res) | |
| 561 { | |
| 1726 | 562 /* we failed in memory allocation, so clean up and leave */ |
|
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
563 xfree (qt); |
| 428 | 564 return NULL; |
| 565 } | |
| 566 | |
| 567 return qt; | |
| 568 } |
