Mercurial > hg > dmlib
annotate src/libgfx.c @ 1300:fd442faa705f
"Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 19 Aug 2017 03:57:15 +0300 |
parents | b0c0be4c76f9 |
children | e03f20d0f785 |
rev | line source |
---|---|
435 | 1 /* |
2 * Functions for reading and converting various restricted | |
3 * C64/etc and/or indexed/paletted graphics formats. | |
4 * Programmed and designed by Matti 'ccr' Hamalainen | |
5 * (C) Copyright 2012 Tecnic Software productions (TNSP) | |
6 * | |
7 * Please read file 'COPYING' for information on license and distribution. | |
8 */ | |
9 #include "libgfx.h" | |
10 #include "dmfile.h" | |
11 #include "dmbstr.h" | |
12 | |
13 #ifdef DM_USE_LIBPNG | |
14 #include <png.h> | |
15 #endif | |
16 | |
17 | |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
18 int dmGFXErrorMode = DM_ERRMODE_FAIL; |
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
19 |
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
20 |
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
21 |
487
b89598501cec
Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
472
diff
changeset
|
22 BOOL dmCompareColor(const DMColor *c1, const DMColor *c2, BOOL alpha) |
b89598501cec
Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
472
diff
changeset
|
23 { |
b89598501cec
Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
472
diff
changeset
|
24 if (c1->r == c2->r && |
b89598501cec
Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
472
diff
changeset
|
25 c1->g == c2->g && |
b89598501cec
Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
472
diff
changeset
|
26 c1->b == c2->b) |
b89598501cec
Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
472
diff
changeset
|
27 return alpha ? (c1->a == c2->a) : TRUE; |
b89598501cec
Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
472
diff
changeset
|
28 else |
b89598501cec
Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
472
diff
changeset
|
29 return FALSE; |
b89598501cec
Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
472
diff
changeset
|
30 } |
b89598501cec
Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
472
diff
changeset
|
31 |
b89598501cec
Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
472
diff
changeset
|
32 |
1286 | 33 int dmImageGetBytesPerPixel(const int format) |
34 { | |
35 switch (format) | |
36 { | |
37 case DM_IFMT_PALETTE : return 1; | |
38 case DM_IFMT_RGB : return 3; | |
39 case DM_IFMT_RGBA : return 4; | |
40 default: return -1; | |
41 } | |
42 } | |
43 | |
44 | |
45 DMImage * dmImageAlloc(const int width, const int height, const int format, const int bpp) | |
435 | 46 { |
928
ebe0d93e03c0
Use dmMalloc0() instead of dmCalloc() here.
Matti Hamalainen <ccr@tnsp.org>
parents:
902
diff
changeset
|
47 DMImage *img = dmMalloc0(sizeof(DMImage)); |
435 | 48 if (img == NULL) |
49 return NULL; | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
50 |
1286 | 51 img->width = width; |
52 img->height = height; | |
53 img->format = format; | |
54 img->bpp = (bpp <= 0) ? dmImageGetBytesPerPixel(format) * 8 : bpp; | |
55 img->pitch = width * img->bpp; | |
56 img->size = img->pitch * img->height; | |
57 img->ctransp = -1; | |
930 | 58 |
929
e1378398be0f
Add size field for allocated data size in DMImage.
Matti Hamalainen <ccr@tnsp.org>
parents:
928
diff
changeset
|
59 if ((img->data = dmMalloc(img->size)) == NULL) |
435 | 60 { |
61 dmFree(img); | |
62 return NULL; | |
63 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
64 |
435 | 65 return img; |
66 } | |
67 | |
68 | |
69 void dmImageFree(DMImage *img) | |
70 { | |
71 if (img != NULL) | |
72 { | |
73 if (!img->constpal) | |
74 { | |
75 dmFree(img->pal); | |
76 } | |
77 dmFree(img->data); | |
78 dmFree(img); | |
79 } | |
80 } | |
81 | |
82 | |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
83 BOOL dmPaletteAlloc(DMColor **ppal, int ncolors, int ctransp) |
451
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
84 { |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
85 int i; |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
86 |
451
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
87 if (ppal == NULL) |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
88 return FALSE; |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
89 |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
90 // Allocate desired amount of palette |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
91 if ((*ppal = dmCalloc(ncolors, sizeof(DMColor))) == NULL) |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
92 return FALSE; |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
93 |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
94 // Set alpha values to max, except for transparent color |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
95 for (i = 0; i < ncolors; i++) |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
96 { |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
97 (*ppal)[i].a = (i == ctransp) ? 0 : 255; |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
98 } |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
99 |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
100 return TRUE; |
451
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
101 } |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
102 |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
103 |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
104 BOOL dmImageAllocPalette(DMImage *img, int ncolors, int ctransp) |
451
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
105 { |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
106 if (img == NULL) |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
107 return FALSE; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
108 |
451
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
109 img->ncolors = ncolors; |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
110 img->ctransp = ctransp; |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
111 return dmPaletteAlloc(&(img->pal), ncolors, ctransp); |
451
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
112 } |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
113 |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
114 |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
115 static BOOL dmReadPaletteData(FILE *fp, DMColor *pal, int ncolors) |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
116 { |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
117 int i; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
118 |
451
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
119 for (i = 0; i < ncolors; i++) |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
120 { |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
121 Uint8 colR, colG, colB; |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
122 if (!dm_fread_byte(fp, &colR) || |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
123 !dm_fread_byte(fp, &colG) || |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
124 !dm_fread_byte(fp, &colB)) |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
125 return FALSE; |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
126 |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
127 pal[i].r = colR; |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
128 pal[i].g = colG; |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
129 pal[i].b = colB; |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
130 } |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
131 |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
132 return TRUE; |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
133 } |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
134 |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
135 |
1290
e7dc9bb9777e
Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents:
1288
diff
changeset
|
136 int dmWriteImageData(DMImage *img, void *cbdata, int (*writeRowCB)(void *, Uint8 *, size_t), const DMImageConvSpec *spec) |
435 | 137 { |
138 int x, y, yscale, xscale, res = 0, rowSize, rowWidth; | |
139 Uint8 *row = NULL; | |
140 | |
141 // Allocate memory for row buffer | |
902
c6c480e8e1c8
Add separate X and Y scaling to gfxconv and libgfx outputters.
Matti Hamalainen <ccr@tnsp.org>
parents:
858
diff
changeset
|
142 rowWidth = img->width * spec->scaleX; |
435 | 143 rowSize = rowWidth * dmImageGetBytesPerPixel(spec->format); |
144 | |
145 if ((row = dmMalloc(rowSize + 16)) == NULL) | |
146 { | |
147 res = DMERR_MALLOC; | |
148 goto done; | |
149 } | |
150 | |
151 // Generate the image | |
152 for (y = 0; y < img->height; y++) | |
153 { | |
436
86f956e4580f
Cosmetics, rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents:
435
diff
changeset
|
154 Uint8 *ptr1 = row, |
435 | 155 *ptr2 = ptr1 + rowWidth, |
1291 | 156 *ptr3 = ptr2 + rowWidth, |
157 *ptr4 = ptr3 + rowWidth; | |
435 | 158 |
159 for (x = 0; x < img->width; x++) | |
160 { | |
1291 | 161 Uint8 c = img->data[(y * img->pitch) + (x * img->bpp) / 8], |
162 qr, qg, qb, qa; | |
163 | |
435 | 164 switch (spec->format) |
165 { | |
166 case DM_IFMT_PALETTE: | |
902
c6c480e8e1c8
Add separate X and Y scaling to gfxconv and libgfx outputters.
Matti Hamalainen <ccr@tnsp.org>
parents:
858
diff
changeset
|
167 for (xscale = 0; xscale < spec->scaleX; xscale++) |
436
86f956e4580f
Cosmetics, rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents:
435
diff
changeset
|
168 *ptr1++ = c; |
435 | 169 break; |
170 | |
171 case DM_IFMT_RGBA: | |
172 qr = img->pal[c].r; | |
173 qg = img->pal[c].g; | |
174 qb = img->pal[c].b; | |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
175 qa = img->pal[c].a; |
1292 | 176 |
1291 | 177 if (spec->planar) |
435 | 178 { |
1291 | 179 for (xscale = 0; xscale < spec->scaleX; xscale++) |
180 { | |
181 *ptr1++ = qr; | |
182 *ptr2++ = qg; | |
183 *ptr3++ = qb; | |
184 *ptr4++ = qa; | |
185 } | |
186 } | |
187 else | |
188 { | |
189 for (xscale = 0; xscale < spec->scaleX; xscale++) | |
190 { | |
191 *ptr1++ = qr; | |
192 *ptr1++ = qg; | |
193 *ptr1++ = qb; | |
194 *ptr1++ = qa; | |
195 } | |
435 | 196 } |
197 break; | |
198 | |
199 case DM_IFMT_RGB: | |
200 qr = img->pal[c].r; | |
201 qg = img->pal[c].g; | |
202 qb = img->pal[c].b; | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
203 |
1291 | 204 if (spec->planar) |
435 | 205 { |
1291 | 206 for (xscale = 0; xscale < spec->scaleX; xscale++) |
207 { | |
208 *ptr1++ = qr; | |
209 *ptr2++ = qg; | |
210 *ptr3++ = qb; | |
211 } | |
435 | 212 } |
1291 | 213 else |
435 | 214 { |
1291 | 215 for (xscale = 0; xscale < spec->scaleX; xscale++) |
216 { | |
217 *ptr1++ = qr; | |
218 *ptr1++ = qg; | |
219 *ptr1++ = qb; | |
220 } | |
435 | 221 } |
222 break; | |
223 } | |
224 } | |
225 | |
902
c6c480e8e1c8
Add separate X and Y scaling to gfxconv and libgfx outputters.
Matti Hamalainen <ccr@tnsp.org>
parents:
858
diff
changeset
|
226 for (yscale = 0; yscale < spec->scaleY; yscale++) |
435 | 227 { |
808
f8126fa1df9b
Use dmlib error code return value in cases where a plain boolean was used
Matti Hamalainen <ccr@tnsp.org>
parents:
800
diff
changeset
|
228 if ((res = writeRowCB(cbdata, row, rowSize)) != DMERR_OK) |
435 | 229 goto done; |
230 } | |
231 } | |
232 | |
233 done: | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
234 dmFree(row); |
435 | 235 return res; |
236 } | |
237 | |
238 | |
239 #define DMCOL(x) (((x) >> 4) & 0xf) | |
240 | |
462 | 241 int dmWriteIFFMasterRAWPalette(FILE *fp, DMImage *img, int ncolors, |
242 const char *indent, const char *type) | |
435 | 243 { |
244 int i; | |
245 | |
246 for (i = 0; i < ncolors; i++) | |
247 { | |
248 int color; | |
249 if (i < img->ncolors) | |
250 { | |
251 color = (DMCOL(img->pal[i].r) << 8) | | |
252 (DMCOL(img->pal[i].g) << 4) | | |
253 (DMCOL(img->pal[i].b)); | |
254 } | |
255 else | |
256 color = 0; | |
257 | |
462 | 258 fprintf(fp, "%s%s $%04X\n", |
259 indent != NULL ? indent : "\t", | |
472
0359697eeb46
Oops, the ARAW palette output should use dc.w, not dw.b .. :P
Matti Hamalainen <ccr@tnsp.org>
parents:
467
diff
changeset
|
260 type != NULL ? type : "dc.w", |
462 | 261 color); |
435 | 262 } |
263 | |
462 | 264 return DMERR_OK; |
435 | 265 } |
266 | |
267 | |
1290
e7dc9bb9777e
Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents:
1288
diff
changeset
|
268 int dmWriteRAWImageFILE(FILE *fp, DMImage *img, const DMImageConvSpec *spec) |
435 | 269 { |
270 int xc, yc, plane, res; | |
800
c63e24f9aa9a
Modularize bitstream reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
566
diff
changeset
|
271 DMBitStreamContext bs; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
272 |
800
c63e24f9aa9a
Modularize bitstream reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
566
diff
changeset
|
273 if ((res = dmInitBitStreamFILE(&bs, fp)) != DMERR_OK) |
435 | 274 return res; |
275 | |
1291 | 276 if (spec->planar) |
435 | 277 { |
1291 | 278 // Output bitplanes in planerd format (each plane of line sequentially) |
435 | 279 for (yc = 0; yc < img->height; yc++) |
280 { | |
281 for (plane = 0; plane < spec->nplanes; plane++) | |
282 { | |
283 Uint8 *sp = img->data + yc * img->pitch; | |
284 for (xc = 0; xc < img->width; xc++) | |
285 { | |
286 if (!dmPutBits(&bs, (sp[xc] & (1 << plane)) ? 1 : 0, 1)) | |
287 return DMERR_FWRITE; | |
288 } | |
289 } | |
290 } | |
291 } | |
292 else | |
293 { | |
294 // Output each bitplane in sequence | |
295 for (plane = 0; plane < spec->nplanes; plane++) | |
296 { | |
297 for (yc = 0; yc < img->height; yc++) | |
298 { | |
299 Uint8 *sp = img->data + yc * img->pitch; | |
300 for (xc = 0; xc < img->width; xc++) | |
301 { | |
302 if (!dmPutBits(&bs, (sp[xc] & (1 << plane)) ? 1 : 0, 1)) | |
303 return DMERR_FWRITE; | |
304 } | |
305 } | |
306 } | |
307 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
308 |
435 | 309 return dmFlushBitStream(&bs); |
310 } | |
311 | |
566
d400e32b62d9
Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents:
487
diff
changeset
|
312 |
1290
e7dc9bb9777e
Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents:
1288
diff
changeset
|
313 int dmWriteRAWImage(const char *filename, DMImage *img, const DMImageConvSpec *spec) |
435 | 314 { |
315 FILE *fp; | |
316 int res; | |
317 | |
318 if ((fp = fopen(filename, "wb")) == NULL) | |
319 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
320 return dmError(DMERR_FOPEN, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
321 "RAW: Could not open file '%s' for writing.\n", |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
322 filename); |
435 | 323 } |
324 | |
566
d400e32b62d9
Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents:
487
diff
changeset
|
325 res = dmWriteRAWImageFILE(fp, img, spec); |
435 | 326 |
327 fclose(fp); | |
328 return res; | |
329 } | |
330 | |
331 | |
808
f8126fa1df9b
Use dmlib error code return value in cases where a plain boolean was used
Matti Hamalainen <ccr@tnsp.org>
parents:
800
diff
changeset
|
332 static int dmWritePPMRow(void *cbdata, Uint8 *row, size_t len) |
435 | 333 { |
808
f8126fa1df9b
Use dmlib error code return value in cases where a plain boolean was used
Matti Hamalainen <ccr@tnsp.org>
parents:
800
diff
changeset
|
334 if (fwrite(row, sizeof(Uint8), len, (FILE *) cbdata) == len) |
f8126fa1df9b
Use dmlib error code return value in cases where a plain boolean was used
Matti Hamalainen <ccr@tnsp.org>
parents:
800
diff
changeset
|
335 return DMERR_OK; |
f8126fa1df9b
Use dmlib error code return value in cases where a plain boolean was used
Matti Hamalainen <ccr@tnsp.org>
parents:
800
diff
changeset
|
336 else |
f8126fa1df9b
Use dmlib error code return value in cases where a plain boolean was used
Matti Hamalainen <ccr@tnsp.org>
parents:
800
diff
changeset
|
337 return DMERR_FWRITE; |
435 | 338 } |
339 | |
340 | |
1290
e7dc9bb9777e
Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents:
1288
diff
changeset
|
341 int dmWritePPMImageFILE(FILE *fp, DMImage *img, const DMImageConvSpec *spec) |
435 | 342 { |
1290
e7dc9bb9777e
Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents:
1288
diff
changeset
|
343 DMImageConvSpec tmpSpec; |
1288 | 344 |
435 | 345 // Write PPM header |
346 fprintf(fp, | |
347 "P6\n%d %d\n255\n", | |
902
c6c480e8e1c8
Add separate X and Y scaling to gfxconv and libgfx outputters.
Matti Hamalainen <ccr@tnsp.org>
parents:
858
diff
changeset
|
348 img->width * spec->scaleX, |
c6c480e8e1c8
Add separate X and Y scaling to gfxconv and libgfx outputters.
Matti Hamalainen <ccr@tnsp.org>
parents:
858
diff
changeset
|
349 img->height * spec->scaleY); |
435 | 350 |
351 // Write image data | |
1290
e7dc9bb9777e
Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents:
1288
diff
changeset
|
352 memcpy(&tmpSpec, spec, sizeof(DMImageConvSpec)); |
1288 | 353 tmpSpec.format = DM_IFMT_RGB; |
354 return dmWriteImageData(img, (void *) fp, dmWritePPMRow, &tmpSpec); | |
435 | 355 } |
356 | |
357 | |
1290
e7dc9bb9777e
Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents:
1288
diff
changeset
|
358 int dmWritePPMImage(const char *filename, DMImage *img, const DMImageConvSpec *spec) |
435 | 359 { |
360 FILE *fp; | |
361 int res; | |
362 | |
363 // Create output file | |
364 if ((fp = fopen(filename, "wb")) == NULL) | |
365 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
366 return dmError(DMERR_FOPEN, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
367 "PPM: could not open file '%s' for writing.\n", |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
368 filename); |
435 | 369 } |
370 | |
371 res = dmWritePPMImageFILE(fp, img, spec); | |
372 | |
373 fclose(fp); | |
374 return res; | |
375 } | |
376 | |
377 | |
378 #ifdef DM_USE_LIBPNG | |
808
f8126fa1df9b
Use dmlib error code return value in cases where a plain boolean was used
Matti Hamalainen <ccr@tnsp.org>
parents:
800
diff
changeset
|
379 static int dmWritePNGRow(void *cbdata, Uint8 *row, size_t len) |
435 | 380 { |
381 png_structp png_ptr = cbdata; | |
382 (void) len; | |
383 | |
384 if (setjmp(png_jmpbuf(png_ptr))) | |
808
f8126fa1df9b
Use dmlib error code return value in cases where a plain boolean was used
Matti Hamalainen <ccr@tnsp.org>
parents:
800
diff
changeset
|
385 return DMERR_INTERNAL; |
435 | 386 |
387 png_write_row(png_ptr, row); | |
388 | |
808
f8126fa1df9b
Use dmlib error code return value in cases where a plain boolean was used
Matti Hamalainen <ccr@tnsp.org>
parents:
800
diff
changeset
|
389 return DMERR_OK; |
435 | 390 } |
391 | |
392 | |
1290
e7dc9bb9777e
Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents:
1288
diff
changeset
|
393 int dmWritePNGImageFILE(FILE *fp, DMImage *img, const DMImageConvSpec *spec) |
435 | 394 { |
395 png_structp png_ptr = NULL; | |
396 png_infop info_ptr = NULL; | |
397 int fmt, res = DMERR_OK; | |
398 | |
399 // Create PNG structures | |
400 png_ptr = png_create_write_struct( | |
401 PNG_LIBPNG_VER_STRING, | |
402 NULL, NULL, NULL); | |
403 | |
404 if (png_ptr == NULL) | |
405 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
406 res = dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
407 "PNG: png_create_write_struct() failed.\n"); |
435 | 408 goto error; |
409 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
410 |
435 | 411 info_ptr = png_create_info_struct(png_ptr); |
412 if (info_ptr == NULL) | |
413 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
414 res = dmError(DMERR_INIT_FAIL, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
415 "PNG: png_create_info_struct(%p) failed.\n", |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
416 png_ptr); |
435 | 417 goto error; |
418 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
419 |
435 | 420 if (setjmp(png_jmpbuf(png_ptr))) |
421 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
422 res = dmError(DMERR_INIT_FAIL, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
423 "PNG: Error during image writing..\n"); |
435 | 424 goto error; |
425 } | |
426 | |
427 png_init_io(png_ptr, fp); | |
428 | |
429 // Write PNG header info | |
430 switch (spec->format) | |
431 { | |
432 case DM_IFMT_PALETTE: fmt = PNG_COLOR_TYPE_PALETTE; break; | |
433 case DM_IFMT_RGB : fmt = PNG_COLOR_TYPE_RGB; break; | |
434 case DM_IFMT_RGBA : fmt = PNG_COLOR_TYPE_RGB_ALPHA; break; | |
435 default: | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
436 res = dmError(DMERR_NOT_SUPPORTED, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
437 "PNG: Unsupported image format %d.\n", |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
438 spec->format); |
435 | 439 goto error; |
440 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
441 |
435 | 442 png_set_IHDR(png_ptr, info_ptr, |
902
c6c480e8e1c8
Add separate X and Y scaling to gfxconv and libgfx outputters.
Matti Hamalainen <ccr@tnsp.org>
parents:
858
diff
changeset
|
443 img->width * spec->scaleX, |
c6c480e8e1c8
Add separate X and Y scaling to gfxconv and libgfx outputters.
Matti Hamalainen <ccr@tnsp.org>
parents:
858
diff
changeset
|
444 img->height * spec->scaleY, |
435 | 445 8, /* bits per component */ |
446 fmt, | |
447 PNG_INTERLACE_NONE, | |
448 PNG_COMPRESSION_TYPE_DEFAULT, | |
449 PNG_FILTER_TYPE_DEFAULT); | |
450 | |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
451 dmMsg(2, "PNG: %d x %d, depth=%d, type=%d\n", |
902
c6c480e8e1c8
Add separate X and Y scaling to gfxconv and libgfx outputters.
Matti Hamalainen <ccr@tnsp.org>
parents:
858
diff
changeset
|
452 img->width * spec->scaleX, |
c6c480e8e1c8
Add separate X and Y scaling to gfxconv and libgfx outputters.
Matti Hamalainen <ccr@tnsp.org>
parents:
858
diff
changeset
|
453 img->height * spec->scaleY, |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
454 8, fmt); |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
455 |
435 | 456 // Palette |
457 if (spec->format == DM_IFMT_PALETTE) | |
458 { | |
459 int i; | |
858
e7019bd83cca
Fix potential longjmp clobbering of variable.
Matti Hamalainen <ccr@tnsp.org>
parents:
834
diff
changeset
|
460 png_colorp palette = png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH * sizeof(png_color)); |
435 | 461 |
462 if (palette == NULL) | |
463 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
464 res = dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
465 "PNG: Could not allocate palette structure."); |
435 | 466 goto error; |
467 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
468 |
1167 | 469 dmMemset(palette, 0, PNG_MAX_PALETTE_LENGTH * sizeof(png_color)); |
435 | 470 |
471 for (i = 0; i < img->ncolors; i++) | |
472 { | |
473 palette[i].red = img->pal[i].r; | |
474 palette[i].green = img->pal[i].g; | |
475 palette[i].blue = img->pal[i].b; | |
476 } | |
477 | |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
478 png_set_PLTE(png_ptr, info_ptr, palette, img->ncolors); |
435 | 479 } |
480 | |
481 // png_set_gAMA(png_ptr, info_ptr, 2.2); | |
482 | |
483 png_write_info(png_ptr, info_ptr); | |
484 | |
485 | |
486 // Write compressed image data | |
487 dmWriteImageData(img, (void *) png_ptr, dmWritePNGRow, spec); | |
488 | |
489 // Write footer | |
490 png_write_end(png_ptr, NULL); | |
491 | |
492 error: | |
493 if (png_ptr && info_ptr) | |
494 png_destroy_write_struct(&png_ptr, &info_ptr); | |
495 | |
496 return res; | |
497 } | |
498 | |
499 | |
1290
e7dc9bb9777e
Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents:
1288
diff
changeset
|
500 int dmWritePNGImage(const char *filename, DMImage *img, const DMImageConvSpec *spec) |
435 | 501 { |
502 int res; | |
503 FILE *fp; | |
504 | |
505 if ((fp = fopen(filename, "wb")) == NULL) | |
506 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
507 return dmError(DMERR_FOPEN, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
508 "PNG: could not open file '%s' for writing.\n", |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
509 filename); |
435 | 510 } |
511 | |
512 res = dmWritePNGImageFILE(fp, img, spec); | |
513 | |
514 fclose(fp); | |
515 return res; | |
516 } | |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
517 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
518 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
519 int dmReadPNGImageFILE(FILE *fp, DMImage **pimg) |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
520 { |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
521 png_structp png_ptr = NULL; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
522 png_infop info_ptr = NULL; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
523 png_colorp palette = NULL; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
524 png_bytep *row_pointers = NULL; |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
525 png_bytep trans = NULL; |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
526 png_uint_32 width, height; |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
527 int i, bit_depth, color_type, ncolors, ntrans; |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
528 int res = DMERR_OK; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
529 DMImage *img; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
530 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
531 // Create PNG structures |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
532 png_ptr = png_create_read_struct( |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
533 PNG_LIBPNG_VER_STRING, |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
534 NULL, NULL, NULL); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
535 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
536 if (png_ptr == NULL) |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
537 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
538 res = dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
539 "PNG: png_create_write_struct() failed.\n"); |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
540 goto error; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
541 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
542 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
543 info_ptr = png_create_info_struct(png_ptr); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
544 if (info_ptr == NULL) |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
545 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
546 res = dmError(DMERR_INIT_FAIL, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
547 "PNG: png_create_info_struct(%p) failed.\n", |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
548 png_ptr); |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
549 goto error; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
550 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
551 |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
552 if (setjmp(png_jmpbuf(png_ptr))) |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
553 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
554 res = dmError(DMERR_INIT_FAIL, |
1291 | 555 "PNG: Error during image reading.\n"); |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
556 goto error; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
557 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
558 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
559 png_init_io(png_ptr, fp); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
560 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
561 // Read image information |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
562 png_read_info(png_ptr, info_ptr); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
563 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
564 png_get_IHDR(png_ptr, info_ptr, &width, &height, |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
565 &bit_depth, &color_type, NULL, NULL, NULL); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
566 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
567 if (width < 1 || height < 1) |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
568 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
569 res = dmError(DMERR_INVALID_DATA, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
570 "PNG: Invalid width or height (%d x %d)\n", |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
571 width, height); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
572 goto error; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
573 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
574 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
575 switch (color_type) |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
576 { |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
577 case PNG_COLOR_TYPE_GRAY: |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
578 if (bit_depth < 8) |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
579 png_set_expand_gray_1_2_4_to_8(png_ptr); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
580 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
581 if (bit_depth > 8) |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
582 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
583 res = dmError(DMERR_NOT_SUPPORTED, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
584 "PNG: Unsupported bit depth for grayscale image: %d\n", |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
585 bit_depth); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
586 goto error; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
587 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
588 break; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
589 |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
590 case PNG_COLOR_TYPE_PALETTE: |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
591 png_set_packing(png_ptr); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
592 break; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
593 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
594 default: |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
595 res = dmError(DMERR_NOT_SUPPORTED, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
596 "PNG: RGB/RGBA images not supported for loading.\n"); |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
597 goto error; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
598 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
599 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
600 // Allocate image |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
601 dmMsg(2, "PNG: %d x %d, depth=%d, type=%d\n", |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
602 width, height, bit_depth, color_type); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
603 |
1300
fd442faa705f
"Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents:
1299
diff
changeset
|
604 if ((*pimg = img = dmImageAlloc(width, height, |
fd442faa705f
"Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents:
1299
diff
changeset
|
605 DM_IFMT_PALETTE, |
fd442faa705f
"Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents:
1299
diff
changeset
|
606 // XXX TODO? When/if we ever handle < 8bit indexed correctly, we can use the actual bpp |
fd442faa705f
"Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents:
1299
diff
changeset
|
607 -1 /* bit_depth */)) == NULL) |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
608 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
609 res = dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
610 "PNG: Could not allocate image data.\n"); |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
611 goto error; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
612 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
613 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
614 // ... |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
615 row_pointers = png_malloc(png_ptr, height * sizeof(png_bytep)); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
616 for (i = 0; i < img->height; i++) |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
617 row_pointers[i] = img->data + (i * img->pitch); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
618 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
619 png_read_image(png_ptr, row_pointers); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
620 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
621 png_read_end(png_ptr, NULL); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
622 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
623 // Create palette |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
624 switch (color_type) |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
625 { |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
626 case PNG_COLOR_TYPE_GRAY: |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
627 ncolors = 256; |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
628 dmMsg(2, "PNG: Generating %d color grayscale palette.\n", ncolors); |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
629 |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
630 if (!dmImageAllocPalette(img, ncolors, -1)) |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
631 { |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
632 res = DMERR_MALLOC; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
633 goto error; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
634 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
635 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
636 for (i = 0; i < img->ncolors; i++) |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
637 { |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
638 img->pal[i].r = img->pal[i].g = img->pal[i].b = i; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
639 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
640 break; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
641 |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
642 case PNG_COLOR_TYPE_PALETTE: |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
643 png_get_PLTE(png_ptr, info_ptr, &palette, &ncolors); |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
644 dmMsg(2, "PNG: Palette of %d colors found.\n", ncolors); |
467
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
645 if (ncolors > 0 && palette != NULL) |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
646 { |
467
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
647 if (!dmImageAllocPalette(img, ncolors, -1)) |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
648 { |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
649 res = DMERR_MALLOC; |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
650 goto error; |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
651 } |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
652 |
467
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
653 for (i = 0; i < img->ncolors; i++) |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
654 { |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
655 img->pal[i].r = palette[i].red; |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
656 img->pal[i].g = palette[i].green; |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
657 img->pal[i].b = palette[i].blue; |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
658 } |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
659 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
660 break; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
661 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
662 |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
663 if (color_type == PNG_COLOR_TYPE_PALETTE || |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
664 color_type == PNG_COLOR_TYPE_GRAY) |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
665 { |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
666 png_get_tRNS(png_ptr, info_ptr, &trans, &ntrans, NULL); |
467
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
667 if (trans != NULL && ntrans > 0) |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
668 { |
467
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
669 for (i = 0; i < img->ncolors && i < ntrans; i++) |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
670 { |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
671 img->pal[i].a = trans[i]; |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
672 if (img->ctransp < 0 && trans[i] == 0) |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
673 img->ctransp = i; |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
674 } |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
675 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
676 } |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
677 |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
678 error: |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
679 // png_free(png_ptr, palette); |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
680 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
681 if (png_ptr && info_ptr) |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
682 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
683 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
684 return res; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
685 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
686 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
687 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
688 int dmReadPNGImage(const char *filename, DMImage **img) |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
689 { |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
690 int res; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
691 FILE *fp; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
692 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
693 if ((fp = fopen(filename, "rb")) == NULL) |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
694 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
695 return dmError(DMERR_FOPEN, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
696 "PNG: Could not open file '%s' for reading.\n", |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
697 filename); |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
698 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
699 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
700 res = dmReadPNGImageFILE(fp, img); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
701 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
702 fclose(fp); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
703 return res; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
704 } |
435 | 705 #endif |
706 | |
707 | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
708 #define DMPCX_PAL_COLORS 16 // Number of internal palette colors |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
709 |
435 | 710 typedef struct |
711 { | |
712 Uint8 r,g,b; | |
713 } DMPCXColor; | |
714 | |
715 | |
716 typedef struct | |
717 { | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
718 Uint8 manufacturer, // always 0x0a |
1288 | 719 version, // Z-Soft PC Paintbrush version: |
720 // 0 = v2.5 | |
721 // 2 = v2.8 with palette, | |
722 // 3 = v2.8 without palette | |
723 // 4 = PC Paintbrush for Windows | |
724 // 5 = v3.0 or better | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
725 encoding, // usually 0x01 = RLE, 0x00 = uncompressed |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
726 bitsPerPlane; // bits per pixel per plane |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
727 |
435 | 728 Uint16 xmin, ymin, xmax, ymax; |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
729 Uint16 hres, vres; // resolution in DPI, can be image dimensions as well. |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
730 DMPCXColor colorMap[DMPCX_PAL_COLORS]; |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
731 Uint8 reserved; // should be set to 0 |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
732 Uint8 nplanes; // number of planes |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
733 Uint16 bpl; // bytes per plane LINE |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
734 Uint16 palInfo; // 1 = color/BW, 2 = grayscale |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
735 Uint16 hScreenSize, vScreenSize; |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
736 Uint8 filler[54]; |
435 | 737 } DMPCXHeader; |
738 | |
739 | |
740 typedef struct | |
741 { | |
742 DMPCXHeader *header; | |
743 Uint8 *buf; | |
744 size_t bufLen, bufOffs; | |
745 int format; | |
746 FILE *fp; | |
747 } DMPCXData; | |
748 | |
749 | |
1298 | 750 // Returns one byte from row buffer (of length len) at offset soffs, |
751 // OR zero if the offset is outside buffer. | |
435 | 752 static inline Uint8 dmPCXGetByte(Uint8 *row, const size_t len, const size_t soffs) |
753 { | |
754 return (soffs < len) ? row[soffs] : 0; | |
755 } | |
756 | |
757 static BOOL dmPCXFlush(DMPCXData *pcx) | |
758 { | |
759 BOOL ret = TRUE; | |
760 if (pcx->bufOffs > 0) | |
761 ret = fwrite(pcx->buf, sizeof(Uint8), pcx->bufOffs, pcx->fp) == pcx->bufOffs; | |
1297 | 762 |
435 | 763 pcx->bufOffs = 0; |
764 return ret; | |
765 } | |
766 | |
767 static inline BOOL dmPCXPutByte(DMPCXData *pcx, const Uint8 val) | |
768 { | |
769 if (pcx->bufOffs < pcx->bufLen) | |
770 { | |
771 pcx->buf[pcx->bufOffs++] = val; | |
772 return TRUE; | |
773 } | |
774 else | |
775 return dmPCXFlush(pcx); | |
776 } | |
777 | |
1297 | 778 |
779 static int dmPCXPutData(DMPCXData *pcx, const Uint8 data, const int count) | |
780 { | |
781 if (count == 1 && (data & 0xC0) != 0xC0) | |
782 { | |
783 if (!dmPCXPutByte(pcx, data)) | |
784 return DMERR_FWRITE; | |
785 } | |
786 else | |
787 { | |
788 if (!dmPCXPutByte(pcx, 0xC0 | count) || | |
789 !dmPCXPutByte(pcx, data)) | |
790 return DMERR_FWRITE; | |
791 } | |
792 return DMERR_OK; | |
793 } | |
794 | |
795 | |
808
f8126fa1df9b
Use dmlib error code return value in cases where a plain boolean was used
Matti Hamalainen <ccr@tnsp.org>
parents:
800
diff
changeset
|
796 static int dmWritePCXRow(void *cbdata, Uint8 *row, size_t len) |
435 | 797 { |
798 DMPCXData *pcx = (DMPCXData *) cbdata; | |
1297 | 799 int err; |
435 | 800 size_t soffs = 0; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
801 |
435 | 802 pcx->bufOffs = 0; |
803 | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
804 for (int plane = 0; plane < pcx->header->nplanes; plane++) |
435 | 805 { |
806 Uint8 data = dmPCXGetByte(row, len, soffs++), | |
807 count = 1; | |
808 | |
1297 | 809 size_t blen = pcx->header->bpl * pcx->header->nplanes; |
435 | 810 while (soffs < blen) |
811 { | |
1297 | 812 if (data == dmPCXGetByte(row, len, soffs) && count < 0x3F) |
435 | 813 { |
814 count++; | |
815 soffs++; | |
816 } | |
817 else | |
818 { | |
1297 | 819 if ((err = dmPCXPutData(pcx, data, count)) != DMERR_OK) |
820 return err; | |
435 | 821 |
822 data = dmPCXGetByte(row, len, soffs++); | |
823 count = 1; | |
824 } | |
825 } | |
826 | |
1297 | 827 if ((err = dmPCXPutData(pcx, data, count)) != DMERR_OK) |
828 return err; | |
435 | 829 |
830 if (!dmPCXFlush(pcx)) | |
808
f8126fa1df9b
Use dmlib error code return value in cases where a plain boolean was used
Matti Hamalainen <ccr@tnsp.org>
parents:
800
diff
changeset
|
831 return DMERR_FWRITE; |
435 | 832 } |
833 | |
834 | |
808
f8126fa1df9b
Use dmlib error code return value in cases where a plain boolean was used
Matti Hamalainen <ccr@tnsp.org>
parents:
800
diff
changeset
|
835 return DMERR_OK; |
435 | 836 } |
837 | |
838 | |
1290
e7dc9bb9777e
Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents:
1288
diff
changeset
|
839 int dmWritePCXImageFILE(FILE *fp, DMImage *img, const DMImageConvSpec *spec) |
435 | 840 { |
841 DMPCXData pcx; | |
842 DMPCXHeader hdr; | |
843 int res; | |
844 | |
845 // Create output file | |
846 pcx.buf = NULL; | |
1291 | 847 pcx.format = spec->paletted ? DM_IFMT_PALETTE : DM_IFMT_RGB; |
435 | 848 pcx.header = &hdr; |
849 pcx.fp = fp; | |
850 | |
851 // Create PCX header | |
1167 | 852 dmMemset(&hdr, 0, sizeof(hdr)); |
1288 | 853 if (spec->paletted && img->pal != NULL) |
435 | 854 { |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
855 for (int i = 0; i < (img->ncolors > DMPCX_PAL_COLORS ? DMPCX_PAL_COLORS : img->ncolors); i++) |
435 | 856 { |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
857 hdr.colorMap[i].r = img->pal[i].r; |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
858 hdr.colorMap[i].g = img->pal[i].g; |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
859 hdr.colorMap[i].b = img->pal[i].b; |
435 | 860 } |
861 } | |
862 hdr.manufacturer = 10; | |
863 hdr.version = 5; | |
864 hdr.encoding = 1; | |
902
c6c480e8e1c8
Add separate X and Y scaling to gfxconv and libgfx outputters.
Matti Hamalainen <ccr@tnsp.org>
parents:
858
diff
changeset
|
865 hdr.hres = img->width * spec->scaleX; |
c6c480e8e1c8
Add separate X and Y scaling to gfxconv and libgfx outputters.
Matti Hamalainen <ccr@tnsp.org>
parents:
858
diff
changeset
|
866 hdr.vres = img->height * spec->scaleY; |
435 | 867 hdr.xmin = hdr.ymin = 0; |
1297 | 868 hdr.xmax = (img->width * spec->scaleX) - 1; |
869 hdr.ymax = (img->height * spec->scaleY) - 1; | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
870 hdr.palInfo = 1; |
1283
642a0dd98c6e
Oops, forgot to adjust the writing and reading when adding two members to
Matti Hamalainen <ccr@tnsp.org>
parents:
1281
diff
changeset
|
871 hdr.hScreenSize = hdr.hres; |
642a0dd98c6e
Oops, forgot to adjust the writing and reading when adding two members to
Matti Hamalainen <ccr@tnsp.org>
parents:
1281
diff
changeset
|
872 hdr.vScreenSize = hdr.vres; |
435 | 873 |
1291 | 874 // TODO XXX .. maybe actually compute these asdf |
875 hdr.bitsPerPlane = 8; | |
876 hdr.nplanes = dmImageGetBytesPerPixel(pcx.format); | |
877 | |
1284 | 878 res = img->width * spec->scaleX; |
435 | 879 hdr.bpl = res / 2; |
880 if (res % 2) hdr.bpl++; | |
881 hdr.bpl *= 2; | |
882 | |
1297 | 883 dmMsg(2, |
884 "PCX: xmin=%d, ymin=%d, xmax=%d, ymax=%d, res=%dx%d, scr=%dx%d\n", | |
885 hdr.xmin, hdr.ymin, hdr.xmax, hdr.ymax, | |
886 hdr.hres, hdr.vres, | |
887 hdr.hScreenSize, hdr.vScreenSize); | |
888 | |
889 dmMsg(2, "PCX: nplanes=%d, bpp=%d, bpl=%d, isPaletted=%s\n", | |
890 hdr.nplanes, hdr.bitsPerPlane, hdr.bpl, spec->paletted ? "yes" : "no"); | |
435 | 891 |
1291 | 892 // TODO XXX this is also bogus |
435 | 893 pcx.bufLen = hdr.bpl * 4; |
894 if ((pcx.buf = dmMalloc(pcx.bufLen)) == NULL) | |
895 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
896 res = dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
897 "PCX: Could not allocate %d bytes for RLE compression buffer.\n", |
435 | 898 pcx.bufLen); |
899 goto error; | |
900 } | |
901 | |
902 // Write PCX header | |
903 if (!dm_fwrite_byte(pcx.fp, hdr.manufacturer) || | |
904 !dm_fwrite_byte(pcx.fp, hdr.version) || | |
905 !dm_fwrite_byte(pcx.fp, hdr.encoding) || | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
906 !dm_fwrite_byte(pcx.fp, hdr.bitsPerPlane)) |
435 | 907 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
908 res = dmError(DMERR_FWRITE, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
909 "PCX: Could not write basic header data.\n"); |
435 | 910 goto error; |
911 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
912 |
435 | 913 if (!dm_fwrite_le16(pcx.fp, hdr.xmin) || |
914 !dm_fwrite_le16(pcx.fp, hdr.ymin) || | |
915 !dm_fwrite_le16(pcx.fp, hdr.xmax) || | |
916 !dm_fwrite_le16(pcx.fp, hdr.ymax) || | |
917 !dm_fwrite_le16(pcx.fp, hdr.hres) || | |
918 !dm_fwrite_le16(pcx.fp, hdr.vres)) | |
919 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
920 res = dmError(DMERR_FWRITE, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
921 "PCX: Could not write image dimensions.\n"); |
435 | 922 goto error; |
923 } | |
924 | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
925 if (!dm_fwrite_str(pcx.fp, (Uint8 *) &hdr.colorMap, sizeof(hdr.colorMap))) |
435 | 926 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
927 res = dmError(DMERR_FWRITE, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
928 "PCX: Could not write colormap.\n"); |
435 | 929 goto error; |
930 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
931 |
435 | 932 if (!dm_fwrite_byte(pcx.fp, hdr.reserved) || |
933 !dm_fwrite_byte(pcx.fp, hdr.nplanes) || | |
934 !dm_fwrite_le16(pcx.fp, hdr.bpl) || | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
935 !dm_fwrite_le16(pcx.fp, hdr.palInfo) || |
1283
642a0dd98c6e
Oops, forgot to adjust the writing and reading when adding two members to
Matti Hamalainen <ccr@tnsp.org>
parents:
1281
diff
changeset
|
936 !dm_fwrite_le16(pcx.fp, hdr.hScreenSize) || |
642a0dd98c6e
Oops, forgot to adjust the writing and reading when adding two members to
Matti Hamalainen <ccr@tnsp.org>
parents:
1281
diff
changeset
|
937 !dm_fwrite_le16(pcx.fp, hdr.vScreenSize) || |
435 | 938 !dm_fwrite_str(pcx.fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler))) |
939 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
940 res = dmError(DMERR_FWRITE, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
941 "PCX: Could not write header remainder.\n"); |
435 | 942 goto error; |
943 } | |
944 | |
945 // Write image data | |
946 res = dmWriteImageData(img, (void *) &pcx, dmWritePCXRow, spec); | |
947 | |
948 // Write VGA palette | |
949 if (spec->paletted) | |
950 { | |
951 int i; | |
1294
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
952 dmMsg(2, "PCX: Writing palette of %d active entries.\n", img->ncolors); |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
953 |
435 | 954 dm_fwrite_byte(pcx.fp, 0x0C); |
955 | |
956 for (i = 0; i < img->ncolors; i++) | |
957 { | |
1294
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
958 if (!dm_fwrite_byte(pcx.fp, img->pal[i].r) || |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
959 !dm_fwrite_byte(pcx.fp, img->pal[i].g) || |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
960 !dm_fwrite_byte(pcx.fp, img->pal[i].b)) |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
961 { |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
962 res = dmError(DMERR_FWRITE, |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
963 "PCX: Could not write palette data.\n"); |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
964 goto error; |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
965 } |
435 | 966 } |
967 | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
968 // Pad the palette, if necessary |
435 | 969 for (; i < 256; i++) |
970 { | |
1294
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
971 if (!dm_fwrite_byte(pcx.fp, 0) || |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
972 !dm_fwrite_byte(pcx.fp, 0) || |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
973 !dm_fwrite_byte(pcx.fp, 0)) |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
974 { |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
975 res = dmError(DMERR_FWRITE, |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
976 "PCX: Could not write palette data.\n"); |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
977 goto error; |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
978 } |
435 | 979 } |
980 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
981 |
435 | 982 error: |
983 dmFree(pcx.buf); | |
984 return res; | |
985 } | |
986 | |
987 | |
1290
e7dc9bb9777e
Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents:
1288
diff
changeset
|
988 int dmWritePCXImage(const char *filename, DMImage *img, const DMImageConvSpec *spec) |
435 | 989 { |
990 FILE *fp; | |
991 int res; | |
992 | |
993 if ((fp = fopen(filename, "wb")) == NULL) | |
994 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
995 return dmError(DMERR_FOPEN, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
996 "PCX: Could not open file '%s' for writing.\n", |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
997 filename); |
435 | 998 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
999 |
435 | 1000 res = dmWritePCXImageFILE(fp, img, spec); |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1001 |
435 | 1002 fclose(fp); |
1003 return res; | |
1004 } | |
1005 | |
1006 | |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
1007 static BOOL dmPCXDecodeRLERow(FILE *fp, Uint8 *buf, const size_t bufLen) |
435 | 1008 { |
1009 size_t offs = 0; | |
1010 do | |
1011 { | |
1012 int count; | |
1013 Uint8 data; | |
1014 | |
1015 if (!dm_fread_byte(fp, &data)) | |
1016 return FALSE; | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1017 |
435 | 1018 if ((data & 0xC0) == 0xC0) |
1019 { | |
1284 | 1020 BOOL skip = FALSE; |
435 | 1021 count = data & 0x3F; |
1284 | 1022 if (count == 0) |
1023 { | |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
1024 switch (dmGFXErrorMode) |
1284 | 1025 { |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
1026 case DM_ERRMODE_RECOV_1: |
1284 | 1027 // Use as literal |
1028 skip = TRUE; | |
1029 count = 1; | |
1030 break; | |
1031 | |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
1032 case DM_ERRMODE_RECOV_2: |
1284 | 1033 // Ignore completely |
1034 skip = TRUE; | |
1035 break; | |
1036 | |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
1037 case DM_ERRMODE_FAIL: |
1284 | 1038 default: |
1039 // Error out on "invalid" data | |
1040 return FALSE; | |
1041 } | |
1042 } | |
1043 | |
1044 if (!skip && !dm_fread_byte(fp, &data)) | |
435 | 1045 return FALSE; |
1046 } | |
1047 else | |
1048 count = 1; | |
1049 | |
1050 while (count-- && offs < bufLen) | |
1051 buf[offs++] = data; | |
1052 | |
1284 | 1053 // Check for remaining output count, error out if we wish to |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
1054 if (count > 0 && dmGFXErrorMode == DM_ERRMODE_FAIL) |
1284 | 1055 return FALSE; |
1056 | |
435 | 1057 } while (offs < bufLen); |
1058 | |
1059 return TRUE; | |
1060 } | |
1061 | |
1062 | |
1063 int dmReadPCXImageFILE(FILE *fp, DMImage **pimg) | |
1064 { | |
1065 DMImage *img; | |
1066 DMPCXData pcx; | |
1067 DMPCXHeader hdr; | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1068 int res = 0; |
1286 | 1069 BOOL isPaletted; |
435 | 1070 pcx.buf = NULL; |
1071 | |
1072 // Read PCX header | |
1073 if (!dm_fread_byte(fp, &hdr.manufacturer) || | |
1074 !dm_fread_byte(fp, &hdr.version) || | |
1075 !dm_fread_byte(fp, &hdr.encoding) || | |
1298 | 1076 !dm_fread_byte(fp, &hdr.bitsPerPlane) || |
1077 !dm_fread_le16(fp, &hdr.xmin) || | |
435 | 1078 !dm_fread_le16(fp, &hdr.ymin) || |
1079 !dm_fread_le16(fp, &hdr.xmax) || | |
1080 !dm_fread_le16(fp, &hdr.ymax) || | |
1081 !dm_fread_le16(fp, &hdr.hres) || | |
1298 | 1082 !dm_fread_le16(fp, &hdr.vres) || |
1083 !dm_fread_str(fp, (Uint8 *) &hdr.colorMap, sizeof(hdr.colorMap)) || | |
1084 !dm_fread_byte(fp, &hdr.reserved) || | |
435 | 1085 !dm_fread_byte(fp, &hdr.nplanes) || |
1086 !dm_fread_le16(fp, &hdr.bpl) || | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1087 !dm_fread_le16(fp, &hdr.palInfo) || |
1283
642a0dd98c6e
Oops, forgot to adjust the writing and reading when adding two members to
Matti Hamalainen <ccr@tnsp.org>
parents:
1281
diff
changeset
|
1088 !dm_fread_le16(fp, &hdr.hScreenSize) || |
642a0dd98c6e
Oops, forgot to adjust the writing and reading when adding two members to
Matti Hamalainen <ccr@tnsp.org>
parents:
1281
diff
changeset
|
1089 !dm_fread_le16(fp, &hdr.vScreenSize) || |
435 | 1090 !dm_fread_str(fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler))) |
1091 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1092 res = dmError(DMERR_FREAD, |
1298 | 1093 "PCX: Could not read image header data.\n"); |
1094 goto error; | |
1095 } | |
1096 | |
1097 if (hdr.manufacturer != 10 || | |
1098 hdr.version > 5 || | |
1099 hdr.encoding != 1) | |
1100 { | |
1101 res = dmError(DMERR_NOT_SUPPORTED, | |
1102 "PCX: Not a PCX file, or unsupported variant.\n"); | |
435 | 1103 goto error; |
1104 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1105 |
1299
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1106 if (hdr.nplanes == 4 && hdr.bitsPerPlane == 4) |
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1107 { |
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1108 dmMsg(2, |
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1109 "PCX: Probably invalid combination of nplanes and bpp, attempting to fix ..\n"); |
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1110 |
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1111 hdr.bitsPerPlane = 1; |
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1112 } |
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1113 |
1295
7a986f33895e
Fix isPaletted check in PCX reader :P
Matti Hamalainen <ccr@tnsp.org>
parents:
1294
diff
changeset
|
1114 isPaletted = (hdr.bitsPerPlane * hdr.nplanes) <= 8; |
1291 | 1115 |
1116 dmMsg(2, | |
1117 "PCX: xmin=%d, ymin=%d, xmax=%d, ymax=%d, res=%dx%d, scr=%dx%d\n", | |
1118 hdr.xmin, hdr.ymin, hdr.xmax, hdr.ymax, | |
1119 hdr.hres, hdr.vres, | |
1120 hdr.hScreenSize, hdr.vScreenSize); | |
1121 | |
1122 dmMsg(2, | |
1123 "PCX: nplanes=%d, bpp=%d, bpl=%d, isPaletted=%s\n", | |
1124 hdr.nplanes, hdr.bitsPerPlane, hdr.bpl, isPaletted ? "yes" : "no"); | |
1125 | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1126 if (hdr.nplanes < 1 || hdr.nplanes > 8) |
435 | 1127 { |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1128 res = dmError(DMERR_NOT_SUPPORTED, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1129 "PCX: Unsupported number of bitplanes %d.\n", |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1130 hdr.nplanes); |
435 | 1131 goto error; |
1132 } | |
1133 | |
1286 | 1134 if (!isPaletted) |
1135 { | |
1136 res = dmError(DMERR_NOT_SUPPORTED, | |
1137 "PCX: Non-indexed (truecolour) PCX images not supported for loading.\n"); | |
1138 goto error; | |
1139 } | |
1140 | |
435 | 1141 // Allocate image |
1286 | 1142 if ((*pimg = img = dmImageAlloc(hdr.xmax - hdr.xmin + 1, hdr.ymax - hdr.ymin + 1, |
1143 isPaletted ? DM_IFMT_PALETTE : DM_IFMT_RGBA, | |
1299
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1144 // XXX TODO? When/if we ever handle < 8bit indexed correctly, we can use the actual bpp |
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1145 // isPaletted ? (hdr.bitsPerPlane * hdr.nplanes) : -1 |
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1146 -1 |
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1147 )) == NULL) |
435 | 1148 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1149 res = dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1150 "PCX: Could not allocate image structure.\n"); |
435 | 1151 goto error; |
1152 } | |
1153 | |
1298 | 1154 // Sanity check bytes per line value |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1155 if (hdr.bpl < (img->width * hdr.bitsPerPlane) / 8) |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1156 { |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1157 res = dmError(DMERR_MALLOC, |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1158 "PCX: The bytes per plane line value %d is smaller than width*bpp/8 = %d!\n", |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1159 hdr.bpl, (img->width * hdr.bitsPerPlane) / 8); |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1160 goto error; |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1161 } |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1162 |
435 | 1163 pcx.bufLen = hdr.nplanes * hdr.bpl; |
1164 if ((pcx.buf = dmMalloc(pcx.bufLen)) == NULL) | |
1165 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1166 res = dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1167 "PCX: Could not allocate RLE buffer.\n"); |
435 | 1168 goto error; |
1169 } | |
1170 | |
1299
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1171 dmMsg(2, |
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1172 "PCX: bufLen=%d\n", |
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1173 pcx.bufLen); |
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1174 |
435 | 1175 // Read image data |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1176 Uint8 *dp = img->data; |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1177 for (int yc = 0; yc < img->height; yc++) |
435 | 1178 { |
1179 // Decode row of RLE'd data | |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
1180 if (!dmPCXDecodeRLERow(fp, pcx.buf, pcx.bufLen)) |
435 | 1181 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1182 res = dmError(DMERR_INVALID_DATA, |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1183 "PCX: Error decoding RLE compressed data.\n"); |
435 | 1184 goto error; |
1185 } | |
1296 | 1186 |
435 | 1187 // Decode bitplanes |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1188 switch (hdr.bitsPerPlane) |
435 | 1189 { |
1296 | 1190 case 32: |
1191 case 24: | |
1192 case 16: | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1193 case 8: |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1194 { |
1286 | 1195 // Actually bytes and bits per plane per pixel .. |
1196 const int bytesPerPlane = hdr.bitsPerPlane / 8; | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1197 |
1286 | 1198 for (int nplane = 0; nplane < hdr.nplanes; nplane++) |
1199 { | |
1200 Uint8 *dptr = dp + (nplane * bytesPerPlane), | |
1201 *sptr = pcx.buf + (hdr.bpl * nplane); | |
1202 | |
1203 memcpy(dptr, sptr, img->width * bytesPerPlane); | |
1204 } | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1205 } |
435 | 1206 break; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1207 |
1280
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1208 case 1: |
1296 | 1209 memset(dp, 0, img->width); |
1210 | |
1280
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1211 for (int nplane = 0; nplane < hdr.nplanes; nplane++) |
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1212 { |
1296 | 1213 Uint8 *sptr = pcx.buf + (hdr.bpl * nplane); |
1280
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1214 |
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1215 for (int xc = 0; xc < img->width; xc++) |
1296 | 1216 { |
1299
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1217 const int px = 7 - (xc & 7); |
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1218 dp[xc] |= ((sptr[xc / 8] & (1 << px)) >> px) << nplane; |
1296 | 1219 } |
1280
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1220 } |
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1221 break; |
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1222 |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1223 default: |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
1224 res = dmError(DMERR_NOT_SUPPORTED, |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1225 "PCX: Unsupported number of bits per plane %d.\n", |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1226 hdr.bitsPerPlane); |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1227 goto error; |
435 | 1228 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1229 |
435 | 1230 dp += img->pitch; |
1231 } | |
1232 | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1233 // Read additional VGA palette, if available |
1286 | 1234 if (isPaletted) |
435 | 1235 { |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1236 int ncolors; |
435 | 1237 Uint8 tmpb; |
1238 BOOL read; | |
1239 | |
1240 if (!dm_fread_byte(fp, &tmpb) || tmpb != 0x0C) | |
1241 { | |
1242 read = FALSE; | |
1299
b0c0be4c76f9
Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1298
diff
changeset
|
1243 ncolors = DMPCX_PAL_COLORS; |
435 | 1244 } |
1245 else | |
1246 { | |
1247 read = TRUE; | |
451
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
1248 ncolors = 256; |
435 | 1249 } |
1250 | |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
1251 if (!dmImageAllocPalette(img, ncolors, -1)) |
435 | 1252 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1253 res = dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1254 "PCX: Could not allocate palette data!\n"); |
435 | 1255 goto error; |
1256 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1257 |
435 | 1258 if (read) |
1259 { | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1260 // Okay, attempt to read the palette data |
1296 | 1261 dmMsg(2, "PCX: Reading palette of %d colors\n", ncolors); |
451
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
1262 if (!dmReadPaletteData(fp, img->pal, ncolors)) |
435 | 1263 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1264 res = dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1265 "PCX: Error reading palette.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1266 goto error; |
435 | 1267 } |
1268 } | |
1269 else | |
1270 { | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1271 // If the extra palette is not available, copy the colors from |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1272 // the header palette to our internal palette structure. |
1296 | 1273 dmMsg(2, "PCX: Initializing palette from header of %d colors\n", ncolors); |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1274 for (int i = 0; i < (img->ncolors > DMPCX_PAL_COLORS ? DMPCX_PAL_COLORS : img->ncolors); i++) |
435 | 1275 { |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1276 img->pal[i].r = hdr.colorMap[i].r; |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1277 img->pal[i].g = hdr.colorMap[i].g; |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1278 img->pal[i].b = hdr.colorMap[i].b; |
435 | 1279 } |
1280 } | |
1281 } | |
1282 | |
1283 error: | |
1284 dmFree(pcx.buf); | |
1285 return res; | |
1286 } | |
1287 | |
1288 | |
1289 int dmReadPCXImage(const char *filename, DMImage **pimg) | |
1290 { | |
1291 FILE *fp; | |
1292 int res; | |
1293 | |
1294 if ((fp = fopen(filename, "rb")) == NULL) | |
1295 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1296 return dmError(DMERR_FOPEN, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1297 "PCX: Could not open file '%s' for reading.\n", |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1298 filename); |
435 | 1299 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1300 |
435 | 1301 res = dmReadPCXImageFILE(fp, pimg); |
1302 | |
1303 fclose(fp); | |
1304 return res; | |
1305 } | |
1306 | |
1307 | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1308 #define IFF_ID_FORM 0x464F524D // "FORM" |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1309 #define IFF_ID_ILBM 0x494C424D // "ILBM" |
464 | 1310 #define IFF_ID_PBM 0x50424D20 // "PBM " |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1311 #define IFF_ID_BMHD 0x424D4844 // "BMHD" |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1312 #define IFF_ID_CMAP 0x434D4150 // "CMAP" |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1313 #define IFF_ID_BODY 0x424F4459 // "BODY" |
449
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1314 #define IFF_ID_CAMG 0x43414D47 // "CAMG" |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1315 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1316 #define IFF_MASK_NONE 0 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1317 #define IFF_MASK_HAS_MASK 1 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1318 #define IFF_MASK_TRANSP 2 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1319 #define IFF_MASK_LASSO 3 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1320 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1321 #define IFF_COMP_NONE 0 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1322 #define IFF_COMP_BYTERUN1 1 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1323 |
449
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1324 #define IFF_CAMG_LACE 0x00000004 |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1325 #define IFF_CAMG_HALFBRITE 0x00000080 |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1326 #define IFF_CAMG_HAM 0x00000800 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1327 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1328 typedef struct |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1329 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1330 Uint32 id; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1331 Uint32 size; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1332 int count; |
445 | 1333 char str[6]; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1334 } DMIFFChunk; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1335 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1336 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1337 typedef struct |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1338 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1339 Uint16 w, h; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1340 Sint16 x, y; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1341 Uint8 nplanes; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1342 Uint8 masking; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1343 Uint8 compression; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1344 Uint8 pad1; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1345 Uint16 transp; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1346 Uint8 xasp, yasp; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1347 Sint16 pagew, pageh; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1348 } DMIFFBMHD; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1349 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1350 |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1351 typedef struct |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1352 { |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1353 DMIFFChunk chBMHD, chCMAP, chBODY; |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1354 DMIFFBMHD bmhd; |
449
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1355 Uint32 camg; |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1356 int ncolors; |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1357 DMColor *pal; |
1286 | 1358 BOOL planar; |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1359 } DMIFF; |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1360 |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1361 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1362 static BOOL dmReadIFFChunk(FILE *fp, DMIFFChunk *chunk) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1363 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1364 if (!dm_fread_be32(fp, &chunk->id) || |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1365 !dm_fread_be32(fp, &chunk->size)) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1366 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1367 dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1368 "ILBM: Could not read IFF chunk header.\n"); |
1264
aa6e8a24b94b
Oops, when did I break this? IFF ILBM reader should now work again.
Matti Hamalainen <ccr@tnsp.org>
parents:
1167
diff
changeset
|
1369 return FALSE; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1370 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1371 else |
1264
aa6e8a24b94b
Oops, when did I break this? IFF ILBM reader should now work again.
Matti Hamalainen <ccr@tnsp.org>
parents:
1167
diff
changeset
|
1372 return TRUE; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1373 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1374 |
445 | 1375 static char * dmGetIFFChunkID(DMIFFChunk *chunk) |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1376 { |
445 | 1377 chunk->str[0] = (chunk->id >> 24) & 0xff; |
1378 chunk->str[1] = (chunk->id >> 16) & 0xff; | |
1379 chunk->str[2] = (chunk->id >> 8) & 0xff; | |
1380 chunk->str[3] = (chunk->id) & 0xff; | |
1381 chunk->str[4] = 0; | |
1382 return chunk->str; | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1383 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1384 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1385 static int dmSkipIFFChunkRest(FILE *fp, const DMIFFChunk *chunk, const Uint32 used) |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1386 { |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1387 if (chunk->size > used) |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1388 { |
465 | 1389 dmMsg(4, "ILBM: Skipping %d bytes (%d of %d consumed)\n", |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1390 chunk->size - used, used, chunk->size); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1391 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1392 if (fseeko(fp, chunk->size - used, SEEK_CUR) != 0) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1393 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1394 return dmError(DMERR_FSEEK, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1395 "ILBM: Failed to skip chunk end.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1396 } |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1397 else |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1398 return DMERR_OK; |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1399 } |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1400 else |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1401 return DMERR_OK; |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1402 } |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1403 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1404 static int dmCheckIFFChunk(DMIFFChunk *dest, DMIFFChunk *chunk, |
445 | 1405 const BOOL multi, const Uint32 minSize) |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1406 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1407 if (dest->count > 0 && !multi) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1408 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1409 return dmError(DMERR_INVALID_DATA, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1410 "ILBM: Multiple instances of chunk %s found.\n", |
445 | 1411 dmGetIFFChunkID(chunk)); |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1412 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1413 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1414 dest->count++; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1415 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1416 if (chunk->size < minSize) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1417 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1418 return dmError(DMERR_OUT_OF_DATA, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1419 "ILBM: Chunk is too small.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1420 } |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1421 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1422 return DMERR_OK; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1423 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1424 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1425 |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1426 static BOOL dmIFFDecodeByteRun1Row(FILE *fp, Uint8 *buf, const size_t bufLen) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1427 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1428 size_t offs = 0; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1429 do |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1430 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1431 Sint8 dcount; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1432 Uint8 data; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1433 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1434 if (!dm_fread_byte(fp, (Uint8 *) &dcount)) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1435 return FALSE; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1436 |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1437 if (dcount == -128) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1438 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1439 if (!dm_fread_byte(fp, &data)) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1440 return FALSE; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1441 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1442 else |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1443 if (dcount < 0) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1444 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1445 int count = (-dcount) + 1; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1446 if (!dm_fread_byte(fp, &data)) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1447 return FALSE; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1448 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1449 while (count-- && offs < bufLen) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1450 buf[offs++] = data; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1451 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1452 else |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1453 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1454 int count = dcount + 1; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1455 while (count-- && offs < bufLen) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1456 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1457 if (!dm_fread_byte(fp, &data)) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1458 return FALSE; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1459 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1460 buf[offs++] = data; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1461 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1462 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1463 } while (offs < bufLen); |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1464 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1465 return TRUE; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1466 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1467 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1468 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1469 static BOOL dmIFFReadOneRow(FILE *fp, DMIFF *iff, Uint8 *buf, const size_t bufLen) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1470 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1471 if (iff->bmhd.compression == IFF_COMP_BYTERUN1) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1472 return dmIFFDecodeByteRun1Row(fp, buf, bufLen); |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1473 else |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1474 return dm_fread_str(fp, buf, bufLen); |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1475 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1476 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1477 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1478 void dmDecodeBitPlane(Uint8 *dp, Uint8 *src, const int width, const int nplane) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1479 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1480 int xc; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1481 for (xc = 0; xc < width; xc++) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1482 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1483 const Uint8 data = (src[xc / 8] >> (7 - (xc & 7))) & 1; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1484 dp[xc] |= (data << nplane); |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1485 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1486 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1487 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1488 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1489 int dmDecodeILBMBody(FILE *fp, DMIFF *iff, DMImage **pimg, Uint32 *read) |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1490 { |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1491 DMImage *img; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1492 Uint8 *buf; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1493 size_t bufLen; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1494 int yc, res = DMERR_OK; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1495 |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1496 *read = 0; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1497 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1498 // Allocate image |
1286 | 1499 if ((*pimg = img = dmImageAlloc(iff->bmhd.w, iff->bmhd.h, |
1300
fd442faa705f
"Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents:
1299
diff
changeset
|
1500 iff->bmhd.nplanes <= 8 ? DM_IFMT_PALETTE : DM_IFMT_RGBA, |
fd442faa705f
"Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents:
1299
diff
changeset
|
1501 // XXX TODO? When/if we ever handle < 8bit indexed correctly, we can use the actual bpp |
fd442faa705f
"Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents:
1299
diff
changeset
|
1502 //iff->bmhd.nplanes <= 8 ? iff->bmhd.nplanes : -1 |
fd442faa705f
"Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents:
1299
diff
changeset
|
1503 -1 |
fd442faa705f
"Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents:
1299
diff
changeset
|
1504 )) == NULL) |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1505 return DMERR_MALLOC; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1506 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1507 // Allocate planar decoding buffer |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1508 bufLen = ((img->width + 15) / 16) * 2; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1509 if ((buf = dmMalloc(bufLen)) == NULL) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1510 return DMERR_MALLOC; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1511 |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
1512 dmMsg(2, "ILBM: plane row size %d bytes.\n", bufLen); |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1513 |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1514 // Decode the chunk |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1515 for (yc = 0; yc < img->height; yc++) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1516 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1517 int plane; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1518 const int nplanes = iff->bmhd.nplanes; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1519 Uint8 *dp = img->data + (yc * img->pitch); |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1520 |
1167 | 1521 dmMemset(dp, 0, img->pitch); |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1522 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1523 for (plane = 0; plane < nplanes; plane++) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1524 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1525 // Decompress or read data |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1526 if (!dmIFFReadOneRow(fp, iff, buf, bufLen)) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1527 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1528 res = dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1529 "ILBM: Error in reading image plane #%d @ %d.\n", |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1530 plane, yc); |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1531 goto error; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1532 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1533 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1534 // Decode bitplane |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1535 dmDecodeBitPlane(dp, buf, img->width, plane); |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1536 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1537 *read += bufLen; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1538 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1539 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1540 // Read mask data |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1541 if (iff->bmhd.masking == IFF_MASK_HAS_MASK) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1542 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1543 int xc; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1544 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1545 // Decompress or read data |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1546 if (!dmIFFReadOneRow(fp, iff, buf, bufLen)) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1547 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1548 res = dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1549 "ILBM: Error in reading mask plane.\n"); |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1550 goto error; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1551 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1552 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1553 // Decode mask |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1554 for (xc = 0; xc < img->width; xc++) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1555 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1556 const Uint8 data = (buf[xc / 8] >> (7 - (xc & 7))) & 1; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1557 |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1558 // Black out any pixels with mask bit 0 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1559 if (!data) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1560 dp[xc] = 0; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1561 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1562 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1563 *read += bufLen; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1564 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1565 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1566 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1567 error: |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1568 dmFree(buf); |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1569 return res; |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1570 } |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1571 |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1572 |
464 | 1573 int dmDecodePBMBody(FILE *fp, DMIFF *iff, DMImage **pimg, Uint32 *read) |
1574 { | |
1575 DMImage *img; | |
1576 int yc, res = DMERR_OK; | |
1577 | |
1578 *read = 0; | |
1579 | |
1580 // Allocate image | |
1286 | 1581 if ((*pimg = img = dmImageAlloc(iff->bmhd.w, iff->bmhd.h, |
1300
fd442faa705f
"Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents:
1299
diff
changeset
|
1582 iff->bmhd.nplanes <= 8 ? DM_IFMT_PALETTE : DM_IFMT_RGBA, |
fd442faa705f
"Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents:
1299
diff
changeset
|
1583 // XXX TODO? When/if we ever handle < 8bit indexed correctly, we can use the actual bpp |
fd442faa705f
"Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents:
1299
diff
changeset
|
1584 //iff->bmhd.nplanes <= 8 ? iff->bmhd.nplanes : -1 |
fd442faa705f
"Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents:
1299
diff
changeset
|
1585 -1 |
fd442faa705f
"Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents:
1299
diff
changeset
|
1586 )) == NULL) |
464 | 1587 return DMERR_MALLOC; |
1588 | |
1589 // Decode the chunk | |
1590 for (yc = 0; yc < img->height; yc++) | |
1591 { | |
1592 Uint8 *dp = img->data + (yc * img->pitch); | |
1593 | |
1594 if (!dmIFFReadOneRow(fp, iff, dp, img->width)) | |
1595 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1596 res = dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1597 "ILBM: Error in reading image row #%d.\n", yc); |
464 | 1598 goto error; |
1599 } | |
1600 | |
1601 *read += img->width; | |
1602 } | |
1603 | |
1604 error: | |
1605 return res; | |
1606 } | |
1607 | |
1608 | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1609 int dmReadILBMImageFILE(FILE *fp, DMImage **pimg) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1610 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1611 Uint32 idILBM; |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1612 DMIFFChunk chunk; |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1613 DMIFF iff; |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1614 Uint32 read; |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1615 BOOL parsed = FALSE; |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1616 int i, res = DMERR_OK; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1617 |
1167 | 1618 dmMemset(&iff, 0, sizeof(iff)); |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1619 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1620 // Read IFF FORM header |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1621 if (!dmReadIFFChunk(fp, &chunk) || |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1622 chunk.id != IFF_ID_FORM || |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1623 chunk.size < 32) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1624 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1625 return dmError(DMERR_NOT_SUPPORTED, |
1265
4e074b9b4789
Improve IFF FORM validity error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
1264
diff
changeset
|
1626 "ILBM: Not a IFF file (%08X vs %08X / %d).\n", |
4e074b9b4789
Improve IFF FORM validity error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
1264
diff
changeset
|
1627 chunk.id, IFF_ID_FORM, chunk.size); |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1628 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1629 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1630 // Check IFF ILBM signature |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1631 if (!dm_fread_be32(fp, &idILBM) || |
464 | 1632 (idILBM != IFF_ID_ILBM && idILBM != IFF_ID_PBM)) |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1633 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1634 return dmError(DMERR_INVALID_DATA, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1635 "ILBM: Not a ILBM file.\n"); |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1636 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1637 |
464 | 1638 iff.planar = (idILBM == IFF_ID_ILBM); |
1639 | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1640 while (!parsed && !feof(fp)) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1641 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1642 if (!dmReadIFFChunk(fp, &chunk)) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1643 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1644 return dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1645 "ILBM: Error reading IFF ILBM data.\n"); |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1646 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1647 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1648 switch (chunk.id) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1649 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1650 case IFF_ID_BMHD: |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1651 // Check for multiple occurences of BMHD |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1652 if ((res = dmCheckIFFChunk(&iff.chBMHD, &chunk, FALSE, sizeof(iff.bmhd))) != DMERR_OK) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1653 return res; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1654 |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1655 // Read BMHD data |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1656 if (!dm_fread_be16(fp, &iff.bmhd.w) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1657 !dm_fread_be16(fp, &iff.bmhd.h) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1658 !dm_fread_be16(fp, (Uint16 *) &iff.bmhd.x) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1659 !dm_fread_be16(fp, (Uint16 *) &iff.bmhd.y) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1660 !dm_fread_byte(fp, &iff.bmhd.nplanes) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1661 !dm_fread_byte(fp, &iff.bmhd.masking) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1662 !dm_fread_byte(fp, &iff.bmhd.compression) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1663 !dm_fread_byte(fp, &iff.bmhd.pad1) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1664 !dm_fread_be16(fp, &iff.bmhd.transp) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1665 !dm_fread_byte(fp, &iff.bmhd.xasp) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1666 !dm_fread_byte(fp, &iff.bmhd.yasp) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1667 !dm_fread_be16(fp, (Uint16 *) &iff.bmhd.pagew) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1668 !dm_fread_be16(fp, (Uint16 *) &iff.bmhd.pageh)) |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1669 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1670 return dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1671 "ILBM: Error reading BMHD chunk.\n"); |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1672 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1673 |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
1674 dmMsg(2, "ILBM: BMHD %d x %d @ %d, %d : nplanes=%d, comp=%d, mask=%d\n", |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1675 iff.bmhd.w, iff.bmhd.h, iff.bmhd.x, iff.bmhd.y, |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1676 iff.bmhd.nplanes, iff.bmhd.compression, iff.bmhd.masking); |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1677 |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1678 // Sanity check |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1679 if (iff.bmhd.nplanes < 1 || iff.bmhd.nplanes > 8 || |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1680 (iff.bmhd.compression != IFF_COMP_NONE && |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1681 iff.bmhd.compression != IFF_COMP_BYTERUN1) || |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1682 (iff.bmhd.masking != IFF_MASK_NONE && |
448
f1aab48a76fe
Add transp mask support to IFF ILBM loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
447
diff
changeset
|
1683 iff.bmhd.masking != IFF_MASK_HAS_MASK && |
f1aab48a76fe
Add transp mask support to IFF ILBM loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
447
diff
changeset
|
1684 iff.bmhd.masking != IFF_MASK_TRANSP)) |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1685 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1686 return dmError(DMERR_NOT_SUPPORTED, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1687 "ILBM: Unsupported features, refusing to load.\n"); |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1688 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1689 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1690 if ((res = dmSkipIFFChunkRest(fp, &chunk, sizeof(iff.bmhd))) != DMERR_OK) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1691 return res; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1692 break; |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1693 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1694 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1695 case IFF_ID_CMAP: |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1696 // Check for multiple occurences of CMAP |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1697 if ((res = dmCheckIFFChunk(&iff.chCMAP, &chunk, FALSE, 3)) != DMERR_OK) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1698 return res; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1699 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1700 // Check for sanity |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1701 if (chunk.size % 3 != 0) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1702 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1703 // Non-fatal |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1704 dmError(DMERR_INVALID_DATA, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1705 "ILBM: CMAP chunk size not divisible by 3, possibly broken file.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1706 } |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1707 |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1708 iff.ncolors = chunk.size / 3; |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
1709 dmMsg(2, "ILBM: CMAP %d entries (%d bytes)\n", |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1710 iff.ncolors, chunk.size, 1 << iff.bmhd.nplanes); |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1711 |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1712 if (iff.bmhd.nplanes > 0 && iff.ncolors != 1 << iff.bmhd.nplanes) |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
1713 dmMsg(2, "ILBM: Expected %d entries in CMAP.\n", 1 << iff.bmhd.nplanes); |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1714 |
451
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
1715 // Read palette |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
1716 if (iff.ncolors > 0) |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1717 { |
461
c7a3aacbd55e
Implement transparent color in IFF ILBM reader and ctransp setting in PNG reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
1718 if (!dmPaletteAlloc(&iff.pal, iff.ncolors, |
c7a3aacbd55e
Implement transparent color in IFF ILBM reader and ctransp setting in PNG reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
1719 (iff.bmhd.masking == IFF_MASK_TRANSP) ? iff.bmhd.transp : -1)) |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1720 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1721 return dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1722 "ILBM: Could not allocate palette data.\n"); |
451
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
1723 } |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
1724 if (!dmReadPaletteData(fp, iff.pal, iff.ncolors)) |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
1725 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1726 return dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1727 "ILBM: Error reading CMAP.\n"); |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1728 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1729 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1730 |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1731 if (iff.chBMHD.count && iff.chBODY.count) |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1732 parsed = TRUE; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1733 break; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1734 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1735 case IFF_ID_BODY: |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1736 // Check for multiple occurences of CMAP |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1737 if ((res = dmCheckIFFChunk(&iff.chBODY, &chunk, FALSE, 1)) != DMERR_OK) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1738 return res; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1739 |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1740 // Check for sanity |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1741 if (!iff.chBMHD.count) |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1742 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1743 return dmError(DMERR_INVALID_DATA, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1744 "ILBM: BODY chunk before BMHD?\n"); |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1745 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1746 |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
1747 dmMsg(2, "ILBM: BODY chunk size %d bytes\n", chunk.size); |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1748 |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1749 // Decode the body |
464 | 1750 if (iff.planar) |
1751 { | |
1752 if ((res = dmDecodeILBMBody(fp, &iff, pimg, &read)) != DMERR_OK) | |
1753 return res; | |
1754 } | |
1755 else | |
1756 { | |
1757 if ((res = dmDecodePBMBody(fp, &iff, pimg, &read)) != DMERR_OK) | |
1758 return res; | |
1759 } | |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1760 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1761 if ((res = dmSkipIFFChunkRest(fp, &chunk, read)) != DMERR_OK) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1762 return res; |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1763 |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1764 if (iff.chCMAP.count) |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1765 parsed = TRUE; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1766 break; |
449
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1767 |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1768 |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1769 case IFF_ID_CAMG: |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1770 if (!dm_fread_be32(fp, &iff.camg)) |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1771 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1772 return dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1773 "ILBM: Error reading CAMG chunk.\n"); |
449
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1774 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1775 |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
1776 dmMsg(2, "ILBM: CAMG value 0x%08x\n", iff.camg); |
449
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1777 |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1778 if ((iff.camg & IFF_CAMG_HAM)) |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1779 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1780 return dmError(DMERR_NOT_SUPPORTED, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1781 "ILBM: HAM files are not supported.\n"); |
449
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1782 } |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1783 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1784 if ((res = dmSkipIFFChunkRest(fp, &chunk, sizeof(Uint32))) != DMERR_OK) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1785 return res; |
449
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1786 break; |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1787 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1788 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1789 default: |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1790 { |
465 | 1791 dmMsg(4, "Unknown chunk ID '%s', size %d\n", |
445 | 1792 dmGetIFFChunkID(&chunk), chunk.size); |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1793 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1794 if (fseeko(fp, chunk.size, SEEK_CUR) != 0) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1795 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1796 return dmError(DMERR_FSEEK, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1797 "ILBM: Error skipping in file."); |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1798 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1799 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1800 break; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1801 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1802 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1803 if (chunk.size & 1) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1804 fgetc(fp); |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1805 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1806 |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1807 // Set colormap after finishing |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1808 if (iff.pal != NULL && iff.ncolors > 0 && *pimg != NULL) |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1809 { |
449
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1810 // If halfbrite is used, duplicate the palette |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1811 if (iff.camg & IFF_CAMG_HALFBRITE) |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1812 { |
834 | 1813 void *ptmp; |
1814 | |
449
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1815 if (iff.ncolors > 128) |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1816 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1817 return dmError(DMERR_NOT_SUPPORTED, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1818 "ILBM: Halfbrite enabled, but ncolors > 128.\n"); |
449
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1819 } |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1820 |
834 | 1821 if ((ptmp = dmRealloc(iff.pal, sizeof(DMColor) * iff.ncolors * 2)) == NULL) |
1822 { | |
1823 dmFree(iff.pal); | |
1824 iff.pal = NULL; | |
449
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1825 return DMERR_MALLOC; |
834 | 1826 } |
1827 else | |
1828 iff.pal = ptmp; | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1829 |
449
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1830 for (i = 0; i < iff.ncolors; i++) |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1831 { |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1832 int i2 = iff.ncolors + i; |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1833 iff.pal[i2].r = iff.pal[i].r / 2; |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1834 iff.pal[i2].g = iff.pal[i].g / 2; |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1835 iff.pal[i2].b = iff.pal[i].b / 2; |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1836 } |
117f94b253af
Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
448
diff
changeset
|
1837 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1838 |
447
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1839 (*pimg)->ncolors = iff.ncolors; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1840 (*pimg)->pal = iff.pal; |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1841 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1842 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1843 return res; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1844 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1845 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1846 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1847 int dmReadILBMImage(const char *filename, DMImage **pimg) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1848 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1849 FILE *fp; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1850 int res; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1851 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1852 if ((fp = fopen(filename, "rb")) == NULL) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1853 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1854 return dmError(DMERR_FOPEN, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1855 "ILBM: Could not open file '%s' for reading.\n", |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1856 filename); |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1857 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1858 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1859 res = dmReadILBMImageFILE(fp, pimg); |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1860 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1861 fclose(fp); |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1862 return res; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1863 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1864 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1865 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1866 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1867 |
435 | 1868 static int fmtProbePNG(const Uint8 *buf, const size_t len) |
1869 { | |
1870 if (len > 64 && buf[0] == 0x89 && | |
1871 buf[1] == 'P' && buf[2] == 'N' && buf[3] == 'G' && | |
1872 buf[4] == 0x0d && buf[5] == 0x0a) | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1873 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1874 if (buf[12] == 'I' && buf[13] == 'H' && |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1875 buf[14] == 'D' && buf[15] == 'R') |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1876 return DM_PROBE_SCORE_MAX; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1877 else |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1878 return DM_PROBE_SCORE_GOOD; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1879 } |
435 | 1880 |
1881 return DM_PROBE_SCORE_FALSE; | |
1882 } | |
1883 | |
1884 | |
1885 static int fmtProbePCX(const Uint8 *buf, const size_t len) | |
1886 { | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1887 if (len > 128 + 32 && |
1288 | 1888 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1889 (buf[1] == 5 || buf[1] == 2 || buf[1] == 3) && |
435 | 1890 buf[2] == 1 && |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1891 (buf[3] == 8 || buf[3] == 4 || buf[3] == 3 || buf[3] == 1) && |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1892 buf[65] >= 1 && buf[65] <= 4) |
435 | 1893 return DM_PROBE_SCORE_GOOD; |
1894 | |
1895 return DM_PROBE_SCORE_FALSE; | |
1896 } | |
1897 | |
1898 | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1899 static int fmtProbeILBM(const Uint8 *buf, const size_t len) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1900 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1901 if (len > 32 && |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1902 buf[ 0] == 'F' && buf[ 1] == 'O' && |
464 | 1903 buf[ 2] == 'R' && buf[ 3] == 'M' && ( |
1904 (buf[ 8] == 'I' && buf[ 9] == 'L' && buf[10] == 'B' && buf[11] == 'M') || | |
1905 (buf[ 8] == 'P' && buf[ 9] == 'B' && buf[10] == 'M' && buf[11] == 0x20) | |
1906 )) | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1907 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1908 if (buf[12] == 'B' && buf[13] == 'M' && |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1909 buf[14] == 'H' && buf[15] == 'D') |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1910 return DM_PROBE_SCORE_MAX; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1911 else |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1912 return DM_PROBE_SCORE_GOOD; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1913 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1914 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1915 return DM_PROBE_SCORE_FALSE; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1916 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1917 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1918 |
435 | 1919 DMImageFormat dmImageFormatList[IMGFMT_LAST] = |
1920 { | |
1921 { | |
1922 "PNG", "Portable Network Graphics", | |
1923 fmtProbePNG, | |
1924 #ifdef DM_USE_LIBPNG | |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
1925 dmReadPNGImage, dmReadPNGImageFILE, |
435 | 1926 dmWritePNGImage, dmWritePNGImageFILE, |
1927 #else | |
1928 NULL, NULL, | |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
1929 NULL, NULL, |
435 | 1930 #endif |
1931 }, | |
1932 { | |
1933 "PPM", "Portable PixMap", | |
1934 NULL, | |
1935 NULL, NULL, | |
1936 dmWritePPMImage, dmWritePPMImageFILE, | |
1937 }, | |
1938 { | |
1939 "PCX", "Z-Soft Paintbrush", | |
1940 fmtProbePCX, | |
1941 dmReadPCXImage, dmReadPCXImageFILE, | |
1942 dmWritePCXImage, dmWritePCXImageFILE, | |
1943 }, | |
1944 { | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1945 "ILBM", "IFF ILBM", |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1946 fmtProbeILBM, |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1947 dmReadILBMImage, dmReadILBMImageFILE, |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1948 NULL, NULL, |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1949 }, |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1950 { |
1291 | 1951 "RAW", "Plain bitplaned (planar or non-planar) RAW", |
566
d400e32b62d9
Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents:
487
diff
changeset
|
1952 NULL, |
d400e32b62d9
Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents:
487
diff
changeset
|
1953 NULL, NULL, |
d400e32b62d9
Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents:
487
diff
changeset
|
1954 dmWriteRAWImage, dmWriteRAWImageFILE, |
d400e32b62d9
Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents:
487
diff
changeset
|
1955 }, |
d400e32b62d9
Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents:
487
diff
changeset
|
1956 { |
435 | 1957 "ARAW", "IFFMaster Amiga RAW", |
1958 NULL, | |
1959 NULL, NULL, | |
566
d400e32b62d9
Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents:
487
diff
changeset
|
1960 dmWriteRAWImage, dmWriteRAWImageFILE, |
435 | 1961 } |
1962 }; | |
1963 | |
1964 | |
1965 int dmImageProbeGeneric(const Uint8 *buf, const size_t len, DMImageFormat **pfmt, int *index) | |
1966 { | |
1967 int i, scoreMax = DM_PROBE_SCORE_FALSE, scoreIndex = -1; | |
1968 | |
1969 for (i = 0; i < IMGFMT_LAST; i++) | |
1970 { | |
1971 DMImageFormat *fmt = &dmImageFormatList[i]; | |
442
a67600e186d0
Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
440
diff
changeset
|
1972 if (fmt->probe != NULL) |
435 | 1973 { |
442
a67600e186d0
Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
440
diff
changeset
|
1974 int score = fmt->probe(buf, len); |
a67600e186d0
Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
440
diff
changeset
|
1975 if (score > scoreMax) |
a67600e186d0
Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
440
diff
changeset
|
1976 { |
a67600e186d0
Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
440
diff
changeset
|
1977 scoreMax = score; |
a67600e186d0
Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
440
diff
changeset
|
1978 scoreIndex = i; |
a67600e186d0
Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
440
diff
changeset
|
1979 } |
435 | 1980 } |
1981 } | |
1982 | |
1983 if (scoreIndex >= 0) | |
1984 { | |
1985 *pfmt = &dmImageFormatList[scoreIndex]; | |
1986 *index = scoreIndex; | |
1987 return scoreMax; | |
1988 } | |
1989 else | |
1990 return DM_PROBE_SCORE_FALSE; | |
1991 } |