-
Notifications
You must be signed in to change notification settings - Fork 2
/
iff.c
1102 lines (946 loc) · 27.7 KB
/
iff.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "impy.h"
#include "private.h"
#include <string.h>
#if 0 // IFF disabled for now
// Good collection of anims to test with at:
// http://www.randelshofer.ch/animations/
// mask values
#define mskNone 0
#define mskHasMask 1
#define mskHasTransparentColor 2
#define mskLasso 3
// compression values
#define cmpNone 0
#define cmpByteRun1 1
typedef struct {
uint16_t w, h; /* raster width & height in pixels */
int16_t x, y; /* pixel position for this image */
uint8_t nPlanes; /* # source bitplanes */
uint8_t masking;
uint8_t compression;
uint8_t pad1; /* unused; for consistency, put 0 here */
uint16_t transparentColor; /* transparent "color number" (sort of) */
uint8_t xAspect, yAspect; /* pixel aspect, a ratio width : height */
int16_t pageWidth, pageHeight; /* source "page" size in pixels */
} BitMapHeader;
typedef struct {
uint8_t operation; /* The compression method:
=0 set directly (normal ILBM BODY),
=1 XOR ILBM mode,
=2 Long Delta mode,
=3 Short Delta mode,
=4 Generalized short/long Delta mode,
=5 Byte Vertical Delta mode
=7 short/long Vertical Delta mode
=74 (ascii 'J') reserved for Eric Graham's
compression technique (details to be
released later).
*/
uint8_t mask; /* XOR mode only */
uint16_t w,h;
int16_t x,y;
uint32_t abstime;
uint32_t reltime; /* in jiffies */
uint8_t interleave;
uint8_t pad0;
uint32_t bits;
uint8_t pad[16];
} AnimHeader;
typedef struct frame {
int num; // which frame number this one is (ie index in ctx->frames[])
char kind[4]; // ILBM or PBM
// ilbm/pbm fields
bool got_bmhd;
BitMapHeader bmhd;
uint8_t* image_data;
uint8_t* mask_data;
uint8_t* cmap_data;
int cmap_len;
bool got_anhd;
AnimHeader anhd;
} frame;
typedef struct context {
// char kind[4]; // "ILBM", "PBM ", "ANIM" (or "root")
int level;
frame** frames;
int nframes;
int capacity;
// a general-purpose buffer
uint8_t* buf;
size_t bufsize;
} context;
static context* ctx_new() {
context* ctx = imalloc(sizeof(context));
memset(ctx,0,sizeof(context));
return ctx;
}
static bool ctx_size_buffer(context* ctx, size_t newsize) {
if (newsize < ctx->bufsize) {
return true; // big enough already
}
if (ctx->buf) {
ifree(ctx->buf);
}
ctx->buf = imalloc(newsize);
if (!ctx->buf) {
ctx->bufsize = 0;
return false;
}
ctx->bufsize = newsize;
return true;
}
static frame* ctx_add_frame(context* ctx, const char* kind)
{
frame* f;
if (ctx->nframes == ctx->capacity) {
// need space to add new child
const int initial_cap = 8;
int newcap = ctx->frames ? ctx->capacity*2 : initial_cap;
frame** newdata = imalloc( sizeof(frame*) * newcap);
if (!newdata) {
return NULL;
}
if (ctx->frames) {
memcpy(newdata, ctx->frames, sizeof(frame*)*ctx->nframes);
ifree(ctx->frames);
}
ctx->frames = newdata;
ctx->capacity = newcap;
}
// create and link in new frame
f = imalloc(sizeof(frame));
if (!f) {
return NULL;
}
memset(f,0,sizeof(frame));
f->num = ctx->nframes;
memcpy( f->kind, kind, 4);
ctx->frames[ctx->nframes] = f;
++ctx->nframes;
return f;
}
static void ctx_free(context* ctx)
{
int i;
if( ctx->frames) {
for( i=0; i<ctx->nframes; ++i) {
frame* f = ctx->frames[i];
if (f->image_data) {
ifree(f->image_data);
}
if (f->mask_data) {
ifree(f->mask_data);
}
if (f->cmap_data) {
ifree(f->cmap_data);
}
ifree(f);
}
ifree(ctx->frames);
}
if( ctx->buf) {
ifree(ctx->buf);
}
ifree(ctx);
}
static inline uint32_t padlen( uint32_t n) {
return (n&1) ? n+1 : n;
}
static inline bool chkcc( const void* a, const void* b) {
const char* aa = a;
const char* bb = b;
return (aa[0]==bb[0]) && (aa[1]==bb[1]) && (aa[2]==bb[2]) && (aa[3]==bb[3]);
}
static bool is_iff(const uint8_t* buf, int nbytes);
static bool match_iff_ext(const char* file_ext);
static im_bundle* read_iff_bundle( im_in* rdr, ImErr* err );
static bool handle_FORM( context* ctx, im_in* rdr, uint32_t chunklen, ImErr* err);
static bool handle_BMHD( context* ctx, im_in* rdr, uint32_t chunklen, ImErr* err);
static bool handle_CAMG( context* ctx, im_in* rdr, uint32_t chunklen, ImErr* err);
static bool handle_CMAP( context* ctx, im_in* rdr, uint32_t chunklen, ImErr* err);
static bool handle_BODY( context* ctx, im_in* rdr, uint32_t chunklen, ImErr* err);
static bool handle_ANHD( context* ctx, im_in* rdr, uint32_t chunklen, ImErr* err);
static bool handle_DLTA( context* ctx, im_in* rdr, uint32_t chunklen, ImErr* err);
static int decodeLine( im_in* rdr, uint8_t* dest, int nbytes);
static bool decodeANIM5chunk(const uint8_t* src, size_t chunklen, uint8_t* dest,
int ncols, int nplanes, int height);
static im_img* frame_to_img(context* ctx, int frameidx, const uint8_t* cmap_data, size_t cmap_len, ImErr* err);
static bool ctx_collect_bundle( context* ctx, im_bundle* out, ImErr* err);
static const char* indent( int n );
#if 0
int dump_chunk( int nest, im_in *rdr );
#endif
struct handler handle_iff = {is_iff, NULL, read_iff_bundle, match_iff_ext};
static int parse_chunk( context* ctx, im_in* rdr, ImErr* err );
static bool is_iff(const uint8_t* buf, int nbytes)
{
if (!chkcc(buf,"FORM") ) {
return false;
}
if (chkcc(buf+8,"ILBM")) {
return true;
}
if (chkcc(buf+8,"PBM ")) {
return true;
}
if (chkcc(buf+8,"ANIM")) {
return true;
}
return false;
}
static bool match_iff_ext(const char* file_ext)
{
if ((istricmp(file_ext,".iff")==0) ||
(istricmp(file_ext,".ilbm")==0) ||
(istricmp(file_ext,".lbm")==0) ||
(istricmp(file_ext,".pbm")==0)) {
return true;
}
return false;
}
static bool handle_FORM( context* ctx, im_in* rdr, uint32_t chunklen, ImErr* err)
{
char kind[4];
context* child = NULL;
int remaining = (int)chunklen;
if (remaining<4) {
*err = IM_ERR_MALFORMED;
return false;
}
if (im_read(rdr, kind,4) != 4) {
*err = IM_ERR_MALFORMED; // TODO: distingush between read error and eof
return false;
}
remaining -= 4;
//printf("%sformtype=%c%c%c%c\n", indent(ctx->level), kind[0], kind[1], kind[2], kind[3]);
if (!chkcc(kind,"ILBM") && !chkcc(kind,"PBM ") && !chkcc(kind,"ANIM")) {
// unsupported FORM type (eg 8SVX sound)- skip it
//printf("%s->skip\n", indent(ctx->level));
if(im_seek(rdr,remaining, SEEK_CUR) != 0 ) {
*err = IM_ERR_FILE;
return false;
}
return true;
}
// starting a new image/frame?
if (chkcc(kind,"ILBM") || chkcc(kind,"PBM ")) {
frame* f = ctx_add_frame(ctx,kind);
if (!f) {
*err = IM_ERR_NOMEM;
return false;
}
}
while (remaining>0) {
int n = parse_chunk(ctx,rdr,err);
if (n < 0) {
return false;
}
remaining -= n;
}
return true;
}
static frame* ctx_curframe(context *ctx) {
if( ctx->frames && ctx->nframes>0) {
return ctx->frames[ctx->nframes-1];
} else {
return NULL;
}
}
static bool handle_BMHD(context* ctx, im_in* rdr, uint32_t chunklen, ImErr *err )
{
BitMapHeader* bmhd;
frame* f = ctx_curframe(ctx);
if( !f) {
*err = IM_ERR_MALFORMED;
return false;
}
bmhd = &f->bmhd;
if (chunklen != 20) {
*err = IM_ERR_MALFORMED;
return false;
}
bmhd->w = im_read_u16be(rdr);
bmhd->h = im_read_u16be(rdr);
bmhd->x = im_read_s16be(rdr);
bmhd->y = im_read_s16be(rdr);
bmhd->nPlanes = im_read_u8(rdr);
bmhd->masking = im_read_u8(rdr);
bmhd->compression = im_read_u8(rdr);
bmhd->pad1 = im_read_u8(rdr);
bmhd->transparentColor = im_read_u16be(rdr);
bmhd->xAspect = im_read_u8(rdr);
bmhd->yAspect = im_read_u8(rdr);
bmhd->pageWidth = im_read_s16be(rdr);
bmhd->pageHeight = im_read_s16be(rdr);
if(im_error(rdr)) {
*err = im_eof(rdr) ? IM_ERR_MALFORMED : IM_ERR_FILE;
return false;
}
//printf("%s %dx%d planes: %d masking: %d compression: 0x%02x\n", indent(ctx->level), bmhd->w, bmhd->h, bmhd->nPlanes, bmhd->masking, bmhd->compression);
f->got_bmhd = true;
return true;
}
static bool handle_CAMG(context* ctx, im_in* rdr, uint32_t chunklen, ImErr *err )
{
uint8_t buf[4];
uint32_t mode;
if( chunklen!=4) {
*err = IM_ERR_MALFORMED;
return false;
}
if (im_read(rdr,buf,4)!=4) {
*err = im_eof(rdr)?IM_ERR_MALFORMED:IM_ERR_FILE;
return false;
}
mode = buf[0] <<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
/*
printf("mode: 0x%08x\n", mode);
if (mode & 0x800) {
printf(" ->HAM\n");
}
*/
return true;
}
static bool handle_CMAP(context* ctx, im_in* rdr, uint32_t chunklen, ImErr *err )
{
uint8_t* data;
frame* f = ctx_curframe(ctx);
if( !f) {
*err = IM_ERR_MALFORMED;
return false;
}
/* if( !ctx->got_bmhd) {
// cmap without bmhd
*err = IM_ERR_MALFORMED;
}
*/
// for now, just stash the chunk verbatim
data = imalloc(chunklen);
if (!data) {
*err = IM_ERR_NOMEM;
return false;
}
if (im_read(rdr,data,chunklen)!=chunklen) {
ifree(data);
*err = IM_ERR_FILE;
return false;
}
if( f->cmap_data) {
ifree(f->cmap_data);
}
f->cmap_data = data;
f->cmap_len = chunklen;
return true;
}
// TODO: FIX! Not reading right num of bytes!
static bool handle_BODY(context* ctx, im_in* rdr, uint32_t chunklen, ImErr *err )
{
int consumed = 0;
int words_per_line, bytes_per_line, data_size;
frame* f = ctx_curframe(ctx);
if( !f) {
*err = IM_ERR_MALFORMED;
return false;
}
// we'll read in and uncompress any data, but for now
// we'll leave it in planar form, to make it easier to
// decode DLTA chunks.
if (!f->got_bmhd) {
*err = IM_ERR_MALFORMED; // got BODY before BMHD
return false;
}
if (f->bmhd.masking !=0) {
//printf("MASKING=%d\n",f->bmhd.masking );
*err = IM_ERR_MALFORMED; // got BODY before BMHD
return false;
}
switch (f->bmhd.compression) {
case cmpByteRun1: break;
case cmpNone: break;
default:
*err = IM_ERR_UNSUPPORTED;
return false;
}
if (f->image_data) {
// already had a BODY chunk?
*err = IM_ERR_MALFORMED;
return false;
}
if (f->bmhd.nPlanes >32) {
*err = IM_ERR_MALFORMED;
return false;
}
// TODO: just slurp chunk into ctx->buf and decode from memory
// actual image data must be multiple of 16 pixels wide
words_per_line = (f->bmhd.w+15)/16;
bytes_per_line = f->bmhd.nPlanes * (words_per_line*2);
data_size = f->bmhd.h * bytes_per_line;
f->image_data = imalloc(data_size);
if( f->bmhd.compression == cmpNone) {
int n = im_read(rdr,f->image_data, data_size);
if (n != data_size) {
// don't need to free image data - context will handle it
*err = IM_ERR_MALFORMED;
return false;
}
consumed += data_size;
} else if (f->bmhd.compression == cmpByteRun1) {
uint8_t* dest = f->image_data;
int y;
// the compression breaks at the end of each line
// but can run across planes.
// TODO: I think the mask is included in the run
for (y=0; y<f->bmhd.h; ++y) {
int n = decodeLine(rdr,dest,bytes_per_line);
if (n<0) {
*err = IM_ERR_MALFORMED;
return false;
}
consumed += n;
dest += bytes_per_line;
}
}
if( consumed<chunklen) {
//printf("%s*** warn: decode left %d bytes\n", indent(ctx->level), chunklen-consumed);
if (im_seek(rdr,chunklen-consumed, SEEK_CUR) != 0 ) {
return false;
}
}
return true;
}
// decode a line of ILBM BODY compression
// TODO: make this memory-based
static int decodeLine( im_in* rdr, uint8_t* dest, int nbytes)
{
int consumed = 0;
while (nbytes>0) {
uint8_t c = im_read_u8(rdr);
if(im_error(rdr)) {
return -1;
}
consumed++;
if( c==128) {
break;
}
if (c>128) {
int i;
int cnt = (257-c);
uint8_t v;
if (nbytes<cnt) {
return false; // overrun
}
v = im_read_u8(rdr);
if(im_error(rdr)) {
return false;
}
consumed++;
for (i=0; i<cnt; ++i) {
*dest++ = v;
}
nbytes -= cnt;
} else {
int cnt = c+1;
if (nbytes<cnt) {
return -1; // overrun
}
if (im_read( rdr, dest, cnt) != cnt) {
return -1;
}
consumed += cnt;
dest += cnt;
nbytes -= cnt;
}
}
// printf("%d bytes left empty\n",nbytes);
return consumed;
}
static bool handle_ANHD( context* ctx, im_in* rdr, uint32_t chunklen, ImErr* err)
{
frame* f = ctx_curframe(ctx);
AnimHeader* anhd;
if( !f) {
*err = IM_ERR_MALFORMED;
return false;
}
anhd = &f->anhd;
anhd->operation = im_read_u8(rdr);
anhd->mask = im_read_u8(rdr);
anhd->w = im_read_u16be(rdr);
anhd->h = im_read_u16be(rdr);
anhd->x = im_read_s16be(rdr);
anhd->y = im_read_s16be(rdr);
anhd->abstime = im_read_u32be(rdr);
anhd->reltime = im_read_u32be(rdr);
anhd->interleave = im_read_u8(rdr);
anhd->pad0 = im_read_u8(rdr);
anhd->bits = im_read_u32be(rdr);
im_read(rdr,anhd->pad,16);
if(im_error(rdr)) {
*err = im_eof(rdr) ? IM_ERR_MALFORMED : IM_ERR_FILE;
return false;
}
/*
printf("%s op: 0x%02x mask: 0x%02x %d,%d %dx%d reltime: %d interleave: 0x%02x, bits: 0x%08x\n",
indent(ctx->level),
anhd->operation,
anhd->mask,
anhd->x,
anhd->y,
anhd->w,
anhd->h,
anhd->reltime,
anhd->interleave,
anhd->bits);
*/
f->got_anhd = true;
return true;
}
static bool handle_DLTA( context* ctx, im_in* rdr, uint32_t chunklen, ImErr* err)
{
frame* first;
frame* cur;
frame* from; //
int fromidx;
int wordswide;
int image_size;
int height;
int nplanes;
int num_cols;
if( !ctx_size_buffer(ctx,chunklen)) {
*err = IM_ERR_NOMEM;
return false;
}
if(im_read(rdr,ctx->buf,chunklen)!=chunklen) {
*err = im_eof(rdr) ? IM_ERR_MALFORMED: IM_ERR_FILE;
return false;
}
if (ctx->nframes < 1) {
*err = IM_ERR_MALFORMED;
return false;
}
first = ctx->frames[0];
cur = ctx_curframe(ctx);
if (!first || !cur || !first->got_bmhd || !cur->got_anhd) {
*err = IM_ERR_MALFORMED;
return false;
}
if (cur->anhd.operation != 0x05) {
// we currently only handle ANIM5
*err = IM_ERR_UNSUPPORTED;
return false;
}
// first, copy the frame we're deltaing from...
if (cur->anhd.interleave==0) {
fromidx = cur->num - 2; // default is two frames back
} else {
fromidx = cur->num - cur->anhd.interleave;
}
if (fromidx<0) {
fromidx = 0;
// *err = IM_ERR_MALFORMED;
// return false;
}
//printf("copy from frame %d\n",fromidx);
from = ctx->frames[fromidx];
if(!from->image_data) {
*err = IM_ERR_MALFORMED;
return false;
}
wordswide = (first->bmhd.w +15)/16;
height = first->bmhd.h;
nplanes = first->bmhd.nPlanes;
image_size = (wordswide*2)*nplanes*height;
cur->image_data = imalloc(image_size);
if (!cur->image_data) {
*err = IM_ERR_NOMEM;
return false;
}
memcpy(cur->image_data, from->image_data, image_size);
// leave the colourmaps for now - handle later, during im_img generation.
// now decode the delta upon the image
num_cols = ((first->bmhd.w + 15)/16)*2; // NOTE: always even number of cols
if (!decodeANIM5chunk(ctx->buf, chunklen,
cur->image_data,
num_cols,
first->bmhd.nPlanes,
first->bmhd.h))
{
*err = IM_ERR_MALFORMED;
return false;
}
return true;
}
static bool decodeANIM5chunk(const uint8_t* src, size_t srclen, uint8_t* dest,
int ncols, int nplanes, int height)
{
int plane;
uint32_t pos;
uint32_t start[8];
int pitch = ncols*nplanes; // dest: bytes per line
// first, unpack the 8 src pointers (and ignore 8 unused ones)
if (srclen<16*4) {
return false;
}
for (plane=0; plane<nplanes; ++plane) {
const uint8_t* p = src + plane*4;
start[plane] = (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
//printf("plane %d @ 0x%08x\n",plane,start[plane]);
}
// dump
/*
int j;
for( j=0; j<srclen; ++j) {
if( (j&7) == 0 ) {
printf("\n%08x: ",j);
}
printf("%02x ",src[j]);
}
printf("\n");
*/
for (plane=0; plane<nplanes; ++plane) {
pos = start[plane];
//printf("plane %d (pos %d)\n", plane,pos);
if (!pos) {
continue;
}
int col;
int y;
uint8_t op;
for (col=0; col<ncols; ++col) {
uint8_t* out = dest + (ncols*plane) + col;
int opcnt;
int i;
if (pos+1>srclen) {
//printf("expect more cols\n");
return false;
}
opcnt = (int)src[pos++];
//printf(" col %d/%d (%d ops)\n", col, ncols, opcnt);
y=0;
for (i=0; i<opcnt; ++i) {
//printf(" y=%d\n", y);
if (pos+1 > srclen) {
return false;
}
op = src[pos++];
if (op==0) {
uint8_t cnt,val;
// repeat
if (pos+2 > srclen) {
return false;
}
cnt = src[pos++];
val = src[pos++];
//printf(" rep %d %d\n", cnt,val);
if (y+cnt > height) {
return false;
}
while (cnt>0) {
*out = val;
out += pitch;
++y;
--cnt;
}
} else if (op<128) {
// skip
int cnt = (int)op;
//printf(" skip %d\n", cnt);
if (y+cnt > height) {
return false;
}
out += pitch*cnt;
y+=cnt;
} else if (op>=128) {
int cnt = (int)(op&0x7f);
//printf(" uniq %d\n", cnt);
if (pos+cnt > srclen || y+cnt > height ) {
return false;
}
while (cnt>0) {
*out = src[pos++];
out += pitch;
--cnt;
++y;
}
}
}
}
}
#if 0
// fetch
// <128 :skip
// 0, cnt, val : repeat val cnt times
// <=128 : copy (n & 0x7f) bytes
#endif
return true;
}
// process a single chunk and all it's children, returns num of bytes consumed (excluding any pad byte)
static int parse_chunk( context* ctx, im_in* rdr, ImErr* err ) {
uint8_t buf[8];
uint32_t chunklen;
bool success;
int consumed = 0;
if(im_read(rdr, buf,8) != 8) {
// FUDGE for malformed files (eg Juggler.anim)
// we'll be lenient if file ends prematurely,
// but on a chunk boundary.
if (im_eof(rdr)) {
return chunklen;
}
*err = IM_ERR_FILE;
return -1;
}
consumed += 8;
chunklen = ((uint32_t)buf[4])<<24 |
((uint32_t)buf[5])<<16 |
((uint32_t)buf[6])<<8 |
((uint32_t)buf[7]);
//printf("%s%c%c%c%c [%d bytes]\n", indent(ctx->level), buf[0], buf[1], buf[2], buf[3], chunklen );
if (chkcc(buf,"FORM")) {
// printf("%d: enter FORM\n", ctx->level);
++ctx->level;
// FORM chunks have children
success = handle_FORM(ctx,rdr,chunklen, err);
--ctx->level;
// printf("%d: exit FORM (%d)\n", ctx->level, success?1:0);
} else if (chkcc(buf,"BMHD")) {
success = handle_BMHD(ctx,rdr,chunklen, err);
} else if (chkcc(buf,"CMAP")) {
success = handle_CMAP(ctx,rdr,chunklen, err);
} else if (chkcc(buf,"CAMG")) {
success = handle_CAMG(ctx,rdr,chunklen, err);
} else if (chkcc(buf,"BODY")) {
success = handle_BODY(ctx,rdr,chunklen, err);
} else if (chkcc(buf,"ANHD")) {
success = handle_ANHD(ctx,rdr,chunklen, err);
} else if (chkcc(buf,"DLTA")) {
success = handle_DLTA(ctx,rdr,chunklen, err);
} else {
// unknown/unhandled chunk type. skip it.
if (im_seek(rdr, chunklen, SEEK_CUR)!=0) {
*err = IM_ERR_FILE;
success = false;
} else {
success = true;
}
}
if (!success) {
// printf("parse_chunk(%c%c%c%c) failed\n", buf[0], buf[1], buf[2], buf[3]);
return -1;
}
consumed += chunklen;
if (chunklen&1) {
// read the pad byte
if (im_seek(rdr, 1, SEEK_CUR)!=0) {
*err = IM_ERR_MALFORMED;
return -1;
}
consumed += 1;
}
// +8 to account for chunk header
return (int)consumed;
}
static im_bundle* read_iff_bundle( im_in* rdr, ImErr* err )
{
/*
dump_chunk(0,rdr);
*err = (ImErr)69;
return false;
*/
im_bundle* bundle = NULL;
context* ctx = ctx_new();
if (!ctx) {
*err = IM_ERR_NOMEM;
goto bailout;
}
if (parse_chunk(ctx,rdr,err)<0 ) {
goto bailout;
}
bundle = im_bundle_new();
if (!bundle)
{
*err = IM_ERR_NOMEM;
goto bailout;
}
if (!ctx_collect_bundle(ctx, bundle, err)) {
goto bailout;
}
ctx_free(ctx);
return bundle;
bailout:
if (ctx) {
ctx_free(ctx);
}
if( bundle) {
im_bundle_free(bundle);
}
return NULL;
}
static bool ctx_collect_bundle( context* ctx, im_bundle* out, ImErr* err)
{
int i;
uint8_t cmap_buf[3*256];
size_t cmap_len = 0;
im_img* img = NULL;
for (i=0; i<ctx->nframes; ++i) {
frame* f = ctx->frames[i];
//printf("add frame %d\n", i);
// track latest cmap as we go
if (f->cmap_data) {
if (f->cmap_len>sizeof(cmap_buf)) {
*err = IM_ERR_MALFORMED;
}
memcpy(cmap_buf, f->cmap_data, f->cmap_len);
if( f->cmap_len>cmap_len) {
cmap_len = f->cmap_len;
}
}
img = frame_to_img(ctx, i, cmap_buf, cmap_len, err);
if (img) {
SlotID id = {0};
id.frame = f->num;
if (!im_bundle_set(out,id,img)) {
*err = IM_ERR_NOMEM;
return false;
}
} else {
return false;
}
}
return true;
}
static im_img* frame_to_img(context* ctx, int frameidx, const uint8_t* cmap_data, size_t cmap_len, ImErr* err)
{
int y;
int plane_pitch;
frame* f;
frame* first;
BitMapHeader* bmhd;
f = ctx->frames[frameidx];
// need the BMHD chunk from the first frame
first = ctx->frames[0];
if( !first->got_bmhd || !f->image_data) {
*err = IM_ERR_MALFORMED;
return NULL;
}
bmhd = &first->bmhd;
// TODO:
if( bmhd->nPlanes>8) {
*err = IM_ERR_UNSUPPORTED;
return NULL;
}
// TODO: handle 24 and 32-plane images (IM_FMT_RGB and IM_FMT_RGBA)
im_img* img = im_img_new(bmhd->w, bmhd->h, 1, IM_FMT_INDEX8);
if( !img) {
*err = IM_ERR_NOMEM;
return NULL;
}
if (chkcc(f->kind,"ILBM")) {
int plane_pitch = 2*((bmhd->w+15)/16);
// convert planar data to 8bit indexed
// TODO: handle/skip mask plane!
for (y=0; y<bmhd->h; ++y) {
uint8_t* dest = im_img_row(img,y);
int x;
for( x=0; x<bmhd->w; ++x) {
const uint8_t* src = f->image_data + (y*plane_pitch*bmhd->nPlanes) + x/8;
int srcbit = 7 - (x&7);
int destbit;
uint8_t pix=0;
for(destbit = 0; destbit<bmhd->nPlanes; ++destbit) {
pix |= (((*src) >> srcbit)&0x01)<<destbit;
src += plane_pitch;
}
*dest = pix;
++dest;
}
}
} else if (chkcc(f->kind,"PBM ")) {
const uint8_t* src = f->image_data;
for (y=0; y<bmhd->h; ++y) {