-
Notifications
You must be signed in to change notification settings - Fork 2
/
3docel.c
1341 lines (1132 loc) · 27.7 KB
/
3docel.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
/*
* 3docel.c: Reads old 3DO Cel files.
*
* Uses information obtained from the 3DO Portfolio programmers
* toolkit, which may not be strictly kosher.
*
* Leo L. Schwab 2012.10.02
*/
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <errno.h>
#include <err.h>
#include <glib/gstdio.h>
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include "3docel.h"
#define LOAD_PROC "file-3DO-load"
/***************************************************************************
* Structure definitions.
*/
typedef struct pxdata {
uint16_t pel; /* Raw value from cel data */
uint16_t p1555; /* After decoding through PLUT. */
uint8_t p; /* P-mode value */
uint8_t mr, mg, mb; /* Multiply value. */
uint8_t dr, dg, db; /* Divide value. */
uint8_t equ_alpha; /* Equivalent alpha value. */
} pxdata;
typedef struct chunkdata {
uint8_t *base;
uint32_t size;
uint32_t idx;
int8_t bit_idx; /* Index into current byte (7-0) */
} chunkdata;
typedef struct celfile {
GError **error;
FILE *file;
char const *filename;
} celfile;
typedef struct celinfo {
struct CCC_chunk *ccc;
struct PLUT_chunk *plut;
struct PDAT_chunk *pdat;
uint32_t PRE0, PRE1;
uint16_t width, height; /* Computed from cel data */
uint8_t bpp;
uint8_t packed;
uint8_t indexed; /* "Coded" in 3DO parlance */
uint8_t rep8;
uint8_t pluta; /* Pre-shifted */
} celinfo;
typedef struct pstore_context {
uint8_t *dest;
uint8_t *cur;
GimpImageBaseType base_type;
GimpImageType img_type;
gint32 gimage;
gint32 glayer;
uint32_t stride;
uint8_t nchan;
} pstore_context;
typedef struct decode_context {
struct celinfo ci;
struct chunkdata cd;
struct pstore_context pstore;
GError **error;
int framenum;
int frametime; /* Milliseconds. */
} decode_context;
struct cccflag {
uint32_t flag;
char *name;
};
#define DEFFLAG(name) { MASKEXPAND (name), #name }
#define NUMOF(array) (sizeof (array) / sizeof (*(array)))
static int decode_cel (struct decode_context *ctx);
/***************************************************************************
* Print some celinfo in human readable form.
*/
static struct cccflag cccflags[] = {
DEFFLAG (CCB_SKIP),
DEFFLAG (CCB_LAST),
DEFFLAG (CCB_NXTPTRTYPE),
DEFFLAG (CCB_SRCPTRTYPE),
DEFFLAG (CCB_PLUTPTRTYPE),
DEFFLAG (CCB_LDSIZE),
DEFFLAG (CCB_LDPRS),
DEFFLAG (CCB_LDPPMP),
DEFFLAG (CCB_LDPLUT),
DEFFLAG (CCB_PRELOC),
DEFFLAG (CCB_YOXY),
DEFFLAG (CCB_ACSC),
DEFFLAG (CCB_ALSC),
DEFFLAG (CCB_ACW),
DEFFLAG (CCB_ACCW),
DEFFLAG (CCB_TWD),
DEFFLAG (CCB_LCE),
DEFFLAG (CCB_ACE),
DEFFLAG (CCB_MARIA),
DEFFLAG (CCB_PXOR),
DEFFLAG (CCB_USEAV),
DEFFLAG (CCB_PACKED),
DEFFLAG (CCB_PLUTPOS),
DEFFLAG (CCB_BGND),
DEFFLAG (CCB_NOBLK),
};
static void
dump_ccc_flags (struct CCC_chunk *ccc)
{
register uint32_t flags;
int i;
flags = ccc->ccc_Flags;
for (i = NUMOF (cccflags); --i >= 0; ) {
if (flags & cccflags[i].flag) {
printf ("%s\n", cccflags[i].name);
}
}
}
/***************************************************************************
* Incremental reading of a memory buffer
*/
static void
init_chunkdata (struct chunkdata *cd, uint8_t *base, uint32_t size)
{
cd->base = base;
cd->size = size;
cd->idx = 0;
cd->bit_idx = 7;
}
/*
* Read next N bits from chunkdata buffer. MSBs are consumed first.
*/
static int
get_next_nbits (
uint32_t *retval,
struct chunkdata *cd,
int nbits
)
{
uint32_t val = 0;
int n;
while (nbits) {
if (cd->idx + sizeof (uint8_t) >= cd->size)
return EOF;
n = nbits > cd->bit_idx + 1 ? cd->bit_idx + 1 : nbits;
/*
* We're abusing the GetBF() macro, which normally takes a
* partial ternary expression of two constants (i.e. 7:4).
*/
val |= GetBF (cd->base[cd->idx],
(cd->bit_idx) : (cd->bit_idx - n + 1));
/* This byte exhausted; advance to next one. */
if ((cd->bit_idx -= n) < 0) {
++cd->idx;
cd->bit_idx = 7;
}
/* Bits yet to get; make room for them. */
if ((nbits -= n) > 0)
val <<= nbits;
}
*retval = val;
return 0;
}
static int
unpk_flush_rest_of_word (struct chunkdata *cd)
{
uint32_t newidx;
newidx = cd->idx;
if (cd->bit_idx < 7) {
++newidx;
}
/* Bump index up to next uint32_t boundary. */
newidx = (newidx + 3) & ~3;
if (newidx >= cd->size)
return EOF;
cd->idx = newidx;
cd->bit_idx = 7;
return 0;
}
static int
get_u32 (uint32_t *val, struct chunkdata *cd)
{
register uint8_t *src;
if (cd->idx + sizeof (uint32_t) >= cd->size)
return EOF;
src = cd->base + cd->idx;
*val = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
cd->idx += sizeof (uint32_t);
cd->bit_idx = 7;
return 0;
}
static int
get_u16 (uint16_t *val, struct chunkdata *cd)
{
register uint8_t *src;
if (cd->idx + sizeof (uint16_t) >= cd->size)
return EOF;
src = cd->base + cd->idx;
*val = (src[0] << 8) | src[1];
cd->idx += sizeof (uint16_t);
cd->bit_idx = 7;
return 0;
}
static int
get_u8 (uint8_t *val, struct chunkdata *cd)
{
register uint8_t *src;
if (cd->idx + sizeof (uint8_t) >= cd->size)
return EOF;
src = cd->base + cd->idx;
*val = *src;
++cd->idx;
cd->bit_idx = 7;
return 0;
}
/***************************************************************************
* Chunk reading and parsing.
*/
static int
skip_chunk_payload (struct chunk_header *hdr, struct celfile *cf)
{
if (fseek (cf->file, hdr->size - sizeof (*hdr), SEEK_CUR) < 0)
return -errno;
return 0;
}
static int
read_n_bytes (void *buf, size_t siz, struct celfile *cf)
{
size_t n;
n = fread (buf, 1, siz, cf->file);
if (n != siz) {
g_set_error (cf->error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"%s: Unexpected end of file.",
gimp_filename_to_utf8 (cf->filename));
return -EINVAL;
}
return 0;
}
static int
read_chunk_header (struct chunk_header *hdr, struct celfile *cf)
{
size_t n;
n = fread (hdr, 1, sizeof (*hdr), cf->file);
if (!n) {
return EOF;
} else if (n != sizeof (*hdr)) {
return -EINVAL;
}
/* Convert fields to local representation. */
hdr->size = be32toh (hdr->size);
return 0;
}
static int
read_chunk_payload (
void **cl_buf,
struct chunk_header *hdr,
struct celfile *cf
)
{
/* Mind the typecasting... */
chunk_header *buf;
int e;
if (!(buf = malloc (hdr->size)))
return -ENOMEM;
*buf = *hdr;
if (e = read_n_bytes (buf + 1,
hdr->size - sizeof (chunk_header),
cf))
{
free (buf);
return e;
}
*cl_buf = (void *) buf;
return 0;
}
static int
read_chunk_CCC (
struct CCC_chunk **cl_CCC,
struct chunk_header *hdr,
struct celfile *cf
)
{
struct CCC_chunk *ccc;
int e;
if (e = read_chunk_payload ((void **) &ccc, hdr, cf))
return e;
/* Convert fields to local representation. */
ccc->ccc_version = be32toh (ccc->ccc_version);
ccc->ccc_Flags = be32toh (ccc->ccc_Flags);
ccc->ccc_NextPtr = be32toh (ccc->ccc_NextPtr);
ccc->ccc_CelData = be32toh (ccc->ccc_CelData);
ccc->ccc_PLUTPtr = be32toh (ccc->ccc_PLUTPtr);
ccc->ccc_X = be32toh (ccc->ccc_X);
ccc->ccc_Y = be32toh (ccc->ccc_Y);
ccc->ccc_hdx = be32toh (ccc->ccc_hdx);
ccc->ccc_hdy = be32toh (ccc->ccc_hdy);
ccc->ccc_vdx = be32toh (ccc->ccc_vdx);
ccc->ccc_vdy = be32toh (ccc->ccc_vdy);
ccc->ccc_ddx = be32toh (ccc->ccc_ddx);
ccc->ccc_ddy = be32toh (ccc->ccc_ddy);
ccc->ccc_PPMP1 = be16toh (ccc->ccc_PPMP1);
ccc->ccc_PPMP0 = be16toh (ccc->ccc_PPMP0);
ccc->ccc_PRE0 = be32toh (ccc->ccc_PRE0);
ccc->ccc_PRE1 = be32toh (ccc->ccc_PRE1);
ccc->ccc_Width = be32toh (ccc->ccc_Width);
ccc->ccc_Height = be32toh (ccc->ccc_Height);
*cl_CCC = ccc;
return 0;
}
static int
read_chunk_PLUT (
struct PLUT_chunk **cl_plut,
struct chunk_header *hdr,
struct celfile *cf
)
{
struct PLUT_chunk *plut;
int32_t maxentries;
int e;
int i;
if (e = read_chunk_payload ((void **) &plut, hdr, cf))
return e;
/*
* Independently calculate the number of PLUT entries based on the
* chunk size.
*/
maxentries = (plut->header.size
- sizeof (plut->header)
- sizeof (plut->numentries))
/ sizeof (XRGB1555);
/* Convert fields to local representation. */
plut->numentries = be32toh (plut->numentries);
if (plut->numentries > maxentries) {
g_set_error (cf->error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"%s: File corrupt (bad PLUT)",
gimp_filename_to_utf8 (cf->filename));
free (plut);
return -EINVAL;
}
for (i = 0; i < plut->numentries; ++i)
plut->PLUT[i] = be16toh (plut->PLUT[i]);
*cl_plut = plut;
return 0;
}
static int
read_chunk_PDAT (
struct PDAT_chunk **cl_pdat,
struct chunk_header *hdr,
struct celfile *cf
)
{
struct PDAT_chunk *pdat;
int e;
uint8_t *buf;
if (e = read_chunk_payload ((void **) &pdat, hdr, cf)) {
if (e == -ENOMEM)
g_set_error (cf->error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Out of memory.");
return e;
}
*cl_pdat = pdat;
return 0;
}
static int
read_chunk_ANIM (
struct ANIM_chunk **cl_anim,
struct chunk_header *hdr,
struct celfile *cf
)
{
struct ANIM_chunk *anim;
int e;
uint8_t *buf;
if (e = read_chunk_payload ((void **) &anim, hdr, cf)) {
if (e == -ENOMEM)
g_set_error (cf->error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Out of memory.");
return e;
}
/* Convert fields to local representation. */
anim->version = be32toh (anim->version);
anim->animType = be32toh (anim->animType);
anim->numFrames = be32toh (anim->numFrames);
anim->frameRate = be32toh (anim->frameRate);
anim->startFrame = be32toh (anim->startFrame);
anim->numLoops = be32toh (anim->numLoops);
*cl_anim = anim;
return 0;
}
static int
parse_file (struct decode_context *ctx, struct celfile *cf)
{
chunk_header ckhdr;
ANIM_chunk *anim;
int e = 0;
uint8_t *pdat;
uint8_t seen_first_chunk = 0;
while (!(e = read_chunk_header (&ckhdr, cf))) {
if (!seen_first_chunk) {
switch (ckhdr.ID) {
case CHUNK_CCB:
ctx->framenum = 0;
break;
case CHUNK_ANIM:
ctx->framenum = 1;
break;
default:
g_set_error
(cf->error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"%s: Not a 3DO cel file.",
gimp_filename_to_utf8 (cf->filename));
return -EINVAL;
}
seen_first_chunk = 1;
}
switch (ckhdr.ID) {
case CHUNK_CCB:
if (ctx->ci.ccc) {
free (ctx->ci.ccc);
ctx->ci.ccc = NULL;
}
if (e = read_chunk_CCC (&ctx->ci.ccc, &ckhdr, cf))
return e;
break;
case CHUNK_PLUT:
if (ctx->ci.plut) {
free (ctx->ci.plut);
ctx->ci.plut = NULL;
}
if (e = read_chunk_PLUT (&ctx->ci.plut, &ckhdr, cf))
return e;
break;
case CHUNK_PDAT:
if (ctx->ci.pdat) {
free (ctx->ci.pdat);
ctx->ci.pdat = NULL;
}
if (e = read_chunk_PDAT (&ctx->ci.pdat, &ckhdr, cf))
return e;
if (ctx->framenum) {
/*
* In an ANIM, a successful load of PDAT means
* it's time to start interpreting the cel
* data.
*/
if (e = decode_cel (ctx))
return e;
++ctx->framenum;
}
break;
case CHUNK_ANIM:
/* We're just picking up the frame rate for now. */
if (e = read_chunk_ANIM (&anim, &ckhdr, cf))
return e;
ctx->frametime = anim->frameRate * 1000 / 60;
free (anim);
break;
case CHUNK_XTRA:
default:
if (e = skip_chunk_payload (&ckhdr, cf))
return e;
break;
}
}
if (!ctx->framenum && e == EOF) {
/*
* Some (broken) cel files have the PLUT after the PDAT (I'm
* looking at *you*, @burgerbecky). If this is not an ANIM,
* defer cel decoding until EOF when we (hopefully) have all
* the chunks we need.
*/
int ed;
if (ed = decode_cel (ctx))
return ed;
}
return e;
}
/***************************************************************************
* Pixel decoding. This is actually a big pain in the neck.
*/
static inline uint8_t
convert_divide_field (uint8_t fieldval)
{
switch (fieldval) {
case PPMPC_SF_16: return 16;
case PPMPC_SF_2: return 2;
case PPMPC_SF_4: return 4;
case PPMPC_SF_8: return 8;
}
}
void
expand_pixel_coded (struct pxdata *pd, struct celinfo *ci, uint16_t val)
{
uint16_t val5;
uint16_t plutval;
uint16_t ppmp;
uint8_t pluta;
pd->pel = val;
/* Compose 5-bit index value. */
val5 = val & 0x1F;
switch (ci->bpp) {
case 1:
val5 |= ci->pluta & 0x1E;
break;
case 2:
val5 |= ci->pluta & 0x1C;
break;
case 4:
val5 |= ci->pluta & 0x10;
break;
default:
break;
}
/* Obtain the P value. */
plutval = ci->plut->PLUT[val5];
switch (ci->bpp) {
case 1:
case 2:
case 4:
pd->p = (plutval & 0x8000) ? 1 : 0;
break;
case 6:
pd->p = (val & 0x20) ? 1 : 0;
break;
case 8:
pd->p = 0;
break;
case 16:
pd->p = (val & 0x8000) ? 1 : 0;
break;
}
ppmp = pd->p ? ci->ccc->ccc_PPMP1 : ci->ccc->ccc_PPMP0;
/* Obtain Primary Multiply Value(s). */
switch (FIELD2VAL (PPMPC, MS, ppmp)) {
case PPMPC_MS_CCB:
pd->mr =
pd->mg =
pd->mb = FIELD2VAL (PPMPC, MF, ppmp) + 1;
break;
case PPMPC_MS_PIN:
switch (ci->bpp) {
case 1:
case 2:
case 4:
case 6:
pd->mr =
pd->mg =
pd->mb = 1;
break;
case 8:
pd->mr =
pd->mg =
pd->mb = (val >> 5 & 0x7) + 1;
break;
case 16:
pd->mr = (val >> 11 & 0x7) + 1;
pd->mg = (val >> 8 & 0x7) + 1;
pd->mb = (val >> 5 & 0x7) + 1;
break;
}
break;
case PPMPC_MS_PDC:
case PPMPC_MS_PDC_MFONLY:
pd->mr = (plutval >> 10 & 0x7) + 1;
pd->mg = (plutval >> 5 & 0x7) + 1;
pd->mb = (plutval & 0x7) + 1;
break;
}
/* Obtain Primary Divide Value(s). */
switch (FIELD2VAL (PPMPC, MS, ppmp)) {
case PPMPC_MS_CCB:
case PPMPC_MS_PIN:
case PPMPC_MS_PDC_MFONLY:
pd->dr =
pd->dg =
pd->db = convert_divide_field (FIELD2VAL (PPMPC, SF, ppmp));
case PPMPC_MS_PDC:
pd->dr = convert_divide_field (plutval >> 13 & 3);
pd->dg = convert_divide_field (plutval >> 8 & 3);
pd->db = convert_divide_field (plutval >> 3 & 3);
break;
}
}
/***************************************************************************
* Pixel data storage, i.e. storing pixels to the destination buffer(s).
*/
static inline void
pstore_set_curptr (
struct pstore_context *pctx,
struct celinfo *ci,
int x,
int y
)
{
pctx->cur = pctx->dest + pctx->stride * y + x * pctx->nchan;
}
static void
write_next_pixel (struct decode_context *ctx, uint32_t val)
{
register uint8_t *p;
p = ctx->pstore.cur;
if (val == ~0) {
/* Declared transparency output from unpacker. */
if (ctx->ci.indexed) {
*p++ = 0;
if (ctx->pstore.nchan > 1)
*p++ = 0;
} else {
*p++ = 0;
*p++ = 0;
*p++ = 0;
if (ctx->pstore.nchan > 3)
*p++ = 0;
}
} else {
/* Indexed ("coded") pixels. */
if (ctx->ci.indexed) {
*p++ = (uint8_t) val;
if (ctx->pstore.nchan > 1)
*p++ = ~0;
} else {
/* Uncoded pixels. */
if (ctx->ci.bpp == 16) {
*p++ = ((val & 0x7C00) >> 7) | ((val & 0x7000) >> 12);
*p++ = ((val & 0x03E0) >> 2) | ((val & 0x0380) >> 7);
*p++ = ((val & 0x001F) << 3) | ((val & 0x001C) >> 2);
if (ctx->pstore.nchan > 3)
*p++ = ~0;
} else {
/* bpp == 8 */
if (ctx->ci.rep8) {
uint8_t w;
w = val & 0xE0;
*p++ = w | (w >> 3) | (w >> 6);
w = val & 0x1C;
*p++ = (w << 3) | w | (w >> 3);
w = val & 0x03;
*p++ = (w << 6) | (w << 4) | (w << 2) | w;
if (ctx->pstore.nchan > 3)
*p++ = ~0;
} else {
*p++ = val & 0xE0;
*p++ = (val & 0x1C) << 3;
*p++ = (val & 0x03) << 6;
if (ctx->pstore.nchan > 3)
*p++ = ~0;
}
}
}
}
ctx->pstore.cur = p;
}
/***************************************************************************
* Pixel data decoding.
*/
/*
* A packed row of pixels has the following format:
* 8- or 10-bit offset to next row of pixels.
* do {
* 2-bit packing control.
* 6-bit count of output pixels.
* pixel value(s).
* } while (row_not_done);
*/
static int
unpk_get_offset (int *cl_offset, struct celinfo *ci, struct chunkdata *cd)
{
int e;
uint16_t v16;
uint8_t v8;
switch (ci->bpp) {
case 1:
case 2:
case 4:
case 6:
if (!(e = get_u8 (&v8, cd)))
*cl_offset = (int) v8;
return e;
case 8:
case 16:
if (!(e = get_u16 (&v16, cd)))
*cl_offset = (int) v16;
return e;
default:
return -EINVAL;
}
}
static int
unpk_row (struct decode_context *ctx, struct chunkdata *cd, int *calc_width)
{
uint32_t packctl;
uint32_t pixcnt;
uint32_t val;
uint32_t max_idx;
uint16_t width = 0;
int next_row_offset;
int e;
int i;
max_idx = cd->idx;
if (e = unpk_get_offset (&next_row_offset, &ctx->ci, cd))
return e;
next_row_offset += 2; // HW fencepost.
max_idx += next_row_offset * sizeof (uint32_t);
do {
if (e = get_next_nbits (&packctl, cd, 2)) {
if (e == EOF)
break;
return e;
}
if (packctl != PACK_EOL) {
if (e = get_next_nbits (&pixcnt, cd, 6))
return e;
++pixcnt; // HW fencepost.
width += pixcnt;
}
switch (packctl) {
case PACK_EOL:
break;
case PACK_LITERAL:
for (i = pixcnt; --i >= 0; ) {
if (e = get_next_nbits (&val, cd, ctx->ci.bpp))
return e;
if (!calc_width)
write_next_pixel (ctx, val);
}
break;
case PACK_TRANSPARENT:
for (i = pixcnt; --i >= 0; ) {
/* Write zeros with alpha=0 to canvas. */
if (!calc_width)
write_next_pixel (ctx, ~0);
}
break;
case PACK_PACKED:
if (e = get_next_nbits (&val, cd, ctx->ci.bpp))
return e;
for (i = pixcnt; --i >= 0; ) {
if (!calc_width)
write_next_pixel (ctx, val);
}
break;
}
} while (packctl != PACK_EOL && cd->idx < max_idx);
if (calc_width)
*calc_width = width;
/* Skip past unused bits, if any. */
cd->idx = max_idx;
cd->bit_idx = 7;
return 0;
}
static int
decode_unpacked (struct decode_context *ctx)
{
uint32_t val;
int x, y;
int e;
for (y = 0; y < ctx->ci.height; ++y) {
pstore_set_curptr (&ctx->pstore, &ctx->ci, 0, y);
for (x = 0; x < ctx->ci.width; ++x) {
if (e = get_next_nbits (&val, &ctx->cd, ctx->ci.bpp))
return e;
write_next_pixel (ctx, val);
}
if (e = unpk_flush_rest_of_word (&ctx->cd))
return e;
}
return 0;
}
static int
decode_packed (struct decode_context *ctx)
{
int y;
int e;
for (y = 0; y < ctx->ci.height; ++y) {
pstore_set_curptr (&ctx->pstore, &ctx->ci, 0, y);
if (e = unpk_row (ctx, &ctx->cd, NULL))
return e;
}
}
/*
* This routine is called when we have collected a valid CCB, PLUT, and PDAT
* chunk, and attached them to the celinfo.
*/
static int
decode_cel (struct decode_context *ctx)
{
GimpDrawable *gdraw;
GimpPixelRgn pixel_rgn;
struct CCC_chunk *ccc;
int e;
gchar *framename;
ccc = ctx->ci.ccc;
ctx->ci.pluta = FIELD2VAL (CCB, PLUTA, ccc->ccc_Flags) << 1;
ctx->ci.packed = FIELD2VAL (CCB, PACKED, ccc->ccc_Flags);
init_chunkdata (&ctx->cd,
ctx->ci.pdat->data,
ctx->ci.pdat->header.size - sizeof (chunk_header));
if (FIELD2VAL (CCB, PRELOC, ccc->ccc_Flags)) {
ctx->ci.PRE0 = ccc->ccc_PRE0;
ctx->ci.PRE1 = ccc->ccc_PRE1;
} else {
/*
* First two uint32s of "pixel" data are the preamble words.
*/
e = get_u32 (&ctx->ci.PRE0, &ctx->cd);
if (!ctx->ci.packed)
e |= get_u32 (&ctx->ci.PRE1, &ctx->cd);
if (e)
return e;
}
ctx->ci.rep8 = FIELD2VAL (PRE0, REP8, ctx->ci.PRE0);
ctx->ci.indexed = !FIELD2VAL (PRE0, LINEAR, ctx->ci.PRE0);
if (ctx->ci.indexed) {
if (!ctx->ci.plut) {
g_set_error (ctx->error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"File corrupt (missing PLUT)");
return -EINVAL;
}
switch (FIELD2VAL (PRE0, BPP, ctx->ci.PRE0)) {
case PRE0_BPP_1:
ctx->ci.bpp = 1; break;
case PRE0_BPP_2:
ctx->ci.bpp = 2; break;
case PRE0_BPP_4:
ctx->ci.bpp = 4; break;
case PRE0_BPP_6:
ctx->ci.bpp = 6; break;
case PRE0_BPP_8:
case PRE0_BPP_16:
g_set_error (ctx->error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Coded 8/16 cels not yet supported.");
return -EINVAL;
default:
g_set_error (ctx->error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Invalid BPP in PRE0 field.");
return -EINVAL;
}
} else {
/* Uncoded cel; only 8bpp and 16bpp are allowed. */
switch (FIELD2VAL (PRE0, BPP, ctx->ci.PRE0)) {
case PRE0_BPP_1:
case PRE0_BPP_2:
case PRE0_BPP_4:
case PRE0_BPP_6:
default:
g_set_error (ctx->error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Invalid BPP in uncoded cel");
return -EINVAL;
case PRE0_BPP_8:
ctx->ci.bpp = 8; break;
case PRE0_BPP_16: