Mercurial > hg > dmlib
comparison libgfx.c @ 451:fdc91f2a0d27
Modularize palette reading and handling code.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 04 Nov 2012 12:06:26 +0200 |
parents | 117f94b253af |
children | d1f7ddc84c7c |
comparison
equal
deleted
inserted
replaced
450:acea24b77bdc | 451:fdc91f2a0d27 |
---|---|
46 dmFree(img); | 46 dmFree(img); |
47 } | 47 } |
48 } | 48 } |
49 | 49 |
50 | 50 |
51 BOOL dmPaletteAlloc(DMColor **ppal, int ncolors) | |
52 { | |
53 if (ppal == NULL) | |
54 return FALSE; | |
55 | |
56 return (*ppal = dmCalloc(ncolors, sizeof(DMColor))) != NULL; | |
57 } | |
58 | |
59 | |
60 BOOL dmImageAllocPalette(DMImage *img, int ncolors) | |
61 { | |
62 if (img == NULL) | |
63 return FALSE; | |
64 | |
65 img->ncolors = ncolors; | |
66 return dmPaletteAlloc(&(img->pal), ncolors); | |
67 } | |
68 | |
69 | |
51 int dmImageGetBytesPerPixel(int format) | 70 int dmImageGetBytesPerPixel(int format) |
52 { | 71 { |
53 switch (format) | 72 switch (format) |
54 { | 73 { |
55 case DM_IFMT_PALETTE : return 1; | 74 case DM_IFMT_PALETTE : return 1; |
59 | 78 |
60 case DM_IFMT_RGBA : return 4; | 79 case DM_IFMT_RGBA : return 4; |
61 | 80 |
62 default: return 0; | 81 default: return 0; |
63 } | 82 } |
83 } | |
84 | |
85 | |
86 static BOOL dmReadPaletteData(FILE *fp, DMColor *pal, int ncolors) | |
87 { | |
88 int i; | |
89 | |
90 for (i = 0; i < ncolors; i++) | |
91 { | |
92 Uint8 colR, colG, colB; | |
93 if (!dm_fread_byte(fp, &colR) || | |
94 !dm_fread_byte(fp, &colG) || | |
95 !dm_fread_byte(fp, &colB)) | |
96 return FALSE; | |
97 | |
98 pal[i].r = colR; | |
99 pal[i].g = colG; | |
100 pal[i].b = colB; | |
101 } | |
102 | |
103 return TRUE; | |
64 } | 104 } |
65 | 105 |
66 | 106 |
67 int dmWriteImageData(DMImage *img, void *cbdata, BOOL (*writeRowCB)(void *, Uint8 *, size_t), const DMImageSpec *spec) | 107 int dmWriteImageData(DMImage *img, void *cbdata, BOOL (*writeRowCB)(void *, Uint8 *, size_t), const DMImageSpec *spec) |
68 { | 108 { |
841 } | 881 } |
842 | 882 |
843 // Read VGA palette | 883 // Read VGA palette |
844 if (paletted) | 884 if (paletted) |
845 { | 885 { |
846 int i; | 886 int i, ncolors; |
847 Uint8 tmpb; | 887 Uint8 tmpb; |
848 BOOL read; | 888 BOOL read; |
849 | 889 |
850 if (!dm_fread_byte(fp, &tmpb) || tmpb != 0x0C) | 890 if (!dm_fread_byte(fp, &tmpb) || tmpb != 0x0C) |
851 { | 891 { |
852 read = FALSE; | 892 read = FALSE; |
853 img->ncolors = 16; | 893 ncolors = 16; |
854 } | 894 } |
855 else | 895 else |
856 { | 896 { |
857 read = TRUE; | 897 read = TRUE; |
858 img->ncolors = 256; | 898 ncolors = 256; |
859 } | 899 } |
860 | 900 |
861 if ((img->pal = dmCalloc(img->ncolors, sizeof(DMColor))) == NULL) | 901 if (!dmImageAllocPalette(img, ncolors)) |
862 { | 902 { |
863 dmError("PCX: Could not allocate palette data!\n"); | 903 dmError("PCX: Could not allocate palette data!\n"); |
864 res = DMERR_MALLOC; | 904 res = DMERR_MALLOC; |
865 goto error; | 905 goto error; |
866 } | 906 } |
867 | 907 |
868 if (read) | 908 if (read) |
869 { | 909 { |
870 for (i = 0; i < img->ncolors; i++) | 910 if (!dmReadPaletteData(fp, img->pal, ncolors)) |
871 { | 911 { |
872 Uint8 tmpR, tmpG, tmpB; | 912 dmError("PCX: Error reading palette.\n"); |
873 if (!dm_fread_byte(fp, &tmpR) || | 913 return DMERR_FREAD; |
874 !dm_fread_byte(fp, &tmpG) || | |
875 !dm_fread_byte(fp, &tmpB)) | |
876 goto error; | |
877 | |
878 img->pal[i].r = tmpR; | |
879 img->pal[i].g = tmpG; | |
880 img->pal[i].b = tmpB; | |
881 } | 914 } |
882 } | 915 } |
883 else | 916 else |
884 { | 917 { |
885 for (i = 0; i < img->ncolors; i++) | 918 for (i = 0; i < img->ncolors; i++) |
890 img->pal[i].g = hdr.colormap[i].g; | 923 img->pal[i].g = hdr.colormap[i].g; |
891 img->pal[i].b = hdr.colormap[i].b; | 924 img->pal[i].b = hdr.colormap[i].b; |
892 } | 925 } |
893 } | 926 } |
894 } | 927 } |
895 | |
896 | |
897 } | 928 } |
898 | 929 |
899 error: | 930 error: |
900 dmFree(pcx.buf); | 931 dmFree(pcx.buf); |
901 return res; | 932 return res; |
1263 iff.ncolors, chunk.size, 1 << iff.bmhd.nplanes); | 1294 iff.ncolors, chunk.size, 1 << iff.bmhd.nplanes); |
1264 | 1295 |
1265 if (iff.bmhd.nplanes > 0 && iff.ncolors != 1 << iff.bmhd.nplanes) | 1296 if (iff.bmhd.nplanes > 0 && iff.ncolors != 1 << iff.bmhd.nplanes) |
1266 dmMsg(2, "ILBM: Expected %d entries in CMAP.\n", 1 << iff.bmhd.nplanes); | 1297 dmMsg(2, "ILBM: Expected %d entries in CMAP.\n", 1 << iff.bmhd.nplanes); |
1267 | 1298 |
1268 if (iff.ncolors == 0) | 1299 // Read palette |
1269 break; | 1300 if (iff.ncolors > 0) |
1270 | |
1271 // Allocate palette | |
1272 if ((iff.pal = dmMalloc(sizeof(DMColor) * iff.ncolors)) == NULL) | |
1273 { | 1301 { |
1274 dmError("ILBM: Could not allocate memory for palette.\n"); | 1302 if (!dmPaletteAlloc(&iff.pal, iff.ncolors)) |
1275 return DMERR_MALLOC; | |
1276 } | |
1277 | |
1278 // Read palette | |
1279 for (i = 0; i < iff.ncolors; i++) | |
1280 { | |
1281 Uint8 colR, colG, colB; | |
1282 if (!dm_fread_byte(fp, &colR) || | |
1283 !dm_fread_byte(fp, &colG) || | |
1284 !dm_fread_byte(fp, &colB)) | |
1285 { | 1303 { |
1286 dmError("ILBM: Error reading CMAP entry #%d, broken file.\n", i); | 1304 dmError("ILBM: Could not allocate palette data.\n"); |
1305 return DMERR_MALLOC; | |
1306 } | |
1307 if (!dmReadPaletteData(fp, iff.pal, iff.ncolors)) | |
1308 { | |
1309 dmError("ILBM: Error reading CMAP.\n"); | |
1287 return DMERR_FREAD; | 1310 return DMERR_FREAD; |
1288 } | 1311 } |
1289 | |
1290 iff.pal[i].r = colR; | |
1291 iff.pal[i].g = colG; | |
1292 iff.pal[i].b = colB; | |
1293 } | 1312 } |
1294 | 1313 |
1295 if (iff.chBMHD.count && iff.chBODY.count) | 1314 if (iff.chBMHD.count && iff.chBODY.count) |
1296 parsed = TRUE; | 1315 parsed = TRUE; |
1297 break; | 1316 break; |