comparison src/libgfx.c @ 1102:e06abfde6c39

Cosmetics pass: Remove excess whitespace.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 03 Mar 2015 23:22:36 +0200
parents 985225a93aeb
children d0898867ec4c
comparison
equal deleted inserted replaced
1101:0577f10dc3de 1102:e06abfde6c39
29 DMImage * dmImageAlloc(int width, int height) 29 DMImage * dmImageAlloc(int width, int height)
30 { 30 {
31 DMImage *img = dmMalloc0(sizeof(DMImage)); 31 DMImage *img = dmMalloc0(sizeof(DMImage));
32 if (img == NULL) 32 if (img == NULL)
33 return NULL; 33 return NULL;
34 34
35 img->width = width; 35 img->width = width;
36 img->height = height; 36 img->height = height;
37 img->pitch = width * sizeof(Uint8); 37 img->pitch = width * sizeof(Uint8);
38 38
39 img->size = img->pitch * img->height; 39 img->size = img->pitch * img->height;
40 if ((img->data = dmMalloc(img->size)) == NULL) 40 if ((img->data = dmMalloc(img->size)) == NULL)
41 { 41 {
42 dmFree(img); 42 dmFree(img);
43 return NULL; 43 return NULL;
44 } 44 }
45 45
46 return img; 46 return img;
47 } 47 }
48 48
49 49
50 void dmImageFree(DMImage *img) 50 void dmImageFree(DMImage *img)
84 84
85 BOOL dmImageAllocPalette(DMImage *img, int ncolors, int ctransp) 85 BOOL dmImageAllocPalette(DMImage *img, int ncolors, int ctransp)
86 { 86 {
87 if (img == NULL) 87 if (img == NULL)
88 return FALSE; 88 return FALSE;
89 89
90 img->ncolors = ncolors; 90 img->ncolors = ncolors;
91 img->ctransp = ctransp; 91 img->ctransp = ctransp;
92 return dmPaletteAlloc(&(img->pal), ncolors, ctransp); 92 return dmPaletteAlloc(&(img->pal), ncolors, ctransp);
93 } 93 }
94 94
110 110
111 111
112 static BOOL dmReadPaletteData(FILE *fp, DMColor *pal, int ncolors) 112 static BOOL dmReadPaletteData(FILE *fp, DMColor *pal, int ncolors)
113 { 113 {
114 int i; 114 int i;
115 115
116 for (i = 0; i < ncolors; i++) 116 for (i = 0; i < ncolors; i++)
117 { 117 {
118 Uint8 colR, colG, colB; 118 Uint8 colR, colG, colB;
119 if (!dm_fread_byte(fp, &colR) || 119 if (!dm_fread_byte(fp, &colR) ||
120 !dm_fread_byte(fp, &colG) || 120 !dm_fread_byte(fp, &colG) ||
165 case DM_IFMT_RGBA: 165 case DM_IFMT_RGBA:
166 qr = img->pal[c].r; 166 qr = img->pal[c].r;
167 qg = img->pal[c].g; 167 qg = img->pal[c].g;
168 qb = img->pal[c].b; 168 qb = img->pal[c].b;
169 qa = img->pal[c].a; 169 qa = img->pal[c].a;
170 170
171 for (xscale = 0; xscale < spec->scaleX; xscale++) 171 for (xscale = 0; xscale < spec->scaleX; xscale++)
172 { 172 {
173 *ptr1++ = qr; 173 *ptr1++ = qr;
174 *ptr1++ = qg; 174 *ptr1++ = qg;
175 *ptr1++ = qb; 175 *ptr1++ = qb;
179 179
180 case DM_IFMT_RGB: 180 case DM_IFMT_RGB:
181 qr = img->pal[c].r; 181 qr = img->pal[c].r;
182 qg = img->pal[c].g; 182 qg = img->pal[c].g;
183 qb = img->pal[c].b; 183 qb = img->pal[c].b;
184 184
185 for (xscale = 0; xscale < spec->scaleX; xscale++) 185 for (xscale = 0; xscale < spec->scaleX; xscale++)
186 { 186 {
187 *ptr1++ = qr; 187 *ptr1++ = qr;
188 *ptr1++ = qg; 188 *ptr1++ = qg;
189 *ptr1++ = qb; 189 *ptr1++ = qb;
192 192
193 case DM_IFMT_RGB_PLANE: 193 case DM_IFMT_RGB_PLANE:
194 qr = img->pal[c].r; 194 qr = img->pal[c].r;
195 qg = img->pal[c].g; 195 qg = img->pal[c].g;
196 qb = img->pal[c].b; 196 qb = img->pal[c].b;
197 197
198 for (xscale = 0; xscale < spec->scaleX; xscale++) 198 for (xscale = 0; xscale < spec->scaleX; xscale++)
199 { 199 {
200 *ptr1++ = qr; 200 *ptr1++ = qr;
201 *ptr2++ = qg; 201 *ptr2++ = qg;
202 *ptr3++ = qb; 202 *ptr3++ = qb;
211 goto done; 211 goto done;
212 } 212 }
213 } 213 }
214 214
215 done: 215 done:
216 dmFree(row); 216 dmFree(row);
217 return res; 217 return res;
218 } 218 }
219 219
220 220
221 #define DMCOL(x) (((x) >> 4) & 0xf) 221 #define DMCOL(x) (((x) >> 4) & 0xf)
249 249
250 int dmWriteRAWImageFILE(FILE *fp, DMImage *img, DMImageSpec *spec) 250 int dmWriteRAWImageFILE(FILE *fp, DMImage *img, DMImageSpec *spec)
251 { 251 {
252 int xc, yc, plane, res; 252 int xc, yc, plane, res;
253 DMBitStreamContext bs; 253 DMBitStreamContext bs;
254 254
255 if ((res = dmInitBitStreamFILE(&bs, fp)) != DMERR_OK) 255 if ((res = dmInitBitStreamFILE(&bs, fp)) != DMERR_OK)
256 return res; 256 return res;
257 257
258 if (spec->interleave) 258 if (spec->interleave)
259 { 259 {
285 return DMERR_FWRITE; 285 return DMERR_FWRITE;
286 } 286 }
287 } 287 }
288 } 288 }
289 } 289 }
290 290
291 return dmFlushBitStream(&bs); 291 return dmFlushBitStream(&bs);
292 } 292 }
293 293
294 294
295 int dmWriteRAWImage(const char *filename, DMImage *img, DMImageSpec *spec) 295 int dmWriteRAWImage(const char *filename, DMImage *img, DMImageSpec *spec)
384 { 384 {
385 res = dmError(DMERR_MALLOC, 385 res = dmError(DMERR_MALLOC,
386 "PNG: png_create_write_struct() failed.\n"); 386 "PNG: png_create_write_struct() failed.\n");
387 goto error; 387 goto error;
388 } 388 }
389 389
390 info_ptr = png_create_info_struct(png_ptr); 390 info_ptr = png_create_info_struct(png_ptr);
391 if (info_ptr == NULL) 391 if (info_ptr == NULL)
392 { 392 {
393 res = dmError(DMERR_INIT_FAIL, 393 res = dmError(DMERR_INIT_FAIL,
394 "PNG: png_create_info_struct(%p) failed.\n", 394 "PNG: png_create_info_struct(%p) failed.\n",
395 png_ptr); 395 png_ptr);
396 goto error; 396 goto error;
397 } 397 }
398 398
399 if (setjmp(png_jmpbuf(png_ptr))) 399 if (setjmp(png_jmpbuf(png_ptr)))
400 { 400 {
401 res = dmError(DMERR_INIT_FAIL, 401 res = dmError(DMERR_INIT_FAIL,
402 "PNG: Error during image writing..\n"); 402 "PNG: Error during image writing..\n");
403 goto error; 403 goto error;
415 res = dmError(DMERR_NOT_SUPPORTED, 415 res = dmError(DMERR_NOT_SUPPORTED,
416 "PNG: Unsupported image format %d.\n", 416 "PNG: Unsupported image format %d.\n",
417 spec->format); 417 spec->format);
418 goto error; 418 goto error;
419 } 419 }
420 420
421 png_set_IHDR(png_ptr, info_ptr, 421 png_set_IHDR(png_ptr, info_ptr,
422 img->width * spec->scaleX, 422 img->width * spec->scaleX,
423 img->height * spec->scaleY, 423 img->height * spec->scaleY,
424 8, /* bits per component */ 424 8, /* bits per component */
425 fmt, 425 fmt,
442 { 442 {
443 res = dmError(DMERR_MALLOC, 443 res = dmError(DMERR_MALLOC,
444 "PNG: Could not allocate palette structure."); 444 "PNG: Could not allocate palette structure.");
445 goto error; 445 goto error;
446 } 446 }
447 447
448 memset(palette, 0, PNG_MAX_PALETTE_LENGTH * sizeof(png_color)); 448 memset(palette, 0, PNG_MAX_PALETTE_LENGTH * sizeof(png_color));
449 449
450 for (i = 0; i < img->ncolors; i++) 450 for (i = 0; i < img->ncolors; i++)
451 { 451 {
452 palette[i].red = img->pal[i].r; 452 palette[i].red = img->pal[i].r;
528 res = dmError(DMERR_INIT_FAIL, 528 res = dmError(DMERR_INIT_FAIL,
529 "PNG: png_create_info_struct(%p) failed.\n", 529 "PNG: png_create_info_struct(%p) failed.\n",
530 png_ptr); 530 png_ptr);
531 goto error; 531 goto error;
532 } 532 }
533 533
534 if (setjmp(png_jmpbuf(png_ptr))) 534 if (setjmp(png_jmpbuf(png_ptr)))
535 { 535 {
536 res = dmError(DMERR_INIT_FAIL, 536 res = dmError(DMERR_INIT_FAIL,
537 "PNG: Error during image reading..\n"); 537 "PNG: Error during image reading..\n");
538 goto error; 538 goto error;
566 "PNG: Unsupported bit depth for grayscale image: %d\n", 566 "PNG: Unsupported bit depth for grayscale image: %d\n",
567 bit_depth); 567 bit_depth);
568 goto error; 568 goto error;
569 } 569 }
570 break; 570 break;
571 571
572 case PNG_COLOR_TYPE_PALETTE: 572 case PNG_COLOR_TYPE_PALETTE:
573 png_set_packing(png_ptr); 573 png_set_packing(png_ptr);
574 break; 574 break;
575 575
576 default: 576 default:
615 for (i = 0; i < img->ncolors; i++) 615 for (i = 0; i < img->ncolors; i++)
616 { 616 {
617 img->pal[i].r = img->pal[i].g = img->pal[i].b = i; 617 img->pal[i].r = img->pal[i].g = img->pal[i].b = i;
618 } 618 }
619 break; 619 break;
620 620
621 case PNG_COLOR_TYPE_PALETTE: 621 case PNG_COLOR_TYPE_PALETTE:
622 png_get_PLTE(png_ptr, info_ptr, &palette, &ncolors); 622 png_get_PLTE(png_ptr, info_ptr, &palette, &ncolors);
623 dmMsg(3, "PNG: Palette of %d colors found.\n", ncolors); 623 dmMsg(3, "PNG: Palette of %d colors found.\n", ncolors);
624 if (ncolors > 0 && palette != NULL) 624 if (ncolors > 0 && palette != NULL)
625 { 625 {
626 if (!dmImageAllocPalette(img, ncolors, -1)) 626 if (!dmImageAllocPalette(img, ncolors, -1))
627 { 627 {
628 res = DMERR_MALLOC; 628 res = DMERR_MALLOC;
650 img->pal[i].a = trans[i]; 650 img->pal[i].a = trans[i];
651 if (img->ctransp < 0 && trans[i] == 0) 651 if (img->ctransp < 0 && trans[i] == 0)
652 img->ctransp = i; 652 img->ctransp = i;
653 } 653 }
654 } 654 }
655 } 655 }
656 656
657 error: 657 error:
658 // png_free(png_ptr, palette); 658 // png_free(png_ptr, palette);
659 659
660 if (png_ptr && info_ptr) 660 if (png_ptr && info_ptr)
745 static int dmWritePCXRow(void *cbdata, Uint8 *row, size_t len) 745 static int dmWritePCXRow(void *cbdata, Uint8 *row, size_t len)
746 { 746 {
747 DMPCXData *pcx = (DMPCXData *) cbdata; 747 DMPCXData *pcx = (DMPCXData *) cbdata;
748 int plane; 748 int plane;
749 size_t soffs = 0; 749 size_t soffs = 0;
750 750
751 // fprintf(stderr, "%d, %d * %d = %d\n", len, pcx->header->bpl, pcx->header->nplanes, pcx->header->nplanes * pcx->header->bpl); 751 // fprintf(stderr, "%d, %d * %d = %d\n", len, pcx->header->bpl, pcx->header->nplanes, pcx->header->nplanes * pcx->header->bpl);
752 752
753 pcx->bufOffs = 0; 753 pcx->bufOffs = 0;
754 754
755 for (plane = 0; plane < pcx->header->nplanes; plane++) 755 for (plane = 0; plane < pcx->header->nplanes; plane++)
862 { 862 {
863 res = dmError(DMERR_FWRITE, 863 res = dmError(DMERR_FWRITE,
864 "PCX: Could not write basic header data.\n"); 864 "PCX: Could not write basic header data.\n");
865 goto error; 865 goto error;
866 } 866 }
867 867
868 if (!dm_fwrite_le16(pcx.fp, hdr.xmin) || 868 if (!dm_fwrite_le16(pcx.fp, hdr.xmin) ||
869 !dm_fwrite_le16(pcx.fp, hdr.ymin) || 869 !dm_fwrite_le16(pcx.fp, hdr.ymin) ||
870 !dm_fwrite_le16(pcx.fp, hdr.xmax) || 870 !dm_fwrite_le16(pcx.fp, hdr.xmax) ||
871 !dm_fwrite_le16(pcx.fp, hdr.ymax) || 871 !dm_fwrite_le16(pcx.fp, hdr.ymax) ||
872 !dm_fwrite_le16(pcx.fp, hdr.hres) || 872 !dm_fwrite_le16(pcx.fp, hdr.hres) ||
881 { 881 {
882 res = dmError(DMERR_FWRITE, 882 res = dmError(DMERR_FWRITE,
883 "PCX: Could not write colormap.\n"); 883 "PCX: Could not write colormap.\n");
884 goto error; 884 goto error;
885 } 885 }
886 886
887 if (!dm_fwrite_byte(pcx.fp, hdr.reserved) || 887 if (!dm_fwrite_byte(pcx.fp, hdr.reserved) ||
888 !dm_fwrite_byte(pcx.fp, hdr.nplanes) || 888 !dm_fwrite_byte(pcx.fp, hdr.nplanes) ||
889 !dm_fwrite_le16(pcx.fp, hdr.bpl) || 889 !dm_fwrite_le16(pcx.fp, hdr.bpl) ||
890 !dm_fwrite_le16(pcx.fp, hdr.palinfo) || 890 !dm_fwrite_le16(pcx.fp, hdr.palinfo) ||
891 !dm_fwrite_str(pcx.fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler))) 891 !dm_fwrite_str(pcx.fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler)))
910 dm_fwrite_byte(pcx.fp, img->pal[i].r); 910 dm_fwrite_byte(pcx.fp, img->pal[i].r);
911 dm_fwrite_byte(pcx.fp, img->pal[i].g); 911 dm_fwrite_byte(pcx.fp, img->pal[i].g);
912 dm_fwrite_byte(pcx.fp, img->pal[i].b); 912 dm_fwrite_byte(pcx.fp, img->pal[i].b);
913 } 913 }
914 914
915 // Pad the palette, if necessary 915 // Pad the palette, if necessary
916 for (; i < 256; i++) 916 for (; i < 256; i++)
917 { 917 {
918 dm_fwrite_byte(pcx.fp, 0); 918 dm_fwrite_byte(pcx.fp, 0);
919 dm_fwrite_byte(pcx.fp, 0); 919 dm_fwrite_byte(pcx.fp, 0);
920 dm_fwrite_byte(pcx.fp, 0); 920 dm_fwrite_byte(pcx.fp, 0);
921 } 921 }
922 } 922 }
923 923
924 error: 924 error:
925 dmFree(pcx.buf); 925 dmFree(pcx.buf);
926 return res; 926 return res;
927 } 927 }
928 928
936 { 936 {
937 return dmError(DMERR_FOPEN, 937 return dmError(DMERR_FOPEN,
938 "PCX: Could not open file '%s' for writing.\n", 938 "PCX: Could not open file '%s' for writing.\n",
939 filename); 939 filename);
940 } 940 }
941 941
942 res = dmWritePCXImageFILE(fp, img, spec); 942 res = dmWritePCXImageFILE(fp, img, spec);
943 943
944 fclose(fp); 944 fclose(fp);
945 return res; 945 return res;
946 } 946 }
947 947
948 948
954 int count; 954 int count;
955 Uint8 data; 955 Uint8 data;
956 956
957 if (!dm_fread_byte(fp, &data)) 957 if (!dm_fread_byte(fp, &data))
958 return FALSE; 958 return FALSE;
959 959
960 if ((data & 0xC0) == 0xC0) 960 if ((data & 0xC0) == 0xC0)
961 { 961 {
962 count = data & 0x3F; 962 count = data & 0x3F;
963 if (!dm_fread_byte(fp, &data)) 963 if (!dm_fread_byte(fp, &data))
964 return FALSE; 964 return FALSE;
994 { 994 {
995 res = dmError(DMERR_FREAD, 995 res = dmError(DMERR_FREAD,
996 "PCX: Could not read basic header data.\n"); 996 "PCX: Could not read basic header data.\n");
997 goto error; 997 goto error;
998 } 998 }
999 999
1000 if (hdr.manufacturer != 10 || 1000 if (hdr.manufacturer != 10 ||
1001 hdr.version != 5 || 1001 hdr.version != 5 ||
1002 hdr.encoding != 1 || 1002 hdr.encoding != 1 ||
1003 hdr.bpp != 8) 1003 hdr.bpp != 8)
1004 { 1004 {
1005 res = dmError(DMERR_NOT_SUPPORTED, 1005 res = dmError(DMERR_NOT_SUPPORTED,
1006 "PCX: Not a PCX file, or unsupported variant.\n"); 1006 "PCX: Not a PCX file, or unsupported variant.\n");
1007 goto error; 1007 goto error;
1008 } 1008 }
1009 1009
1010 if (!dm_fread_le16(fp, &hdr.xmin) || 1010 if (!dm_fread_le16(fp, &hdr.xmin) ||
1011 !dm_fread_le16(fp, &hdr.ymin) || 1011 !dm_fread_le16(fp, &hdr.ymin) ||
1012 !dm_fread_le16(fp, &hdr.xmax) || 1012 !dm_fread_le16(fp, &hdr.xmax) ||
1013 !dm_fread_le16(fp, &hdr.ymax) || 1013 !dm_fread_le16(fp, &hdr.ymax) ||
1014 !dm_fread_le16(fp, &hdr.hres) || 1014 !dm_fread_le16(fp, &hdr.hres) ||
1023 { 1023 {
1024 res = dmError(DMERR_FREAD, 1024 res = dmError(DMERR_FREAD,
1025 "PCX: Could not read colormap.\n"); 1025 "PCX: Could not read colormap.\n");
1026 goto error; 1026 goto error;
1027 } 1027 }
1028 1028
1029 if (!dm_fread_byte(fp, &hdr.reserved) || 1029 if (!dm_fread_byte(fp, &hdr.reserved) ||
1030 !dm_fread_byte(fp, &hdr.nplanes) || 1030 !dm_fread_byte(fp, &hdr.nplanes) ||
1031 !dm_fread_le16(fp, &hdr.bpl) || 1031 !dm_fread_le16(fp, &hdr.bpl) ||
1032 !dm_fread_le16(fp, &hdr.palinfo) || 1032 !dm_fread_le16(fp, &hdr.palinfo) ||
1033 !dm_fread_str(fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler))) 1033 !dm_fread_str(fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler)))
1034 { 1034 {
1035 res = dmError(DMERR_FREAD, 1035 res = dmError(DMERR_FREAD,
1036 "PCX: Could not read header remainder.\n"); 1036 "PCX: Could not read header remainder.\n");
1037 goto error; 1037 goto error;
1038 } 1038 }
1039 1039
1040 if (hdr.nplanes != 3 && hdr.nplanes != 1) 1040 if (hdr.nplanes != 3 && hdr.nplanes != 1)
1041 { 1041 {
1042 res = dmError(DMERR_FREAD, 1042 res = dmError(DMERR_FREAD,
1043 "PCX: Unsupported number of bitplanes %d.\n", 1043 "PCX: Unsupported number of bitplanes %d.\n",
1044 hdr.nplanes); 1044 hdr.nplanes);
1071 { 1071 {
1072 res = dmError(DMERR_INVALID_DATA, 1072 res = dmError(DMERR_INVALID_DATA,
1073 "PCX: Error decoding RLE data.\n"); 1073 "PCX: Error decoding RLE data.\n");
1074 goto error; 1074 goto error;
1075 } 1075 }
1076 1076
1077 // Decode bitplanes 1077 // Decode bitplanes
1078 switch (hdr.nplanes) 1078 switch (hdr.nplanes)
1079 { 1079 {
1080 case 1: 1080 case 1:
1081 memcpy(dp, pcx.buf, img->width); 1081 memcpy(dp, pcx.buf, img->width);
1082 break; 1082 break;
1083 1083
1084 case 3: 1084 case 3:
1085 { 1085 {
1086 Uint8 *dptr = dp, 1086 Uint8 *dptr = dp,
1087 *sptr1 = pcx.buf, 1087 *sptr1 = pcx.buf,
1088 *sptr2 = sptr1 + hdr.bpl, 1088 *sptr2 = sptr1 + hdr.bpl,
1095 *dptr++ = *sptr3++; 1095 *dptr++ = *sptr3++;
1096 } 1096 }
1097 } 1097 }
1098 break; 1098 break;
1099 } 1099 }
1100 1100
1101 dp += img->pitch; 1101 dp += img->pitch;
1102 } 1102 }
1103 1103
1104 // Read VGA palette 1104 // Read VGA palette
1105 if (paletted) 1105 if (paletted)
1123 { 1123 {
1124 res = dmError(DMERR_MALLOC, 1124 res = dmError(DMERR_MALLOC,
1125 "PCX: Could not allocate palette data!\n"); 1125 "PCX: Could not allocate palette data!\n");
1126 goto error; 1126 goto error;
1127 } 1127 }
1128 1128
1129 if (read) 1129 if (read)
1130 { 1130 {
1131 if (!dmReadPaletteData(fp, img->pal, ncolors)) 1131 if (!dmReadPaletteData(fp, img->pal, ncolors))
1132 { 1132 {
1133 res = dmError(DMERR_FREAD, 1133 res = dmError(DMERR_FREAD,
1164 { 1164 {
1165 return dmError(DMERR_FOPEN, 1165 return dmError(DMERR_FOPEN,
1166 "PCX: Could not open file '%s' for reading.\n", 1166 "PCX: Could not open file '%s' for reading.\n",
1167 filename); 1167 filename);
1168 } 1168 }
1169 1169
1170 res = dmReadPCXImageFILE(fp, pimg); 1170 res = dmReadPCXImageFILE(fp, pimg);
1171 1171
1172 fclose(fp); 1172 fclose(fp);
1173 return res; 1173 return res;
1174 } 1174 }
1300 Sint8 dcount; 1300 Sint8 dcount;
1301 Uint8 data; 1301 Uint8 data;
1302 1302
1303 if (!dm_fread_byte(fp, (Uint8 *) &dcount)) 1303 if (!dm_fread_byte(fp, (Uint8 *) &dcount))
1304 return FALSE; 1304 return FALSE;
1305 1305
1306 if (dcount == -128) 1306 if (dcount == -128)
1307 { 1307 {
1308 if (!dm_fread_byte(fp, &data)) 1308 if (!dm_fread_byte(fp, &data))
1309 return FALSE; 1309 return FALSE;
1310 } 1310 }
1359 { 1359 {
1360 DMImage *img; 1360 DMImage *img;
1361 Uint8 *buf; 1361 Uint8 *buf;
1362 size_t bufLen; 1362 size_t bufLen;
1363 int yc, res = DMERR_OK; 1363 int yc, res = DMERR_OK;
1364 1364
1365 *read = 0; 1365 *read = 0;
1366 1366
1367 // Allocate image 1367 // Allocate image
1368 if ((*pimg = img = dmImageAlloc(iff->bmhd.w, iff->bmhd.h)) == NULL) 1368 if ((*pimg = img = dmImageAlloc(iff->bmhd.w, iff->bmhd.h)) == NULL)
1369 return DMERR_MALLOC; 1369 return DMERR_MALLOC;
1372 bufLen = ((img->width + 15) / 16) * 2; 1372 bufLen = ((img->width + 15) / 16) * 2;
1373 if ((buf = dmMalloc(bufLen)) == NULL) 1373 if ((buf = dmMalloc(bufLen)) == NULL)
1374 return DMERR_MALLOC; 1374 return DMERR_MALLOC;
1375 1375
1376 dmMsg(3, "ILBM: plane row size %d bytes.\n", bufLen); 1376 dmMsg(3, "ILBM: plane row size %d bytes.\n", bufLen);
1377 1377
1378 // Decode the chunk 1378 // Decode the chunk
1379 for (yc = 0; yc < img->height; yc++) 1379 for (yc = 0; yc < img->height; yc++)
1380 { 1380 {
1381 int plane; 1381 int plane;
1382 const int nplanes = iff->bmhd.nplanes; 1382 const int nplanes = iff->bmhd.nplanes;
1416 1416
1417 // Decode mask 1417 // Decode mask
1418 for (xc = 0; xc < img->width; xc++) 1418 for (xc = 0; xc < img->width; xc++)
1419 { 1419 {
1420 const Uint8 data = (buf[xc / 8] >> (7 - (xc & 7))) & 1; 1420 const Uint8 data = (buf[xc / 8] >> (7 - (xc & 7))) & 1;
1421 1421
1422 // Black out any pixels with mask bit 0 1422 // Black out any pixels with mask bit 0
1423 if (!data) 1423 if (!data)
1424 dp[xc] = 0; 1424 dp[xc] = 0;
1425 } 1425 }
1426 1426
1482 chunk.size < 32) 1482 chunk.size < 32)
1483 { 1483 {
1484 return dmError(DMERR_NOT_SUPPORTED, 1484 return dmError(DMERR_NOT_SUPPORTED,
1485 "ILBM: Not a IFF file.\n"); 1485 "ILBM: Not a IFF file.\n");
1486 } 1486 }
1487 1487
1488 // Check IFF ILBM signature 1488 // Check IFF ILBM signature
1489 if (!dm_fread_be32(fp, &idILBM) || 1489 if (!dm_fread_be32(fp, &idILBM) ||
1490 (idILBM != IFF_ID_ILBM && idILBM != IFF_ID_PBM)) 1490 (idILBM != IFF_ID_ILBM && idILBM != IFF_ID_PBM))
1491 { 1491 {
1492 return dmError(DMERR_INVALID_DATA, 1492 return dmError(DMERR_INVALID_DATA,
1500 if (!dmReadIFFChunk(fp, &chunk)) 1500 if (!dmReadIFFChunk(fp, &chunk))
1501 { 1501 {
1502 return dmError(DMERR_FREAD, 1502 return dmError(DMERR_FREAD,
1503 "ILBM: Error reading IFF ILBM data.\n"); 1503 "ILBM: Error reading IFF ILBM data.\n");
1504 } 1504 }
1505 1505
1506 switch (chunk.id) 1506 switch (chunk.id)
1507 { 1507 {
1508 case IFF_ID_BMHD: 1508 case IFF_ID_BMHD:
1509 // Check for multiple occurences of BMHD 1509 // Check for multiple occurences of BMHD
1510 if ((res = dmCheckIFFChunk(&iff.chBMHD, &chunk, FALSE, sizeof(iff.bmhd))) != DMERR_OK) 1510 if ((res = dmCheckIFFChunk(&iff.chBMHD, &chunk, FALSE, sizeof(iff.bmhd))) != DMERR_OK)
1530 } 1530 }
1531 1531
1532 dmMsg(3, "ILBM: BMHD %d x %d @ %d, %d : nplanes=%d, comp=%d, mask=%d\n", 1532 dmMsg(3, "ILBM: BMHD %d x %d @ %d, %d : nplanes=%d, comp=%d, mask=%d\n",
1533 iff.bmhd.w, iff.bmhd.h, iff.bmhd.x, iff.bmhd.y, 1533 iff.bmhd.w, iff.bmhd.h, iff.bmhd.x, iff.bmhd.y,
1534 iff.bmhd.nplanes, iff.bmhd.compression, iff.bmhd.masking); 1534 iff.bmhd.nplanes, iff.bmhd.compression, iff.bmhd.masking);
1535 1535
1536 // Sanity check 1536 // Sanity check
1537 if (iff.bmhd.nplanes < 1 || iff.bmhd.nplanes > 8 || 1537 if (iff.bmhd.nplanes < 1 || iff.bmhd.nplanes > 8 ||
1538 (iff.bmhd.compression != IFF_COMP_NONE && 1538 (iff.bmhd.compression != IFF_COMP_NONE &&
1539 iff.bmhd.compression != IFF_COMP_BYTERUN1) || 1539 iff.bmhd.compression != IFF_COMP_BYTERUN1) ||
1540 (iff.bmhd.masking != IFF_MASK_NONE && 1540 (iff.bmhd.masking != IFF_MASK_NONE &&
1567 dmMsg(3, "ILBM: CMAP %d entries (%d bytes)\n", 1567 dmMsg(3, "ILBM: CMAP %d entries (%d bytes)\n",
1568 iff.ncolors, chunk.size, 1 << iff.bmhd.nplanes); 1568 iff.ncolors, chunk.size, 1 << iff.bmhd.nplanes);
1569 1569
1570 if (iff.bmhd.nplanes > 0 && iff.ncolors != 1 << iff.bmhd.nplanes) 1570 if (iff.bmhd.nplanes > 0 && iff.ncolors != 1 << iff.bmhd.nplanes)
1571 dmMsg(3, "ILBM: Expected %d entries in CMAP.\n", 1 << iff.bmhd.nplanes); 1571 dmMsg(3, "ILBM: Expected %d entries in CMAP.\n", 1 << iff.bmhd.nplanes);
1572 1572
1573 // Read palette 1573 // Read palette
1574 if (iff.ncolors > 0) 1574 if (iff.ncolors > 0)
1575 { 1575 {
1576 if (!dmPaletteAlloc(&iff.pal, iff.ncolors, 1576 if (!dmPaletteAlloc(&iff.pal, iff.ncolors,
1577 (iff.bmhd.masking == IFF_MASK_TRANSP) ? iff.bmhd.transp : -1)) 1577 (iff.bmhd.masking == IFF_MASK_TRANSP) ? iff.bmhd.transp : -1))
1587 } 1587 }
1588 1588
1589 if (iff.chBMHD.count && iff.chBODY.count) 1589 if (iff.chBMHD.count && iff.chBODY.count)
1590 parsed = TRUE; 1590 parsed = TRUE;
1591 break; 1591 break;
1592 1592
1593 case IFF_ID_BODY: 1593 case IFF_ID_BODY:
1594 // Check for multiple occurences of CMAP 1594 // Check for multiple occurences of CMAP
1595 if ((res = dmCheckIFFChunk(&iff.chBODY, &chunk, FALSE, 1)) != DMERR_OK) 1595 if ((res = dmCheckIFFChunk(&iff.chBODY, &chunk, FALSE, 1)) != DMERR_OK)
1596 return res; 1596 return res;
1597 1597
1601 return dmError(DMERR_INVALID_DATA, 1601 return dmError(DMERR_INVALID_DATA,
1602 "ILBM: BODY chunk before BMHD?\n"); 1602 "ILBM: BODY chunk before BMHD?\n");
1603 } 1603 }
1604 1604
1605 dmMsg(3, "ILBM: BODY chunk size %d bytes\n", chunk.size); 1605 dmMsg(3, "ILBM: BODY chunk size %d bytes\n", chunk.size);
1606 1606
1607 // Decode the body 1607 // Decode the body
1608 if (iff.planar) 1608 if (iff.planar)
1609 { 1609 {
1610 if ((res = dmDecodeILBMBody(fp, &iff, pimg, &read)) != DMERR_OK) 1610 if ((res = dmDecodeILBMBody(fp, &iff, pimg, &read)) != DMERR_OK)
1611 return res; 1611 return res;
1628 if (!dm_fread_be32(fp, &iff.camg)) 1628 if (!dm_fread_be32(fp, &iff.camg))
1629 { 1629 {
1630 return dmError(DMERR_FREAD, 1630 return dmError(DMERR_FREAD,
1631 "ILBM: Error reading CAMG chunk.\n"); 1631 "ILBM: Error reading CAMG chunk.\n");
1632 } 1632 }
1633 1633
1634 dmMsg(3, "ILBM: CAMG value 0x%08x\n", iff.camg); 1634 dmMsg(3, "ILBM: CAMG value 0x%08x\n", iff.camg);
1635 1635
1636 if ((iff.camg & IFF_CAMG_HAM)) 1636 if ((iff.camg & IFF_CAMG_HAM))
1637 { 1637 {
1638 return dmError(DMERR_NOT_SUPPORTED, 1638 return dmError(DMERR_NOT_SUPPORTED,
1646 1646
1647 default: 1647 default:
1648 { 1648 {
1649 dmMsg(4, "Unknown chunk ID '%s', size %d\n", 1649 dmMsg(4, "Unknown chunk ID '%s', size %d\n",
1650 dmGetIFFChunkID(&chunk), chunk.size); 1650 dmGetIFFChunkID(&chunk), chunk.size);
1651 1651
1652 if (fseeko(fp, chunk.size, SEEK_CUR) != 0) 1652 if (fseeko(fp, chunk.size, SEEK_CUR) != 0)
1653 { 1653 {
1654 return dmError(DMERR_FSEEK, 1654 return dmError(DMERR_FSEEK,
1655 "ILBM: Error skipping in file."); 1655 "ILBM: Error skipping in file.");
1656 } 1656 }
1657 } 1657 }
1658 break; 1658 break;
1659 } 1659 }
1660 1660
1661 if (chunk.size & 1) 1661 if (chunk.size & 1)
1662 fgetc(fp); 1662 fgetc(fp);
1663 } 1663 }
1664 1664
1665 // Set colormap after finishing 1665 // Set colormap after finishing
1682 iff.pal = NULL; 1682 iff.pal = NULL;
1683 return DMERR_MALLOC; 1683 return DMERR_MALLOC;
1684 } 1684 }
1685 else 1685 else
1686 iff.pal = ptmp; 1686 iff.pal = ptmp;
1687 1687
1688 for (i = 0; i < iff.ncolors; i++) 1688 for (i = 0; i < iff.ncolors; i++)
1689 { 1689 {
1690 int i2 = iff.ncolors + i; 1690 int i2 = iff.ncolors + i;
1691 iff.pal[i2].r = iff.pal[i].r / 2; 1691 iff.pal[i2].r = iff.pal[i].r / 2;
1692 iff.pal[i2].g = iff.pal[i].g / 2; 1692 iff.pal[i2].g = iff.pal[i].g / 2;
1693 iff.pal[i2].b = iff.pal[i].b / 2; 1693 iff.pal[i2].b = iff.pal[i].b / 2;
1694 } 1694 }
1695 } 1695 }
1696 1696
1697 (*pimg)->ncolors = iff.ncolors; 1697 (*pimg)->ncolors = iff.ncolors;
1698 (*pimg)->pal = iff.pal; 1698 (*pimg)->pal = iff.pal;
1699 } 1699 }
1700 1700
1701 return res; 1701 return res;
1711 { 1711 {
1712 return dmError(DMERR_FOPEN, 1712 return dmError(DMERR_FOPEN,
1713 "ILBM: Could not open file '%s' for reading.\n", 1713 "ILBM: Could not open file '%s' for reading.\n",
1714 filename); 1714 filename);
1715 } 1715 }
1716 1716
1717 res = dmReadILBMImageFILE(fp, pimg); 1717 res = dmReadILBMImageFILE(fp, pimg);
1718 1718
1719 fclose(fp); 1719 fclose(fp);
1720 return res; 1720 return res;
1721 } 1721 }