Debian Codegraph @

common/shell_loadimage.cc

raw
1 /*****************************************************************************
2 * Free42 -- an HP-42S calculator simulator
3 * Copyright (C) 2004-2026 Thomas Okken
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/licenses/.
16 *****************************************************************************/
17
18 #include <ctype.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <new>
23
24
25 #include "shell_skin.h"
26
27
28 struct SkinPixmap {
29 unsigned char *pixels;
30 SkinColor *cmap;
31 int depth;
32 int width, height, bytesperline;
33 };
34
35 static SkinPixmap pixmap;
36
37 struct GifColorMap {
38 SkinColor color[256];
39 short index[256];
40 GifColorMap() {
41 for (int i = 0; i < 256; i++) {
42 index[i] = -1;
43 color[i].r = color[i].g = color[i].b = 0;
44 }
45 }
46 };
47
48 static bool read_byte(int *n) {
49 int c = skin_getchar();
50 if (c == EOF)
51 return false;
52 *n = c;
53 return true;
54 }
55
56 static bool read_short(int *n) {
57 int c1, c2;
58 c1 = skin_getchar();
59 if (c1 == EOF)
60 return false;
61 c2 = skin_getchar();
62 if (c2 == EOF)
63 return false;
64 *n = c1 + (c2 << 8);
65 return true;
66 }
67
68
69 bool shell_loadimage() {
70 SkinPixmap *pm = &pixmap;
71 GifColorMap *lcmap = NULL;
72 GifColorMap gcmap;
73 int image_colors = 0;
74
75 int sig;
76
77 int info;
78 int background;
79 int zero;
80
81 int has_global_cmap;
82 int bpp;
83 int ncolors;
84 int size;
85
86 bool mono;
87 bool black_used = false;
88 bool white_used = false;
89
90 int i, j, type;
91 bool res;
92 unsigned char *ptr;
93
94 if (!read_byte(&sig) || sig != 'G'
95 || !read_byte(&sig) || sig != 'I'
96 || !read_byte(&sig) || sig != 'F'
97 || !read_byte(&sig) || sig != '8'
98 || !read_byte(&sig) || (sig != '7' && sig != '9')
99 || !read_byte(&sig) || sig != 'a') {
100 fprintf(stderr, "GIF signature not found.\n");
101 return false;
102 }
103
104 if (!read_short(&pm->width)
105 || !read_short(&pm->height)
106 || !read_byte(&info)
107 || !read_byte(&background)
108 || !read_byte(&zero)
109 || zero != 0) {
110 fprintf(stderr, "Fatally premature EOF.\n");
111 return false;
112 }
113
114 has_global_cmap = (info & 128) != 0;
115 bpp = (info & 7) + 1;
116 ncolors = 1 << bpp;
117 size = 0;
118
119 /* Bits 6..4 of info contain one less than the "color resolution",
120 * defined as the number of significant bits per RGB component in
121 * the source image's color palette. If the source image (from
122 * which the GIF was generated) was 24-bit true color, the color
123 * resolution is 8, so the value in bits 6..4 is 7. If the source
124 * image had an EGA color cube (2x2x2), the color resolution would
125 * be 2, etc.
126 * Bit 3 of info must be zero in GIF87a; in GIF89a, if it is set,
127 * it indicates that the global colormap is sorted, the most
128 * important entries being first. In PseudoColor environments this
129 * can be used to make sure to get the most important colors from
130 * the X server first, to optimize the image's appearance in the
131 * event that not all the colors from the colormap can actually be
132 * obtained at the same time.
133 * The 'zero' field is always 0 in GIF87a; in GIF89a, it indicates
134 * the pixel aspect ratio, as (PAR + 15) : 64. If PAR is zero,
135 * this means no aspect ratio information is given, PAR = 1 means
136 * 1:4 (narrow), PAR = 49 means 1:1 (square), PAR = 255 means
137 * slightly over 4:1 (wide), etc.
138 */
139
140 pm->cmap = (SkinColor *) malloc(256 * sizeof(SkinColor));
141 // TODO - handle memory allocation failure
142 if (has_global_cmap) {
143 for (i = 0; i < ncolors; i++) {
144 int r, g, b;
145 if (!read_byte(&r)
146 || !read_byte(&g)
147 || !read_byte(&b)) {
148 fprintf(stderr, "Fatally premature EOF.\n");
149 goto failed;
150 }
151 gcmap.color[i].r = r;
152 gcmap.color[i].g = g;
153 gcmap.color[i].b = b;
154 }
155 } else {
156 for (i = 0; i < ncolors; i++) {
157 int k = (i * 255) / (ncolors - 1);
158 gcmap.color[i].r = k;
159 gcmap.color[i].g = k;
160 gcmap.color[i].b = k;
161 }
162 }
163
164 mono = false;
165 if (ncolors <= 2) {
166 /* Test for true black & white */
167 mono = true;
168 for (i = 0; i < ncolors; i++)
169 if (!(gcmap.color[i].r == 0
170 && gcmap.color[i].g == 0
171 && gcmap.color[i].b == 0
172 || gcmap.color[i].r == 255
173 && gcmap.color[i].g == 255
174 && gcmap.color[i].b == 255)) {
175 mono = false;
176 break;
177 }
178 }
179
180 if (mono) {
181 pm->depth = 1;
182 pm->bytesperline = ((pm->width + 31) >> 3) & ~3;
183 } else {
184 pm->depth = 8;
185 pm->bytesperline = (pm->width + 3) & ~3;
186 }
187
188 size = pm->bytesperline * pm->height;
189 pm->pixels = (unsigned char *) malloc(size);
190 // TODO - handle memory allocation failure
191
192 if (has_global_cmap) {
193 if (pm->depth == 1) {
194 memset(pm->pixels, background * 255, size);
195 if (background == 0)
196 black_used = true;
197 else
198 white_used = true;
199 } else {
200 memset(pm->pixels, background, size);
201 pm->cmap[0] = gcmap.color[background];
202 gcmap.index[background] = 0;
203 image_colors = 1;
204 }
205 } else {
206 memset(pm->pixels, 0, size);
207 }
208
209 while (1) {
210 int whatnext;
211 if (!read_byte(&whatnext))
212 goto unexp_eof;
213 if (whatnext == ',') {
214 /* Image */
215 int ileft, itop, iwidth, iheight;
216 int info;
217
218 int lbpp;
219 int lncolors;
220
221 int interlaced;
222 int h;
223 int v;
224 int codesize;
225 int bytecount;
226
227 short prefix_table[4096];
228 short code_table[4096];
229
230 int maxcode;
231 int clear_code;
232 int end_code;
233
234 int curr_code_size;
235 int curr_code;
236 int old_code;
237 int bits_needed;
238 int end_code_seen;
239
240 if (!read_short(&ileft)
241 || !read_short(&itop)
242 || !read_short(&iwidth)
243 || !read_short(&iheight)
244 || !read_byte(&info))
245 goto unexp_eof;
246
247 if (itop + iheight > pm->height
248 || ileft + iwidth > pm->width) {
249 fprintf(stderr, "Image position and size not contained within screen size!\n");
250 goto failed;
251 }
252
253 /* Bit 3 of info must be zero in GIF87a; in GIF89a, if it
254 * is set, it indicates that the local colormap is sorted,
255 * the most important entries being first. In PseudoColor
256 * environments this can be used to make sure to get the
257 * most important colors from the X server first, to
258 * optimize the image's appearance in the event that not
259 * all the colors from the colormap can actually be
260 * obtained at the same time.
261 */
262 if ((info & 128) == 0) {
263 /* Using global color map */
264 lcmap = &gcmap;
265 lbpp = bpp;
266 lncolors = ncolors;
267 } else {
268 /* Using local color map */
269 lbpp = (info & 7) + 1;
270 lncolors = 1 << lbpp;
271 lcmap = new (std::nothrow) GifColorMap;
272 // TODO - handle memory allocation failure
273 for (i = 0; i < lncolors; i++) {
274 int r, g, b;
275 if (!read_byte(&r)
276 || !read_byte(&g)
277 || !read_byte(&b))
278 goto unexp_eof;
279 lcmap->color[i].r = r;
280 lcmap->color[i].g = g;
281 lcmap->color[i].b = b;
282 }
283 }
284
285 interlaced = (info & 64) != 0;
286 h = 0;
287 v = 0;
288 if (!read_byte(&codesize) || !read_byte(&bytecount))
289 goto unexp_eof;
290
291 maxcode = 1 << codesize;
292 for (i = 0; i < maxcode + 2; i++) {
293 prefix_table[i] = -1;
294 code_table[i] = i;
295 }
296 clear_code = maxcode++;
297 end_code = maxcode++;
298
299 curr_code_size = codesize + 1;
300 curr_code = 0;
301 old_code = -1;
302 bits_needed = curr_code_size;
303 end_code_seen = 0;
304
305 while (bytecount != 0) {
306 for (i = 0; i < bytecount; i++) {
307 int currbyte;
308 int bits_available;
309
310 if (!read_byte(&currbyte))
311 goto unexp_eof;
312 if (end_code_seen)
313 continue;
314
315 bits_available = 8;
316 while (bits_available != 0) {
317 int bits_copied = bits_needed < bits_available ?
318 bits_needed : bits_available;
319 int bits = currbyte >> (8 - bits_available);
320 bits &= 255 >> (8 - bits_copied);
321 curr_code |= bits << (curr_code_size - bits_needed);
322 bits_available -= bits_copied;
323 bits_needed -= bits_copied;
324
325 if (bits_needed == 0) {
326 if (curr_code == end_code) {
327 end_code_seen = 1;
328 } else if (curr_code == clear_code) {
329 maxcode = (1 << codesize) + 2;
330 curr_code_size = codesize + 1;
331 old_code = -1;
332 } else {
333 unsigned char expanded[4096];
334 int explen;
335
336 if (curr_code < maxcode) {
337 if (maxcode < 4096 && old_code != -1) {
338 int c = curr_code;
339 while (prefix_table[c] != -1)
340 c = prefix_table[c];
341 c = code_table[c];
342 prefix_table[maxcode] = old_code;
343 code_table[maxcode] = c;
344 maxcode++;
345 if (maxcode == (1 << curr_code_size)
346 && curr_code_size < 12)
347 curr_code_size++;
348 }
349 } else if (curr_code == maxcode) {
350 /* Once maxcode == 4096, we can't get here
351 * any more, because we refuse to raise
352 * curr_code_size above 12 -- so we can
353 * never read a bigger code than 4095.
354 */
355 int c;
356 if (old_code == -1) {
357 fprintf(stderr, "Out-of-sequence code in compressed data.\n");
358 goto done;
359 }
360 c = old_code;
361 while (prefix_table[c] != -1)
362 c = prefix_table[c];
363 c = code_table[c];
364 prefix_table[maxcode] = old_code;
365 code_table[maxcode] = c;
366 maxcode++;
367 if (maxcode == (1 << curr_code_size)
368 && curr_code_size < 12)
369 curr_code_size++;
370 } else {
371 fprintf(stderr, "Out-of-sequence code in compressed data.\n");
372 goto done;
373 }
374
375 old_code = curr_code;
376
377 /* Output curr_code! */
378 explen = 0;
379 while (curr_code != -1) {
380 expanded[explen++] =
381 (unsigned char) code_table[curr_code];
382 curr_code = prefix_table[curr_code];
383 }
384 for (j = explen - 1; j >= 0; j--) {
385 int pixel = expanded[j];
386 if (pm->depth == 1) {
387 int p = lcmap->index[pixel];
388 if (p == -1) {
389 unsigned char r = lcmap->color[pixel].r;
390 unsigned char g = lcmap->color[pixel].g;
391 unsigned char b = lcmap->color[pixel].b;
392 if (r == 0 && g == 0 && b == 0) {
393 p = 0;
394 black_used = true;
395 } else if (r == 255 && g == 255 && b == 255) {
396 p = 1;
397 white_used = true;
398 } else {
399 /* Not black & white; switch to 8-bit */
400 int newbytesperline = pm->width;
401 int newsize = newbytesperline * pm->height;
402 unsigned char *newpixels = (unsigned char *)
403 malloc(newsize);
404 // TODO - handle memory allocation failure
405 if (!white_used || !black_used)
406 memset(newpixels, 0, newsize);
407 else
408 for (int v = 0; v < pm->height; v++) {
409 unsigned char *newpixel = newpixels + newbytesperline * v;
410 for (int h = 0; h < pm->width; h++) {
411 unsigned char px = (pm->pixels[pm->bytesperline * v + (h >> 3)] >> (h & 7)) & 1;
412 *newpixel++ = px;
413 }
414 }
415 free(pm->pixels);
416 pm->pixels = newpixels;
417 pm->bytesperline = newbytesperline;
418 pm->depth = 8;
419 if (black_used && white_used) {
420 pm->cmap[0].r = 0;
421 pm->cmap[0].g = 0;
422 pm->cmap[0].b = 0;
423 pm->cmap[1].r = 255;
424 pm->cmap[1].g = 255;
425 pm->cmap[1].b = 255;
426 image_colors = 2;
427 } else if (black_used || white_used) {
428 unsigned char c = black_used ? 0 : 255;
429 pm->cmap[0].r = c;
430 pm->cmap[0].g = c;
431 pm->cmap[0].b = c;
432 image_colors = 1;
433 } else {
434 image_colors = 0;
435 }
436 if (white_used && !black_used)
437 for (i = 0; i < 256; i++)
438 if (lcmap->index[i] == 1)
439 lcmap->index[i] = 0;
440 goto indexed;
441 }
442 lcmap->index[pixel] = p;
443 }
444 int x = ileft + h;
445 int index = pm->bytesperline * (itop + v) + (x >> 3);
446 unsigned char mask = 1 << (x & 7);
447 if (p)
448 pm->pixels[index] |= mask;
449 else
450 pm->pixels[index] &= ~mask;
451 } else if (pm->depth == 8) {
452 indexed:
453 int p = lcmap->index[pixel];
454 if (p == -1) {
455 unsigned char r = lcmap->color[pixel].r;
456 unsigned char g = lcmap->color[pixel].g;
457 unsigned char b = lcmap->color[pixel].b;
458 for (p = 0; p < image_colors; p++)
459 if (pm->cmap[p].r == r
460 && pm->cmap[p].g == g
461 && pm->cmap[p].b == b) {
462 lcmap->index[pixel] = p;
463 goto found;
464 }
465 if (image_colors == 256) {
466 /* Out of colormap entries; switch to truecolor */
467 int newbytesperline = pm->width * 3;
468 unsigned char *newpixels = (unsigned char *)
469 malloc(newbytesperline * pm->height);
470 // TODO - handle memory allocation failure
471 for (int v = 0; v < pm->height; v++) {
472 unsigned char *newpixel = newpixels + newbytesperline * v;
473 for (int h = 0; h < pm->width; h++) {
474 unsigned char px = pm->pixels[pm->bytesperline * v + h];
475 *newpixel++ = pm->cmap[px].r;
476 *newpixel++ = pm->cmap[px].g;
477 *newpixel++ = pm->cmap[px].b;
478 }
479 }
480 free(pm->pixels);
481 pm->pixels = newpixels;
482 pm->bytesperline = newbytesperline;
483 pm->depth = 24;
484 goto truecolor;
485 }
486 p = image_colors++;
487 pm->cmap[p].r = r;
488 pm->cmap[p].g = g;
489 pm->cmap[p].b = b;
490 lcmap->index[pixel] = p;
491 }
492 found:
493 pm->pixels[pm->bytesperline * (itop + v) + ileft + h] = p;
494 } else {
495 truecolor:
496 unsigned char *rgb = pm->pixels + (pm->bytesperline * (itop + v) + 3 * (ileft + h));
497 *rgb++ = lcmap->color[pixel].r;
498 *rgb++ = lcmap->color[pixel].g;
499 *rgb = lcmap->color[pixel].b;
500 }
501 if (++h == iwidth) {
502 h = 0;
503 if (interlaced) {
504 switch (v & 7) {
505 case 0:
506 v += 8;
507 if (v < iheight)
508 break;
509 /* Some GIF en/decoders go
510 * straight from the '0'
511 * pass to the '4' pass
512 * without checking the
513 * height, and blow up on
514 * 2/3/4 pixel high
515 * interlaced images.
516 */
517 if (iheight > 4)
518 v = 4;
519 else if (iheight > 2)
520 v = 2;
521 else if (iheight > 1)
522 v = 1;
523 else
524 end_code_seen = 1;
525 break;
526 case 4:
527 v += 8;
528 if (v >= iheight)
529 v = 2;
530 break;
531 case 2:
532 case 6:
533 v += 4;
534 if (v >= iheight)
535 v = 1;
536 break;
537 case 1:
538 case 3:
539 case 5:
540 case 7:
541 v += 2;
542 if (v >= iheight)
543 end_code_seen = 1;
544 break;
545 }
546 if (end_code_seen)
547 break;
548 } else {
549 if (++v == iheight) {
550 end_code_seen = 1;
551 break;
552 }
553 }
554 }
555 }
556 }
557 curr_code = 0;
558 bits_needed = curr_code_size;
559 }
560 }
561 }
562 if (!read_byte(&bytecount))
563 goto unexp_eof;
564 }
565
566 if (lcmap != &gcmap)
567 delete lcmap;
568 lcmap = NULL;
569
570 } else if (whatnext == '!') {
571 /* Extension block */
572 int function_code, byte_count;
573 if (!read_byte(&function_code))
574 goto unexp_eof;
575 if (!read_byte(&byte_count))
576 goto unexp_eof;
577 while (byte_count != 0) {
578 for (i = 0; i < byte_count; i++) {
579 int dummy;
580 if (!read_byte(&dummy))
581 goto unexp_eof;
582 }
583 if (!read_byte(&byte_count))
584 goto unexp_eof;
585 }
586 } else if (whatnext == ';') {
587 /* Terminator */
588 break;
589 } else {
590 fprintf(stderr, "Unrecognized tag '%c' (0x%02x).\n",
591 whatnext, whatnext);
592 goto failed;
593 }
594 }
595
596 done:
597 if (lcmap != NULL && lcmap != &gcmap)
598 delete lcmap;
599 if (pm->depth == 1 || pm->depth == 24) {
600 free(pm->cmap);
601 pm->cmap = NULL;
602 }
603
604 /* Successfully read the image. Now, write it out: */
605 if (pm->depth == 1)
606 type = IMGTYPE_MONO;
607 else if (pm->depth == 8)
608 type = IMGTYPE_COLORMAPPED;
609 else /* pm->depth == 24 */
610 type = IMGTYPE_TRUECOLOR;
611
612 res = skin_init_image(type, 256, pm->cmap, pm->width, pm->height);
613 if (res) {
614 ptr = pm->pixels;
615 for (i = 0; i < pm->height; i++) {
616 skin_put_pixels(ptr);
617 ptr += pm->bytesperline;
618 }
619 skin_finish_image();
620 }
621
622 /* Done writing; delete the image and return. */
623 if (pm->cmap != NULL)
624 free(pm->cmap);
625 free(pm->pixels);
626 return res;
627
628
629 unexp_eof:
630 fprintf(stderr, "Unexpected EOF.\n");
631 goto done;
632
633
634 failed:
635 if (lcmap != NULL && lcmap != &gcmap)
636 delete lcmap;
637 if (pm->cmap != NULL) {
638 free(pm->cmap);
639 pm->cmap = NULL;
640 }
641 if (pm->pixels != NULL) {
642 free(pm->pixels);
643 pm->pixels = NULL;
644 }
645 return false;
646 }