winlivebg_test1

view src/imagelbm.c @ 2:a9025f31ae2d

sortof works, testing with colcycle hack
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Oct 2019 16:07:25 +0200
parents
children
line source
1 /*
2 colcycle - color cycling image viewer
3 Copyright (C) 2016-2017 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <assert.h>
21 #include <inttypes.h>
22 #include <string.h>
23 #if defined(__WATCOMC__) || defined(WIN32)
24 #include <malloc.h>
25 #else
26 #include <alloca.h>
27 #endif
28 #include "imagelbm.h"
30 #ifdef __GNUC__
31 #define PACKED __attribute__((packed))
32 #endif
34 #ifdef __BYTE_ORDER__
35 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
36 #define LENDIAN
37 #else
38 #define BENDIAN
39 #endif
40 #endif
42 #define MKID(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
44 #define IS_IFF_CONTAINER(id) ((id) == IFF_FORM || (id) == IFF_CAT || (id) == IFF_LIST)
46 enum {
47 IFF_FORM = MKID('F', 'O', 'R', 'M'),
48 IFF_CAT = MKID('C', 'A', 'T', ' '),
49 IFF_LIST = MKID('L', 'I', 'S', 'T'),
50 IFF_ILBM = MKID('I', 'L', 'B', 'M'),
51 IFF_PBM = MKID('P', 'B', 'M', ' '),
52 IFF_BMHD = MKID('B', 'M', 'H', 'D'),
53 IFF_CMAP = MKID('C', 'M', 'A', 'P'),
54 IFF_BODY = MKID('B', 'O', 'D', 'Y'),
55 IFF_CRNG = MKID('C', 'R', 'N', 'G')
56 };
59 struct chdr {
60 uint32_t id;
61 uint32_t size;
62 };
64 #ifdef __WATCOMC__
65 #pragma push(pack, 1)
66 #endif
67 struct bitmap_header {
68 uint16_t width, height;
69 int16_t xoffs, yoffs;
70 uint8_t nplanes;
71 uint8_t masking;
72 uint8_t compression;
73 uint8_t padding;
74 uint16_t colorkey;
75 uint8_t aspect_num, aspect_denom;
76 int16_t pgwidth, pgheight;
77 } PACKED;
78 #ifdef __WATCOMC__
79 #pragma pop(pack)
80 #endif
82 enum {
83 MASK_NONE,
84 MASK_PLANE,
85 MASK_COLORKEY,
86 MASK_LASSO
87 };
89 struct crng {
90 uint16_t padding;
91 uint16_t rate;
92 uint16_t flags;
93 uint8_t low, high;
94 };
96 enum {
97 CRNG_ENABLE = 1,
98 CRNG_REVERSE = 2
99 };
101 static int read_header(FILE *fp, struct chdr *hdr);
102 static int read_ilbm_pbm(FILE *fp, uint32_t type, uint32_t size, struct image *img);
103 static int read_bmhd(FILE *fp, struct bitmap_header *bmhd);
104 static int read_crng(FILE *fp, struct crng *crng);
105 static int read_body_ilbm(FILE *fp, struct bitmap_header *bmhd, struct image *img);
106 static int read_body_pbm(FILE *fp, struct bitmap_header *bmhd, struct image *img);
107 static int read_compressed_scanline(FILE *fp, unsigned char *scanline, int width);
108 static int read16(FILE *fp, uint16_t *res);
109 static int read32(FILE *fp, uint32_t *res);
110 static uint16_t swap16(uint16_t x);
111 static uint32_t swap32(uint32_t x);
113 int file_is_lbm(FILE *fp)
114 {
115 uint32_t type;
116 struct chdr hdr;
118 while(read_header(fp, &hdr) != -1) {
119 if(IS_IFF_CONTAINER(hdr.id)) {
120 if(read32(fp, &type) == -1) {
121 break;
122 }
124 if(type == IFF_ILBM || type == IFF_PBM ) {
125 rewind(fp);
126 return 1;
127 }
128 hdr.size -= sizeof type; /* so we will seek fwd correctly */
129 }
130 fseek(fp, hdr.size, SEEK_CUR);
131 }
132 fseek(fp, 0, SEEK_SET);
133 return 0;
134 }
136 void print_chunkid(uint32_t id)
137 {
138 char str[5] = {0};
139 #ifdef LENDIAN
140 id = swap32(id);
141 #endif
142 memcpy(str, &id, 4);
143 puts(str);
144 }
146 int load_image_lbm(struct image *img, FILE *fp)
147 {
148 uint32_t type;
149 struct chdr hdr;
151 while(read_header(fp, &hdr) != -1) {
152 if(IS_IFF_CONTAINER(hdr.id)) {
153 if(read32(fp, &type) == -1) {
154 break;
155 }
156 hdr.size -= sizeof type; /* to compensate for having advanced 4 more bytes */
158 if(type == IFF_ILBM) {
159 if(read_ilbm_pbm(fp, type, hdr.size, img) == -1) {
160 return -1;
161 }
162 return 0;
163 }
164 if(type == IFF_PBM) {
165 if(read_ilbm_pbm(fp, type, hdr.size, img) == -1) {
166 return -1;
167 }
168 return 0;
169 }
170 }
171 fseek(fp, hdr.size, SEEK_CUR);
172 }
173 return 0;
174 }
176 static int read_header(FILE *fp, struct chdr *hdr)
177 {
178 if(fread(hdr, 1, sizeof *hdr, fp) < sizeof *hdr) {
179 return -1;
180 }
181 #ifdef LENDIAN
182 hdr->id = swap32(hdr->id);
183 hdr->size = swap32(hdr->size);
184 #endif
185 return 0;
186 }
188 static int read_ilbm_pbm(FILE *fp, uint32_t type, uint32_t size, struct image *img)
189 {
190 int i, res = -1;
191 struct chdr hdr;
192 struct bitmap_header bmhd;
193 struct crng crng;
194 struct colrange *crnode;
195 unsigned char pal[3 * 256];
196 unsigned char *pptr;
197 long start = ftell(fp);
199 memset(img, 0, sizeof *img);
201 while(read_header(fp, &hdr) != -1 && ftell(fp) - start < size) {
202 switch(hdr.id) {
203 case IFF_BMHD:
204 assert(hdr.size == 20);
205 if(read_bmhd(fp, &bmhd) == -1) {
206 return -1;
207 }
208 img->width = bmhd.width;
209 img->height = bmhd.height;
210 img->bpp = bmhd.nplanes;
211 if(bmhd.nplanes > 8) {
212 fprintf(stderr, "%d planes found, only paletized LBM files supported\n", bmhd.nplanes);
213 return -1;
214 }
215 if(!(img->pixels = malloc(img->width * img->height))) {
216 fprintf(stderr, "failed to allocate %dx%d image\n", img->width, img->height);
217 return -1;
218 }
219 break;
221 case IFF_CMAP:
222 assert(hdr.size / 3 <= 256);
224 if(fread(pal, 1, hdr.size, fp) < hdr.size) {
225 fprintf(stderr, "failed to read colormap\n");
226 return -1;
227 }
228 pptr = pal;
229 for(i=0; i<256; i++) {
230 img->palette[i].r = *pptr++;
231 img->palette[i].g = *pptr++;
232 img->palette[i].b = *pptr++;
233 }
234 break;
236 case IFF_CRNG:
237 assert(hdr.size == sizeof crng);
239 if(read_crng(fp, &crng) == -1) {
240 fprintf(stderr, "failed to read color cycling range chunk\n");
241 return -1;
242 }
243 if(crng.low != crng.high && crng.rate > 0) {
244 if(!(crnode = malloc(sizeof *crnode))) {
245 fprintf(stderr, "failed to allocate color range node\n");
246 return -1;
247 }
248 crnode->low = crng.low;
249 crnode->high = crng.high;
250 crnode->cmode = (crng.flags & CRNG_REVERSE) ? CYCLE_REVERSE : CYCLE_NORMAL;
251 crnode->rate = crng.rate;
252 crnode->next = img->range;
253 img->range = crnode;
254 ++img->num_ranges;
255 }
256 break;
258 case IFF_BODY:
259 if(!img->pixels) {
260 fprintf(stderr, "malformed ILBM image: encountered BODY chunk before BMHD\n");
261 return -1;
262 }
263 if(type == IFF_ILBM) {
264 if(read_body_ilbm(fp, &bmhd, img) == -1) {
265 fprintf(stderr, "failed to read interleaved pixel data\n");
266 return -1;
267 }
268 } else {
269 assert(type == IFF_PBM);
270 if(read_body_pbm(fp, &bmhd, img) == -1) {
271 fprintf(stderr, "failed to read linear pixel data\n");
272 return -1;
273 }
274 }
275 res = 0; /* sucessfully read image */
276 break;
278 default:
279 /* skip unknown chunks */
280 fseek(fp, hdr.size, SEEK_CUR);
281 if(ftell(fp) & 1) {
282 /* chunks must start at even offsets */
283 fseek(fp, 1, SEEK_CUR);
284 }
285 }
286 }
288 return res;
289 }
292 static int read_bmhd(FILE *fp, struct bitmap_header *bmhd)
293 {
294 if(fread(bmhd, sizeof *bmhd, 1, fp) < 1) {
295 return -1;
296 }
297 #ifdef LENDIAN
298 bmhd->width = swap16(bmhd->width);
299 bmhd->height = swap16(bmhd->height);
300 bmhd->xoffs = swap16(bmhd->xoffs);
301 bmhd->yoffs = swap16(bmhd->yoffs);
302 bmhd->colorkey = swap16(bmhd->colorkey);
303 bmhd->pgwidth = swap16(bmhd->pgwidth);
304 bmhd->pgheight = swap16(bmhd->pgheight);
305 #endif
306 return 0;
307 }
309 static int read_crng(FILE *fp, struct crng *crng)
310 {
311 if(fread(crng, sizeof *crng, 1, fp) < 1) {
312 return -1;
313 }
314 #ifdef LENDIAN
315 crng->rate = swap16(crng->rate);
316 crng->flags = swap16(crng->flags);
317 #endif
318 return 0;
319 }
321 /* scanline: [bp0 row][bp1 row]...[bpN-1 row][opt mask row]
322 * each uncompressed row is width / 8 bytes
323 */
324 static int read_body_ilbm(FILE *fp, struct bitmap_header *bmhd, struct image *img)
325 {
326 int i, j, k, bitidx;
327 int rowsz = img->width / 8;
328 unsigned char *src, *dest = img->pixels;
329 unsigned char *rowbuf = alloca(rowsz);
331 assert(bmhd->width == img->width);
332 assert(bmhd->height == img->height);
333 assert(img->pixels);
335 for(i=0; i<img->height; i++) {
337 memset(dest, 0, img->width); /* clear the whole scanline to OR bits into place */
339 for(j=0; j<bmhd->nplanes; j++) {
340 // read a row corresponding to bitplane j
341 if(bmhd->compression) {
342 if(read_compressed_scanline(fp, rowbuf, rowsz) == -1) {
343 return -1;
344 }
345 } else {
346 if(fread(rowbuf, 1, rowsz, fp) < rowsz) {
347 return -1;
348 }
349 }
351 // distribute all bits across the linear output scanline
352 src = rowbuf;
353 bitidx = 0;
355 for(k=0; k<img->width; k++) {
356 dest[k] |= ((*src >> (7 - bitidx)) & 1) << j;
358 if(++bitidx >= 8) {
359 bitidx = 0;
360 ++src;
361 }
362 }
363 }
365 if(bmhd->masking & MASK_PLANE) {
366 /* skip the mask (1bpp) */
367 fseek(fp, rowsz, SEEK_CUR);
368 }
370 dest += img->width;
371 }
372 return 0;
373 }
375 static int read_body_pbm(FILE *fp, struct bitmap_header *bmhd, struct image *img)
376 {
377 int i;
378 int npixels = img->width * img->height;
379 unsigned char *dptr = img->pixels;
381 assert(bmhd->width == img->width);
382 assert(bmhd->height == img->height);
383 assert(img->pixels);
385 if(bmhd->compression) {
386 for(i=0; i<img->height; i++) {
387 if(read_compressed_scanline(fp, dptr, img->width) == -1) {
388 return -1;
389 }
390 dptr += img->width;
391 }
393 } else {
394 /* uncompressed */
395 if(fread(img->pixels, 1, npixels, fp) < npixels) {
396 return -1;
397 }
398 }
399 return 0;
400 }
402 static int read_compressed_scanline(FILE *fp, unsigned char *scanline, int width)
403 {
404 int i, count, x = 0;
405 signed char ctl;
407 while(x < width) {
408 if(fread(&ctl, 1, 1, fp) < 1) return -1;
410 if(ctl == -128) continue;
412 if(ctl >= 0) {
413 count = ctl + 1;
414 if(fread(scanline, 1, count, fp) < count) return -1;
415 scanline += count;
417 } else {
418 unsigned char pixel;
419 count = 1 - ctl;
420 if(fread(&pixel, 1, 1, fp) < 1) return -1;
422 for(i=0; i<count; i++) {
423 *scanline++ = pixel;
424 }
425 }
427 x += count;
428 }
430 return 0;
431 }
433 static int read16(FILE *fp, uint16_t *res)
434 {
435 if(fread(res, sizeof *res, 1, fp) < 1) {
436 return -1;
437 }
438 #ifdef LENDIAN
439 *res = swap16(*res);
440 #endif
441 return 0;
442 }
444 static int read32(FILE *fp, uint32_t *res)
445 {
446 if(fread(res, sizeof *res, 1, fp) < 1) {
447 return -1;
448 }
449 #ifdef LENDIAN
450 *res = swap32(*res);
451 #endif
452 return 0;
453 }
455 static uint16_t swap16(uint16_t x)
456 {
457 return (x << 8) | (x >> 8);
458 }
460 static uint32_t swap32(uint32_t x)
461 {
462 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
463 }