-
Notifications
You must be signed in to change notification settings - Fork 8
/
ilbm.c
1460 lines (1329 loc) · 35.9 KB
/
ilbm.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 <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgimp/gimp.h>
#include "plugin.h"
#include "gui.h"
#include "iff.h"
#include "byterun1.h"
#include "hamehb.h"
#include "grayscale.h"
#include "ilbm.h"
/**** BMHD BitMap HeaDer ****/
static gboolean readILBM(FILE *file, ILBMbmhd *bmhd)
{
g_assert(bmhd != NULL);
return readUword(file, &bmhd->w) &&
readUword(file, &bmhd->h) &&
readUword(file, (guint16 *) &bmhd->x) &&
readUword(file, (guint16 *) &bmhd->y) &&
readUchar(file, &bmhd->nPlanes) &&
readUchar(file, &bmhd->masking) &&
readUchar(file, &bmhd->compression) &&
readUchar(file, &bmhd->pad1) &&
readUword(file, &bmhd->transparentColor) &&
readUchar(file, &bmhd->xAspect) &&
readUchar(file, &bmhd->yAspect) &&
readUword(file, (guint16 *) &bmhd->pageWidth) &&
readUword(file, (guint16 *) &bmhd->pageHeight);
}
static void genBMHD(ILBMbmhd *bmhd, guint16 width, guint16 height, guint8 depth)
{
memset(bmhd, 0, sizeof *bmhd);
bmhd->w = bmhd->pageWidth = GUINT16_TO_BE(width);
bmhd->h = bmhd->pageHeight = GUINT16_TO_BE(height);
bmhd->nPlanes = depth;
bmhd->compression = cmpNone;
bmhd->xAspect = bmhd->yAspect = 1;
}
static void dumpBMHD(const ILBMbmhd *bmhd)
{
printf("%u-bit image %ux%u, max %lu colors, %s\n", bmhd->nPlanes, bmhd->w, bmhd->h, 1L << bmhd->nPlanes, (bmhd->nPlanes >= 12) ? "RGB" : "indexed");
/* RGB check okay? */
printf("(x,y):(%d,%d), Masking:%u, Compr.:%u\n", bmhd->x, bmhd->y, bmhd->masking, bmhd->compression);
printf("Transparent color: %u, Aspect: %u/%u\n", bmhd->transparentColor, bmhd->xAspect, bmhd->yAspect);
printf("Page size: %ux%u\n", bmhd->pageWidth, bmhd->pageHeight);
}
/**** CAMG Commodore AMiGa ****/
static gboolean readCAMG(FILE *file, ILBMcamg *camg)
{
g_assert(camg != NULL);
return readUlong(file, &camg->viewModes);
}
static void dumpCAMG(const ILBMcamg *camg)
{
printf("ViewModes: 0x%08lx ", (unsigned long) camg->viewModes);
if(camg->viewModes & hiRes)
fputs("HIRES ", stdout);
if(camg->viewModes & ham)
fputs("HAM ", stdout);
if(camg->viewModes & extraHalfbrite)
fputs("EHB ", stdout);
if(camg->viewModes & lace)
fputs("LACE ", stdout);
fputs("\n", stdout);
}
/**** DPI Dots Per Inch ****/
static gboolean readDPI(FILE *file, ILBMdpi *dpi)
{
g_assert(dpi != NULL);
return readUword(file, &dpi->dpiX) && readUword(file, &dpi->dpiY);
}
static void dumpDPI(const ILBMdpi *dpi)
{
printf("Scan resolution: %hux%hu dpi\n", dpi->dpiX, dpi->dpiY);
}
/**** GRAB Image hotspot ****/
static gboolean readGRAB(FILE *file, ILBMgrab *grab)
{
g_assert(file != NULL && grab != NULL);
return readUword(file, (guint16 *) &grab->grabX) && readUword(file, (guint16 *) &grab->grabY);
}
static void dumpGRAB(const ILBMgrab *grab)
{
printf("Grab hotspot: (%hd/%hd)\n", grab->grabX, grab->grabY);
}
/**** DEST DestMerge masks ****/
static gboolean readDEST(FILE *file, ILBMdest *dest)
{
g_assert(file != NULL && dest != NULL);
return readUchar(file, &dest->depth) && readUchar(file, &dest->pad) &&
readUword(file, &dest->planePick) && readUword(file, &dest->planeOnOff) &&
readUword(file, &dest->planeMask);
}
static void dumpDEST(ILBMdest *dest)
{
printf("DEST depth: %hd\n"
" planePick: %04hX planeOnOff: %04hX\n"
" planeMask: %04hX\n",
(short) dest->depth, dest->planePick, dest->planeOnOff, dest->planeMask);
}
/**** SPRT Sprite ****/
static gboolean readSPRT(FILE *file, ILBMsprt *sprt)
{
g_assert(file != NULL && sprt != NULL);
return readUword(file, &sprt->preced);
}
static void dumpSPRT(ILBMsprt *sprt)
{
printf("Sprite Precedence: %hu\n", sprt->preced);
}
/**** RGBN8 ****/
static gboolean packRGBN8(FILE *file, const grayval *rgbbuf, gint32 pixelNeeded, IffID ftype, gboolean alpha)
{
gboolean success = TRUE;
guint32 actclong = 0;
guint16 repeat = 0;
gboolean isfirst = TRUE;
g_assert((ftype == ID_RGB8) || (ftype == ID_RGBN));
if(!pixelNeeded)
return TRUE;
alpha = alpha != 0;
while(pixelNeeded > 0)
{
guint32 clong;
if(ftype == ID_RGB8)
{
clong = (((guint32)*rgbbuf)<<24)
| (((guint32)rgbbuf[1])<<16)
| (((guint32)rgbbuf[2])<<8)
| (((guint32)alpha)<<7);
}
else
{
clong = (((guint16)gray8ToHam4(*rgbbuf))<<12)
| (((guint16)gray8ToHam4(rgbbuf[1]))<<8)
| (((guint16)gray8ToHam4(rgbbuf[2]))<<4)
| (((guint16)alpha)<<3);
}
if(isfirst)
{
actclong = clong;
isfirst = 0;
}
if((clong != actclong) || (repeat == ((ftype == ID_RGB8)?127:7)))
{
actclong |= repeat;
if(ftype == ID_RGB8)
success = writeUlong(file, actclong);
else
success = writeUword(file, actclong);
if(!success)
break;
actclong = clong;
isfirst = 0; /* FIXME: optimize */
repeat = TRUE;
}
else
++repeat; /* FIXME: check for max. */
rgbbuf += byteppRGB + (alpha != 0);
--pixelNeeded;
}
if(repeat)
{
actclong |= repeat;
if(ftype == ID_RGB8)
success = writeUlong(file, actclong);
else
success = writeUword(file, actclong);
}
return success;
}
static gboolean unpackRGBN8(FILE *file, grayval *rgbbuf, gint32 pixelNeeded, IffID ftype, gboolean alpha)
{
gboolean success = TRUE;
grayval ca = 0; /* Make gcc happy */
while(pixelNeeded > 0)
{
guint32 repeat;
grayval cr, cg, cb;
if(ID_RGBN == ftype)
{
guint16 cword;
if(!(success = readUword(file, &cword)))
break;
else
{
cr = ham4bitToGray8(cword >> 12);
cg = ham4bitToGray8((cword >> 8) & 0xF);
cb = ham4bitToGray8((cword >> 4) & 0xF);
repeat = cword & 7;
if(alpha)
ca = ((cword >> 3) & 1) ? transparent : opaque;
}
}
else
{
guint32 clong;
if(!(success = readUlong(file, &clong)))
break;
else
{
cr = clong >> 24;
cg = (clong >> 16) & 0xFF;
cb = (clong >> 8) & 0xFF;
repeat = clong & 0x7F;
if(alpha)
ca = ((clong >> 7) & 1) ? transparent : opaque;
}
}
if(!repeat)
{
guint8 cbyte;
if(!(success = readUchar(file, &cbyte)))
break;
else
{
if(cbyte)
repeat = cbyte;
else
{
guint16 cword;
if(!(success = readUword(file, &cword)))
break;
else
{
if(!cword)
repeat = 65536;
else
repeat = cword;
}
}
}
}
while(pixelNeeded && repeat--)
{
*rgbbuf++ = cr;
*rgbbuf++ = cg;
*rgbbuf++ = cb;
if(alpha)
*rgbbuf++ = ca;
--pixelNeeded;
}
}
return success;
}
static void unpackBits(const guint8 *bitlinebuf, guint8 *destline, gint bitnr, gint width)
{
const guint8 setmask = 1 << bitnr;
g_assert(bitlinebuf != NULL);
g_assert(destline != NULL);
while(width > 0)
{
guint8 checkmask;
for(checkmask = 1 << 7; width && checkmask; checkmask >>= 1)
{
if(*bitlinebuf & checkmask)
*destline |= setmask;
++destline;
--width;
}
++bitlinebuf;
}
}
static void setBits(guint8 *destline, gint bitnr, gint width)
{
const guint8 setmask = 1 << bitnr;
for(width <<= 3; width > 0; destline++, width--)
*destline |= setmask;
}
static void clrBits(guint8 *destline, gint bitnr, gint width)
{
const guint8 clrmask = ~(1 << bitnr);
for(width <<= 3; width > 0; destline++, width--)
*destline &= clrmask;
}
static gint numBitsSet(guint32 val)
{
gint bitsSet = 0;
for(bitsSet = 0; val != 0; val >>= 1)
{
if(val & 1)
++bitsSet;
}
return bitsSet;
}
static void writeRGB(const grayval *destline, grayval *dest, gint numPixels, gint bytepp)
{
g_assert(destline != NULL);
g_assert(dest != NULL);
g_assert(bytepp > 0);
while(numPixels--)
{
*dest = *destline++;
dest += bytepp;
}
}
static void bitExpandStep(guint8 *dest, const guint8 *src, gint width, gint step)
{
/* Expands alpha masks to 0x00/0xFF style lines */
guint8 bit = 1 << 7;
g_assert(dest != NULL);
g_assert(src != NULL);
g_assert(step > 0);
while(width--)
{
*dest = (*src & bit) ? opaque : transparent;
dest += step;
bit >>= 1;
if(bit == 0)
{
bit = 1 << 7;
++src;
}
}
}
static gboolean readPlaneRow(FILE *file, guint8 *bitDest, gint bytesInPlaneRow, gint16 compression)
{
gboolean success = FALSE;
switch(compression)
{
case cmpByteRun1:
success = unpackRow(file, (gint8 *) bitDest, bytesInPlaneRow);
break;
case cmpRGBN:
case cmpRGB8:
fputs("(RGBN/RGB8 compression mode 4/x found)\n", stderr);
default: /* fall-thru */
fputs("Unknown compression mode---reading raw.\n", stderr);
case cmpNone:
success = iffReadData(file, bitDest, bytesInPlaneRow);
if(!success)
{
if(feof(file))
fputs("Unexpected EOF\n", stderr);
else
perror("readPlaneRow");
}
break;
}
return success;
}
static guint8 * allocPackedBuf(gint bytesInPlaneRow, gint16 compression)
{
guint8 *packedBuf = NULL; /* NULL is a valid value here */
if(cmpByteRun1 == compression)
{
packedBuf = g_new(guint8, bytesInPlaneRow + 8); /* FIXME: 8 enough? */
if(packedBuf == NULL)
fputs("Out of memory.\n", stderr);
}
return packedBuf;
}
static void freePackedBuf(guint8 *packedBuf)
{
g_free(packedBuf);
}
static gboolean writePlaneRow(FILE *file, const guint8 *bitSrc, gint bytesInPlaneRow, gint16 compression, guint8 *packedBuf)
{
gboolean success;
switch(compression)
{
case cmpByteRun1:
{
const gint32 written = packRow(packedBuf, bitSrc, bytesInPlaneRow);
if(written > 0)
success = iffWriteData(file, packedBuf, written);
else
success = FALSE;
}
break;
default: /* Error message? */
case cmpNone:
success = iffWriteData(file, bitSrc, bytesInPlaneRow);
if(!success)
perror("writePlaneRow");
break;
}
if(!success)
fprintf(stderr, "writePlaneRow: Error.\n");
return success;
}
static void parseLines(FILE *file, guint8 *dst, gint width, gint hereheight, const ILBMbmhd *bmhd, const ILBMdest *dest, const grayval *cmap,
guint32 viewModes, const grayval *grayTrans, IffID ftype)
{
guint8 * const destline = g_new(guint8, width); /* +1? */
guint8 * const bitlinebuf = g_new(guint8, BYTEPL(width));
/* FIXME: check both */
if(ftype == ID_RGB8 || ftype == ID_RGBN)
{
const gboolean success = unpackRGBN8(file, (grayval *) dst, width * hereheight, ftype, bmhd->nPlanes == 13);
if(!success)
g_warning("Failed to unpack RGBN8 pixels");
else /* This way alpha doesn't work with RGB8 */
dst += width * hereheight * ((13 == bmhd->nPlanes) ? byteppRGBA : byteppRGB);
}
else if(cmap == NULL)
{
/* RGB(A) */
/* if(VERBOSE) fputs("Creating RGB(A)\n", stdout);*/
while(hereheight--)
{
gint rgb, i;
for(rgb = 0; rgb < byteppRGB; ++rgb)
{
gint bitnr;
memset(destline, 0, width);
for(bitnr = 0; bitnr < bitppGray; ++bitnr)
{
readPlaneRow(file, bitlinebuf, BYTEPL(width), bmhd->compression);
unpackBits(bitlinebuf, destline, bitnr, width);
}
writeRGB(destline, dst + rgb, width, byteppRGB + (bmhd->masking != mskNone));
}
if(bmhd->masking != mskNone)
{
switch(bmhd->masking)
{
case mskHasMask:
readPlaneRow(file, bitlinebuf, BYTEPL(width), bmhd->compression);
bitExpandStep(dst + byteppRGB, bitlinebuf, width, byteppRGBA);
break;
case mskHasTransparentColor:
/* Impossible in non-indexed images */
/* Hrm.. maybe lookup in palette and use this? FIXME */
fputs("mskHasTransparentColor in non-indexed image.\n", stderr);
default:
for(i = 0; i < width; ++i)
dst[byteppRGB + i * byteppRGBA] = opaque;
break;
}
}
dst += width * (byteppRGB + (bmhd->masking != mskNone));
}
}
else if(!(viewModes & ham))
{
/* indexed */
while(hereheight--)
{
gint bitnr;
if(ID_PBM_ == ftype)
{
/* Chunky */
/* We always read 1 byte per pixel, so "width" bytes. */
readPlaneRow(file, destline, width, bmhd->compression);
/* Are there IPBMs with maybe another byte pp for alpha? */
}
else
{
memset(destline, 0, width);
/* todo: should really unpack line-by-line, not plane-by-plane. */
for(bitnr = 0; bitnr < dest->depth; ++bitnr)
{
if(dest->planePick & (1 << bitnr))
{
/* "put the next source bitplane into this bitplane" */
readPlaneRow(file, bitlinebuf, BYTEPL(width), bmhd->compression);
unpackBits(bitlinebuf, destline, bitnr, width); /* +7/8? */
}
else
{
/* "put the corresponding bit from planeOnOff into this bitplane" */
/* We ignore dest->planeMask since this is no transparency issue */
if(dest->planeOnOff & (1 << bitnr))
setBits(destline, bitnr, width);
else
clrBits(destline, bitnr, width);
}
}
}
if(grayTrans)
transGray(destline, width, grayTrans);
if(bmhd->masking == mskNone)
memcpy(dst, destline, width); /* no alpha */
else
{
writeRGB(destline, dst, width, byteppGrayA); /* make space for alpha */
switch(bmhd->masking)
{
case mskHasMask:
/* Only if image is plane- and per-line-based */
if(ID_ILBM == ftype)
{
readPlaneRow(file, bitlinebuf, BYTEPL(width), bmhd->compression);
bitExpandStep(dst + byteppGray, bitlinebuf, width, byteppGrayA);
}
break;
case mskHasTransparentColor:
{
const guint8 *indx = destline; /* may read dst */
guint8 *toalpha = dst + byteppGray;
for(gint i = 0; i < width; ++i)
{
const gboolean trans = *indx++ == bmhd->transparentColor;
*toalpha = trans ? transparent : opaque;
toalpha += byteppGrayA;
}
}
break;
default:
{
gint i;
for(i = 0; i < width; ++i)
dst[byteppGray + i * byteppGrayA] = opaque;
}
break;
}
dst += width;
}
dst += width;
}
}
else
{
/* HAM */
while(hereheight--)
{
gint bitnr;
memset(destline, 0, width);
for(bitnr = 0; bitnr < bmhd->nPlanes; ++bitnr)
{
readPlaneRow(file, bitlinebuf, BYTEPL(width), bmhd->compression);
unpackBits(bitlinebuf, destline, bitnr, width);
}
deHam(dst, destline, width, bmhd->nPlanes, cmap, bmhd->masking != mskNone);
if(bmhd->masking != mskNone)
{
switch(bmhd->masking)
{
case mskHasMask:
readPlaneRow(file, bitlinebuf, BYTEPL(width), bmhd->compression);
bitExpandStep(dst + byteppRGB, bitlinebuf, width, byteppRGBA);
break;
case mskHasTransparentColor:
{
gint i;
guint8 *indx = destline;
guint8 *toalpha = dst + byteppRGB;
for(i = width; i > 0; --i)
{
if(*indx++ == bmhd->transparentColor)
*toalpha = transparent;
else
*toalpha = opaque;
toalpha += byteppRGBA;
}
}
break;
default:
{
gint i;
for(i = 0; i < width; ++i)
dst[byteppRGB + i * byteppRGBA] = opaque;
}
break;
}
}
dst += width * (byteppRGB + (bmhd->masking != mskNone));
}
}
g_free(bitlinebuf);
g_free(destline);
}
static void setCmap(gint32 imageID, const guint8 *cmap, gint ncols)
{
guint8 colormap[3 * 256];
gint i, offset;
gboolean really_8bit = FALSE;
g_assert(NULL != cmap);
g_assert(0 != ncols);
g_assert(3u * ncols <= (sizeof colormap / sizeof *colormap));
if(VERBOSE)
printf("Setting cmap (%d colors)...\n", ncols);
/* Try to check if the colormap really uses all 8 bits. */
for(i = offset = 0; i < ncols; i++, offset += 3)
{
const guint8 red = cmap[offset + 0], green = cmap[offset + 1], blue = cmap[offset + 2];
if((red & 0x0f) != 0 || (green & 0x0f) != 0 || (blue & 0x0f) != 0)
{
really_8bit = TRUE;
break;
}
}
if(!really_8bit)
{
/* Make sure the colormap GIMP sees is fully saturated, map Amiga's white (0xfff) to 0xffffff, not 0xf0f0f0. */
for(i = offset = 0; i < ncols; i++, offset += 3)
{
const guint8 red = cmap[offset + 0], green = cmap[offset + 1], blue = cmap[offset + 2];
colormap[offset + 0] = red | (red >> 4);
colormap[offset + 1] = green | (green >> 4);
colormap[offset + 2] = blue | (blue >> 4);
}
/* printf("Force-saturated colors, converted incoming 0xR0G0B0 to 0xRRGGBB\n");*/
}
else
memcpy(colormap, cmap, 3 * ncols);
gimp_image_set_colormap(imageID, colormap, ncols);
}
/**** Loading ****/
/* loadBODY (fil, &bmhd, &camg, cmap, grayTrans);*/
static gboolean loadBODY(gboolean succ, FILE *file, IffID ftype, const gchar *filename, const ILBMbmhd *bmhd, ILBMcamg *camg,
ILBMdest *dest, gboolean hasDest, grayval **cmap, gint ncols, const grayval *grayTrans, gint32 *imageID)
{
GimpDrawable *drawable;
guint tileHeight; /* guint is the return value */
guint8 *buf;
gboolean exportRGB, isGray = 0;
gint32 layerID = -1;
GimpPixelRgn pixelRegion;
if(hasDest)
{
/* mmh, should we take mask in account here? */
/* now we assume a mask just to be the "highest" bit */
if(bmhd->nPlanes > dest->depth)
g_warning("More planes in file than allowed by DEST.");
if(numBitsSet(dest->planePick) != bmhd->nPlanes)
g_warning("Number of planes doesn't match bits set in planePick.");
}
else
{
dest->depth = bmhd->nPlanes;
dest->planePick = dest->planeMask = (1L << bmhd->nPlanes) - 1;
}
/* "Any higher order bits should be ignored" */
dest->planePick &= 0xFF;
dest->planeOnOff &= 0xFF;
dest->planeMask &= 0xFF;
/* Some old (pre-AGA) programs saved HAM6 pictures
* without setting the right (or any) CAMG flags.
* Let's guess then.
*/
if(!(camg->viewModes & ham) && (6 == bmhd->nPlanes) && (16 == ncols))
{
if(VERBOSE)
printf("Guessing HAM6.\n");
camg->viewModes |= ham;
}
if(*cmap && ((bitppRGB == bmhd->nPlanes) || (ID_RGBN == ftype) || (ID_RGB8 == ftype)))
{
/* Well, I've already seen an 24bit image
* with a 32color CMAP. :-/ Ignore it. */
g_free(*cmap);
*cmap = NULL;
}
if(!*cmap && (bitppGray == bmhd->nPlanes))
{
/* Well, seems to be a grayscale. */
*cmap = allocGrayscale();
ncols = maxGrayshades;
isGray = 1;
grayTrans = allocGrayKeep(); /* 1:1 */
}
else if(*cmap && !(camg->viewModes & ham))
{
isGray = isGrayscale(*cmap, ncols);
if(isGray)
{
if(ncols > 4)
{
if(VERBOSE)
printf("Promoting to grayscale.\n");
grayTrans = allocGrayTrans(*cmap, ncols);
}
else
{
if(VERBOSE)
printf("Is gray, but with this few colors Grayscale makes no sense.\n");
isGray = 0;
}
}
}
if(VERBOSE)
printf("isGray:%d\n", (int) isGray);
/* if(DEBUG && grayTrans) dumpGrayTrans (grayTrans); */
exportRGB = !*cmap || (camg->viewModes & ham);
if(!exportRGB && !*cmap)
fprintf(stderr, "Colormap (CMAP) missing!\n");
if(camg->viewModes & extraHalfbrite)
*cmap = reallocEhbCmap(*cmap, &ncols);
/* !! Caution (FIXME): Every msk.. needs alpha, but we don't support everyone yet !! */
*imageID = gimp_image_new(bmhd->w, bmhd->h, (exportRGB ? GIMP_RGB : (isGray ? GIMP_GRAY : GIMP_INDEXED)));
gimp_image_set_filename(*imageID, filename);
if(!exportRGB && *cmap && !isGray)
setCmap(*imageID, *cmap, ncols);
const gboolean needsAlpha = (bmhd->masking != mskNone) || ((ID_RGBN == ftype) && (bmhd->nPlanes == 13)) || ((ID_RGB8 == ftype) && (bmhd->nPlanes == 25));
if(VERBOSE)
printf("exportRGB:%d needsAlpha:%d\n", (int) exportRGB, (int) needsAlpha);
layerID = gimp_layer_new(*imageID, "Background", bmhd->w, bmhd->h,
needsAlpha ?
(exportRGB ? GIMP_RGBA_IMAGE : (isGray ? GIMP_GRAYA_IMAGE : GIMP_INDEXEDA_IMAGE))
: (exportRGB ? GIMP_RGB_IMAGE : (isGray ? GIMP_GRAY_IMAGE : GIMP_INDEXED_IMAGE)),
100, GIMP_NORMAL_MODE);
gimp_image_add_layer(*imageID, layerID, 0);
drawable = gimp_drawable_get(layerID);
gimp_pixel_rgn_init(&pixelRegion, drawable, 0, 0, drawable->width, drawable->height, TRUE, FALSE);
tileHeight = gimp_tile_height();
if(VERBOSE)
printf("Gimp tileHeight: %d\n", tileHeight);
const gsize size = tileHeight * bmhd->w * ((exportRGB ? byteppRGB : byteppGray) + (needsAlpha ? 1 : 0));
buf = g_new(guint8, size);
if(!buf)
{
fputs("Buffer allocation\n", stderr);
succ = FALSE;
}
else
{
for(guint actTop = 0; actTop < (guint) bmhd->h; actTop += tileHeight)
{
const gint scanlines = MIN(tileHeight, bmhd->h - actTop);
if(VERBOSE)
printf("Processing lines %d..%d (%2d)...\n", actTop, actTop + scanlines - 1, scanlines);
parseLines(file, buf, bmhd->w, scanlines, bmhd, dest, *cmap, camg->viewModes, grayTrans, ftype);
gimp_progress_update((double) actTop / bmhd->h);
gimp_pixel_rgn_set_rect(&pixelRegion, buf, 0, actTop, drawable->width, scanlines);
}
gimp_progress_update(1.0);
g_free(buf);
}
// gimp_drawable_flush(drawable); /* unneccessary? */
gimp_drawable_detach(drawable);
return succ;
}
static GTimer *the_timer = NULL;
void timerStart(void)
{
fputs("Starting timer.\n", stderr);
if(the_timer == NULL)
the_timer = g_timer_new();
if(the_timer != NULL)
g_timer_start(the_timer);
}
void timerStop(void)
{
gulong microseconds;
if(the_timer == NULL)
return;
g_timer_stop(the_timer);
g_timer_elapsed(the_timer, µseconds);
printf("Timer stopped after %.1f ms\n", 1e-3 * microseconds);
}
gint32 loadImage(const gchar *filename)
{
gint32 imageID = -1;
gboolean succ = TRUE;
gchar *name;
const gsize name_size = strlen(filename) + 10;
if(VERBOSE)
timerStart();
name = g_new(gchar, name_size);
if(!name)
{
fputs("Out of memory.\n", stderr);
return imageID;
}
g_snprintf(name, name_size, "Loading %s:", filename);
gimp_progress_init(name);
{
FILE *file;
if(VERBOSE)
printf("=== Loading %s...\n", filename);
file = fopen(filename, "rb");
succ = succ && (file != NULL);
if(!file)
fprintf(stderr, "File %s not found.\n", filename);
else
{
IffChunkHeader fhead;
if((!iffReadHeader(file, &fhead)) || (ID_FORM != fhead.id))
{
succ = FALSE;
g_warning("No FORM header found.");
}
else
{
IffID hdrid;
IffID ftype = 0;
succ = succ && readUlong(file, &hdrid);
switch(hdrid)
{
case ID_ILBM: /* native */
case ID_PBM_: /* DPaintIIe (PC) */
case ID_RGB8: /* TurboSilver (Impulse) */
case ID_RGBN: /* TurboSilver (Impulse) */
ftype = hdrid;
break;
/* Further possible types:
IFF-ACBM, Amiga Continuous BitMap
IFF-YUVN, VLab (Macrosystems)
IFF-DEEP, XiPaint/TVPaint
IFF-FAXX,
IFF-RGFX, Retargetable Graphics
IFF-SHAM/DHAM/DHIRES, IFF FORMS
IFF-DCOL, Direct color
IFF-PCHG, Palette changes
SVG SuperView Graphics
*/
default:
ftype = 0;
break;
}
if(!ftype)
fprintf(stderr, "No ILBM/RGBN/RGB8 header found.\n");
else
{
gint32 hunksize;
ILBMbmhd bmhd;
guint8 *cmap = 0;
guint8 *grayTrans = 0;
gint ncols = 0;
ILBMcamg camg = {0x00000000};
ILBMdest dest;
gboolean hasDest = FALSE;
gboolean running = TRUE;
IffChunkHeader chead;
bmhd.w = 0;
bmhd.h = 0;
bmhd.nPlanes = 0; /* == not yet used */
while(running && (iffReadHeader(file, &chead)))
{
/*hunksize = ((chead.len + 1) / 2) * 2;*/
hunksize = (chead.len + 1) & ~1UL;
if(VERBOSE)
iffDumpHeader(&chead);
switch(chead.id)
{
case ID_BMHD:
succ = succ && readILBM(file, &bmhd);
if(succ)
{
if(VERBOSE)
dumpBMHD(&bmhd);
}
break;
case ID_CMAP:
ncols = chead.len / byteppRGB;
if(VERBOSE)
printf("%d colors in CMAP.\n", ncols);
cmap = g_new(guint8, ncols * byteppRGB + 1 /*pad */ );
succ = succ && iffReadData(file, cmap, hunksize); /* FIXME: +1 unneeded? */
break;
case ID_CAMG:
succ = succ && readCAMG(file, &camg);
if(succ)
{
if(VERBOSE)
dumpCAMG(&camg);
} /* FIXME: error */
break;
case ID_DPI_:
{
ILBMdpi dpi;
succ = succ && readDPI(file, &dpi);
if(succ)
{
if(VERBOSE)
dumpDPI(&dpi);
} /* FIXME: error */
}
break;
case ID_DEST:
succ = succ && readDEST(file, &dest);
if(succ)
{
if(VERBOSE)
{
dumpDEST(&dest);
}
hasDest = TRUE;
} /* FIXME: error */
break;
case ID_ANNO:
case ID__C__:
case ID_CHRS:
case ID_NAME:
case ID_TEXT:
case ID_copy:
case ID_AUTH:
case ID_FVER:
{
guint8 *cont = g_new(guint8, chead.len + 1 + 1);
/* FIXME: check */
succ = succ && iffReadData(file, cont, hunksize);
if(succ)
{
if(VERBOSE)
{
gchar idstr[5];
cont[chead.len] = '\0';
idToString(chead.id, idstr, sizeof idstr);
printf("%s: %s\n", idstr, cont);
}
} /* FIXME: error */
g_free(cont);
}
break;
case ID_GRAB:
{
ILBMgrab grab;
succ = succ && readGRAB(file, &grab);
if(succ)
{
if(VERBOSE)