Mercurial > hg > dmlib
annotate tools/libgfx.h @ 2093:d17512dbb4ef
Some work on reading >8bpp images.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 28 Feb 2019 12:32:07 +0200 |
parents | 614b161c0aa5 |
children | 4276b8c0fef0 |
rev | line source |
---|---|
435 | 1 /* |
2 * Functions for loading and saving bitmap images | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
2066 | 4 * (C) Copyright 2012-2018 Tecnic Software productions (TNSP) |
435 | 5 * |
6 * Please read file 'COPYING' for information on license and distribution. | |
7 */ | |
8 #ifndef LIBMGFX_H | |
9 #define LIBMGFX_H 1 | |
10 | |
11 #include "dmlib.h" | |
1609
c29adf5ce240
Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents:
1545
diff
changeset
|
12 #include "dmres.h" |
947
5ac8f5513978
Delete useless include line.
Matti Hamalainen <ccr@tnsp.org>
parents:
929
diff
changeset
|
13 |
435 | 14 |
15 #ifdef __cplusplus | |
16 extern "C" { | |
17 #endif | |
18 | |
19 | |
20 enum | |
21 { | |
1543
416d7b3ba3b2
Rename libgfx IMGFMT_* constants to DM_IMGFMT_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
1341
diff
changeset
|
22 DM_IMGFMT_PNG, |
416d7b3ba3b2
Rename libgfx IMGFMT_* constants to DM_IMGFMT_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
1341
diff
changeset
|
23 DM_IMGFMT_PPM, |
416d7b3ba3b2
Rename libgfx IMGFMT_* constants to DM_IMGFMT_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
1341
diff
changeset
|
24 DM_IMGFMT_PCX, |
2064
3617ef01c1de
Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
2047
diff
changeset
|
25 DM_IMGFMT_IFF_ILBM, |
3617ef01c1de
Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
2047
diff
changeset
|
26 DM_IMGFMT_IFF_PBM, |
2068 | 27 DM_IMGFMT_IFF_ACBM, |
1543
416d7b3ba3b2
Rename libgfx IMGFMT_* constants to DM_IMGFMT_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
1341
diff
changeset
|
28 DM_IMGFMT_RAW, |
416d7b3ba3b2
Rename libgfx IMGFMT_* constants to DM_IMGFMT_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
1341
diff
changeset
|
29 DM_IMGFMT_ARAW, |
2047
3829c292df02
Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1921
diff
changeset
|
30 DM_IMGFMT_CDUMP, |
435 | 31 }; |
32 | |
33 | |
34 enum | |
35 { | |
1801 | 36 DM_COLFMT_PALETTE, |
2092
614b161c0aa5
Initial support for reading PPM/PGM.
Matti Hamalainen <ccr@tnsp.org>
parents:
2086
diff
changeset
|
37 DM_COLFMT_GRAYSCALE, |
1801 | 38 DM_COLFMT_RGB, |
39 DM_COLFMT_RGBA, | |
435 | 40 }; |
41 | |
42 | |
1544 | 43 // Error handling modes |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
44 enum |
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
45 { |
1544 | 46 DM_ERRMODE_FAIL = 0, |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
47 DM_ERRMODE_RECOV_1, |
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
48 DM_ERRMODE_RECOV_2, |
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
49 }; |
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
50 |
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
51 |
1544 | 52 // Probe scores |
53 enum | |
54 { | |
55 DM_PROBE_SCORE_FALSE = 0, | |
56 DM_PROBE_SCORE_MAYBE = 250, | |
57 DM_PROBE_SCORE_AVG = 500, | |
58 DM_PROBE_SCORE_GOOD = 750, | |
59 DM_PROBE_SCORE_MAX = 1000, | |
60 }; | |
61 | |
62 | |
1545
3b613fcbf3ff
Improve how format read/write capabilities are marked and shown.
Matti Hamalainen <ccr@tnsp.org>
parents:
1544
diff
changeset
|
63 // Flags for readability/writeability of formats |
3b613fcbf3ff
Improve how format read/write capabilities are marked and shown.
Matti Hamalainen <ccr@tnsp.org>
parents:
1544
diff
changeset
|
64 enum |
3b613fcbf3ff
Improve how format read/write capabilities are marked and shown.
Matti Hamalainen <ccr@tnsp.org>
parents:
1544
diff
changeset
|
65 { |
1759
027fb7313d85
Add a format flag for marking formats that have broken/incomplete support and use it.
Matti Hamalainen <ccr@tnsp.org>
parents:
1733
diff
changeset
|
66 DM_FMT_WR = 0x0001, // Format can be written |
027fb7313d85
Add a format flag for marking formats that have broken/incomplete support and use it.
Matti Hamalainen <ccr@tnsp.org>
parents:
1733
diff
changeset
|
67 DM_FMT_RD = 0x0002, // Format can be read |
027fb7313d85
Add a format flag for marking formats that have broken/incomplete support and use it.
Matti Hamalainen <ccr@tnsp.org>
parents:
1733
diff
changeset
|
68 DM_FMT_RDWR = (DM_FMT_RD | DM_FMT_WR), // Read and write support |
027fb7313d85
Add a format flag for marking formats that have broken/incomplete support and use it.
Matti Hamalainen <ccr@tnsp.org>
parents:
1733
diff
changeset
|
69 DM_FMT_BROKEN = 0x1000, // Support is broken/incomplete |
1545
3b613fcbf3ff
Improve how format read/write capabilities are marked and shown.
Matti Hamalainen <ccr@tnsp.org>
parents:
1544
diff
changeset
|
70 }; |
3b613fcbf3ff
Improve how format read/write capabilities are marked and shown.
Matti Hamalainen <ccr@tnsp.org>
parents:
1544
diff
changeset
|
71 |
3b613fcbf3ff
Improve how format read/write capabilities are marked and shown.
Matti Hamalainen <ccr@tnsp.org>
parents:
1544
diff
changeset
|
72 |
1609
c29adf5ce240
Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents:
1545
diff
changeset
|
73 // Bitmapped image struct |
435 | 74 typedef struct |
75 { | |
1801 | 76 int format; // one of types specified by DM_COLFMT_* |
1286 | 77 int width, height; |
78 int pitch; // bytes per scanline | |
79 int bpp; // bits per pixel | |
80 | |
1628
a549d33d543a
Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents:
1616
diff
changeset
|
81 float aspect; // aspect ratio (vert / horiz), <= 0 if not set |
a549d33d543a
Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents:
1616
diff
changeset
|
82 |
1286 | 83 int ncolors; // number of colors in palette, if any |
84 int ctransp; // transparency color index | |
85 BOOL constpal; // is the palette a const? | |
86 DMColor *pal; // pointer to palette struct, NULL if no palette | |
87 | |
929
e1378398be0f
Add size field for allocated data size in DMImage.
Matti Hamalainen <ccr@tnsp.org>
parents:
902
diff
changeset
|
88 size_t size; |
435 | 89 Uint8 *data; |
90 } DMImage; | |
91 | |
92 | |
93 typedef struct | |
94 { | |
2065
451980580189
Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
2064
diff
changeset
|
95 int fmtid; // DM_IMGFMT_* of target format (a bit of a kludge here) |
2070 | 96 int format; // Target color format DM_COLFMT_* |
2065
451980580189
Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
2064
diff
changeset
|
97 int scaleX, scaleY; // Scale factors (1..) |
451980580189
Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
2064
diff
changeset
|
98 int nplanes, bpp, mask; |
451980580189
Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
2064
diff
changeset
|
99 BOOL planar; |
451980580189
Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
2064
diff
changeset
|
100 int compression; // Use compression/compression level (0 = none, 9 = max) |
1290
e7dc9bb9777e
Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents:
1288
diff
changeset
|
101 } DMImageConvSpec; |
435 | 102 |
103 | |
104 typedef struct | |
105 { | |
106 char *fext; | |
1616
36d073c45327
Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
1609
diff
changeset
|
107 char *name; |
36d073c45327
Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
1609
diff
changeset
|
108 int fmtid; // DM_IMGFMT_* |
36d073c45327
Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
1609
diff
changeset
|
109 int flags; // DM_FMT_* flags |
435 | 110 int (*probe)(const Uint8 *buf, const size_t len); |
1886
1af79412f249
Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents:
1809
diff
changeset
|
111 int (*read)(DMResource *fp, DMImage **pimg); |
1af79412f249
Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents:
1809
diff
changeset
|
112 int (*write)(DMResource *fp, const DMImage *pimg, const DMImageConvSpec *spec); |
435 | 113 } DMImageFormat; |
114 | |
115 | |
1616
36d073c45327
Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
1609
diff
changeset
|
116 extern const DMImageFormat dmImageFormatList[]; |
36d073c45327
Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
1609
diff
changeset
|
117 extern const int ndmImageFormatList; |
1285
e4bda4909d72
Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
118 extern int dmGFXErrorMode; |
435 | 119 |
120 | |
1286 | 121 DMImage * dmImageAlloc(const int width, const int height, const int format, const int bpp); |
435 | 122 void dmImageFree(DMImage *img); |
1286 | 123 int dmImageGetBytesPerPixel(const int format); |
2093
d17512dbb4ef
Some work on reading >8bpp images.
Matti Hamalainen <ccr@tnsp.org>
parents:
2092
diff
changeset
|
124 int dmImageGetBitsPerPixel(const int format); |
1616
36d073c45327
Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
1609
diff
changeset
|
125 int dmImageProbeGeneric(const Uint8 *buf, const size_t len, const DMImageFormat **fmt, int *index); |
435 | 126 |
487
b89598501cec
Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
127 BOOL dmCompareColor(const DMColor *c1, const DMColor *c2, BOOL alpha); |
1733
59c2b08a80a6
Make some palette allocation functions public.
Matti Hamalainen <ccr@tnsp.org>
parents:
1628
diff
changeset
|
128 BOOL dmPaletteAlloc(DMColor **ppal, int ncolors, int ctransp); |
59c2b08a80a6
Make some palette allocation functions public.
Matti Hamalainen <ccr@tnsp.org>
parents:
1628
diff
changeset
|
129 BOOL dmImagePaletteAlloc(DMImage *img, int ncolors, int ctransp); |
487
b89598501cec
Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
130 |
435 | 131 |
1886
1af79412f249
Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents:
1809
diff
changeset
|
132 int dmWriteImageData(const DMImage *img, void *cbdata, |
1af79412f249
Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents:
1809
diff
changeset
|
133 int (*writeRowCB)(void *, const Uint8 *, const size_t), const DMImageConvSpec *spec); |
1af79412f249
Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents:
1809
diff
changeset
|
134 |
435 | 135 |
2065
451980580189
Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents:
2064
diff
changeset
|
136 int dmWriteIFFMasterRAWHeader(DMResource *fp, const char *filename, const char *prefix, const DMImage *img, const DMImageConvSpec *spec); |
1886
1af79412f249
Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents:
1809
diff
changeset
|
137 int dmWriteRAWImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec); |
435 | 138 |
1886
1af79412f249
Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents:
1809
diff
changeset
|
139 int dmWritePPMImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec); |
2092
614b161c0aa5
Initial support for reading PPM/PGM.
Matti Hamalainen <ccr@tnsp.org>
parents:
2086
diff
changeset
|
140 int dmReadPPMImage(DMResource *fp, DMImage **pimg); |
435 | 141 |
142 #ifdef DM_USE_LIBPNG | |
1886
1af79412f249
Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents:
1809
diff
changeset
|
143 int dmWritePNGImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec); |
1af79412f249
Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents:
1809
diff
changeset
|
144 int dmReadPNGImage(DMResource *fp, DMImage **pimg); |
435 | 145 #endif |
146 | |
1886
1af79412f249
Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents:
1809
diff
changeset
|
147 int dmWritePCXImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec); |
1af79412f249
Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents:
1809
diff
changeset
|
148 int dmReadPCXImage(DMResource *fp, DMImage **pimg); |
435 | 149 |
1892
cbc911ffd21e
Rename ILBM functions to IFF, which is more approriate as we support both ILBM and PBM variants of the IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
1886
diff
changeset
|
150 int dmReadIFFImage(DMResource *fp, DMImage **pimg); |
1896
f80b2dc77c30
Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents:
1892
diff
changeset
|
151 int dmWriteIFFImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec); |
1809
a20655e40e6c
Move ILBM function declarations to position that better reflects their
Matti Hamalainen <ccr@tnsp.org>
parents:
1801
diff
changeset
|
152 |
435 | 153 |
1309
5ad7d780a09b
Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents:
1307
diff
changeset
|
154 typedef struct _DMBitStreamContext |
5ad7d780a09b
Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents:
1307
diff
changeset
|
155 { |
5ad7d780a09b
Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents:
1307
diff
changeset
|
156 void *handle; |
5ad7d780a09b
Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents:
1307
diff
changeset
|
157 |
2086
aedadff9e116
Implement dmGetBits() to the DMBitStreamContext API. Also improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
2070
diff
changeset
|
158 int (*putByte)(struct _DMBitStreamContext *ctx); |
aedadff9e116
Implement dmGetBits() to the DMBitStreamContext API. Also improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
2070
diff
changeset
|
159 int (*getByte)(struct _DMBitStreamContext *ctx); |
1309
5ad7d780a09b
Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents:
1307
diff
changeset
|
160 |
2086
aedadff9e116
Implement dmGetBits() to the DMBitStreamContext API. Also improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
2070
diff
changeset
|
161 unsigned int |
aedadff9e116
Implement dmGetBits() to the DMBitStreamContext API. Also improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
2070
diff
changeset
|
162 outBuf, outBitCount, outByteCount, |
aedadff9e116
Implement dmGetBits() to the DMBitStreamContext API. Also improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
2070
diff
changeset
|
163 inBuf, inBitCount, inByteCount; |
1309
5ad7d780a09b
Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents:
1307
diff
changeset
|
164 } DMBitStreamContext; |
5ad7d780a09b
Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents:
1307
diff
changeset
|
165 |
5ad7d780a09b
Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents:
1307
diff
changeset
|
166 |
5ad7d780a09b
Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents:
1307
diff
changeset
|
167 void dmInitBitStreamContext(DMBitStreamContext *ctx); |
5ad7d780a09b
Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents:
1307
diff
changeset
|
168 int dmFlushBitStream(DMBitStreamContext *ctx); |
2086
aedadff9e116
Implement dmGetBits() to the DMBitStreamContext API. Also improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
2070
diff
changeset
|
169 int dmInitBitStreamRes(DMBitStreamContext *ctx, DMResource *fp); |
aedadff9e116
Implement dmGetBits() to the DMBitStreamContext API. Also improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
2070
diff
changeset
|
170 |
aedadff9e116
Implement dmGetBits() to the DMBitStreamContext API. Also improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
2070
diff
changeset
|
171 int dmPutBits(DMBitStreamContext *ctx, const unsigned int val, const unsigned int n); |
aedadff9e116
Implement dmGetBits() to the DMBitStreamContext API. Also improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
2070
diff
changeset
|
172 int dmGetBits(DMBitStreamContext *ctx, unsigned int *val, const unsigned int n); |
1309
5ad7d780a09b
Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents:
1307
diff
changeset
|
173 |
5ad7d780a09b
Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents:
1307
diff
changeset
|
174 |
435 | 175 #ifdef __cplusplus |
176 } | |
177 #endif | |
178 | |
179 #endif // LIBMGFX_H |