-
Notifications
You must be signed in to change notification settings - Fork 0
/
inv_cmap.c
898 lines (850 loc) · 23.3 KB
/
inv_cmap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
/*
* This software is copyrighted as noted below. It may be freely copied,
* modified, and redistributed, provided that the copyright notice is
* preserved on all copies.
*
* There is no warranty or other guarantee of fitness for this software,
* it is provided solely "as is". Bug reports or fixes may be sent
* to the author, who may or may not act on them as he desires.
*
* You may not include this software in a program or other software product
* without supplying the source, or without informing the end-user that the
* source is available for no extra charge.
*
* If you modify this software, you should include a notice giving the
* name of the person performing the modification, the date of modification,
* and the reason for such modification.
*/
/*
* inv_cmap.c - Compute an inverse colormap.
*
* Author: Spencer W. Thomas
* EECS Dept.
* University of Michigan
* Date: Thu Sep 20 1990
* Copyright (c) 1990, University of Michigan
*
* $Id$
*/
#include <math.h>
#include <stdio.h>
#ifndef NO_INV_CMAP_TRACKING
#ifdef DEBUG
static int cred, cgreen, cblue;
static int red, green;
static unsigned char *zrgbp;
#endif
static int bcenter, gcenter, rcenter;
static long gdist, rdist, cdist;
static long cbinc, cginc, crinc;
static unsigned long *gdp, *rdp, *cdp;
static unsigned char *grgbp, *rrgbp, *crgbp;
static int gstride, rstride;
static long x, xsqr, colormax;
static int cindex;
#ifdef INSTRUMENT_IT
static long outercount = 0, innercount = 0;
#endif
#ifdef USE_PROTOTYPES
static void maxfill( unsigned long *, long );
static int redloop( void );
static int greenloop( int );
static int blueloop( int );
#else
static void maxfill();
static int redloop();
static int greenloop();
static int blueloop();
#endif
/*****************************************************************
* TAG( inv_cmap )
*
* Compute an inverse colormap efficiently.
* Inputs:
* colors: Number of colors in the forward colormap.
* colormap: The forward colormap.
* bits: Number of quantization bits. The inverse
* colormap will have (2^bits)^3 entries.
* dist_buf: An array of (2^bits)^3 long integers to be
* used as scratch space.
* Outputs:
* rgbmap: The output inverse colormap. The entry
* rgbmap[(r<<(2*bits)) + (g<<bits) + b]
* is the colormap entry that is closest to the
* (quantized) color (r,g,b).
* Assumptions:
* Quantization is performed by right shift (low order bits are
* truncated). Thus, the distance to a quantized color is
* actually measured to the color at the center of the cell
* (i.e., to r+.5, g+.5, b+.5, if (r,g,b) is a quantized color).
* Algorithm:
* Uses a "distance buffer" algorithm:
* The distance from each representative in the forward color map
* to each point in the rgb space is computed. If it is less
* than the distance currently stored in dist_buf, then the
* corresponding entry in rgbmap is replaced with the current
* representative (and the dist_buf entry is replaced with the
* new distance).
*
* The distance computation uses an efficient incremental formulation.
*
* Distances are computed "outward" from each color. If the
* colors are evenly distributed in color space, the expected
* number of cells visited for color I is N^3/I.
* Thus, the complexity of the algorithm is O(log(K) N^3),
* where K = colors, and N = 2^bits.
*/
/*
* Here's the idea: scan from the "center" of each cell "out"
* until we hit the "edge" of the cell -- that is, the point
* at which some other color is closer -- and stop. In 1-D,
* this is simple:
* for i := here to max do
* if closer then buffer[i] = this color
* else break
* repeat above loop with i := here-1 to min by -1
*
* In 2-D, it's trickier, because along a "scan-line", the
* region might start "after" the "center" point. A picture
* might clarify:
* | ...
* | ... .
* ... .
* ... | .
* . + .
* . .
* . .
* .........
*
* The + marks the "center" of the above region. On the top 2
* lines, the region "begins" to the right of the "center".
*
* Thus, we need a loop like this:
* detect := false
* for i := here to max do
* if closer then
* buffer[..., i] := this color
* if !detect then
* here = i
* detect = true
* else
* if detect then
* break
*
* Repeat the above loop with i := here-1 to min by -1. Note that
* the "detect" value should not be reinitialized. If it was
* "true", and center is not inside the cell, then none of the
* cell lies to the left and this loop should exit
* immediately.
*
* The outer loops are similar, except that the "closer" test
* is replaced by a call to the "next in" loop; its "detect"
* value serves as the test. (No assignment to the buffer is
* done, either.)
*
* Each time an outer loop starts, the "here", "min", and
* "max" values of the next inner loop should be
* re-initialized to the center of the cell, 0, and cube size,
* respectively. Otherwise, these values will carry over from
* one "call" to the inner loop to the next. This tracks the
* edges of the cell and minimizes the number of
* "unproductive" comparisons that must be made.
*
* Finally, the inner-most loop can have the "if !detect"
* optimized out of it by splitting it into two loops: one
* that finds the first color value on the scan line that is
* in this cell, and a second that fills the cell until
* another one is closer:
* if !detect then {needed for "down" loop}
* for i := here to max do
* if closer then
* buffer[..., i] := this color
* detect := true
* break
* for i := i+1 to max do
* if closer then
* buffer[..., i] := this color
* else
* break
*
* In this implementation, each level will require the
* following variables. Variables labelled (l) are local to each
* procedure. The ? should be replaced with r, g, or b:
* cdist: The distance at the starting point.
* ?center: The value of this component of the color
* c?inc: The initial increment at the ?center position.
* ?stride: The amount to add to the buffer
* pointers (dp and rgbp) to get to the
* "next row".
* min(l): The "low edge" of the cell, init to 0
* max(l): The "high edge" of the cell, init to
* colormax-1
* detect(l): True if this row has changed some
* buffer entries.
* i(l): The index for this row.
* ?xx: The accumulated increment value.
*
* here(l): The starting index for this color. The
* following variables are associated with here,
* in the sense that they must be updated if here
* is changed.
* ?dist: The current distance for this level. The
* value of dist from the previous level (g or r,
* for level b or g) initializes dist on this
* level. Thus gdist is associated with here(b)).
* ?inc: The initial increment for the row.
* ?dp: Pointer into the distance buffer. The value
* from the previous level initializes this level.
* ?rgbp: Pointer into the rgb buffer. The value
* from the previous level initializes this level.
*
* The blue and green levels modify 'here-associated' variables (dp,
* rgbp, dist) on the green and red levels, respectively, when here is
* changed.
*/
void
inv_cmap( colors, colormap, bits, dist_buf, rgbmap )
int colors, bits;
unsigned char *colormap[3], *rgbmap;
unsigned long *dist_buf;
{
int nbits = 8 - bits;
colormax = 1 << bits;
x = 1 << nbits;
xsqr = 1 << (2 * nbits);
/* Compute "strides" for accessing the arrays. */
gstride = colormax;
rstride = colormax * colormax;
#ifdef INSTRUMENT_IT
outercount = 0;
innercount = 0;
#endif
maxfill( dist_buf, colormax );
for ( cindex = 0; cindex < colors; cindex++ )
{
/*
* Distance formula is
* (red - map[0])^2 + (green - map[1])^2 + (blue - map[2])^2
*
* Because of quantization, we will measure from the center of
* each quantized "cube", so blue distance is
* (blue + x/2 - map[2])^2,
* where x = 2^(8 - bits).
* The step size is x, so the blue increment is
* 2*x*blue - 2*x*map[2] + 2*x^2
*
* Now, b in the code below is actually blue/x, so our
* increment will be 2*(b*x^2 + x^2 - x*map[2]). For
* efficiency, we will maintain this quantity in a separate variable
* that will be updated incrementally by adding 2*x^2 each time.
*/
/* The initial position is the cell containing the colormap
* entry. We get this by quantizing the colormap values.
*/
rcenter = colormap[0][cindex] >> nbits;
gcenter = colormap[1][cindex] >> nbits;
bcenter = colormap[2][cindex] >> nbits;
#ifdef DEBUG
cred = colormap[0][cindex];
cgreen = colormap[1][cindex];
cblue = colormap[2][cindex];
fprintf( stderr, "---Starting %d: %d,%d,%d -> %d,%d,%d\n",
cindex, cred, cgreen, cblue,
rcenter, gcenter, bcenter );
zrgbp = rgbmap;
#endif
rdist = colormap[0][cindex] - (rcenter * x + x/2);
gdist = colormap[1][cindex] - (gcenter * x + x/2);
cdist = colormap[2][cindex] - (bcenter * x + x/2);
cdist = rdist*rdist + gdist*gdist + cdist*cdist;
crinc = 2 * ((rcenter + 1) * xsqr - (colormap[0][cindex] * x));
cginc = 2 * ((gcenter + 1) * xsqr - (colormap[1][cindex] * x));
cbinc = 2 * ((bcenter + 1) * xsqr - (colormap[2][cindex] * x));
/* Array starting points. */
cdp = dist_buf + rcenter * rstride + gcenter * gstride + bcenter;
crgbp = rgbmap + rcenter * rstride + gcenter * gstride + bcenter;
(void)redloop();
}
#ifdef INSTRUMENT_IT
fprintf( stderr, "K = %d, N = %d, outer count = %ld, inner count = %ld\n",
colors, colormax, outercount, innercount );
#endif
}
/* redloop -- loop up and down from red center. */
static int
redloop()
{
int detect;
int r;
int first;
long txsqr = xsqr + xsqr;
static long rxx;
detect = 0;
/* Basic loop up. */
for ( r = rcenter, rdist = cdist, rxx = crinc,
rdp = cdp, rrgbp = crgbp, first = 1;
r < colormax;
r++, rdp += rstride, rrgbp += rstride,
rdist += rxx, rxx += txsqr, first = 0 )
{
#ifdef DEBUG
red = r;
#endif
if ( greenloop( first ) )
detect = 1;
else if ( detect )
break;
}
/* Basic loop down. */
for ( r = rcenter - 1, rxx = crinc - txsqr, rdist = cdist - rxx,
rdp = cdp - rstride, rrgbp = crgbp - rstride, first = 1;
r >= 0;
r--, rdp -= rstride, rrgbp -= rstride,
rxx -= txsqr, rdist -= rxx, first = 0 )
{
#ifdef DEBUG
red = r;
#endif
if ( greenloop( first ) )
detect = 1;
else if ( detect )
break;
}
return detect;
}
/* greenloop -- loop up and down from green center. */
static int
greenloop( restart )
int restart;
{
int detect;
int g;
int first;
long txsqr = xsqr + xsqr;
static int here, min, max;
#ifdef MINMAX_TRACK
static int prevmax, prevmin;
int thismax, thismin;
#endif
static long ginc, gxx, gcdist; /* "gc" variables maintain correct */
static unsigned long *gcdp; /* values for bcenter position, */
static unsigned char *gcrgbp; /* despite modifications by blueloop */
/* to gdist, gdp, grgbp. */
if ( restart )
{
here = gcenter;
min = 0;
max = colormax - 1;
ginc = cginc;
#ifdef MINMAX_TRACK
prevmax = 0;
prevmin = colormax;
#endif
}
#ifdef MINMAX_TRACK
thismin = min;
thismax = max;
#endif
detect = 0;
/* Basic loop up. */
for ( g = here, gcdist = gdist = rdist, gxx = ginc,
gcdp = gdp = rdp, gcrgbp = grgbp = rrgbp, first = 1;
g <= max;
g++, gdp += gstride, gcdp += gstride, grgbp += gstride, gcrgbp += gstride,
gdist += gxx, gcdist += gxx, gxx += txsqr, first = 0 )
{
#ifdef DEBUG
green = g;
#endif
if ( blueloop( first ) )
{
if ( !detect )
{
/* Remember here and associated data! */
if ( g > here )
{
here = g;
rdp = gcdp;
rrgbp = gcrgbp;
rdist = gcdist;
ginc = gxx;
#ifdef MINMAX_TRACK
thismin = here;
#endif
#ifdef DEBUG
fprintf( stderr, "===Adjusting green here up at %d,%d\n",
red, here );
#endif
}
detect = 1;
}
}
else if ( detect )
{
#ifdef MINMAX_TRACK
thismax = g - 1;
#endif
break;
}
}
/* Basic loop down. */
for ( g = here - 1, gxx = ginc - txsqr, gcdist = gdist = rdist - gxx,
gcdp = gdp = rdp - gstride, gcrgbp = grgbp = rrgbp - gstride,
first = 1;
g >= min;
g--, gdp -= gstride, gcdp -= gstride, grgbp -= gstride, gcrgbp -= gstride,
gxx -= txsqr, gdist -= gxx, gcdist -= gxx, first = 0 )
{
#ifdef DEBUG
green = g;
#endif
if ( blueloop( first ) )
{
if ( !detect )
{
/* Remember here! */
here = g;
rdp = gcdp;
rrgbp = gcrgbp;
rdist = gcdist;
ginc = gxx;
#ifdef MINMAX_TRACK
thismax = here;
#endif
#ifdef DEBUG
fprintf( stderr, "===Adjusting green here down at %d,%d\n",
red, here );
#endif
detect = 1;
}
}
else if ( detect )
{
#ifdef MINMAX_TRACK
thismin = g + 1;
#endif
break;
}
}
#ifdef MINMAX_TRACK
/* If we saw something, update the edge trackers. For now, only
* tracks edges that are "shrinking" (min increasing, max
* decreasing.
*/
if ( detect )
{
if ( thismax < prevmax )
max = thismax;
prevmax = thismax;
if ( thismin > prevmin )
min = thismin;
prevmin = thismin;
}
#endif
return detect;
}
/* blueloop -- loop up and down from blue center. */
static int
blueloop( restart )
int restart;
{
int detect;
register unsigned long *dp;
register unsigned char *rgbp;
register unsigned long bdist;
register long bxx;
register int b, i = cindex;
register long txsqr = xsqr + xsqr;
register int lim;
static int here, min, max;
#ifdef MINMAX_TRACK
static int prevmin, prevmax;
int thismin, thismax;
#ifdef DMIN_DMAX_TRACK
static int dmin, dmax;
#endif /* DMIN_DMAX_TRACK */
#endif /* MINMAX_TRACK */
static long binc;
#ifdef DEBUG
long dist, tdist;
#endif
if ( restart )
{
here = bcenter;
min = 0;
max = colormax - 1;
binc = cbinc;
#ifdef MINMAX_TRACK
prevmin = colormax;
prevmax = 0;
#ifdef DMIN_DMAX_TRACK
dmin = 0;
dmax = 0;
#endif /* DMIN_DMAX_TRACK */
#endif /* MINMAX_TRACK */
}
detect = 0;
#ifdef MINMAX_TRACK
thismin = min;
thismax = max;
#endif
#ifdef DEBUG
tdist = cred - red * x - x/2;
dist = tdist*tdist;
tdist = cgreen - green * x - x/2;
dist += tdist*tdist;
tdist = cblue - here * x - x/2;
dist += tdist*tdist;
if ( gdist != dist )
fprintf( stderr, "*** At %d,%d,%d; dist = %ld != gdist = %ld\n",
red, green, here, dist, gdist );
if ( grgbp != zrgbp + red*rstride + green*gstride + here )
fprintf( stderr, "*** At %d,%d,%d: buffer pointer is at %d,%d,%d\n",
red, green, here,
(grgbp - zrgbp) / rstride,
((grgbp - zrgbp) % rstride) / gstride,
(grgbp - zrgbp) % gstride );
#endif /* DEBUG */
/* Basic loop up. */
/* First loop just finds first applicable cell. */
for ( b = here, bdist = gdist, bxx = binc, dp = gdp, rgbp = grgbp, lim = max;
b <= lim;
b++, dp++, rgbp++,
bdist += bxx, bxx += txsqr )
{
#ifdef INSTRUMENT_IT
outercount++;
#endif
if ( *dp > bdist )
{
/* Remember new 'here' and associated data! */
if ( b > here )
{
here = b;
gdp = dp;
grgbp = rgbp;
gdist = bdist;
binc = bxx;
#ifdef MINMAX_TRACK
thismin = here;
#endif
#ifdef DEBUG
fprintf( stderr, "===Adjusting blue here up at %d,%d,%d\n",
red, green, here );
tdist = cred - red * x - x/2;
dist = tdist*tdist;
tdist = cgreen - green * x - x/2;
dist += tdist*tdist;
tdist = cblue - here * x - x/2;
dist += tdist*tdist;
if ( gdist != dist )
fprintf( stderr,
"*** Adjusting here up at %d,%d,%d; dist = %ld != gdist = %ld\n",
red, green, here, dist, gdist );
#endif /* DEBUG */
}
detect = 1;
#ifdef INSTRUMENT_IT
outercount--;
#endif
break;
}
}
/* Second loop fills in a run of closer cells. */
for ( ;
b <= lim;
b++, dp++, rgbp++,
bdist += bxx, bxx += txsqr )
{
#ifdef INSTRUMENT_IT
outercount++;
#endif
if ( *dp > bdist )
{
#ifdef INSTRUMENT_IT
innercount++;
#endif
*dp = bdist;
*rgbp = i;
}
else
{
#ifdef MINMAX_TRACK
thismax = b - 1;
#endif
break;
}
}
#ifdef DEBUG
tdist = cred - red * x - x/2;
dist = tdist*tdist;
tdist = cgreen - green * x - x/2;
dist += tdist*tdist;
tdist = cblue - b * x - x/2;
dist += tdist*tdist;
if ( bdist != dist )
fprintf( stderr,
"*** After up loop at %d,%d,%d; dist = %ld != bdist = %ld\n",
red, green, b, dist, bdist );
#endif /* DEBUG */
/* Basic loop down. */
/* Do initializations here, since the 'find' loop might not get
* executed.
*/
lim = min;
b = here - 1;
bxx = binc - txsqr;
bdist = gdist - bxx;
dp = gdp - 1;
rgbp = grgbp - 1;
/* The 'find' loop is executed only if we didn't already find
* something.
*/
if ( !detect )
for ( ;
b >= lim;
b--, dp--, rgbp--,
bxx -= txsqr, bdist -= bxx )
{
#ifdef INSTRUMENT_IT
outercount++;
#endif
if ( *dp > bdist )
{
/* Remember here! */
/* No test for b against here necessary because b <
* here by definition.
*/
here = b;
gdp = dp;
grgbp = rgbp;
gdist = bdist;
binc = bxx;
#ifdef MINMAX_TRACK
thismax = here;
#endif
detect = 1;
#ifdef DEBUG
fprintf( stderr, "===Adjusting blue here down at %d,%d,%d\n",
red, green, here );
tdist = cred - red * x - x/2;
dist = tdist*tdist;
tdist = cgreen - green * x - x/2;
dist += tdist*tdist;
tdist = cblue - here * x - x/2;
dist += tdist*tdist;
if ( gdist != dist )
fprintf( stderr,
"*** Adjusting here down at %d,%d,%d; dist = %ld != gdist = %ld\n",
red, green, here, dist, gdist );
#endif /* DEBUG */
#ifdef INSTRUMENT_IT
outercount--;
#endif
break;
}
}
/* The 'update' loop. */
for ( ;
b >= lim;
b--, dp--, rgbp--,
bxx -= txsqr, bdist -= bxx )
{
#ifdef INSTRUMENT_IT
outercount++;
#endif
if ( *dp > bdist )
{
#ifdef INSTRUMENT_IT
innercount++;
#endif
*dp = bdist;
*rgbp = i;
}
else
{
#ifdef MINMAX_TRACK
thismin = b + 1;
#endif
break;
}
}
#ifdef DEBUG
tdist = cred - red * x - x/2;
dist = tdist*tdist;
tdist = cgreen - green * x - x/2;
dist += tdist*tdist;
tdist = cblue - b * x - x/2;
dist += tdist*tdist;
if ( bdist != dist )
fprintf( stderr,
"*** After down loop at %d,%d,%d; dist = %ld != bdist = %ld\n",
red, green, b, dist, bdist );
#endif /* DEBUG */
/* If we saw something, update the edge trackers. */
#ifdef MINMAX_TRACK
if ( detect )
{
#ifdef DMIN_DMAX_TRACK
/* Predictively track. Not clear this is a win. */
/* If there was a previous line, update the dmin/dmax values. */
if ( prevmax >= prevmin )
{
if ( thismin > 0 )
dmin = thismin - prevmin - 1;
else
dmin = 0;
if ( thismax < colormax - 1 )
dmax = thismax - prevmax + 1;
else
dmax = 0;
/* Update the min and max values by the differences. */
max = thismax + dmax;
if ( max >= colormax )
max = colormax - 1;
min = thismin + dmin;
if ( min < 0 )
min = 0;
}
#else /* !DMIN_DMAX_TRACK */
/* Only tracks edges that are "shrinking" (min increasing, max
* decreasing.
*/
if ( thismax < prevmax )
max = thismax;
if ( thismin > prevmin )
min = thismin;
#endif /* DMIN_DMAX_TRACK */
/* Remember the min and max values. */
prevmax = thismax;
prevmin = thismin;
}
#endif /* MINMAX_TRACK */
return detect;
}
static void
maxfill( buffer, side )
unsigned long *buffer;
long side;
{
register unsigned long maxv = ~0L;
register long i;
register unsigned long *bp;
for ( i = colormax * colormax * colormax, bp = buffer;
i > 0;
i--, bp++ )
*bp = maxv;
}
#else /* !NO_INV_CMAP_TRACKING */
/*****************************************************************
* TAG( inv_cmap )
*
* Compute an inverse colormap efficiently.
* Inputs:
* colors: Number of colors in the forward colormap.
* colormap: The forward colormap.
* bits: Number of quantization bits. The inverse
* colormap will have (2^bits)^3 entries.
* dist_buf: An array of (2^bits)^3 long integers to be
* used as scratch space.
* Outputs:
* rgbmap: The output inverse colormap. The entry
* rgbmap[(r<<(2*bits)) + (g<<bits) + b]
* is the colormap entry that is closest to the
* (quantized) color (r,g,b).
* Assumptions:
* Quantization is performed by right shift (low order bits are
* truncated). Thus, the distance to a quantized color is
* actually measured to the color at the center of the cell
* (i.e., to r+.5, g+.5, b+.5, if (r,g,b) is a quantized color).
* Algorithm:
* Uses a "distance buffer" algorithm:
* The distance from each representative in the forward color map
* to each point in the rgb space is computed. If it is less
* than the distance currently stored in dist_buf, then the
* corresponding entry in rgbmap is replaced with the current
* representative (and the dist_buf entry is replaced with the
* new distance).
*
* The distance computation uses an efficient incremental formulation.
*
* Right now, distances are computed for all entries in the rgb
* space. Thus, the complexity of the algorithm is O(K N^3),
* where K = colors, and N = 2^bits.
*/
void
inv_cmap( colors, colormap, bits, dist_buf, rgbmap )
int colors, bits;
unsigned char *colormap[3], *rgbmap;
unsigned long *dist_buf;
{
register unsigned long *dp;
register unsigned char *rgbp;
register long bdist, bxx;
register int b, i;
int nbits = 8 - bits;
register int colormax = 1 << bits;
register long xsqr = 1 << (2 * nbits);
int x = 1 << nbits;
int rinc, ginc, binc, r, g;
long rdist, gdist, rxx, gxx;
#ifdef INSTRUMENT_IT
long outercount = 0, innercount = 0;
#endif
for ( i = 0; i < colors; i++ )
{
/*
* Distance formula is
* (red - map[0])^2 + (green - map[1])^2 + (blue - map[2])^2
*
* Because of quantization, we will measure from the center of
* each quantized "cube", so blue distance is
* (blue + x/2 - map[2])^2,
* where x = 2^(8 - bits).
* The step size is x, so the blue increment is
* 2*x*blue - 2*x*map[2] + 2*x^2
*
* Now, b in the code below is actually blue/x, so our
* increment will be 2*x*x*b + (2*x^2 - 2*x*map[2]). For
* efficiency, we will maintain this quantity in a separate variable
* that will be updated incrementally by adding 2*x^2 each time.
*/
rdist = colormap[0][i] - x/2;
gdist = colormap[1][i] - x/2;
bdist = colormap[2][i] - x/2;
rdist = rdist*rdist + gdist*gdist + bdist*bdist;
rinc = 2 * (xsqr - (colormap[0][i] << nbits));
ginc = 2 * (xsqr - (colormap[1][i] << nbits));
binc = 2 * (xsqr - (colormap[2][i] << nbits));
dp = dist_buf;
rgbp = rgbmap;
for ( r = 0, rxx = rinc;
r < colormax;
rdist += rxx, r++, rxx += xsqr + xsqr )
for ( g = 0, gdist = rdist, gxx = ginc;
g < colormax;
gdist += gxx, g++, gxx += xsqr + xsqr )
for ( b = 0, bdist = gdist, bxx = binc;
b < colormax;
bdist += bxx, b++, dp++, rgbp++,
bxx += xsqr + xsqr )
{
#ifdef INSTRUMENT_IT
outercount++;
#endif
if ( i == 0 || *dp > bdist )
{
#ifdef INSTRUMENT_IT
innercount++;
#endif
*dp = bdist;
*rgbp = i;
}
}
}
#ifdef INSTRUMENT_IT
fprintf( stderr, "K = %d, N = %d, outer count = %ld, inner count = %ld\n",
colors, colormax, outercount, innercount );
#endif
}
#endif /* NO_INV_CMAP_TRACKING */