annotate src/imgproc.c @ 269:b2472a1930f2 r20-5b33

Import from CVS: tag r20-5b33
author cvs
date Mon, 13 Aug 2007 10:27:19 +0200
parents 966663fcf606
children c5d627a313b1
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
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
47 static void
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
48 get_histogram(quant_table *qt, unsigned char *pic,
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
49 int width, int height, Colorbox* box)
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
50 {
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
51 register unsigned char *inptr;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
52 register int red, green, blue;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
53 register unsigned int j, i;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
54
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
55 box->rmin = box->gmin = box->bmin = 999;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
56 box->rmax = box->gmax = box->bmax = -1;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
57 box->total = width * height;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
58
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
59 inptr = pic;
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
60 for (i = 0; i < height; i++)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
61 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
62 for (j = width; j-- > 0;)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
63 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
64 red = *inptr++ >> COLOR_SHIFT;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
65 green = *inptr++ >> COLOR_SHIFT;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
66 blue = *inptr++ >> COLOR_SHIFT;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
67 if (red < box->rmin)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
68 box->rmin = red;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
69 if (red > box->rmax)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
70 box->rmax = red;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
71 if (green < box->gmin)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
72 box->gmin = green;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
73 if (green > box->gmax)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
74 box->gmax = green;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
75 if (blue < box->bmin)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
76 box->bmin = blue;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
77 if (blue > box->bmax)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
78 box->bmax = blue;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
79 qt->histogram[red][green][blue]++;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
80 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
81 }
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 static Colorbox *
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
85 largest_box(quant_table *qt)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
86 {
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
87 register Colorbox *p, *b;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
88 register int size;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
89
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
90 b = NULL;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
91 size = -1;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
92 for (p = qt->usedboxes; p != NULL; p = p->next)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
93 if ((p->rmax > p->rmin || p->gmax > p->gmin ||
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
94 p->bmax > p->bmin) && p->total > size)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
95 size = (b = p)->total;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
96 return (b);
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
97 }
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
98
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
99 static void
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
100 shrinkbox(quant_table *qt, Colorbox* box)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
101 {
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
102 register int *histp, ir, ig, ib;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
103
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
104 if (box->rmax > box->rmin)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
105 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
106 for (ir = box->rmin; ir <= box->rmax; ++ir)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
107 for (ig = box->gmin; ig <= box->gmax; ++ig)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
108 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
109 histp = &(qt->histogram[ir][ig][box->bmin]);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
110 for (ib = box->bmin; ib <= box->bmax; ++ib)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
111 if (*histp++ != 0)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
112 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
113 box->rmin = ir;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
114 goto have_rmin;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
115 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
116 }
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
117 have_rmin:
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
118 if (box->rmax > box->rmin)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
119 for (ir = box->rmax; ir >= box->rmin; --ir)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
120 for (ig = box->gmin; ig <= box->gmax; ++ig)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
121 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
122 histp = &(qt->histogram[ir][ig][box->bmin]);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
123 ib = box->bmin;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
124 for (; ib <= box->bmax; ++ib)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
125 if (*histp++ != 0)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
126 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
127 box->rmax = ir;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
128 goto have_rmax;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
129 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
130 }
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
131 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
132 have_rmax:
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
133 if (box->gmax > box->gmin)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
134 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
135 for (ig = box->gmin; ig <= box->gmax; ++ig)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
136 for (ir = box->rmin; ir <= box->rmax; ++ir)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
137 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
138 histp = &(qt->histogram[ir][ig][box->bmin]);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
139 for (ib = box->bmin; ib <= box->bmax; ++ib)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
140 if (*histp++ != 0)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
141 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
142 box->gmin = ig;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
143 goto have_gmin;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
144 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
145 }
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
146 have_gmin:
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
147 if (box->gmax > box->gmin)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
148 for (ig = box->gmax; ig >= box->gmin; --ig)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
149 for (ir = box->rmin; ir <= box->rmax; ++ir)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
150 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
151 histp = &(qt->histogram[ir][ig][box->bmin]);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
152 ib = box->bmin;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
153 for (; ib <= box->bmax; ++ib)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
154 if (*histp++ != 0)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
155 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
156 box->gmax = ig;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
157 goto have_gmax;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
158 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
159 }
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
160 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
161 have_gmax:
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
162 if (box->bmax > box->bmin)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
163 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
164 for (ib = box->bmin; ib <= box->bmax; ++ib)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
165 for (ir = box->rmin; ir <= box->rmax; ++ir)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
166 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
167 histp = &(qt->histogram[ir][box->gmin][ib]);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
168 for (ig = box->gmin; ig <= box->gmax; ++ig)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
169 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
170 if (*histp != 0)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
171 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
172 box->bmin = ib;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
173 goto have_bmin;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
174 }
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
175 histp += B_LEN;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
176 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
177 }
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
178 have_bmin:
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
179 if (box->bmax > box->bmin)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
180 for (ib = box->bmax; ib >= box->bmin; --ib)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
181 for (ir = box->rmin; ir <= box->rmax; ++ir)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
182 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
183 histp = &(qt->histogram[ir][box->gmin][ib]);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
184 ig = box->gmin;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
185 for (; ig <= box->gmax; ++ig)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
186 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
187 if (*histp != 0)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
188 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
189 box->bmax = ib;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
190 goto have_bmax;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
191 }
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
192 histp += B_LEN;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
193 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
194 }
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
195 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
196 have_bmax:
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
197 ;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
198 }
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
199
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
200 static void
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
201 splitbox(quant_table *qt, Colorbox* ptr)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
202 {
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
203 int hist2[B_LEN];
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
204 int first, last;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
205 register Colorbox *new;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
206 register int *iptr, *histp;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
207 register int i, j;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
208 register int ir,ig,ib;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
209 register int sum, sum1, sum2;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
210 enum { RED, GREEN, BLUE } axis;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
211
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
212 /*
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
213 * See which axis is the largest, do a histogram along that
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
214 * axis. Split at median point. Contract both new boxes to
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
215 * fit points and return
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
216 */
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
217 i = ptr->rmax - ptr->rmin;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
218 if (i >= ptr->gmax - ptr->gmin && i >= ptr->bmax - ptr->bmin)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
219 axis = RED;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
220 else if (ptr->gmax - ptr->gmin >= ptr->bmax - ptr->bmin)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
221 axis = GREEN;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
222 else
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
223 axis = BLUE;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
224 /* get histogram along longest axis */
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
225 switch (axis)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
226 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
227 case RED:
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
228 histp = &hist2[ptr->rmin];
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
229 for (ir = ptr->rmin; ir <= ptr->rmax; ++ir)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
230 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
231 *histp = 0;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
232 for (ig = ptr->gmin; ig <= ptr->gmax; ++ig)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
233 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
234 iptr = &(qt->histogram[ir][ig][ptr->bmin]);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
235 for (ib = ptr->bmin; ib <= ptr->bmax; ++ib)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
236 *histp += *iptr++;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
237 }
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
238 histp++;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
239 }
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
240 first = ptr->rmin;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
241 last = ptr->rmax;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
242 break;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
243 case GREEN:
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
244 histp = &hist2[ptr->gmin];
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
245 for (ig = ptr->gmin; ig <= ptr->gmax; ++ig)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
246 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
247 *histp = 0;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
248 for (ir = ptr->rmin; ir <= ptr->rmax; ++ir)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
249 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
250 iptr = &(qt->histogram[ir][ig][ptr->bmin]);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
251 for (ib = ptr->bmin; ib <= ptr->bmax; ++ib)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
252 *histp += *iptr++;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
253 }
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
254 histp++;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
255 }
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
256 first = ptr->gmin;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
257 last = ptr->gmax;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
258 break;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
259 case BLUE:
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
260 histp = &hist2[ptr->bmin];
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
261 for (ib = ptr->bmin; ib <= ptr->bmax; ++ib)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
262 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
263 *histp = 0;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
264 for (ir = ptr->rmin; ir <= ptr->rmax; ++ir)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
265 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
266 iptr = &(qt->histogram[ir][ptr->gmin][ib]);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
267 for (ig = ptr->gmin; ig <= ptr->gmax; ++ig)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
268 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
269 *histp += *iptr;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
270 iptr += B_LEN;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
271 }
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
272 }
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
273 histp++;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
274 }
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
275 first = ptr->bmin;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
276 last = ptr->bmax;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
277 break;
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
278 }
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
279 /* find median point */
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
280 sum2 = ptr->total / 2;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
281 histp = &hist2[first];
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
282 sum = 0;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
283 for (i = first; i <= last && (sum += *histp++) < sum2; ++i)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
284 ;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
285 if (i == first)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
286 i++;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
287
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
288 /* Create new box, re-allocate points */
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
289 new = qt->freeboxes;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
290 qt->freeboxes = new->next;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
291 if (qt->freeboxes)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
292 qt->freeboxes->prev = NULL;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
293 if (qt->usedboxes)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
294 qt->usedboxes->prev = new;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
295 new->next = qt->usedboxes;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
296 qt->usedboxes = new;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
297
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
298 histp = &hist2[first];
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
299 for (sum1 = 0, j = first; j < i; j++)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
300 sum1 += *histp++;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
301 for (sum2 = 0, j = i; j <= last; j++)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
302 sum2 += *histp++;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
303 new->total = sum1;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
304 ptr->total = sum2;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
305
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
306 new->rmin = ptr->rmin;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
307 new->rmax = ptr->rmax;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
308 new->gmin = ptr->gmin;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
309 new->gmax = ptr->gmax;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
310 new->bmin = ptr->bmin;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
311 new->bmax = ptr->bmax;
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
312 switch (axis)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
313 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
314 case RED:
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
315 new->rmax = i-1;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
316 ptr->rmin = i;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
317 break;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
318 case GREEN:
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
319 new->gmax = i-1;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
320 ptr->gmin = i;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
321 break;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
322 case BLUE:
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
323 new->bmax = i-1;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
324 ptr->bmin = i;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
325 break;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
326 }
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
327 shrinkbox (qt, new);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
328 shrinkbox (qt, ptr);
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
329 }
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
330
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
331
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
332 static C_cell *
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
333 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
334 {
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
335 register int ir, ig, ib, i;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
336 register C_cell *ptr;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
337 int mindist, next_n;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
338 register int tmp, dist, n;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
339
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
340 ir = red >> (COLOR_DEPTH-C_DEPTH);
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
341 ig = green >> (COLOR_DEPTH-C_DEPTH);
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
342 ib = blue >> (COLOR_DEPTH-C_DEPTH);
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
343 ptr = (C_cell *)xmalloc(sizeof (C_cell));
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
344 *(qt->ColorCells + ir*C_LEN*C_LEN + ig*C_LEN + ib) = ptr;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
345 ptr->num_ents = 0;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
346
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
347 /*
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
348 * Step 1: find all colors inside this cell, while we're at
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
349 * it, find distance of centermost point to furthest corner
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
350 */
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
351 mindist = 99999999;
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
352 for (i = 0; i < num_colors; ++i)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
353 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
354 if (qt->rm[i]>>(COLOR_DEPTH-C_DEPTH) != ir ||
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
355 qt->gm[i]>>(COLOR_DEPTH-C_DEPTH) != ig ||
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
356 qt->bm[i]>>(COLOR_DEPTH-C_DEPTH) != ib)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
357 continue;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
358 ptr->entries[ptr->num_ents][0] = i;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
359 ptr->entries[ptr->num_ents][1] = 0;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
360 ++ptr->num_ents;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
361 tmp = qt->rm[i] - red;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
362 if (tmp < (MAX_COLOR/C_LEN/2))
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
363 tmp = MAX_COLOR/C_LEN-1 - tmp;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
364 dist = tmp*tmp;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
365 tmp = qt->gm[i] - green;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
366 if (tmp < (MAX_COLOR/C_LEN/2))
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
367 tmp = MAX_COLOR/C_LEN-1 - tmp;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
368 dist += tmp*tmp;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
369 tmp = qt->bm[i] - blue;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
370 if (tmp < (MAX_COLOR/C_LEN/2))
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
371 tmp = MAX_COLOR/C_LEN-1 - tmp;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
372 dist += tmp*tmp;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
373 if (dist < mindist)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
374 mindist = dist;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
375 }
265
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 * Step 3: find all points within that distance to cell.
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
379 */
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
380 for (i = 0; i < num_colors; ++i)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
381 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
382 if (qt->rm[i] >> (COLOR_DEPTH-C_DEPTH) == ir &&
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
383 qt->gm[i] >> (COLOR_DEPTH-C_DEPTH) == ig &&
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
384 qt->bm[i] >> (COLOR_DEPTH-C_DEPTH) == ib)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
385 continue;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
386 dist = 0;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
387 if ((tmp = red - qt->rm[i]) > 0 ||
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
388 (tmp = qt->rm[i] - (red + MAX_COLOR/C_LEN-1)) > 0 )
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
389 dist += tmp*tmp;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
390 if ((tmp = green - qt->gm[i]) > 0 ||
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
391 (tmp = qt->gm[i] - (green + MAX_COLOR/C_LEN-1)) > 0 )
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
392 dist += tmp*tmp;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
393 if ((tmp = blue - qt->bm[i]) > 0 ||
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
394 (tmp = qt->bm[i] - (blue + MAX_COLOR/C_LEN-1)) > 0 )
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
395 dist += tmp*tmp;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
396 if (dist < mindist)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
397 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
398 ptr->entries[ptr->num_ents][0] = i;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
399 ptr->entries[ptr->num_ents][1] = dist;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
400 ++ptr->num_ents;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
401 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
402 }
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
403
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
404 /*
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
405 * Sort color cells by distance, use cheap exchange sort
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
406 */
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
407 for (n = ptr->num_ents - 1; n > 0; n = next_n)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
408 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
409 next_n = 0;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
410 for (i = 0; i < n; ++i)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
411 if (ptr->entries[i][1] > ptr->entries[i+1][1])
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
412 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
413 tmp = ptr->entries[i][0];
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
414 ptr->entries[i][0] = ptr->entries[i+1][0];
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
415 ptr->entries[i+1][0] = tmp;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
416 tmp = ptr->entries[i][1];
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
417 ptr->entries[i][1] = ptr->entries[i+1][1];
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
418 ptr->entries[i+1][1] = tmp;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
419 next_n = i;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
420 }
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
421 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
422 return (ptr);
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
423 }
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
424
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
425 static int
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
426 map_colortable(quant_table *qt, int num_colors)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
427 {
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
428 register int *histp = &(qt->histogram[0][0][0]);
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
429 register C_cell *cell;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
430 register int j, tmp, d2, dist;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
431 int ir, ig, ib, i;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
432
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
433 for (ir = 0; ir < B_LEN; ++ir)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
434 for (ig = 0; ig < B_LEN; ++ig)
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
435 for (ib = 0; ib < B_LEN; ++ib, histp++)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
436 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
437 if (*histp == 0)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
438 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
439 *histp = -1;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
440 continue;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
441 }
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
442 cell = *(qt->ColorCells +
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
443 (((ir>>(B_DEPTH-C_DEPTH)) << C_DEPTH*2) +
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
444 ((ig>>(B_DEPTH-C_DEPTH)) << C_DEPTH) +
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
445 (ib>>(B_DEPTH-C_DEPTH))));
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
446 if (cell == NULL )
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
447 cell = create_colorcell (qt, num_colors,
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
448 ir << COLOR_SHIFT,
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
449 ig << COLOR_SHIFT,
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
450 ib << COLOR_SHIFT);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
451 if (cell == NULL) /* memory exhausted! punt! */
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
452 return -1;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
453 dist = 9999999;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
454 for (i = 0; i < cell->num_ents &&
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
455 dist > cell->entries[i][1]; ++i)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
456 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
457 j = cell->entries[i][0];
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
458 d2 = qt->rm[j] - (ir << COLOR_SHIFT);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
459 d2 *= d2;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
460 tmp = qt->gm[j] - (ig << COLOR_SHIFT);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
461 d2 += tmp*tmp;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
462 tmp = qt->bm[j] - (ib << COLOR_SHIFT);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
463 d2 += tmp*tmp;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
464 if (d2 < dist)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
465 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
466 dist = d2;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
467 *histp = j;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
468 }
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
469 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
470 }
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
471 return 0;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
472 }
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
473
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
474 quant_table *
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
475 build_EImage_quantable(unsigned char *eimage, int width, int height, int num_colors)
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
476 {
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
477 quant_table *qt;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
478 Colorbox *box_list, *ptr;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
479 int i,res;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
480
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
481 qt = (quant_table*)xmalloc_and_zero (sizeof(quant_table));
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
482 if (qt == NULL) return NULL;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
483
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
484 assert (num_colors < 257 && num_colors > 2);
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
485 /*
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
486 * STEP 1: create empty boxes
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
487 */
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
488 qt->usedboxes = NULL;
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
489 box_list = qt->freeboxes = (Colorbox *)xmalloc (num_colors*sizeof (Colorbox));
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
490 qt->freeboxes[0].next = &(qt->freeboxes[1]);
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
491 qt->freeboxes[0].prev = NULL;
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
492 for (i = 1; i < num_colors-1; ++i)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
493 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
494 qt->freeboxes[i].next = &(qt->freeboxes[i+1]);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
495 qt->freeboxes[i].prev = &(qt->freeboxes[i-1]);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
496 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
497 qt->freeboxes[num_colors-1].next = NULL;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
498 qt->freeboxes[num_colors-1].prev = &(qt->freeboxes[num_colors-2]);
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
499
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
500 /*
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
501 * STEP 2: get histogram, initialize first box
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
502 */
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
503 ptr = qt->freeboxes;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
504 qt->freeboxes = ptr->next;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
505 if (qt->freeboxes)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
506 qt->freeboxes->prev = NULL;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
507 ptr->next = qt->usedboxes;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
508 qt->usedboxes = ptr;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
509 if (ptr->next)
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
510 ptr->next->prev = ptr;
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
511 get_histogram (qt, eimage, width, height, ptr);
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
512
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
513 /*
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
514 * STEP 3: continually subdivide boxes until no more free
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
515 * boxes remain or until all colors assigned.
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
516 */
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
517 while (qt->freeboxes != NULL)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
518 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
519 ptr = largest_box(qt);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
520 if (ptr != NULL)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
521 splitbox (qt, ptr);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
522 else
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
523 qt->freeboxes = NULL;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
524 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
525
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
526 /*
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
527 * STEP 4: assign colors to all boxes
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
528 */
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
529 for (i = 0, ptr = qt->usedboxes; ptr != NULL; ++i, ptr = ptr->next)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
530 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
531 qt->rm[i] = ((ptr->rmin + ptr->rmax) << COLOR_SHIFT) / 2;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
532 qt->gm[i] = ((ptr->gmin + ptr->gmax) << COLOR_SHIFT) / 2;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
533 qt->bm[i] = ((ptr->bmin + ptr->bmax) << COLOR_SHIFT) / 2;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
534 qt->um[i] = ptr->total;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
535 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
536 qt->num_active_colors = i;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
537
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
538 /* We're done with the boxes now */
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
539 xfree (box_list);
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
540 qt->freeboxes = qt->usedboxes = NULL;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
541
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
542 /*
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
543 * STEP 5: scan histogram and map all values to closest color
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
544 */
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
545 /* 5a: create cell list as described in Heckbert */
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
546 qt->ColorCells = (C_cell **)xmalloc_and_zero (C_LEN*C_LEN*C_LEN*sizeof (C_cell*));
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
547 /* 5b: create mapping from truncated pixel space to color
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
548 table entries */
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
549 res = map_colortable (qt, num_colors);
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
550
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
551 /* 5c: done with ColorCells */
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
552 for (i = 0; i < C_LEN*C_LEN*C_LEN; i++)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
553 if (qt->ColorCells[i]) xfree (qt->ColorCells[i]);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
554 xfree (qt->ColorCells);
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
555
269
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
556 if (res)
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
557 {
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
558 /* we failed in memory allocation, so clean up an leave */
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
559 xfree(qt);
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
560 return NULL;
b2472a1930f2 Import from CVS: tag r20-5b33
cvs
parents: 267
diff changeset
561 }
265
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
562
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
563 return qt;
8efd647ea9ca Import from CVS: tag r20-5b31
cvs
parents:
diff changeset
564 }