annotate src/imgproc.c @ 267:966663fcf606 r20-5b32

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