annotate src/libgfx.c @ 1286:b812fad6f33e

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