annotate src/imgproc.c @ 5284:d27c1ee1943b

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