Mercurial > hg > dmlib
annotate src/libgfx.c @ 1296:228cab109c6a
More work on PCX reader.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 19 Aug 2017 01:32:46 +0300 |
parents | 7a986f33895e |
children | 5bd64397453b |
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 |
1286 | 604 if ((*pimg = img = dmImageAlloc(width, height, DM_IFMT_PALETTE, bit_depth)) == NULL) |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
605 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
606 res = dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
607 "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
|
608 goto error; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
609 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
610 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
611 // ... |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
612 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
|
613 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
|
614 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
|
615 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
616 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
|
617 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
618 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
|
619 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
620 // Create palette |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
621 switch (color_type) |
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 case PNG_COLOR_TYPE_GRAY: |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
624 ncolors = 256; |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
625 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
|
626 |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
627 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
|
628 { |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
629 res = DMERR_MALLOC; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
630 goto error; |
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 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
633 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
|
634 { |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
635 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
|
636 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
637 break; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
638 |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
639 case PNG_COLOR_TYPE_PALETTE: |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
640 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
|
641 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
|
642 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
|
643 { |
467
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
644 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
|
645 { |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
646 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
|
647 goto error; |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
648 } |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
649 |
467
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
650 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
|
651 { |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
652 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
|
653 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
|
654 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
|
655 } |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
656 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
657 break; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
658 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
659 |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
660 if (color_type == PNG_COLOR_TYPE_PALETTE || |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
661 color_type == PNG_COLOR_TYPE_GRAY) |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
662 { |
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
663 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
|
664 if (trans != NULL && ntrans > 0) |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
665 { |
467
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
666 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
|
667 { |
80d09ea85e75
Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents:
465
diff
changeset
|
668 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
|
669 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
|
670 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
|
671 } |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
672 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
673 } |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
674 |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
675 error: |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
676 // 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
|
677 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
678 if (png_ptr && info_ptr) |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
679 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
|
680 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
681 return res; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
682 } |
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 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
685 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
|
686 { |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
687 int res; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
688 FILE *fp; |
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 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
|
691 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
692 return dmError(DMERR_FOPEN, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
693 "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
|
694 filename); |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
695 } |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
696 |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
697 res = dmReadPNGImageFILE(fp, img); |
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 fclose(fp); |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
700 return res; |
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
701 } |
435 | 702 #endif |
703 | |
704 | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
705 #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
|
706 |
435 | 707 typedef struct |
708 { | |
709 Uint8 r,g,b; | |
710 } DMPCXColor; | |
711 | |
712 | |
713 typedef struct | |
714 { | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
715 Uint8 manufacturer, // always 0x0a |
1288 | 716 version, // Z-Soft PC Paintbrush version: |
717 // 0 = v2.5 | |
718 // 2 = v2.8 with palette, | |
719 // 3 = v2.8 without palette | |
720 // 4 = PC Paintbrush for Windows | |
721 // 5 = v3.0 or better | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
722 encoding, // usually 0x01 = RLE, 0x00 = uncompressed |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
723 bitsPerPlane; // bits per pixel per plane |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
724 |
435 | 725 Uint16 xmin, ymin, xmax, ymax; |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
726 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
|
727 DMPCXColor colorMap[DMPCX_PAL_COLORS]; |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
728 Uint8 reserved; // should be set to 0 |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
729 Uint8 nplanes; // number of planes |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
730 Uint16 bpl; // bytes per plane LINE |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
731 Uint16 palInfo; // 1 = color/BW, 2 = grayscale |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
732 Uint16 hScreenSize, vScreenSize; |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
733 Uint8 filler[54]; |
435 | 734 } DMPCXHeader; |
735 | |
736 | |
737 typedef struct | |
738 { | |
739 DMPCXHeader *header; | |
740 Uint8 *buf; | |
741 size_t bufLen, bufOffs; | |
742 int format; | |
743 FILE *fp; | |
744 } DMPCXData; | |
745 | |
746 | |
747 static inline Uint8 dmPCXGetByte(Uint8 *row, const size_t len, const size_t soffs) | |
748 { | |
749 return (soffs < len) ? row[soffs] : 0; | |
750 } | |
751 | |
752 static BOOL dmPCXFlush(DMPCXData *pcx) | |
753 { | |
754 BOOL ret = TRUE; | |
755 if (pcx->bufOffs > 0) | |
756 ret = fwrite(pcx->buf, sizeof(Uint8), pcx->bufOffs, pcx->fp) == pcx->bufOffs; | |
757 pcx->bufOffs = 0; | |
758 return ret; | |
759 } | |
760 | |
761 static inline BOOL dmPCXPutByte(DMPCXData *pcx, const Uint8 val) | |
762 { | |
763 if (pcx->bufOffs < pcx->bufLen) | |
764 { | |
765 pcx->buf[pcx->bufOffs++] = val; | |
766 return TRUE; | |
767 } | |
768 else | |
769 return dmPCXFlush(pcx); | |
770 } | |
771 | |
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
|
772 static int dmWritePCXRow(void *cbdata, Uint8 *row, size_t len) |
435 | 773 { |
774 DMPCXData *pcx = (DMPCXData *) cbdata; | |
775 size_t soffs = 0; | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
776 |
435 | 777 // fprintf(stderr, "%d, %d * %d = %d\n", len, pcx->header->bpl, pcx->header->nplanes, pcx->header->nplanes * pcx->header->bpl); |
778 | |
779 pcx->bufOffs = 0; | |
780 | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
781 for (int plane = 0; plane < pcx->header->nplanes; plane++) |
435 | 782 { |
783 Uint8 data = dmPCXGetByte(row, len, soffs++), | |
784 count = 1; | |
785 | |
786 // size_t blen = pcx->header->bpl * pcx->header->nplanes; | |
787 size_t blen = pcx->header->bpl; | |
788 while (soffs < blen) | |
789 { | |
790 if (data == dmPCXGetByte(row, len, soffs) && count < 63) | |
791 { | |
792 count++; | |
793 soffs++; | |
794 } | |
795 else | |
796 { | |
797 if (count == 1 && (data & 0xC0) != 0xC0) | |
798 { | |
799 if (!dmPCXPutByte(pcx, data)) | |
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
|
800 return DMERR_FWRITE; |
435 | 801 } |
802 else | |
803 { | |
804 if (!dmPCXPutByte(pcx, 0xC0 | count) || | |
805 !dmPCXPutByte(pcx, data)) | |
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
|
806 return DMERR_FWRITE; |
435 | 807 } |
808 | |
809 data = dmPCXGetByte(row, len, soffs++); | |
810 count = 1; | |
811 } | |
812 } | |
813 | |
814 if (count > 1) | |
815 { | |
816 if (!dmPCXPutByte(pcx, 0xC0 | count) || | |
817 !dmPCXPutByte(pcx, data)) | |
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
|
818 return DMERR_FWRITE; |
435 | 819 } |
820 | |
821 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
|
822 return DMERR_FWRITE; |
435 | 823 } |
824 | |
825 | |
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
|
826 return DMERR_OK; |
435 | 827 } |
828 | |
829 | |
1290
e7dc9bb9777e
Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents:
1288
diff
changeset
|
830 int dmWritePCXImageFILE(FILE *fp, DMImage *img, const DMImageConvSpec *spec) |
435 | 831 { |
832 DMPCXData pcx; | |
833 DMPCXHeader hdr; | |
834 int res; | |
835 | |
836 // Create output file | |
837 pcx.buf = NULL; | |
1291 | 838 pcx.format = spec->paletted ? DM_IFMT_PALETTE : DM_IFMT_RGB; |
435 | 839 pcx.header = &hdr; |
840 pcx.fp = fp; | |
841 | |
842 // Create PCX header | |
1167 | 843 dmMemset(&hdr, 0, sizeof(hdr)); |
1288 | 844 if (spec->paletted && img->pal != NULL) |
435 | 845 { |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
846 for (int i = 0; i < (img->ncolors > DMPCX_PAL_COLORS ? DMPCX_PAL_COLORS : img->ncolors); i++) |
435 | 847 { |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
848 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
|
849 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
|
850 hdr.colorMap[i].b = img->pal[i].b; |
435 | 851 } |
852 } | |
853 hdr.manufacturer = 10; | |
854 hdr.version = 5; | |
855 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
|
856 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
|
857 hdr.vres = img->height * spec->scaleY; |
435 | 858 hdr.xmin = hdr.ymin = 0; |
859 hdr.xmax = hdr.hres - 1; | |
860 hdr.ymax = hdr.vres - 1; | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
861 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
|
862 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
|
863 hdr.vScreenSize = hdr.vres; |
435 | 864 |
1291 | 865 // TODO XXX .. maybe actually compute these asdf |
866 hdr.bitsPerPlane = 8; | |
867 hdr.nplanes = dmImageGetBytesPerPixel(pcx.format); | |
868 | |
1284 | 869 res = img->width * spec->scaleX; |
435 | 870 hdr.bpl = res / 2; |
871 if (res % 2) hdr.bpl++; | |
872 hdr.bpl *= 2; | |
873 | |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
874 dmMsg(2, "PCX: paletted=%d, nplanes=%d, bpp=%d, bpl=%d\n", |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
875 spec->paletted, hdr.nplanes, hdr.bitsPerPlane, hdr.bpl); |
435 | 876 |
1291 | 877 // TODO XXX this is also bogus |
435 | 878 pcx.bufLen = hdr.bpl * 4; |
879 if ((pcx.buf = dmMalloc(pcx.bufLen)) == NULL) | |
880 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
881 res = dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
882 "PCX: Could not allocate %d bytes for RLE compression buffer.\n", |
435 | 883 pcx.bufLen); |
884 goto error; | |
885 } | |
886 | |
887 // Write PCX header | |
888 if (!dm_fwrite_byte(pcx.fp, hdr.manufacturer) || | |
889 !dm_fwrite_byte(pcx.fp, hdr.version) || | |
890 !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
|
891 !dm_fwrite_byte(pcx.fp, hdr.bitsPerPlane)) |
435 | 892 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
893 res = dmError(DMERR_FWRITE, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
894 "PCX: Could not write basic header data.\n"); |
435 | 895 goto error; |
896 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
897 |
435 | 898 if (!dm_fwrite_le16(pcx.fp, hdr.xmin) || |
899 !dm_fwrite_le16(pcx.fp, hdr.ymin) || | |
900 !dm_fwrite_le16(pcx.fp, hdr.xmax) || | |
901 !dm_fwrite_le16(pcx.fp, hdr.ymax) || | |
902 !dm_fwrite_le16(pcx.fp, hdr.hres) || | |
903 !dm_fwrite_le16(pcx.fp, hdr.vres)) | |
904 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
905 res = dmError(DMERR_FWRITE, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
906 "PCX: Could not write image dimensions.\n"); |
435 | 907 goto error; |
908 } | |
909 | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
910 if (!dm_fwrite_str(pcx.fp, (Uint8 *) &hdr.colorMap, sizeof(hdr.colorMap))) |
435 | 911 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
912 res = dmError(DMERR_FWRITE, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
913 "PCX: Could not write colormap.\n"); |
435 | 914 goto error; |
915 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
916 |
435 | 917 if (!dm_fwrite_byte(pcx.fp, hdr.reserved) || |
918 !dm_fwrite_byte(pcx.fp, hdr.nplanes) || | |
919 !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
|
920 !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
|
921 !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
|
922 !dm_fwrite_le16(pcx.fp, hdr.vScreenSize) || |
435 | 923 !dm_fwrite_str(pcx.fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler))) |
924 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
925 res = dmError(DMERR_FWRITE, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
926 "PCX: Could not write header remainder.\n"); |
435 | 927 goto error; |
928 } | |
929 | |
930 // Write image data | |
931 res = dmWriteImageData(img, (void *) &pcx, dmWritePCXRow, spec); | |
932 | |
933 // Write VGA palette | |
934 if (spec->paletted) | |
935 { | |
936 int i; | |
1294
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
937 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
|
938 |
435 | 939 dm_fwrite_byte(pcx.fp, 0x0C); |
940 | |
941 for (i = 0; i < img->ncolors; i++) | |
942 { | |
1294
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
943 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
|
944 !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
|
945 !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
|
946 { |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
947 res = dmError(DMERR_FWRITE, |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
948 "PCX: Could not write palette data.\n"); |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
949 goto error; |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
950 } |
435 | 951 } |
952 | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
953 // Pad the palette, if necessary |
435 | 954 for (; i < 256; i++) |
955 { | |
1294
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
956 if (!dm_fwrite_byte(pcx.fp, 0) || |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
957 !dm_fwrite_byte(pcx.fp, 0) || |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
958 !dm_fwrite_byte(pcx.fp, 0)) |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
959 { |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
960 res = dmError(DMERR_FWRITE, |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
961 "PCX: Could not write palette data.\n"); |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
962 goto error; |
9f2117f1584a
Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents:
1292
diff
changeset
|
963 } |
435 | 964 } |
965 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
966 |
435 | 967 error: |
968 dmFree(pcx.buf); | |
969 return res; | |
970 } | |
971 | |
972 | |
1290
e7dc9bb9777e
Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents:
1288
diff
changeset
|
973 int dmWritePCXImage(const char *filename, DMImage *img, const DMImageConvSpec *spec) |
435 | 974 { |
975 FILE *fp; | |
976 int res; | |
977 | |
978 if ((fp = fopen(filename, "wb")) == NULL) | |
979 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
980 return dmError(DMERR_FOPEN, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
981 "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
|
982 filename); |
435 | 983 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
984 |
435 | 985 res = dmWritePCXImageFILE(fp, img, spec); |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
986 |
435 | 987 fclose(fp); |
988 return res; | |
989 } | |
990 | |
991 | |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
992 static BOOL dmPCXDecodeRLERow(FILE *fp, Uint8 *buf, const size_t bufLen) |
435 | 993 { |
994 size_t offs = 0; | |
995 do | |
996 { | |
997 int count; | |
998 Uint8 data; | |
999 | |
1000 if (!dm_fread_byte(fp, &data)) | |
1001 return FALSE; | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1002 |
435 | 1003 if ((data & 0xC0) == 0xC0) |
1004 { | |
1284 | 1005 BOOL skip = FALSE; |
435 | 1006 count = data & 0x3F; |
1284 | 1007 if (count == 0) |
1008 { | |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
1009 switch (dmGFXErrorMode) |
1284 | 1010 { |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
1011 case DM_ERRMODE_RECOV_1: |
1284 | 1012 // Use as literal |
1013 skip = TRUE; | |
1014 count = 1; | |
1015 break; | |
1016 | |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
1017 case DM_ERRMODE_RECOV_2: |
1284 | 1018 // Ignore completely |
1019 skip = TRUE; | |
1020 break; | |
1021 | |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
1022 case DM_ERRMODE_FAIL: |
1284 | 1023 default: |
1024 // Error out on "invalid" data | |
1025 return FALSE; | |
1026 } | |
1027 } | |
1028 | |
1029 if (!skip && !dm_fread_byte(fp, &data)) | |
435 | 1030 return FALSE; |
1031 } | |
1032 else | |
1033 count = 1; | |
1034 | |
1035 while (count-- && offs < bufLen) | |
1036 buf[offs++] = data; | |
1037 | |
1284 | 1038 // 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
|
1039 if (count > 0 && dmGFXErrorMode == DM_ERRMODE_FAIL) |
1284 | 1040 return FALSE; |
1041 | |
435 | 1042 } while (offs < bufLen); |
1043 | |
1044 return TRUE; | |
1045 } | |
1046 | |
1047 | |
1048 int dmReadPCXImageFILE(FILE *fp, DMImage **pimg) | |
1049 { | |
1050 DMImage *img; | |
1051 DMPCXData pcx; | |
1052 DMPCXHeader hdr; | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1053 int res = 0; |
1286 | 1054 BOOL isPaletted; |
435 | 1055 |
1056 pcx.buf = NULL; | |
1057 | |
1058 // Read PCX header | |
1059 if (!dm_fread_byte(fp, &hdr.manufacturer) || | |
1060 !dm_fread_byte(fp, &hdr.version) || | |
1061 !dm_fread_byte(fp, &hdr.encoding) || | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1062 !dm_fread_byte(fp, &hdr.bitsPerPlane)) |
435 | 1063 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1064 res = dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1065 "PCX: Could not read basic header data.\n"); |
435 | 1066 goto error; |
1067 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1068 |
435 | 1069 if (hdr.manufacturer != 10 || |
1070 hdr.version != 5 || | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1071 hdr.encoding != 1) |
435 | 1072 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1073 res = dmError(DMERR_NOT_SUPPORTED, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1074 "PCX: Not a PCX file, or unsupported variant.\n"); |
435 | 1075 goto error; |
1076 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1077 |
435 | 1078 if (!dm_fread_le16(fp, &hdr.xmin) || |
1079 !dm_fread_le16(fp, &hdr.ymin) || | |
1080 !dm_fread_le16(fp, &hdr.xmax) || | |
1081 !dm_fread_le16(fp, &hdr.ymax) || | |
1082 !dm_fread_le16(fp, &hdr.hres) || | |
1083 !dm_fread_le16(fp, &hdr.vres)) | |
1084 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1085 res = dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1086 "PCX: Could not read image dimensions.\n"); |
435 | 1087 goto error; |
1088 } | |
1089 | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1090 if (!dm_fread_str(fp, (Uint8 *) &hdr.colorMap, sizeof(hdr.colorMap))) |
435 | 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, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1093 "PCX: Could not read colormap.\n"); |
435 | 1094 goto error; |
1095 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1096 |
435 | 1097 if (!dm_fread_byte(fp, &hdr.reserved) || |
1098 !dm_fread_byte(fp, &hdr.nplanes) || | |
1099 !dm_fread_le16(fp, &hdr.bpl) || | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1100 !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
|
1101 !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
|
1102 !dm_fread_le16(fp, &hdr.vScreenSize) || |
435 | 1103 !dm_fread_str(fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler))) |
1104 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1105 res = dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1106 "PCX: Could not read header remainder.\n"); |
435 | 1107 goto error; |
1108 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1109 |
1295
7a986f33895e
Fix isPaletted check in PCX reader :P
Matti Hamalainen <ccr@tnsp.org>
parents:
1294
diff
changeset
|
1110 isPaletted = (hdr.bitsPerPlane * hdr.nplanes) <= 8; |
1291 | 1111 |
1112 dmMsg(2, | |
1113 "PCX: xmin=%d, ymin=%d, xmax=%d, ymax=%d, res=%dx%d, scr=%dx%d\n", | |
1114 hdr.xmin, hdr.ymin, hdr.xmax, hdr.ymax, | |
1115 hdr.hres, hdr.vres, | |
1116 hdr.hScreenSize, hdr.vScreenSize); | |
1117 | |
1118 dmMsg(2, | |
1119 "PCX: nplanes=%d, bpp=%d, bpl=%d, isPaletted=%s\n", | |
1120 hdr.nplanes, hdr.bitsPerPlane, hdr.bpl, isPaletted ? "yes" : "no"); | |
1121 | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1122 if (hdr.nplanes < 1 || hdr.nplanes > 8) |
435 | 1123 { |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1124 res = dmError(DMERR_NOT_SUPPORTED, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1125 "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
|
1126 hdr.nplanes); |
435 | 1127 goto error; |
1128 } | |
1129 | |
1286 | 1130 if (!isPaletted) |
1131 { | |
1132 res = dmError(DMERR_NOT_SUPPORTED, | |
1133 "PCX: Non-indexed (truecolour) PCX images not supported for loading.\n"); | |
1134 goto error; | |
1135 } | |
1136 | |
435 | 1137 // Allocate image |
1286 | 1138 if ((*pimg = img = dmImageAlloc(hdr.xmax - hdr.xmin + 1, hdr.ymax - hdr.ymin + 1, |
1139 isPaletted ? DM_IFMT_PALETTE : DM_IFMT_RGBA, | |
1140 isPaletted ? (hdr.bitsPerPlane * hdr.nplanes) : -1)) == NULL) | |
435 | 1141 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1142 res = dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1143 "PCX: Could not allocate image structure.\n"); |
435 | 1144 goto error; |
1145 } | |
1146 | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1147 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
|
1148 { |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1149 res = dmError(DMERR_MALLOC, |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1150 "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
|
1151 hdr.bpl, (img->width * hdr.bitsPerPlane) / 8); |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1152 goto error; |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1153 } |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1154 |
435 | 1155 pcx.bufLen = hdr.nplanes * hdr.bpl; |
1156 if ((pcx.buf = dmMalloc(pcx.bufLen)) == NULL) | |
1157 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1158 res = dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1159 "PCX: Could not allocate RLE buffer.\n"); |
435 | 1160 goto error; |
1161 } | |
1162 | |
1163 // Read image data | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1164 Uint8 *dp = img->data; |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1165 for (int yc = 0; yc < img->height; yc++) |
435 | 1166 { |
1167 // 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
|
1168 if (!dmPCXDecodeRLERow(fp, pcx.buf, pcx.bufLen)) |
435 | 1169 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1170 res = dmError(DMERR_INVALID_DATA, |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1171 "PCX: Error decoding RLE compressed data.\n"); |
435 | 1172 goto error; |
1173 } | |
1296 | 1174 |
435 | 1175 // Decode bitplanes |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1176 switch (hdr.bitsPerPlane) |
435 | 1177 { |
1296 | 1178 case 32: |
1179 case 24: | |
1180 case 16: | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1181 case 8: |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1182 { |
1286 | 1183 // Actually bytes and bits per plane per pixel .. |
1184 const int bytesPerPlane = hdr.bitsPerPlane / 8; | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1185 |
1286 | 1186 for (int nplane = 0; nplane < hdr.nplanes; nplane++) |
1187 { | |
1188 Uint8 *dptr = dp + (nplane * bytesPerPlane), | |
1189 *sptr = pcx.buf + (hdr.bpl * nplane); | |
1190 | |
1191 memcpy(dptr, sptr, img->width * bytesPerPlane); | |
1192 } | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1193 } |
435 | 1194 break; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1195 |
1280
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1196 case 1: |
1296 | 1197 memset(dp, 0, img->width); |
1198 | |
1280
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1199 for (int nplane = 0; nplane < hdr.nplanes; nplane++) |
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1200 { |
1296 | 1201 Uint8 *sptr = pcx.buf + (hdr.bpl * nplane); |
1280
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1202 |
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1203 for (int xc = 0; xc < img->width; xc++) |
1296 | 1204 { |
1205 const int qx = xc * 2; | |
1206 const int px = 7 - (qx & 7); | |
1207 dp[xc] |= (sptr[qx / 8] & (1 << px)) >> (px - nplane); | |
1208 } | |
1280
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1209 } |
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1210 break; |
300a51e98fc3
Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
1279
diff
changeset
|
1211 |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1212 default: |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1284
diff
changeset
|
1213 res = dmError(DMERR_NOT_SUPPORTED, |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1214 "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
|
1215 hdr.bitsPerPlane); |
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1216 goto error; |
435 | 1217 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1218 |
435 | 1219 dp += img->pitch; |
1220 } | |
1221 | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1222 // Read additional VGA palette, if available |
1286 | 1223 if (isPaletted) |
435 | 1224 { |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1225 int ncolors; |
435 | 1226 Uint8 tmpb; |
1227 BOOL read; | |
1228 | |
1229 if (!dm_fread_byte(fp, &tmpb) || tmpb != 0x0C) | |
1230 { | |
1231 read = FALSE; | |
1296 | 1232 ncolors = 256; // KLUDGE |
435 | 1233 } |
1234 else | |
1235 { | |
1236 read = TRUE; | |
451
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
1237 ncolors = 256; |
435 | 1238 } |
1239 | |
460
0af039b6c0ae
Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
1240 if (!dmImageAllocPalette(img, ncolors, -1)) |
435 | 1241 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1242 res = dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1243 "PCX: Could not allocate palette data!\n"); |
435 | 1244 goto error; |
1245 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1246 |
435 | 1247 if (read) |
1248 { | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1249 // Okay, attempt to read the palette data |
1296 | 1250 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
|
1251 if (!dmReadPaletteData(fp, img->pal, ncolors)) |
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_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1254 "PCX: Error reading palette.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1255 goto error; |
435 | 1256 } |
1257 } | |
1258 else | |
1259 { | |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1260 // 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
|
1261 // the header palette to our internal palette structure. |
1296 | 1262 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
|
1263 for (int i = 0; i < (img->ncolors > DMPCX_PAL_COLORS ? DMPCX_PAL_COLORS : img->ncolors); i++) |
435 | 1264 { |
1279
0d3f5f44c0c4
Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1265
diff
changeset
|
1265 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
|
1266 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
|
1267 img->pal[i].b = hdr.colorMap[i].b; |
435 | 1268 } |
1269 } | |
1270 } | |
1271 | |
1272 error: | |
1273 dmFree(pcx.buf); | |
1274 return res; | |
1275 } | |
1276 | |
1277 | |
1278 int dmReadPCXImage(const char *filename, DMImage **pimg) | |
1279 { | |
1280 FILE *fp; | |
1281 int res; | |
1282 | |
1283 if ((fp = fopen(filename, "rb")) == NULL) | |
1284 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1285 return dmError(DMERR_FOPEN, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1286 "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
|
1287 filename); |
435 | 1288 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1289 |
435 | 1290 res = dmReadPCXImageFILE(fp, pimg); |
1291 | |
1292 fclose(fp); | |
1293 return res; | |
1294 } | |
1295 | |
1296 | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1297 #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
|
1298 #define IFF_ID_ILBM 0x494C424D // "ILBM" |
464 | 1299 #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
|
1300 #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
|
1301 #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
|
1302 #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
|
1303 #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
|
1304 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1305 #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
|
1306 #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
|
1307 #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
|
1308 #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
|
1309 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1310 #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
|
1311 #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
|
1312 |
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
|
1313 #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
|
1314 #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
|
1315 #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
|
1316 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1317 typedef struct |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1318 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1319 Uint32 id; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1320 Uint32 size; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1321 int count; |
445 | 1322 char str[6]; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1323 } DMIFFChunk; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1324 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1325 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1326 typedef struct |
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 Uint16 w, h; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1329 Sint16 x, y; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1330 Uint8 nplanes; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1331 Uint8 masking; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1332 Uint8 compression; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1333 Uint8 pad1; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1334 Uint16 transp; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1335 Uint8 xasp, yasp; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1336 Sint16 pagew, pageh; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1337 } DMIFFBMHD; |
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 |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1340 typedef struct |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1341 { |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1342 DMIFFChunk chBMHD, chCMAP, chBODY; |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1343 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
|
1344 Uint32 camg; |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1345 int ncolors; |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1346 DMColor *pal; |
1286 | 1347 BOOL planar; |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1348 } DMIFF; |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1349 |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1350 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1351 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
|
1352 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1353 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
|
1354 !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
|
1355 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1356 dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1357 "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
|
1358 return FALSE; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1359 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1360 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
|
1361 return TRUE; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1362 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1363 |
445 | 1364 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
|
1365 { |
445 | 1366 chunk->str[0] = (chunk->id >> 24) & 0xff; |
1367 chunk->str[1] = (chunk->id >> 16) & 0xff; | |
1368 chunk->str[2] = (chunk->id >> 8) & 0xff; | |
1369 chunk->str[3] = (chunk->id) & 0xff; | |
1370 chunk->str[4] = 0; | |
1371 return chunk->str; | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1372 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1373 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1374 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
|
1375 { |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1376 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
|
1377 { |
465 | 1378 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
|
1379 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
|
1380 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1381 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
|
1382 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1383 return dmError(DMERR_FSEEK, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1384 "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
|
1385 } |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1386 else |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1387 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
|
1388 } |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1389 else |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1390 return DMERR_OK; |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1391 } |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1392 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1393 static int dmCheckIFFChunk(DMIFFChunk *dest, DMIFFChunk *chunk, |
445 | 1394 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
|
1395 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1396 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
|
1397 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1398 return dmError(DMERR_INVALID_DATA, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1399 "ILBM: Multiple instances of chunk %s found.\n", |
445 | 1400 dmGetIFFChunkID(chunk)); |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1401 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1402 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1403 dest->count++; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1404 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1405 if (chunk->size < minSize) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1406 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1407 return dmError(DMERR_OUT_OF_DATA, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1408 "ILBM: Chunk is too small.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1409 } |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1410 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1411 return DMERR_OK; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1412 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1413 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1414 |
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
|
1415 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
|
1416 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1417 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
|
1418 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
|
1419 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1420 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
|
1421 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
|
1422 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1423 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
|
1424 return FALSE; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
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 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
|
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 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
|
1429 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
|
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 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
|
1432 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
|
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 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
|
1435 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
|
1436 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
|
1437 |
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 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
|
1439 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
|
1440 } |
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 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
|
1442 { |
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 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
|
1444 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
|
1445 { |
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 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
|
1450 } |
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 } 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
|
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 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
|
1455 } |
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 |
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 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
|
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 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
|
1461 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
|
1462 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
|
1463 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
|
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 |
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 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
|
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 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
|
1470 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
|
1471 { |
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 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
|
1473 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
|
1474 } |
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 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
|
1479 { |
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
|
1480 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
|
1481 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
|
1482 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
|
1483 int yc, res = DMERR_OK; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1484 |
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
|
1485 *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
|
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 // Allocate image |
1286 | 1488 if ((*pimg = img = dmImageAlloc(iff->bmhd.w, iff->bmhd.h, |
1489 iff->bmhd.nplanes < 8 ? DM_IFMT_PALETTE : DM_IFMT_RGBA, | |
1490 iff->bmhd.nplanes < 8 ? iff->bmhd.nplanes : -1)) == 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
|
1491 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
|
1492 |
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 // 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
|
1494 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
|
1495 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
|
1496 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
|
1497 |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
1498 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
|
1499 |
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
|
1500 // 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
|
1501 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
|
1502 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1503 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
|
1504 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
|
1505 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
|
1506 |
1167 | 1507 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
|
1508 |
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 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
|
1510 { |
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 // 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
|
1512 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
|
1513 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1514 res = dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1515 "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
|
1516 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
|
1517 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
|
1518 } |
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 |
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 // 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
|
1521 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
|
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 *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
|
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 |
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 // 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
|
1527 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
|
1528 { |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1529 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
|
1530 |
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 // 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
|
1532 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
|
1533 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1534 res = dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1535 "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
|
1536 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
|
1537 } |
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 // 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
|
1540 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
|
1541 { |
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 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
|
1543 |
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
|
1544 // 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
|
1545 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
|
1546 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
|
1547 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1548 |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1549 *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
|
1550 } |
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 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
|
1554 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
|
1555 return res; |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1556 } |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1557 |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1558 |
464 | 1559 int dmDecodePBMBody(FILE *fp, DMIFF *iff, DMImage **pimg, Uint32 *read) |
1560 { | |
1561 DMImage *img; | |
1562 int yc, res = DMERR_OK; | |
1563 | |
1564 *read = 0; | |
1565 | |
1566 // Allocate image | |
1286 | 1567 if ((*pimg = img = dmImageAlloc(iff->bmhd.w, iff->bmhd.h, |
1568 iff->bmhd.nplanes < 8 ? DM_IFMT_PALETTE : DM_IFMT_RGBA, | |
1569 iff->bmhd.nplanes < 8 ? iff->bmhd.nplanes : -1)) == NULL) | |
464 | 1570 return DMERR_MALLOC; |
1571 | |
1572 // Decode the chunk | |
1573 for (yc = 0; yc < img->height; yc++) | |
1574 { | |
1575 Uint8 *dp = img->data + (yc * img->pitch); | |
1576 | |
1577 if (!dmIFFReadOneRow(fp, iff, dp, img->width)) | |
1578 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1579 res = dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1580 "ILBM: Error in reading image row #%d.\n", yc); |
464 | 1581 goto error; |
1582 } | |
1583 | |
1584 *read += img->width; | |
1585 } | |
1586 | |
1587 error: | |
1588 return res; | |
1589 } | |
1590 | |
1591 | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1592 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
|
1593 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1594 Uint32 idILBM; |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1595 DMIFFChunk chunk; |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1596 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
|
1597 Uint32 read; |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1598 BOOL parsed = FALSE; |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1599 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
|
1600 |
1167 | 1601 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
|
1602 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1603 // Read IFF FORM header |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1604 if (!dmReadIFFChunk(fp, &chunk) || |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1605 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
|
1606 chunk.size < 32) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1607 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1608 return dmError(DMERR_NOT_SUPPORTED, |
1265
4e074b9b4789
Improve IFF FORM validity error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
1264
diff
changeset
|
1609 "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
|
1610 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
|
1611 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1612 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1613 // Check IFF ILBM signature |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1614 if (!dm_fread_be32(fp, &idILBM) || |
464 | 1615 (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
|
1616 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1617 return dmError(DMERR_INVALID_DATA, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1618 "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
|
1619 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1620 |
464 | 1621 iff.planar = (idILBM == IFF_ID_ILBM); |
1622 | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1623 while (!parsed && !feof(fp)) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1624 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1625 if (!dmReadIFFChunk(fp, &chunk)) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1626 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1627 return dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1628 "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
|
1629 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1630 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1631 switch (chunk.id) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1632 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1633 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
|
1634 // 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
|
1635 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
|
1636 return res; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1637 |
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
|
1638 // Read BMHD data |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1639 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
|
1640 !dm_fread_be16(fp, &iff.bmhd.h) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1641 !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
|
1642 !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
|
1643 !dm_fread_byte(fp, &iff.bmhd.nplanes) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1644 !dm_fread_byte(fp, &iff.bmhd.masking) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1645 !dm_fread_byte(fp, &iff.bmhd.compression) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1646 !dm_fread_byte(fp, &iff.bmhd.pad1) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1647 !dm_fread_be16(fp, &iff.bmhd.transp) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1648 !dm_fread_byte(fp, &iff.bmhd.xasp) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1649 !dm_fread_byte(fp, &iff.bmhd.yasp) || |
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1650 !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
|
1651 !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
|
1652 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1653 return dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1654 "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
|
1655 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1656 |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
1657 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
|
1658 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
|
1659 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
|
1660 |
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
|
1661 // 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
|
1662 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
|
1663 (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
|
1664 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
|
1665 (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
|
1666 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
|
1667 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
|
1668 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1669 return dmError(DMERR_NOT_SUPPORTED, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1670 "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
|
1671 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1672 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1673 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
|
1674 return res; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1675 break; |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1676 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1677 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1678 case IFF_ID_CMAP: |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1679 // 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
|
1680 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
|
1681 return res; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1682 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1683 // Check for sanity |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1684 if (chunk.size % 3 != 0) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1685 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1686 // Non-fatal |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1687 dmError(DMERR_INVALID_DATA, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1688 "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
|
1689 } |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1690 |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1691 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
|
1692 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
|
1693 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
|
1694 |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1695 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
|
1696 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
|
1697 |
451
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
1698 // Read palette |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
1699 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
|
1700 { |
461
c7a3aacbd55e
Implement transparent color in IFF ILBM reader and ctransp setting in PNG reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
1701 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
|
1702 (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
|
1703 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1704 return dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1705 "ILBM: Could not allocate palette data.\n"); |
451
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
1706 } |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
1707 if (!dmReadPaletteData(fp, iff.pal, iff.ncolors)) |
fdc91f2a0d27
Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents:
449
diff
changeset
|
1708 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1709 return dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1710 "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
|
1711 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1712 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1713 |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1714 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
|
1715 parsed = TRUE; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1716 break; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1717 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1718 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
|
1719 // 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
|
1720 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
|
1721 return res; |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1722 |
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
|
1723 // Check for sanity |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1724 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
|
1725 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1726 return dmError(DMERR_INVALID_DATA, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1727 "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
|
1728 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1729 |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
1730 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
|
1731 |
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
|
1732 // Decode the body |
464 | 1733 if (iff.planar) |
1734 { | |
1735 if ((res = dmDecodeILBMBody(fp, &iff, pimg, &read)) != DMERR_OK) | |
1736 return res; | |
1737 } | |
1738 else | |
1739 { | |
1740 if ((res = dmDecodePBMBody(fp, &iff, pimg, &read)) != DMERR_OK) | |
1741 return res; | |
1742 } | |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1743 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1744 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
|
1745 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
|
1746 |
444
7d588807f91d
Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
443
diff
changeset
|
1747 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
|
1748 parsed = TRUE; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1749 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
|
1750 |
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
|
1751 |
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
|
1752 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
|
1753 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
|
1754 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1755 return dmError(DMERR_FREAD, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1756 "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
|
1757 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1758 |
1287
32051ad352c8
Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
1286
diff
changeset
|
1759 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
|
1760 |
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
|
1761 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
|
1762 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1763 return dmError(DMERR_NOT_SUPPORTED, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1764 "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
|
1765 } |
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
|
1766 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1767 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
|
1768 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
|
1769 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
|
1770 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1771 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1772 default: |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1773 { |
465 | 1774 dmMsg(4, "Unknown chunk ID '%s', size %d\n", |
445 | 1775 dmGetIFFChunkID(&chunk), chunk.size); |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1776 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1777 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
|
1778 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1779 return dmError(DMERR_FSEEK, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1780 "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
|
1781 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1782 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1783 break; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1784 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1785 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1786 if (chunk.size & 1) |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1787 fgetc(fp); |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1788 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1789 |
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
|
1790 // 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
|
1791 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
|
1792 { |
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
|
1793 // 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
|
1794 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
|
1795 { |
834 | 1796 void *ptmp; |
1797 | |
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
|
1798 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
|
1799 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1800 return dmError(DMERR_NOT_SUPPORTED, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1801 "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
|
1802 } |
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
|
1803 |
834 | 1804 if ((ptmp = dmRealloc(iff.pal, sizeof(DMColor) * iff.ncolors * 2)) == NULL) |
1805 { | |
1806 dmFree(iff.pal); | |
1807 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
|
1808 return DMERR_MALLOC; |
834 | 1809 } |
1810 else | |
1811 iff.pal = ptmp; | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1812 |
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
|
1813 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
|
1814 { |
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 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
|
1816 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
|
1817 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
|
1818 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
|
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 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1821 |
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
|
1822 (*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
|
1823 (*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
|
1824 } |
0e27860ddcfe
Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents:
445
diff
changeset
|
1825 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1826 return res; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1827 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1828 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1829 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1830 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
|
1831 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1832 FILE *fp; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1833 int res; |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1834 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1835 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
|
1836 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1837 return dmError(DMERR_FOPEN, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
930
diff
changeset
|
1838 "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
|
1839 filename); |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1840 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1841 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1842 res = dmReadILBMImageFILE(fp, pimg); |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1843 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1844 fclose(fp); |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1845 return res; |
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 |
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 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1850 |
435 | 1851 static int fmtProbePNG(const Uint8 *buf, const size_t len) |
1852 { | |
1853 if (len > 64 && buf[0] == 0x89 && | |
1854 buf[1] == 'P' && buf[2] == 'N' && buf[3] == 'G' && | |
1855 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
|
1856 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1857 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
|
1858 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
|
1859 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
|
1860 else |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1861 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
|
1862 } |
435 | 1863 |
1864 return DM_PROBE_SCORE_FALSE; | |
1865 } | |
1866 | |
1867 | |
1868 static int fmtProbePCX(const Uint8 *buf, const size_t len) | |
1869 { | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1870 if (len > 128 + 32 && |
1288 | 1871 |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1872 (buf[1] == 5 || buf[1] == 2 || buf[1] == 3) && |
435 | 1873 buf[2] == 1 && |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1874 (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
|
1875 buf[65] >= 1 && buf[65] <= 4) |
435 | 1876 return DM_PROBE_SCORE_GOOD; |
1877 | |
1878 return DM_PROBE_SCORE_FALSE; | |
1879 } | |
1880 | |
1881 | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1882 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
|
1883 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1884 if (len > 32 && |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1885 buf[ 0] == 'F' && buf[ 1] == 'O' && |
464 | 1886 buf[ 2] == 'R' && buf[ 3] == 'M' && ( |
1887 (buf[ 8] == 'I' && buf[ 9] == 'L' && buf[10] == 'B' && buf[11] == 'M') || | |
1888 (buf[ 8] == 'P' && buf[ 9] == 'B' && buf[10] == 'M' && buf[11] == 0x20) | |
1889 )) | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1890 { |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1891 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
|
1892 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
|
1893 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
|
1894 else |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1895 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
|
1896 } |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1897 |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1898 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
|
1899 } |
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 |
435 | 1902 DMImageFormat dmImageFormatList[IMGFMT_LAST] = |
1903 { | |
1904 { | |
1905 "PNG", "Portable Network Graphics", | |
1906 fmtProbePNG, | |
1907 #ifdef DM_USE_LIBPNG | |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
1908 dmReadPNGImage, dmReadPNGImageFILE, |
435 | 1909 dmWritePNGImage, dmWritePNGImageFILE, |
1910 #else | |
1911 NULL, NULL, | |
453
349a2ff11531
Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents:
452
diff
changeset
|
1912 NULL, NULL, |
435 | 1913 #endif |
1914 }, | |
1915 { | |
1916 "PPM", "Portable PixMap", | |
1917 NULL, | |
1918 NULL, NULL, | |
1919 dmWritePPMImage, dmWritePPMImageFILE, | |
1920 }, | |
1921 { | |
1922 "PCX", "Z-Soft Paintbrush", | |
1923 fmtProbePCX, | |
1924 dmReadPCXImage, dmReadPCXImageFILE, | |
1925 dmWritePCXImage, dmWritePCXImageFILE, | |
1926 }, | |
1927 { | |
443
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1928 "ILBM", "IFF ILBM", |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1929 fmtProbeILBM, |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1930 dmReadILBMImage, dmReadILBMImageFILE, |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1931 NULL, NULL, |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1932 }, |
f7c9d1619c74
Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents:
442
diff
changeset
|
1933 { |
1291 | 1934 "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
|
1935 NULL, |
d400e32b62d9
Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents:
487
diff
changeset
|
1936 NULL, NULL, |
d400e32b62d9
Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents:
487
diff
changeset
|
1937 dmWriteRAWImage, dmWriteRAWImageFILE, |
d400e32b62d9
Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents:
487
diff
changeset
|
1938 }, |
d400e32b62d9
Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents:
487
diff
changeset
|
1939 { |
435 | 1940 "ARAW", "IFFMaster Amiga RAW", |
1941 NULL, | |
1942 NULL, NULL, | |
566
d400e32b62d9
Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents:
487
diff
changeset
|
1943 dmWriteRAWImage, dmWriteRAWImageFILE, |
435 | 1944 } |
1945 }; | |
1946 | |
1947 | |
1948 int dmImageProbeGeneric(const Uint8 *buf, const size_t len, DMImageFormat **pfmt, int *index) | |
1949 { | |
1950 int i, scoreMax = DM_PROBE_SCORE_FALSE, scoreIndex = -1; | |
1951 | |
1952 for (i = 0; i < IMGFMT_LAST; i++) | |
1953 { | |
1954 DMImageFormat *fmt = &dmImageFormatList[i]; | |
442
a67600e186d0
Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
440
diff
changeset
|
1955 if (fmt->probe != NULL) |
435 | 1956 { |
442
a67600e186d0
Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
440
diff
changeset
|
1957 int score = fmt->probe(buf, len); |
a67600e186d0
Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
440
diff
changeset
|
1958 if (score > scoreMax) |
a67600e186d0
Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
440
diff
changeset
|
1959 { |
a67600e186d0
Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
440
diff
changeset
|
1960 scoreMax = score; |
a67600e186d0
Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
440
diff
changeset
|
1961 scoreIndex = i; |
a67600e186d0
Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
440
diff
changeset
|
1962 } |
435 | 1963 } |
1964 } | |
1965 | |
1966 if (scoreIndex >= 0) | |
1967 { | |
1968 *pfmt = &dmImageFormatList[scoreIndex]; | |
1969 *index = scoreIndex; | |
1970 return scoreMax; | |
1971 } | |
1972 else | |
1973 return DM_PROBE_SCORE_FALSE; | |
1974 } |