1188
|
1 /*
|
|
2 * SetMap - Palette creator for ViewGIF 2.0
|
|
3 * These routines are the heart of the color analysis. They take the
|
|
4 * global bitmap and, based on the dithering factor, produce a CoCo color
|
|
5 * palette.
|
|
6 */
|
|
7
|
|
8 #include "viewgif.h"
|
|
9
|
|
10 int toler2(), exact();
|
|
11 bool nearcolor();
|
|
12
|
|
13 /*
|
|
14 * setmap() -- determine whether, given the specified dithering factor,
|
|
15 * there are CLUTs for the screens we've been told to use that will let
|
|
16 * us come reasonably close to representing the colors in the GIF image.
|
|
17 * We change the semantics from the original version, returning FALSE
|
|
18 * if not all the CLUT etnries can be fit in, or TRUE if they all work.
|
|
19 *
|
|
20 * Method: look for approximations to each color in use in terms of colors
|
|
21 * already in the CLUTs. If none is close enough, then add the color
|
|
22 * (or some decomposition thereof, in the multiple screen case) to the
|
|
23 * CLUTs. If there's no room left to add the color, we return FALSE.
|
|
24 *
|
|
25 * Note: we're trying to trade space for speed in the innermost loop of
|
|
26 * doline() by always leaving an end marker, avoiding double testing in
|
|
27 * said loop. We'll see how it works out.
|
|
28 */
|
|
29 bool
|
|
30 setmap()
|
|
31 {
|
|
32 rgbcolor next;
|
|
33 register int add;
|
|
34 register rgbcolor *gcscan;
|
|
35 register xlate *trscan;
|
|
36 int numtrans, tolrnce, x, uplim, lowlim;
|
|
37
|
|
38 /* should be a loop if we really do change NSCREENS */
|
|
39 screen[0].clutsize = screen[1].clutsize = 0;
|
|
40
|
|
41 lowlim = low0;
|
|
42 uplim = up0;
|
|
43
|
|
44 if (dfactor > 0) {
|
|
45 tolrnce = 0;
|
|
46 lowlim -= dfactor;
|
|
47 } else
|
|
48 tolrnce = -dfactor;
|
|
49
|
|
50 if (dfactor > (int) flicker)
|
|
51 uplim += dfactor;
|
|
52
|
|
53 for (gcscan = globclut, x = 0; x < globcolors; gcscan++, x++) {
|
|
54 if (coloruse[x]) {
|
|
55 numtrans = 0;
|
|
56 trscan = &transtab[x][0];
|
|
57 for (add = lowlim; add < uplim; add = minadd(gcscan, add)) {
|
|
58 trscan->addval = add;
|
|
59 addoff(&next, gcscan, add);
|
|
60 if (!(*approx)(&next, trscan, tolrnce))
|
|
61 return FALSE;
|
|
62 if (++numtrans > 4)
|
|
63 fatal("BUG: numtrans>4");
|
|
64 trscan++;
|
|
65 }
|
|
66 /* mark end of transtab entries for this color */
|
|
67 trscan->addval = BOGUSDITH;
|
|
68 }
|
|
69 }
|
|
70 return TRUE;
|
|
71 }
|
|
72
|
|
73 addoff(color, color0, offset)
|
|
74 register BYTE *color, *color0;
|
|
75 int offset;
|
|
76 {
|
|
77 register int accum, x;
|
|
78
|
|
79 for (x = 3; --x >= 0; ) {
|
|
80 if ((accum = arith(*color0++) + offset) > 0xff)
|
|
81 accum = 0xff;
|
|
82 *color++ = accum;
|
|
83 }
|
|
84 }
|
|
85
|
|
86 /*
|
|
87 * approx1() -- the approximation seeker if there's only one screen
|
|
88 */
|
|
89 bool
|
|
90 approx1(goal, trans, toler)
|
|
91 rgbcolor *goal;
|
|
92 xlate *trans;
|
|
93 int toler;
|
|
94 {
|
|
95 register rgbcolor *cscan, *nearest;
|
|
96 int x, mintoler, tv;
|
|
97
|
|
98 mintoler = toler + 1;
|
|
99 nearest = NULL;
|
|
100 for (cscan = screen[0].clut, x = screen[0].clutsize; --x >= 0; cscan++) {
|
|
101 if ((tv = tolerval(cscan, goal)) < mintoler) {
|
|
102 nearest = cscan;
|
|
103 if ((mintoler = tv) == 0)
|
|
104 break;
|
|
105 }
|
|
106 }
|
|
107
|
|
108 if (nearest == NULL) {
|
|
109 if (++screen[0].clutsize > MCCLUT)
|
|
110 return FALSE;
|
|
111 cscan->red = arith(goal->red) / SCALE1;
|
|
112 cscan->green = arith(goal->green) / SCALE1;
|
|
113 cscan->blue = arith(goal->blue) / SCALE1;
|
|
114 nearest = cscan;
|
|
115 }
|
|
116 trans->clutval[0] = nearest - screen[0].clut;
|
|
117 return TRUE;
|
|
118 }
|
|
119
|
|
120 /*
|
|
121 * approx2() -- a two-screen approximator
|
|
122 *
|
|
123 * The general idea here takes multiple passes:
|
|
124 * 1. Iterate over sums of pairs of colors taken one from each screen.
|
|
125 * 2. If that fails, then for each screen, try adding something that
|
|
126 * should come close with each of the colors in the CLUT for the
|
|
127 * other screen. (Try it with the smallest CLUT first, to save space.)
|
|
128 * 3. If *that* fails, extend both CLUTs with the new color (at half
|
|
129 * intensity).
|
|
130 *
|
|
131 * Since in step 1, which one would think eats the most time, viewgif was
|
|
132 * making repeated calls to nearcolor(), we've switched over to keeping
|
|
133 * a table remembering which colors in the palettes are near as determined
|
|
134 * by nearcolor(). (Donald Michie would be proud of us.)
|
|
135 */
|
|
136
|
|
137 static bool neartab[MCCLUT][MCCLUT];
|
|
138
|
|
139 #define ROW 1
|
|
140 #define COLUMN 2
|
|
141
|
|
142 bool
|
|
143 approx2(goal, trans, toler)
|
|
144 rgbcolor *goal;
|
|
145 xlate *trans;
|
|
146 int toler;
|
|
147 {
|
|
148 register int c1, c0;
|
|
149 rgbcolor *scan0, *scan1, *near0, *near1;
|
|
150 cocoscreen *extend, *exam, *temp;
|
|
151 int (*tolfun)();
|
|
152 char *near_row;
|
|
153 rgbcolor scalergb;
|
|
154 int mintoler, tv;
|
|
155 int x;
|
|
156 bool found;
|
|
157
|
|
158 scalergb.red = arith(goal->red) / SCALE2;
|
|
159 scalergb.green = arith(goal->green) / SCALE2;
|
|
160 scalergb.blue = arith(goal->blue) / SCALE2;
|
|
161
|
|
162 if (toler > 0)
|
|
163 tolfun = toler2;
|
|
164 else {
|
|
165 goal = &scalergb;
|
|
166 tolfun = exact;
|
|
167 }
|
|
168
|
|
169 found = FALSE;
|
|
170 mintoler = toler + 1;
|
|
171
|
|
172 for (scan0 = &screen[0].clut[c0 = screen[0].clutsize];
|
|
173 scan0--, --c0 >= 0; )
|
|
174 {
|
|
175 near_row = neartab[c0];
|
|
176 for (c1 = screen[1].clutsize; --c1 >= 0; ) {
|
|
177 if (near_row[c1]) {
|
|
178 scan1 = &screen[1].clut[c1];
|
|
179 if ((tv = (*tolfun)(scan0, scan1, goal)) < mintoler) {
|
|
180 near0 = scan0;
|
|
181 near1 = scan1;
|
|
182 found = TRUE;
|
|
183 if ((mintoler = tv) == 0)
|
|
184 goto out;
|
|
185 }
|
|
186 }
|
|
187 }
|
|
188 }
|
|
189
|
|
190 out:
|
|
191 if (!found) {
|
|
192 if (screen[1].clutsize < screen[0].clutsize) {
|
|
193 extend = &screen[1];
|
|
194 exam = &screen[0];
|
|
195 } else {
|
|
196 extend = &screen[0];
|
|
197 exam = &screen[1];
|
|
198 }
|
|
199 for (x = 2; --x >= 0; ) {
|
|
200 if (extend->clutsize < MCCLUT) {
|
|
201 near0 = &extend->clut[extend->clutsize];
|
|
202 for (scan1 = &exam->clut[c1 = exam->clutsize];
|
|
203 scan1--, --c1 >= 0; )
|
|
204 {
|
|
205 forcenear(near0, scan1, &scalergb);
|
|
206 if ((tv = (*tolfun)(near0, scan1, goal)) < mintoler) {
|
|
207 near1 = scan1;
|
|
208 found = TRUE;
|
|
209 if ((mintoler = tv) == 0)
|
|
210 break;
|
|
211 }
|
|
212 }
|
|
213 if (found)
|
|
214 break;
|
|
215 }
|
|
216 temp = extend;
|
|
217 extend = exam;
|
|
218 exam = temp;
|
|
219 }
|
|
220
|
|
221 if (found) {
|
|
222 if (mintoler > 0)
|
|
223 forcenear(near0, near1, &scalergb);
|
|
224 extend->clutsize++;
|
|
225 if (extend == &screen[0])
|
|
226 nearfill(ROW);
|
|
227 else {
|
|
228 temp = (cocoscreen *) near0;
|
|
229 near0 = near1;
|
|
230 near1 = (rgbcolor *) temp;
|
|
231 nearfill(COLUMN);
|
|
232 }
|
|
233 } else {
|
|
234 if (screen[0].clutsize >= MCCLUT ||
|
|
235 screen[1].clutsize >= MCCLUT)
|
|
236 return FALSE;
|
|
237 near0 = &screen[0].clut[screen[0].clutsize++];
|
|
238 near0->red = arith(scalergb.red) / 2;
|
|
239 near0->green = arith(scalergb.green) / 2;
|
|
240 near0->blue = arith(scalergb.blue) / 2;
|
|
241 near1 = &screen[1].clut[screen[1].clutsize++];
|
|
242 near1->red = (arith(scalergb.red) + 1) / 2;
|
|
243 near1->green = (arith(scalergb.green) + 1) / 2;
|
|
244 near1->blue = (arith(scalergb.blue) + 1) / 2;
|
|
245 nearfill(ROW | COLUMN);
|
|
246 }
|
|
247 }
|
|
248 trans->clutval[0] = near0 - screen[0].clut;
|
|
249 trans->clutval[1] = near1 - screen[1].clut;
|
|
250 return TRUE;
|
|
251 }
|
|
252
|
|
253 nearfill(section)
|
|
254 int section;
|
|
255 {
|
|
256 register int i, j;
|
|
257 rgbcolor *newrgb;
|
|
258
|
|
259 if (section & ROW) {
|
|
260 newrgb = &screen[0].clut[j = screen[0].clutsize - 1];
|
|
261 for (i = screen[1].clutsize; --i >= 0; )
|
|
262 neartab[j][i] = nearcolor(newrgb, &screen[1].clut[i]);
|
|
263 }
|
|
264 if (section & COLUMN) {
|
|
265 newrgb = &screen[1].clut[j = screen[1].clutsize - 1];
|
|
266 for (i = screen[0].clutsize; --i >= 0; )
|
|
267 neartab[i][j] = nearcolor(&screen[0].clut[i], newrgb);
|
|
268 }
|
|
269 }
|
|
270
|
|
271 /*
|
|
272 * nearcolor() -- tell approx2() whether two colors are close enough
|
|
273 * to be considered for approximate summation to a color in the GIF CLUT.
|
|
274 */
|
|
275 bool
|
|
276 nearcolor(c1, c2)
|
|
277 register rgbcolor *c1, *c2;
|
|
278 {
|
|
279 return abs(arith(c2->red) - arith(c1->red)) < 2 &&
|
|
280 abs(arith(c2->green) - arith(c1->green)) < 2 &&
|
|
281 abs(arith(c2->blue) - arith(c1->blue)) < 2;
|
|
282 }
|
|
283
|
|
284 /*
|
|
285 * forcenear() -- stuff a color into a CLUT that is guaranteed to
|
|
286 * pass the nearcolor test with the other fixed color (so we don't
|
|
287 * have to call nearcolor() explicitly), and should come close to
|
|
288 * adding to the fixed color (already in the other screen's CLUT)
|
|
289 * to give a specified goal color from the GIF CLUT.
|
|
290 */
|
|
291 forcenear(vary, fixed, goal)
|
|
292 register BYTE vary[], fixed[], goal[];
|
|
293 {
|
|
294 register int d, x;
|
|
295
|
|
296 for (x = 3; --x >= 0; ) {
|
|
297 if ((d = arith(*vary = *goal++ - *fixed) - arith(*fixed)) < 0)
|
|
298 d = -1;
|
|
299 else if (d > 0)
|
|
300 d = 1;
|
|
301 *vary++ = *fixed++ + d;
|
|
302 }
|
|
303 }
|
|
304
|
|
305 int
|
|
306 minadd(color, add)
|
|
307 register BYTE *color;
|
|
308 int add;
|
|
309 {
|
|
310 int maxval, tryadd, x;
|
|
311
|
|
312 maxval = 0;
|
|
313 for (x = 3; --x >= 0; ) {
|
|
314 if ((tryadd = (arith(*color++) + add) % minmod) > maxval)
|
|
315 maxval = tryadd;
|
|
316 }
|
|
317 return minmod - maxval + add;
|
|
318 }
|
|
319
|
|
320 int
|
|
321 tolerval(ccolor, gcolor)
|
|
322 register BYTE *ccolor, *gcolor;
|
|
323 {
|
|
324 int x, v, mv;
|
|
325
|
|
326 mv = 0;
|
|
327 for (x = 3; --x >= 0; ) {
|
|
328 v = abs(SCALE1 * *ccolor++ + SCALE1 / 2 - arith(*gcolor++)) -
|
|
329 SCALE1 / 2;
|
|
330 if (v > mv)
|
|
331 mv = v;
|
|
332 }
|
|
333 return mv;
|
|
334 }
|
|
335
|
|
336 int
|
|
337 toler2(ccolor1, ccolor2, gcolor)
|
|
338 register BYTE *ccolor1, *ccolor2, *gcolor;
|
|
339 {
|
|
340 int x, v, mv;
|
|
341
|
|
342 mv = 0;
|
|
343 for (x = 3; --x >= 0; ) {
|
|
344 v = abs(SCALE2 * (*ccolor1++ + *ccolor2++) + SCALE2 / 2 -
|
|
345 arith(*gcolor++)) - SCALE2 / 2;
|
|
346 if (v > mv)
|
|
347 mv = v;
|
|
348 }
|
|
349 return mv;
|
|
350 }
|
|
351
|
|
352 int
|
|
353 exact(ccolor1, ccolor2, gcolor)
|
|
354 register BYTE *ccolor1, *ccolor2, *gcolor;
|
|
355 {
|
|
356 if (arith(ccolor1[0]) + arith(ccolor2[0]) != arith(gcolor[0]))
|
|
357 return 2;
|
|
358 if (arith(ccolor1[1]) + arith(ccolor2[1]) != arith(gcolor[1]))
|
|
359 return 2;
|
|
360 if (arith(ccolor1[2]) + arith(ccolor2[2]) != arith(gcolor[2]))
|
|
361 return 2;
|
|
362 return 0;
|
|
363 }
|