annotate tools/libgfx.c @ 2085:b31bb3dc1310

Cosmetic.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 11 Dec 2018 15:17:40 +0200
parents 9d6c30c071ed
children aedadff9e116
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 /*
1308
8f71ca1900ea Update header blurps and copyrights.
Matti Hamalainen <ccr@tnsp.org>
parents: 1307
diff changeset
2 * Functions for reading and converting various graphics file formats
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
1894
2e3f188c6bf0 Bump copyright year.
Matti Hamalainen <ccr@tnsp.org>
parents: 1892
diff changeset
4 * (C) Copyright 2012-2018 Tecnic Software productions (TNSP)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include "libgfx.h"
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #ifdef DM_USE_LIBPNG
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include <png.h>
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #endif
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14
1285
e4bda4909d72 Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1284
diff changeset
15 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
16
e4bda4909d72 Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1284
diff changeset
17
1309
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
18 void dmInitBitStreamContext(DMBitStreamContext *ctx)
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
19 {
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
20 ctx->outBuf = 0;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
21 ctx->outByteCount = 0;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
22 ctx->outBitCount = 8;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
23 }
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
24
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
25
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
26 BOOL dmPutBits(DMBitStreamContext *ctx, const int val, const int n)
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
27 {
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
28 unsigned int mask = 1 << (n - 1);
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
29
1583
fd0d1b4efc83 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1543
diff changeset
30 for (int i = 0; i < n; i++)
1309
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
31 {
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
32 ctx->outBuf <<= 1;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
33
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
34 if (val & mask)
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
35 ctx->outBuf |= 1;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
36
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
37 mask >>= 1;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
38 ctx->outBitCount--;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
39
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
40 if (ctx->outBitCount == 0)
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
41 {
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
42 if (!ctx->putByte(ctx, ctx->outBuf & 0xff))
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
43 return FALSE;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
44
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
45 ctx->outBitCount = 8;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
46 ctx->outByteCount++;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
47 }
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
48 }
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
49
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
50 return TRUE;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
51 }
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
52
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
53
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
54 int dmFlushBitStream(DMBitStreamContext *ctx)
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
55 {
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
56 if (ctx == NULL)
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
57 return DMERR_NULLPTR;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
58
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
59 if (ctx->outBitCount != 8)
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
60 dmPutBits(ctx, 0, ctx->outBitCount);
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
61
2083
feed844a5ae1 Return DMERR_OK where appropriate instead of 0.
Matti Hamalainen <ccr@tnsp.org>
parents: 2082
diff changeset
62 return DMERR_OK;
1309
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
63 }
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
64
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
65
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
66 static BOOL dmPutByteFILE(DMBitStreamContext *ctx, const Uint8 val)
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
67 {
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
68 return dmf_write_byte((DMResource *) ctx->handle, val);
1309
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
69 }
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
70
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
71
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
72 int dmInitBitStreamFILE(DMBitStreamContext *ctx, DMResource *fp)
1309
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
73 {
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
74 if (ctx == NULL || fp == NULL)
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
75 return DMERR_NULLPTR;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
76
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
77 ctx->putByte = dmPutByteFILE;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
78 ctx->handle = (void *) fp;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
79
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
80 dmInitBitStreamContext(ctx);
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
81
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
82 return DMERR_OK;
5ad7d780a09b Move bitstream reading functions to libgfx, as they were only used there.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
83 }
1285
e4bda4909d72 Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1284
diff changeset
84
1339
65f8fd1bf76e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1309
diff changeset
85
487
b89598501cec Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 472
diff changeset
86 BOOL dmCompareColor(const DMColor *c1, const DMColor *c2, BOOL alpha)
b89598501cec Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 472
diff changeset
87 {
b89598501cec Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 472
diff changeset
88 if (c1->r == c2->r &&
b89598501cec Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 472
diff changeset
89 c1->g == c2->g &&
b89598501cec Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 472
diff changeset
90 c1->b == c2->b)
b89598501cec Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 472
diff changeset
91 return alpha ? (c1->a == c2->a) : TRUE;
b89598501cec Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 472
diff changeset
92 else
b89598501cec Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 472
diff changeset
93 return FALSE;
b89598501cec Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 472
diff changeset
94 }
b89598501cec Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 472
diff changeset
95
b89598501cec Move dmCompareColor() to libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 472
diff changeset
96
1286
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
97 int dmImageGetBytesPerPixel(const int format)
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
98 {
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
99 switch (format)
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
100 {
1801
0562dd55a1f6 s/DM_IFMT_/DM_COLFMT_/g
Matti Hamalainen <ccr@tnsp.org>
parents: 1800
diff changeset
101 case DM_COLFMT_PALETTE : return 1;
0562dd55a1f6 s/DM_IFMT_/DM_COLFMT_/g
Matti Hamalainen <ccr@tnsp.org>
parents: 1800
diff changeset
102 case DM_COLFMT_RGB : return 3;
0562dd55a1f6 s/DM_IFMT_/DM_COLFMT_/g
Matti Hamalainen <ccr@tnsp.org>
parents: 1800
diff changeset
103 case DM_COLFMT_RGBA : return 4;
0562dd55a1f6 s/DM_IFMT_/DM_COLFMT_/g
Matti Hamalainen <ccr@tnsp.org>
parents: 1800
diff changeset
104 default : return -1;
1286
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
105 }
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
106 }
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
107
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
108
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
109 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
110 {
928
ebe0d93e03c0 Use dmMalloc0() instead of dmCalloc() here.
Matti Hamalainen <ccr@tnsp.org>
parents: 902
diff changeset
111 DMImage *img = dmMalloc0(sizeof(DMImage));
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 if (img == NULL)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 return NULL;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
114
1286
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
115 img->width = width;
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
116 img->height = height;
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
117 img->format = format;
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
118 img->bpp = (bpp <= 0) ? dmImageGetBytesPerPixel(format) * 8 : bpp;
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
119 img->pitch = width * img->bpp;
1628
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
120 img->size = img->pitch * img->height;
1286
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
121 img->ctransp = -1;
1628
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
122 img->aspect = -1;
930
efbad8817b79 Some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 929
diff changeset
123
929
e1378398be0f Add size field for allocated data size in DMImage.
Matti Hamalainen <ccr@tnsp.org>
parents: 928
diff changeset
124 if ((img->data = dmMalloc(img->size)) == NULL)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 dmFree(img);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 return NULL;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
129
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 return img;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 void dmImageFree(DMImage *img)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 if (img != NULL)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 if (!img->constpal)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 dmFree(img->pal);
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 dmFree(img->data);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 dmFree(img);
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 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147
460
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
148 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
149 {
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
150 if (ppal == NULL)
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
151 return FALSE;
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
152
460
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
153 // Allocate desired amount of palette
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
154 if ((*ppal = dmCalloc(ncolors, sizeof(DMColor))) == NULL)
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
155 return FALSE;
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
156
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
157 // Set alpha values to max, except for transparent color
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
158 for (int i = 0; i < ncolors; i++)
460
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
159 {
1655
a05e3fcc60ec Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1654
diff changeset
160 (*ppal)[i].a = (i == ctransp) ? 0x00 : 0xff;
460
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
161 }
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
162
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
163 return TRUE;
451
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
164 }
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
165
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
166
1655
a05e3fcc60ec Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1654
diff changeset
167 BOOL dmImagePaletteAlloc(DMImage *img, int ncolors, int ctransp)
451
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
168 {
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
169 if (img == NULL)
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
170 return FALSE;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
171
451
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
172 img->ncolors = ncolors;
460
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
173 img->ctransp = ctransp;
1730
881a3fc421d8 Use the default C64 palette whenever we need it, but make it possible to override it.
Matti Hamalainen <ccr@tnsp.org>
parents: 1698
diff changeset
174 img->constpal = FALSE;
881a3fc421d8 Use the default C64 palette whenever we need it, but make it possible to override it.
Matti Hamalainen <ccr@tnsp.org>
parents: 1698
diff changeset
175
460
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
176 return dmPaletteAlloc(&(img->pal), ncolors, ctransp);
451
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
177 }
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
178
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
179
1655
a05e3fcc60ec Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1654
diff changeset
180 static BOOL dmPaletteReadData(DMResource *fp, DMColor *pal, int ncolors)
451
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
181 {
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
182 for (int i = 0; i < ncolors; i++)
451
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
183 {
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
184 Uint8 colR, colG, colB;
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
185 if (!dmf_read_byte(fp, &colR) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
186 !dmf_read_byte(fp, &colG) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
187 !dmf_read_byte(fp, &colB))
451
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
188 return FALSE;
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
189
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
190 pal[i].r = colR;
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
191 pal[i].g = colG;
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
192 pal[i].b = colB;
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
193 }
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
194
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
195 return TRUE;
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
196 }
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
197
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
198
1341
ae2ba8cb510f Constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 1339
diff changeset
199 int dmWriteImageData(const DMImage *img, void *cbdata, int (*writeRowCB)(void *, const Uint8 *, const size_t), const DMImageConvSpec *spec)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 int x, y, yscale, xscale, res = 0, rowSize, rowWidth;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 Uint8 *row = NULL;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 // 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
205 rowWidth = img->width * spec->scaleX;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 rowSize = rowWidth * dmImageGetBytesPerPixel(spec->format);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 if ((row = dmMalloc(rowSize + 16)) == NULL)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 res = DMERR_MALLOC;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 goto done;
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 // Generate the image
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 for (y = 0; y < img->height; y++)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 {
436
86f956e4580f Cosmetics, rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 435
diff changeset
217 Uint8 *ptr1 = row,
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218 *ptr2 = ptr1 + rowWidth,
1291
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
219 *ptr3 = ptr2 + rowWidth,
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
220 *ptr4 = ptr3 + rowWidth;
435
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 for (x = 0; x < img->width; x++)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 {
1291
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
224 Uint8 c = img->data[(y * img->pitch) + (x * img->bpp) / 8],
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
225 qr, qg, qb, qa;
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
226
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 switch (spec->format)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 {
1801
0562dd55a1f6 s/DM_IFMT_/DM_COLFMT_/g
Matti Hamalainen <ccr@tnsp.org>
parents: 1800
diff changeset
229 case DM_COLFMT_PALETTE:
902
c6c480e8e1c8 Add separate X and Y scaling to gfxconv and libgfx outputters.
Matti Hamalainen <ccr@tnsp.org>
parents: 858
diff changeset
230 for (xscale = 0; xscale < spec->scaleX; xscale++)
436
86f956e4580f Cosmetics, rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 435
diff changeset
231 *ptr1++ = c;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 break;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233
1801
0562dd55a1f6 s/DM_IFMT_/DM_COLFMT_/g
Matti Hamalainen <ccr@tnsp.org>
parents: 1800
diff changeset
234 case DM_COLFMT_RGBA:
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235 qr = img->pal[c].r;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 qg = img->pal[c].g;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 qb = img->pal[c].b;
460
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
238 qa = img->pal[c].a;
1292
92e99ea23811 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1291
diff changeset
239
1291
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
240 if (spec->planar)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 {
1291
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
242 for (xscale = 0; xscale < spec->scaleX; xscale++)
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
243 {
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
244 *ptr1++ = qr;
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
245 *ptr2++ = qg;
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
246 *ptr3++ = qb;
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
247 *ptr4++ = qa;
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
248 }
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
249 }
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
250 else
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
251 {
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
252 for (xscale = 0; xscale < spec->scaleX; xscale++)
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
253 {
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
254 *ptr1++ = qr;
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
255 *ptr1++ = qg;
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
256 *ptr1++ = qb;
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
257 *ptr1++ = qa;
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
258 }
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 break;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261
1801
0562dd55a1f6 s/DM_IFMT_/DM_COLFMT_/g
Matti Hamalainen <ccr@tnsp.org>
parents: 1800
diff changeset
262 case DM_COLFMT_RGB:
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 qr = img->pal[c].r;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 qg = img->pal[c].g;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 qb = img->pal[c].b;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
266
1291
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
267 if (spec->planar)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268 {
1291
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
269 for (xscale = 0; xscale < spec->scaleX; xscale++)
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
270 {
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
271 *ptr1++ = qr;
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
272 *ptr2++ = qg;
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
273 *ptr3++ = qb;
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
274 }
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275 }
1291
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
276 else
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 {
1291
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
278 for (xscale = 0; xscale < spec->scaleX; xscale++)
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
279 {
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
280 *ptr1++ = qr;
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
281 *ptr1++ = qg;
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
282 *ptr1++ = qb;
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
283 }
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
285 break;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286 }
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
902
c6c480e8e1c8 Add separate X and Y scaling to gfxconv and libgfx outputters.
Matti Hamalainen <ccr@tnsp.org>
parents: 858
diff changeset
289 for (yscale = 0; yscale < spec->scaleY; yscale++)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 {
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
291 if ((res = writeRowCB(cbdata, row, rowSize)) != DMERR_OK)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292 goto done;
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 done:
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
297 dmFree(row);
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298 return res;
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
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
301
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
302 #define DMCOL(x) (((x) >> 4) & 0xf)
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
303
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
304 int dmWriteIFFMasterRAWHeader(
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
305 DMResource *fp, const char *filename, const char *prefix,
2065
451980580189 Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 2064
diff changeset
306 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: 1881
diff changeset
307 {
2049
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
308 if (dmfprintf(fp,
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
309 "%s_width: dw.w %d\n"
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
310 "%s_height: dw.w %d\n"
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
311 "%s_nplanes: dw.w %d\n",
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
312 prefix, img->width * spec->scaleX,
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
313 prefix, img->height * spec->scaleY,
2049
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
314 prefix, spec->nplanes) < 0)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
315 return dmferror(fp);
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
316
2065
451980580189 Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 2064
diff changeset
317 if (spec->fmtid == DM_IMGFMT_ARAW)
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
318 {
2049
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
319 if (dmfprintf(fp,
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
320 "%s_ncolors: dw.w %d\n"
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
321 "%s_palette:\n",
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
322 prefix, img->ncolors,
2049
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
323 prefix) < 0)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
324 return dmferror(fp);
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
325
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
326 for (int i = 0; i < (1 << spec->nplanes); i++)
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
327 {
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
328 Uint32 color;
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
329 if (i < img->ncolors)
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
330 {
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
331 color = (DMCOL(img->pal[i].r) << 8) |
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
332 (DMCOL(img->pal[i].g) << 4) |
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
333 (DMCOL(img->pal[i].b));
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
334 }
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
335 else
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
336 color = 0;
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
337
2049
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
338 if (dmfprintf(fp,
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
339 "\tdc.w $%04X\n",
2049
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
340 color) < 0)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
341 return dmferror(fp);
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
342 }
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
343
2049
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
344 if (dmfprintf(fp,
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
345 "%s: incbin \"%s\"\n",
2049
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
346 prefix, filename) < 0)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
347 return dmferror(fp);
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
348 }
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
349
1887
297aa8f0ca7f Return DMERR_OK instead of dmferror(fp) dmWriteIFFMasterRAWHeader().
Matti Hamalainen <ccr@tnsp.org>
parents: 1886
diff changeset
350 return DMERR_OK;
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
351 }
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
352
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
353
2085
b31bb3dc1310 Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 2084
diff changeset
354 static BOOL dmWriteRAWRow(DMBitStreamContext *bs, const DMImage *img,
b31bb3dc1310 Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 2084
diff changeset
355 const DMImageConvSpec *spec, const int yc, const int plane)
1872
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
356 {
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
357 const Uint8 *sp = img->data + (yc * img->pitch);
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
358 for (int xc = 0; xc < img->width; xc++)
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
359 {
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
360 for (int xscale = 0; xscale < spec->scaleX; xscale++)
2084
9d6c30c071ed Simplify dmPutBits() call in dmWriteRAWRow().
Matti Hamalainen <ccr@tnsp.org>
parents: 2083
diff changeset
361 if (!dmPutBits(bs, (sp[xc] >> plane) & 1, 1))
1872
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
362 return FALSE;
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
363 }
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
364 return TRUE;
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
365 }
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
366
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
367
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
368 int dmWriteRAWImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369 {
1622
009a3e3c54bf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1621
diff changeset
370 int res;
800
c63e24f9aa9a Modularize bitstream reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
371 DMBitStreamContext bs;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
372
800
c63e24f9aa9a Modularize bitstream reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
373 if ((res = dmInitBitStreamFILE(&bs, fp)) != DMERR_OK)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 return res;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375
1291
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
376 if (spec->planar)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377 {
1339
65f8fd1bf76e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1309
diff changeset
378 // Output bitplanes in planar format
65f8fd1bf76e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1309
diff changeset
379 // (each plane of line sequentially)
1622
009a3e3c54bf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1621
diff changeset
380 for (int yc = 0; yc < img->height; yc++)
1872
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
381 for (int yscale = 0; yscale < spec->scaleY; yscale++)
1622
009a3e3c54bf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1621
diff changeset
382 for (int plane = 0; plane < spec->nplanes; plane++)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383 {
1872
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
384 if (!dmWriteRAWRow(&bs, img, spec, yc, plane))
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
385 return DMERR_FWRITE;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
388 else
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 // Output each bitplane in sequence
1622
009a3e3c54bf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1621
diff changeset
391 for (int plane = 0; plane < spec->nplanes; plane++)
009a3e3c54bf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1621
diff changeset
392 for (int yc = 0; yc < img->height; yc++)
1872
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
393 for (int yscale = 0; yscale < spec->scaleY; yscale++)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
394 {
1872
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
395 if (!dmWriteRAWRow(&bs, img, spec, yc, plane))
fe15412eec10 Actually fix also the ARAW/RAW image data writing to honor the scaling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1814
diff changeset
396 return DMERR_FWRITE;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
398 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
399
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
400 return dmFlushBitStream(&bs);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
401 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402
566
d400e32b62d9 Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents: 487
diff changeset
403
2049
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
404 static BOOL dmPutByteCDump(DMBitStreamContext *ctx, const Uint8 val)
2047
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
405 {
2049
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
406 return dmfprintf((DMResource *) ctx->handle, "0x%02x, ", val) > 0;
2047
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
407 }
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
408
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
409
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
410 int dmWriteCDumpImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
411 {
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
412 int res;
2049
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
413 DMBitStreamContext bs;
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
414
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
415 if (dmfprintf(fp,
2047
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
416 "#define SET_WIDTH %d\n"
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
417 "#define SET_HEIGHT %d\n"
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
418 "\n"
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
419 "Uint8 img_data[] = {\n",
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
420 img->width * spec->scaleX,
2049
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
421 img->height * spec->scaleY) < 0)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
422 return dmferror(fp);
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
423
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
424 dmInitBitStreamContext(&bs);
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
425 bs.putByte = dmPutByteCDump;
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
426 bs.handle = (void *) fp;
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
427
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
428 if (spec->planar)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
429 {
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
430 // Output bitplanes in planar format
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
431 // (each plane of line sequentially)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
432 for (int yc = 0; yc < img->height; yc++)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
433 for (int yscale = 0; yscale < spec->scaleY; yscale++)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
434 for (int plane = 0; plane < spec->nplanes; plane++)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
435 {
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
436 if (!dmWriteRAWRow(&bs, img, spec, yc, plane))
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
437 return DMERR_FWRITE;
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
438
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
439 if (dmfprintf(fp, "\n") < 0)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
440 return dmferror(fp);
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
441 }
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
442 }
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
443 else
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
444 {
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
445 // Output each bitplane in sequence
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
446 for (int plane = 0; plane < spec->nplanes; plane++)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
447 for (int yc = 0; yc < img->height; yc++)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
448 for (int yscale = 0; yscale < spec->scaleY; yscale++)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
449 {
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
450 if (!dmWriteRAWRow(&bs, img, spec, yc, plane))
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
451 return DMERR_FWRITE;
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
452
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
453 if (dmfprintf(fp, "\n") < 0)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
454 return dmferror(fp);
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
455 }
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
456 }
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
457
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
458 res = dmFlushBitStream(&bs);
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
459
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
460 if (res == DMERR_OK &&
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
461 dmfprintf(fp, "};\n") < 0)
a945a5f2fd70 Improve the cdump output format support of gfxconv. Also add some error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2047
diff changeset
462 res = dmferror(fp);
2047
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
463
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
464 return res;
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
465 }
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
466
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
467
1301
e03f20d0f785 Constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 1300
diff changeset
468 static int dmWritePPMRow(void *cbdata, const Uint8 *row, const size_t len)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
469 {
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
470 if (dmf_write_str((DMResource *) cbdata, row, len))
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
471 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
472 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
473 return DMERR_FWRITE;
435
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
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
476
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
477 int dmWritePPMImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
478 {
1290
e7dc9bb9777e Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents: 1288
diff changeset
479 DMImageConvSpec tmpSpec;
1288
6c8b19d1d196 More work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1287
diff changeset
480
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
481 // Write PPM header
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
482 char *tmp = dm_strdup_printf(
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
483 "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
484 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
485 img->height * spec->scaleY);
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
486
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
487 if (tmp == NULL)
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
488 return DMERR_MALLOC;
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
489
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
490 dmfputs(tmp, fp);
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
491 dmFree(tmp);
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
492
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
493 // Write image data
1290
e7dc9bb9777e Rename struct DMImageSpec to DMImageConvSpec.
Matti Hamalainen <ccr@tnsp.org>
parents: 1288
diff changeset
494 memcpy(&tmpSpec, spec, sizeof(DMImageConvSpec));
1801
0562dd55a1f6 s/DM_IFMT_/DM_COLFMT_/g
Matti Hamalainen <ccr@tnsp.org>
parents: 1800
diff changeset
495 tmpSpec.format = DM_COLFMT_RGB;
1288
6c8b19d1d196 More work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1287
diff changeset
496 return dmWriteImageData(img, (void *) fp, dmWritePPMRow, &tmpSpec);
435
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
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 #ifdef DM_USE_LIBPNG
1613
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
501 static int fmtProbePNG(const Uint8 *buf, const size_t len)
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
502 {
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
503 if (len > 64 && buf[0] == 0x89 &&
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
504 buf[1] == 'P' && buf[2] == 'N' && buf[3] == 'G' &&
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
505 buf[4] == 0x0d && buf[5] == 0x0a)
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
506 {
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
507 if (buf[12] == 'I' && buf[13] == 'H' &&
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
508 buf[14] == 'D' && buf[15] == 'R')
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
509 return DM_PROBE_SCORE_MAX;
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
510 else
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
511 return DM_PROBE_SCORE_GOOD;
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
512 }
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
513
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
514 return DM_PROBE_SCORE_FALSE;
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
515 }
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
516
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
517
1301
e03f20d0f785 Constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 1300
diff changeset
518 static int dmWritePNGRow(void *cbdata, const Uint8 *row, const size_t len)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
519 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
520 png_structp png_ptr = cbdata;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
521 (void) len;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
522
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
523 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
524 return DMERR_INTERNAL;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
525
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
526 png_write_row(png_ptr, row);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527
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
528 return DMERR_OK;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
529 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
532 static void dmPNGWriteData(png_structp png_ptr, png_bytep data, png_size_t length)
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
533 {
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
534 DMResource *res = (DMResource *) png_get_io_ptr(png_ptr);
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
535
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
536 // XXX TODO: How the fuck does one do error handling here?
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
537 dmf_write_str(res, data, length);
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
538 }
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
539
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
540
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
541 static void dmPNGWriteFlush(png_structp png_ptr)
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
542 {
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
543 (void) png_ptr;
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
544 }
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
545
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
546
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
547 int dmWritePNGImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
548 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549 png_structp png_ptr = NULL;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
550 png_infop info_ptr = NULL;
1304
e968b259605b Fix a warning about potentially clobbered variable due to setjmp() use.
Matti Hamalainen <ccr@tnsp.org>
parents: 1303
diff changeset
551 int fmt, res;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
552
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
553 // Create PNG structures
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
554 png_ptr = png_create_write_struct(
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
555 PNG_LIBPNG_VER_STRING,
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
556 NULL, NULL, NULL);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
557
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
558 if (png_ptr == NULL)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
560 res = dmError(DMERR_MALLOC,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
561 "PNG: png_create_write_struct() failed.\n");
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
562 goto error;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
563 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
564
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
565 info_ptr = png_create_info_struct(png_ptr);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566 if (info_ptr == NULL)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
567 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
568 res = dmError(DMERR_INIT_FAIL,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
569 "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
570 png_ptr);
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
571 goto error;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
572 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
573
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
574 if (setjmp(png_jmpbuf(png_ptr)))
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
575 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
576 res = dmError(DMERR_INIT_FAIL,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
577 "PNG: Error during image writing..\n");
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
578 goto error;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
579 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
580
1304
e968b259605b Fix a warning about potentially clobbered variable due to setjmp() use.
Matti Hamalainen <ccr@tnsp.org>
parents: 1303
diff changeset
581 res = DMERR_OK;
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
582 png_set_write_fn(png_ptr, fp, dmPNGWriteData, dmPNGWriteFlush);
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
583
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
584 // Write PNG header info
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
585 switch (spec->format)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
586 {
1801
0562dd55a1f6 s/DM_IFMT_/DM_COLFMT_/g
Matti Hamalainen <ccr@tnsp.org>
parents: 1800
diff changeset
587 case DM_COLFMT_PALETTE: fmt = PNG_COLOR_TYPE_PALETTE; break;
0562dd55a1f6 s/DM_IFMT_/DM_COLFMT_/g
Matti Hamalainen <ccr@tnsp.org>
parents: 1800
diff changeset
588 case DM_COLFMT_RGB : fmt = PNG_COLOR_TYPE_RGB; break;
0562dd55a1f6 s/DM_IFMT_/DM_COLFMT_/g
Matti Hamalainen <ccr@tnsp.org>
parents: 1800
diff changeset
589 case DM_COLFMT_RGBA : fmt = PNG_COLOR_TYPE_RGB_ALPHA; break;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
590 default:
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
591 res = dmError(DMERR_NOT_SUPPORTED,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
592 "PNG: Unsupported image format %d.\n",
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
593 spec->format);
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
594 goto error;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
595 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
596
2022
eb6dafdd36dc Add option -C for controlling output file compression (PNG and IFF for now) in gfxconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 2020
diff changeset
597 png_set_compression_level(png_ptr, spec->compression);
eb6dafdd36dc Add option -C for controlling output file compression (PNG and IFF for now) in gfxconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 2020
diff changeset
598
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
599 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
600 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
601 img->height * spec->scaleY,
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
602 8, /* bits per component */
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
603 fmt,
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
604 PNG_INTERLACE_NONE,
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
605 PNG_COMPRESSION_TYPE_DEFAULT,
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
606 PNG_FILTER_TYPE_DEFAULT);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
607
1287
32051ad352c8 Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1286
diff changeset
608 dmMsg(2, "PNG: %d x %d, depth=%d, type=%d\n",
902
c6c480e8e1c8 Add separate X and Y scaling to gfxconv and libgfx outputters.
Matti Hamalainen <ccr@tnsp.org>
parents: 858
diff changeset
609 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
610 img->height * spec->scaleY,
460
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
611 8, fmt);
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
612
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
613 // Palette
1801
0562dd55a1f6 s/DM_IFMT_/DM_COLFMT_/g
Matti Hamalainen <ccr@tnsp.org>
parents: 1800
diff changeset
614 if (spec->format == DM_COLFMT_PALETTE)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
615 {
858
e7019bd83cca Fix potential longjmp clobbering of variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 834
diff changeset
616 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
617
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
618 if (palette == NULL)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
619 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
620 res = dmError(DMERR_MALLOC,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
621 "PNG: Could not allocate palette structure.");
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
622 goto error;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
623 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
624
1167
848a88ce7a57 Use dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1152
diff changeset
625 dmMemset(palette, 0, PNG_MAX_PALETTE_LENGTH * sizeof(png_color));
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
626
1939
bc00373e5a90 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1938
diff changeset
627 for (int i = 0; i < img->ncolors; i++)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
628 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
629 palette[i].red = img->pal[i].r;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
630 palette[i].green = img->pal[i].g;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
631 palette[i].blue = img->pal[i].b;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
632 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
633
460
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
634 png_set_PLTE(png_ptr, info_ptr, palette, img->ncolors);
1938
a8e475eede4a Fix two memory leaks in the PNG writer/reader of libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1921
diff changeset
635 png_free(png_ptr, palette);
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
636 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
637
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
638 // png_set_gAMA(png_ptr, info_ptr, 2.2);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
639
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
640 png_write_info(png_ptr, info_ptr);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
641
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
642
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
643 // Write compressed image data
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
644 dmWriteImageData(img, (void *) png_ptr, dmWritePNGRow, spec);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
645
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
646 // Write footer
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
647 png_write_end(png_ptr, NULL);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
648
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
649 error:
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
650 if (png_ptr && info_ptr)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
651 png_destroy_write_struct(&png_ptr, &info_ptr);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
652
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
653 return res;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
654 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
655
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
656
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
657 void dmPNGReadData(png_structp png_ptr, png_bytep data, png_size_t length)
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
658 {
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
659 DMResource *res = (DMResource *) png_get_io_ptr(png_ptr);
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
660
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
661 // XXX TODO: How the fuck does one do error handling here?
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
662 dmf_read_str(res, data, length);
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
663 }
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
664
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
665
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
666 int dmReadPNGImage(DMResource *fp, DMImage **pimg)
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
667 {
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
668 png_structp png_ptr = NULL;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
669 png_infop info_ptr = NULL;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
670 png_colorp palette = NULL;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
671 png_bytep *row_pointers = NULL;
460
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
672 png_bytep trans = NULL;
1940
c77019065b96 Initialize two variables to zero, as valgrind complains about them despite
Matti Hamalainen <ccr@tnsp.org>
parents: 1939
diff changeset
673 png_uint_32 width, height, res_x = 0, res_y = 0;
1628
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
674 int i, bit_depth, color_type, ncolors, ntrans, unit_type;
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
675 int res = DMERR_OK;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
676 DMImage *img;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
677
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
678 // Create PNG structures
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
679 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
680 PNG_LIBPNG_VER_STRING,
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
681 NULL, NULL, NULL);
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 if (png_ptr == NULL)
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
684 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
685 res = dmError(DMERR_MALLOC,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
686 "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
687 goto error;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
688 }
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
689
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
690 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
691 if (info_ptr == NULL)
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
692 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
693 res = dmError(DMERR_INIT_FAIL,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
694 "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
695 png_ptr);
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
696 goto error;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
697 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
698
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
699 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
700 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
701 res = dmError(DMERR_INIT_FAIL,
1291
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
702 "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
703 goto error;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
704 }
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
705
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
706 png_set_read_fn(png_ptr, fp, dmPNGReadData);
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
707
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
708 // Read image information
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
709 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
710
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
711 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
712 &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
713
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
714 if (width < 1 || height < 1)
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
715 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
716 res = dmError(DMERR_INVALID_DATA,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
717 "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
718 width, height);
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
719 goto error;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
720 }
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
721
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
722 switch (color_type)
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
723 {
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
724 case PNG_COLOR_TYPE_GRAY:
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
725 if (bit_depth < 8)
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
726 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
727
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
728 if (bit_depth > 8)
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
729 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
730 res = dmError(DMERR_NOT_SUPPORTED,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
731 "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
732 bit_depth);
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
733 goto error;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
734 }
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
735 break;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
736
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
737 case PNG_COLOR_TYPE_PALETTE:
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
738 png_set_packing(png_ptr);
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
739 break;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
740
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
741 default:
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
742 res = dmError(DMERR_NOT_SUPPORTED,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
743 "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
744 goto error;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
745 }
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
746
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
747 // Allocate image
1287
32051ad352c8 Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1286
diff changeset
748 dmMsg(2, "PNG: %d x %d, depth=%d, type=%d\n",
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
749 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
750
1300
fd442faa705f "Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents: 1299
diff changeset
751 if ((*pimg = img = dmImageAlloc(width, height,
1801
0562dd55a1f6 s/DM_IFMT_/DM_COLFMT_/g
Matti Hamalainen <ccr@tnsp.org>
parents: 1800
diff changeset
752 DM_COLFMT_PALETTE,
1300
fd442faa705f "Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents: 1299
diff changeset
753 // XXX TODO? When/if we ever handle < 8bit indexed correctly, we can use the actual bpp
fd442faa705f "Fix" the PNG/ILBM decoding to not use actual bitdepths for indexed images, but force 8bits.
Matti Hamalainen <ccr@tnsp.org>
parents: 1299
diff changeset
754 -1 /* bit_depth */)) == NULL)
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
755 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
756 res = dmError(DMERR_MALLOC,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
757 "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
758 goto error;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
759 }
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
760
1628
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
761 // Set image aspect ratio
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
762 png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y, &unit_type);
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
763
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
764 if (res_x > 0 && res_y > 0 &&
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
765 res_y / res_x != (unsigned) (img->height / img->width))
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
766 img->aspect = (float) res_y / (float) res_x;
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
767
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
768 // ...
1938
a8e475eede4a Fix two memory leaks in the PNG writer/reader of libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1921
diff changeset
769 if ((row_pointers = png_malloc(png_ptr, height * sizeof(png_bytep))) == NULL)
a8e475eede4a Fix two memory leaks in the PNG writer/reader of libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1921
diff changeset
770 goto error;
a8e475eede4a Fix two memory leaks in the PNG writer/reader of libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1921
diff changeset
771
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
772 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
773 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
774
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
775 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
776
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
777 png_read_end(png_ptr, NULL);
1945
80d67f0e8567 Better fix for the PNG reader memory leak.
Matti Hamalainen <ccr@tnsp.org>
parents: 1942
diff changeset
778 png_free(png_ptr, row_pointers);
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
779
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
780 // Create palette
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
781 switch (color_type)
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
782 {
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
783 case PNG_COLOR_TYPE_GRAY:
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
784 ncolors = 256;
1287
32051ad352c8 Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1286
diff changeset
785 dmMsg(2, "PNG: Generating %d color grayscale palette.\n", ncolors);
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
786
1655
a05e3fcc60ec Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1654
diff changeset
787 if (!dmImagePaletteAlloc(img, ncolors, -1))
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
788 {
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
789 res = DMERR_MALLOC;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
790 goto error;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
791 }
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
792
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
793 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
794 {
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
795 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
796 }
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
797 break;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
798
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
799 case PNG_COLOR_TYPE_PALETTE:
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
800 png_get_PLTE(png_ptr, info_ptr, &palette, &ncolors);
1287
32051ad352c8 Adjust debug messages and debug message levels in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1286
diff changeset
801 dmMsg(2, "PNG: Palette of %d colors found.\n", ncolors);
467
80d09ea85e75 Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents: 465
diff changeset
802 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
803 {
1655
a05e3fcc60ec Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1654
diff changeset
804 if (!dmImagePaletteAlloc(img, ncolors, -1))
467
80d09ea85e75 Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents: 465
diff changeset
805 {
80d09ea85e75 Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents: 465
diff changeset
806 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
807 goto error;
80d09ea85e75 Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents: 465
diff changeset
808 }
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
809
467
80d09ea85e75 Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents: 465
diff changeset
810 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
811 {
80d09ea85e75 Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents: 465
diff changeset
812 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
813 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
814 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
815 }
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
816 }
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
817 break;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
818 }
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
819
460
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
820 if (color_type == PNG_COLOR_TYPE_PALETTE ||
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
821 color_type == PNG_COLOR_TYPE_GRAY)
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
822 {
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
823 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
824 if (trans != NULL && ntrans > 0)
460
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
825 {
467
80d09ea85e75 Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents: 465
diff changeset
826 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
827 {
80d09ea85e75 Check for NULL pointers from png_get_tRNS and png_get_PLTE.
Matti Hamalainen <ccr@tnsp.org>
parents: 465
diff changeset
828 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
829 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
830 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
831 }
460
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
832 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
833 }
460
0af039b6c0ae Improve transparent color handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
834
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
835 error:
1939
bc00373e5a90 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1938
diff changeset
836 if (png_ptr != NULL && info_ptr != NULL)
453
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
837 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
838
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
839 return res;
349a2ff11531 Implement PNG (1-8bpp indexed) reading support via libPNG.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
840 }
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
841 #endif
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
842
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
843
1613
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
844 //
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
845 // Z-Soft PCX format
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
846 //
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
847 #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
848
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
849 typedef struct
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
850 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
851 Uint8 r,g,b;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
852 } DMPCXColor;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
853
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
854
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
855 typedef struct
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
856 {
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
857 Uint8 manufacturer, // always 0x0a
1288
6c8b19d1d196 More work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1287
diff changeset
858 version, // Z-Soft PC Paintbrush version:
6c8b19d1d196 More work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1287
diff changeset
859 // 0 = v2.5
6c8b19d1d196 More work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1287
diff changeset
860 // 2 = v2.8 with palette,
6c8b19d1d196 More work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1287
diff changeset
861 // 3 = v2.8 without palette
6c8b19d1d196 More work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1287
diff changeset
862 // 4 = PC Paintbrush for Windows
6c8b19d1d196 More work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1287
diff changeset
863 // 5 = v3.0 or better
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
864 encoding, // usually 0x01 = RLE, 0x00 = uncompressed
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
865 bitsPerPlane; // bits per pixel per plane
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
866
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
867 Uint16 xmin, ymin, xmax, ymax;
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
868 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
869 DMPCXColor colorMap[DMPCX_PAL_COLORS];
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
870 Uint8 reserved; // should be set to 0
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
871 Uint8 nplanes; // number of planes
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
872 Uint16 bpl; // bytes per plane LINE
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
873 Uint16 palInfo; // 1 = color/BW, 2 = grayscale
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
874 Uint16 hScreenSize, vScreenSize;
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
875 Uint8 filler[54];
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
876 } DMPCXHeader;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
877
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
878
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
879 typedef struct
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
880 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
881 DMPCXHeader *header;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
882 Uint8 *buf;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
883 size_t bufLen, bufOffs;
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
884 DMResource *fp;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
885 } DMPCXData;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
886
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
887
1613
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
888 static int fmtProbePCX(const Uint8 *buf, const size_t len)
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
889 {
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
890 if (len > 128 + 32 &&
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
891
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
892 (buf[1] == 5 || buf[1] == 2 || buf[1] == 3) &&
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
893 buf[2] == 1 &&
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
894 (buf[3] == 8 || buf[3] == 4 || buf[3] == 3 || buf[3] == 1) &&
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
895 buf[65] >= 1 && buf[65] <= 4)
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
896 return DM_PROBE_SCORE_GOOD;
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
897
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
898 return DM_PROBE_SCORE_FALSE;
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
899 }
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
900
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
901
1298
f0d6aac3adc4 Some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1297
diff changeset
902 // Returns one byte from row buffer (of length len) at offset soffs,
f0d6aac3adc4 Some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1297
diff changeset
903 // OR zero if the offset is outside buffer.
1301
e03f20d0f785 Constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 1300
diff changeset
904 static inline Uint8 dmPCXGetByte(const Uint8 *row, const size_t len, const size_t soffs)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
905 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
906 return (soffs < len) ? row[soffs] : 0;
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 static BOOL dmPCXFlush(DMPCXData *pcx)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
910 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
911 BOOL ret = TRUE;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
912 if (pcx->bufOffs > 0)
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
913 ret = dmf_write_str(pcx->fp, pcx->buf, pcx->bufOffs);
1297
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
914
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
915 pcx->bufOffs = 0;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
916 return ret;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
917 }
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 static inline BOOL dmPCXPutByte(DMPCXData *pcx, const Uint8 val)
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 if (pcx->bufOffs < pcx->bufLen)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
922 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
923 pcx->buf[pcx->bufOffs++] = val;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
924 return TRUE;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
925 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
926 else
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
927 return dmPCXFlush(pcx);
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
1297
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
930
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
931 static int dmPCXPutData(DMPCXData *pcx, const Uint8 data, const int count)
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
932 {
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
933 if (count == 1 && (data & 0xC0) != 0xC0)
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
934 {
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
935 if (!dmPCXPutByte(pcx, data))
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
936 return DMERR_FWRITE;
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
937 }
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
938 else
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
939 {
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
940 if (!dmPCXPutByte(pcx, 0xC0 | count) ||
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
941 !dmPCXPutByte(pcx, data))
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
942 return DMERR_FWRITE;
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
943 }
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
944 return DMERR_OK;
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
945 }
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
946
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
947
1301
e03f20d0f785 Constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 1300
diff changeset
948 static int dmWritePCXRow(void *cbdata, const Uint8 *row, const size_t len)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
949 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
950 DMPCXData *pcx = (DMPCXData *) cbdata;
1297
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
951 int err;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
952 size_t soffs = 0;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
953
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
954 pcx->bufOffs = 0;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
955
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
956 for (int plane = 0; plane < pcx->header->nplanes; plane++)
435
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 Uint8 data = dmPCXGetByte(row, len, soffs++),
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
959 count = 1;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
960
1297
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
961 size_t blen = pcx->header->bpl * pcx->header->nplanes;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
962 while (soffs < blen)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
963 {
1297
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
964 if (data == dmPCXGetByte(row, len, soffs) && count < 0x3F)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
965 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
966 count++;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
967 soffs++;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
968 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
969 else
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
970 {
1297
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
971 if ((err = dmPCXPutData(pcx, data, count)) != DMERR_OK)
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
972 return err;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
973
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
974 data = dmPCXGetByte(row, len, soffs++);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
975 count = 1;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
976 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
977 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
978
1297
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
979 if ((err = dmPCXPutData(pcx, data, count)) != DMERR_OK)
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
980 return err;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
981
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
982 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
983 return DMERR_FWRITE;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
984 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
985
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
986
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
987 return DMERR_OK;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
988 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
989
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
990
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
991 int dmWritePCXImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *pspec)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
992 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
993 DMPCXData pcx;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
994 DMPCXHeader hdr;
1303
be30466fbc47 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1302
diff changeset
995 DMImageConvSpec spec;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
996 int res;
1302
38614c07c2e2 Now with "almost" working 24bit PCX output. Almost.
Matti Hamalainen <ccr@tnsp.org>
parents: 1301
diff changeset
997
1303
be30466fbc47 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1302
diff changeset
998 // Always force planar for PCX
1302
38614c07c2e2 Now with "almost" working 24bit PCX output. Almost.
Matti Hamalainen <ccr@tnsp.org>
parents: 1301
diff changeset
999 memcpy(&spec, pspec, sizeof(DMImageConvSpec));
38614c07c2e2 Now with "almost" working 24bit PCX output. Almost.
Matti Hamalainen <ccr@tnsp.org>
parents: 1301
diff changeset
1000 spec.planar = TRUE;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1001
1450
61a486e25dc7 24bit PCX writing does not work correctly, so return DMERR_NOT_SUPPORTED in the writer function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1341
diff changeset
1002 // XXX: 24bit PCX does not work yet ..
2065
451980580189 Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 2064
diff changeset
1003 if (spec.format != DM_COLFMT_PALETTE)
1800
45e7688336dc Fix checks for NULL palette in index/palette images. Also improve error
Matti Hamalainen <ccr@tnsp.org>
parents: 1730
diff changeset
1004 {
45e7688336dc Fix checks for NULL palette in index/palette images. Also improve error
Matti Hamalainen <ccr@tnsp.org>
parents: 1730
diff changeset
1005 return dmError(DMERR_NOT_SUPPORTED,
45e7688336dc Fix checks for NULL palette in index/palette images. Also improve error
Matti Hamalainen <ccr@tnsp.org>
parents: 1730
diff changeset
1006 "24bit PCX not supported yet.\n");
45e7688336dc Fix checks for NULL palette in index/palette images. Also improve error
Matti Hamalainen <ccr@tnsp.org>
parents: 1730
diff changeset
1007 }
45e7688336dc Fix checks for NULL palette in index/palette images. Also improve error
Matti Hamalainen <ccr@tnsp.org>
parents: 1730
diff changeset
1008
2065
451980580189 Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 2064
diff changeset
1009 if (spec.format == DM_COLFMT_PALETTE && img->pal == NULL)
1800
45e7688336dc Fix checks for NULL palette in index/palette images. Also improve error
Matti Hamalainen <ccr@tnsp.org>
parents: 1730
diff changeset
1010 {
45e7688336dc Fix checks for NULL palette in index/palette images. Also improve error
Matti Hamalainen <ccr@tnsp.org>
parents: 1730
diff changeset
1011 return dmError(DMERR_NULLPTR,
45e7688336dc Fix checks for NULL palette in index/palette images. Also improve error
Matti Hamalainen <ccr@tnsp.org>
parents: 1730
diff changeset
1012 "Image spec says paletted/indexed image, but palette pointer is NULL.\n");
45e7688336dc Fix checks for NULL palette in index/palette images. Also improve error
Matti Hamalainen <ccr@tnsp.org>
parents: 1730
diff changeset
1013 }
1450
61a486e25dc7 24bit PCX writing does not work correctly, so return DMERR_NOT_SUPPORTED in the writer function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1341
diff changeset
1014
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1015 // Create output file
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1016 pcx.buf = NULL;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1017 pcx.header = &hdr;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1018 pcx.fp = fp;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1019
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1020 // Create PCX header
1167
848a88ce7a57 Use dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1152
diff changeset
1021 dmMemset(&hdr, 0, sizeof(hdr));
2065
451980580189 Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 2064
diff changeset
1022 if (spec.format == DM_COLFMT_PALETTE)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1023 {
2016
7114ea4c3c42 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1948
diff changeset
1024 const int ncolors = img->ncolors > DMPCX_PAL_COLORS ? DMPCX_PAL_COLORS : img->ncolors;
7114ea4c3c42 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1948
diff changeset
1025 for (int i = 0; i < ncolors; i++)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1026 {
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1027 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
1028 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
1029 hdr.colorMap[i].b = img->pal[i].b;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1030 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1031 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1032 hdr.manufacturer = 10;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1033 hdr.version = 5;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1034 hdr.encoding = 1;
1302
38614c07c2e2 Now with "almost" working 24bit PCX output. Almost.
Matti Hamalainen <ccr@tnsp.org>
parents: 1301
diff changeset
1035 hdr.hres = img->width * spec.scaleX;
38614c07c2e2 Now with "almost" working 24bit PCX output. Almost.
Matti Hamalainen <ccr@tnsp.org>
parents: 1301
diff changeset
1036 hdr.vres = img->height * spec.scaleY;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1037 hdr.xmin = hdr.ymin = 0;
1302
38614c07c2e2 Now with "almost" working 24bit PCX output. Almost.
Matti Hamalainen <ccr@tnsp.org>
parents: 1301
diff changeset
1038 hdr.xmax = (img->width * spec.scaleX) - 1;
38614c07c2e2 Now with "almost" working 24bit PCX output. Almost.
Matti Hamalainen <ccr@tnsp.org>
parents: 1301
diff changeset
1039 hdr.ymax = (img->height * spec.scaleY) - 1;
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1040 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
1041 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
1042 hdr.vScreenSize = hdr.vres;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1043
1291
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
1044 // TODO XXX .. maybe actually compute these asdf
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
1045 hdr.bitsPerPlane = 8;
1302
38614c07c2e2 Now with "almost" working 24bit PCX output. Almost.
Matti Hamalainen <ccr@tnsp.org>
parents: 1301
diff changeset
1046 hdr.nplanes = dmImageGetBytesPerPixel(spec.format);
1291
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
1047
1302
38614c07c2e2 Now with "almost" working 24bit PCX output. Almost.
Matti Hamalainen <ccr@tnsp.org>
parents: 1301
diff changeset
1048 res = img->width * spec.scaleX;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1049 hdr.bpl = res / 2;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1050 if (res % 2) hdr.bpl++;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1051 hdr.bpl *= 2;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1052
1297
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
1053 dmMsg(2,
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
1054 "PCX: xmin=%d, ymin=%d, xmax=%d, ymax=%d, res=%dx%d, scr=%dx%d\n",
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
1055 hdr.xmin, hdr.ymin, hdr.xmax, hdr.ymax,
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
1056 hdr.hres, hdr.vres,
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
1057 hdr.hScreenSize, hdr.vScreenSize);
5bd64397453b More work on PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1296
diff changeset
1058
2065
451980580189 Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 2064
diff changeset
1059 dmMsg(2, "PCX: nplanes=%d, bpp=%d, bpl=%d, colfmt=%d, planar=%s\n",
1302
38614c07c2e2 Now with "almost" working 24bit PCX output. Almost.
Matti Hamalainen <ccr@tnsp.org>
parents: 1301
diff changeset
1060 hdr.nplanes, hdr.bitsPerPlane, hdr.bpl,
2065
451980580189 Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 2064
diff changeset
1061 spec.format,
1302
38614c07c2e2 Now with "almost" working 24bit PCX output. Almost.
Matti Hamalainen <ccr@tnsp.org>
parents: 1301
diff changeset
1062 spec.planar ? "yes" : "no"
38614c07c2e2 Now with "almost" working 24bit PCX output. Almost.
Matti Hamalainen <ccr@tnsp.org>
parents: 1301
diff changeset
1063 );
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1064
1291
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
1065 // TODO XXX this is also bogus
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1066 pcx.bufLen = hdr.bpl * 4;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1067 if ((pcx.buf = dmMalloc(pcx.bufLen)) == NULL)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1068 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1069 res = dmError(DMERR_MALLOC,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1070 "PCX: Could not allocate %d bytes for RLE compression buffer.\n",
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1071 pcx.bufLen);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1072 goto error;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1073 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1074
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1075 // Write PCX header
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1076 if (!dmf_write_byte(pcx.fp, hdr.manufacturer) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1077 !dmf_write_byte(pcx.fp, hdr.version) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1078 !dmf_write_byte(pcx.fp, hdr.encoding) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1079 !dmf_write_byte(pcx.fp, hdr.bitsPerPlane))
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1080 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1081 res = dmError(DMERR_FWRITE,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1082 "PCX: Could not write basic header data.\n");
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 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1085
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1086 if (!dmf_write_le16(pcx.fp, hdr.xmin) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1087 !dmf_write_le16(pcx.fp, hdr.ymin) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1088 !dmf_write_le16(pcx.fp, hdr.xmax) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1089 !dmf_write_le16(pcx.fp, hdr.ymax) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1090 !dmf_write_le16(pcx.fp, hdr.hres) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1091 !dmf_write_le16(pcx.fp, hdr.vres))
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1092 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1093 res = dmError(DMERR_FWRITE,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1094 "PCX: Could not write image dimensions.\n");
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1095 goto error;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1096 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1097
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1098 if (!dmf_write_str(pcx.fp, (Uint8 *) &hdr.colorMap, sizeof(hdr.colorMap)))
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1099 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1100 res = dmError(DMERR_FWRITE,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1101 "PCX: Could not write colormap.\n");
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1102 goto error;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1103 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1104
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1105 if (!dmf_write_byte(pcx.fp, hdr.reserved) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1106 !dmf_write_byte(pcx.fp, hdr.nplanes) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1107 !dmf_write_le16(pcx.fp, hdr.bpl) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1108 !dmf_write_le16(pcx.fp, hdr.palInfo) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1109 !dmf_write_le16(pcx.fp, hdr.hScreenSize) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1110 !dmf_write_le16(pcx.fp, hdr.vScreenSize) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1111 !dmf_write_str(pcx.fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler)))
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1112 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1113 res = dmError(DMERR_FWRITE,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1114 "PCX: Could not write header remainder.\n");
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1115 goto error;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1116 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1117
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1118 // Write image data
1302
38614c07c2e2 Now with "almost" working 24bit PCX output. Almost.
Matti Hamalainen <ccr@tnsp.org>
parents: 1301
diff changeset
1119 res = dmWriteImageData(img, (void *) &pcx, dmWritePCXRow, &spec);
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1120
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1121 // Write VGA palette
2065
451980580189 Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 2064
diff changeset
1122 if (spec.format == DM_COLFMT_PALETTE)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1123 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1124 int i;
1294
9f2117f1584a Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1292
diff changeset
1125 dmMsg(2, "PCX: Writing palette of %d active entries.\n", img->ncolors);
9f2117f1584a Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1292
diff changeset
1126
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1127 dmf_write_byte(pcx.fp, 0x0C);
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1128
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1129 for (i = 0; i < img->ncolors; i++)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1130 {
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1131 if (!dmf_write_byte(pcx.fp, img->pal[i].r) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1132 !dmf_write_byte(pcx.fp, img->pal[i].g) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1133 !dmf_write_byte(pcx.fp, img->pal[i].b))
1294
9f2117f1584a Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1292
diff changeset
1134 {
9f2117f1584a Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1292
diff changeset
1135 res = dmError(DMERR_FWRITE,
9f2117f1584a Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1292
diff changeset
1136 "PCX: Could not write palette data.\n");
9f2117f1584a Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1292
diff changeset
1137 goto error;
9f2117f1584a Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1292
diff changeset
1138 }
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1139 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1140
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1141 // Pad the palette, if necessary
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1142 for (; i < 256; i++)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1143 {
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1144 if (!dmf_write_byte(pcx.fp, 0) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1145 !dmf_write_byte(pcx.fp, 0) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1146 !dmf_write_byte(pcx.fp, 0))
1294
9f2117f1584a Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1292
diff changeset
1147 {
9f2117f1584a Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1292
diff changeset
1148 res = dmError(DMERR_FWRITE,
9f2117f1584a Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1292
diff changeset
1149 "PCX: Could not write palette data.\n");
9f2117f1584a Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1292
diff changeset
1150 goto error;
9f2117f1584a Improve error checking in PCX writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 1292
diff changeset
1151 }
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1152 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1153 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1154
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1155 error:
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1156 dmFree(pcx.buf);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1157 return res;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1158 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1159
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1160
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1161 static BOOL dmPCXDecodeRLERow(DMResource *fp, Uint8 *buf, const size_t bufLen)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1162 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1163 size_t offs = 0;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1164 do
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1165 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1166 int count;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1167 Uint8 data;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1168
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1169 if (!dmf_read_byte(fp, &data))
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1170 return FALSE;
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 if ((data & 0xC0) == 0xC0)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1173 {
1284
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1174 BOOL skip = FALSE;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1175 count = data & 0x3F;
1284
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1176 if (count == 0)
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1177 {
1285
e4bda4909d72 Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1284
diff changeset
1178 switch (dmGFXErrorMode)
1284
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1179 {
1285
e4bda4909d72 Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1284
diff changeset
1180 case DM_ERRMODE_RECOV_1:
1284
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1181 // Use as literal
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1182 skip = TRUE;
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1183 count = 1;
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1184 break;
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1185
1285
e4bda4909d72 Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1284
diff changeset
1186 case DM_ERRMODE_RECOV_2:
1284
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1187 // Ignore completely
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1188 skip = TRUE;
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1189 break;
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1190
1285
e4bda4909d72 Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1284
diff changeset
1191 case DM_ERRMODE_FAIL:
1284
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1192 default:
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1193 // Error out on "invalid" data
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1194 return FALSE;
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1195 }
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1196 }
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1197
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1198 if (!skip && !dmf_read_byte(fp, &data))
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1199 return FALSE;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1200 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1201 else
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1202 count = 1;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1203
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1204 while (count-- && offs < bufLen)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1205 buf[offs++] = data;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1206
1284
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1207 // 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
1208 if (count > 0 && dmGFXErrorMode == DM_ERRMODE_FAIL)
1284
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1209 return FALSE;
28be63226b05 More work on PCX loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1283
diff changeset
1210
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1211 } while (offs < bufLen);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1212
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1213 return TRUE;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1214 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1215
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1216
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
1217 int dmReadPCXImage(DMResource *fp, DMImage **pimg)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1218 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1219 DMImage *img;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1220 DMPCXData pcx;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1221 DMPCXHeader hdr;
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1222 int res = 0;
1286
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1223 BOOL isPaletted;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1224 pcx.buf = NULL;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1225
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1226 // Read PCX header
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1227 if (!dmf_read_byte(fp, &hdr.manufacturer) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1228 !dmf_read_byte(fp, &hdr.version) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1229 !dmf_read_byte(fp, &hdr.encoding) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1230 !dmf_read_byte(fp, &hdr.bitsPerPlane) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1231 !dmf_read_le16(fp, &hdr.xmin) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1232 !dmf_read_le16(fp, &hdr.ymin) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1233 !dmf_read_le16(fp, &hdr.xmax) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1234 !dmf_read_le16(fp, &hdr.ymax) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1235 !dmf_read_le16(fp, &hdr.hres) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1236 !dmf_read_le16(fp, &hdr.vres) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1237 !dmf_read_str(fp, (Uint8 *) &hdr.colorMap, sizeof(hdr.colorMap)) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1238 !dmf_read_byte(fp, &hdr.reserved) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1239 !dmf_read_byte(fp, &hdr.nplanes) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1240 !dmf_read_le16(fp, &hdr.bpl) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1241 !dmf_read_le16(fp, &hdr.palInfo) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1242 !dmf_read_le16(fp, &hdr.hScreenSize) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1243 !dmf_read_le16(fp, &hdr.vScreenSize) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1244 !dmf_read_str(fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler)))
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1245 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1246 res = dmError(DMERR_FREAD,
1298
f0d6aac3adc4 Some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1297
diff changeset
1247 "PCX: Could not read image header data.\n");
f0d6aac3adc4 Some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1297
diff changeset
1248 goto error;
f0d6aac3adc4 Some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1297
diff changeset
1249 }
f0d6aac3adc4 Some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1297
diff changeset
1250
f0d6aac3adc4 Some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1297
diff changeset
1251 if (hdr.manufacturer != 10 ||
f0d6aac3adc4 Some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1297
diff changeset
1252 hdr.version > 5 ||
f0d6aac3adc4 Some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1297
diff changeset
1253 hdr.encoding != 1)
f0d6aac3adc4 Some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1297
diff changeset
1254 {
f0d6aac3adc4 Some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1297
diff changeset
1255 res = dmError(DMERR_NOT_SUPPORTED,
f0d6aac3adc4 Some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1297
diff changeset
1256 "PCX: Not a PCX file, or unsupported variant.\n");
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1257 goto error;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1258 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1259
1299
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1260 if (hdr.nplanes == 4 && hdr.bitsPerPlane == 4)
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1261 {
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1262 dmMsg(2,
2078
b2f1ce24f81b Be more informative when attempting to figure out broken PCX file.
Matti Hamalainen <ccr@tnsp.org>
parents: 2071
diff changeset
1263 "PCX: Probably invalid combination of nplanes=%d and bpp=%d, attempting to fix ..\n",
b2f1ce24f81b Be more informative when attempting to figure out broken PCX file.
Matti Hamalainen <ccr@tnsp.org>
parents: 2071
diff changeset
1264 hdr.nplanes, hdr.bitsPerPlane);
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1265
1299
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1266 hdr.bitsPerPlane = 1;
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1267 }
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1268
1295
7a986f33895e Fix isPaletted check in PCX reader :P
Matti Hamalainen <ccr@tnsp.org>
parents: 1294
diff changeset
1269 isPaletted = (hdr.bitsPerPlane * hdr.nplanes) <= 8;
1291
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
1270
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
1271 dmMsg(2,
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
1272 "PCX: xmin=%d, ymin=%d, xmax=%d, ymax=%d, res=%dx%d, scr=%dx%d\n",
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
1273 hdr.xmin, hdr.ymin, hdr.xmax, hdr.ymax,
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
1274 hdr.hres, hdr.vres,
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
1275 hdr.hScreenSize, hdr.vScreenSize);
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
1276
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
1277 dmMsg(2,
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
1278 "PCX: nplanes=%d, bpp=%d, bpl=%d, isPaletted=%s\n",
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
1279 hdr.nplanes, hdr.bitsPerPlane, hdr.bpl, isPaletted ? "yes" : "no");
2c4acbc3e7bf More work on libgfx etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 1290
diff changeset
1280
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1281 if (hdr.nplanes < 1 || hdr.nplanes > 8)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1282 {
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1283 res = dmError(DMERR_NOT_SUPPORTED,
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1284 "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
1285 hdr.nplanes);
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1286 goto error;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1287 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1288
1286
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1289 if (!isPaletted)
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1290 {
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1291 res = dmError(DMERR_NOT_SUPPORTED,
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1292 "PCX: Non-indexed (truecolour) PCX images not supported for loading.\n");
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1293 goto error;
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1294 }
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1295
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1296 // Allocate image
1286
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1297 if ((*pimg = img = dmImageAlloc(hdr.xmax - hdr.xmin + 1, hdr.ymax - hdr.ymin + 1,
1801
0562dd55a1f6 s/DM_IFMT_/DM_COLFMT_/g
Matti Hamalainen <ccr@tnsp.org>
parents: 1800
diff changeset
1298 isPaletted ? DM_COLFMT_PALETTE : DM_COLFMT_RGBA,
1299
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1299 // XXX TODO? When/if we ever handle < 8bit indexed correctly, we can use the actual bpp
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1300 // isPaletted ? (hdr.bitsPerPlane * hdr.nplanes) : -1
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1301 -1
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1302 )) == NULL)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1303 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1304 res = dmError(DMERR_MALLOC,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1305 "PCX: Could not allocate image structure.\n");
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1306 goto error;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1307 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1308
1628
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
1309 // Set image aspect ratio
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
1310 if (hdr.hScreenSize > 0 && hdr.vScreenSize > 0 &&
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
1311 hdr.vScreenSize / hdr.hScreenSize != img->height / img->width)
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
1312 img->aspect = (float) hdr.vScreenSize / (float) hdr.hScreenSize;
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
1313
1298
f0d6aac3adc4 Some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1297
diff changeset
1314 // Sanity check bytes per line value
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1315 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
1316 {
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1317 res = dmError(DMERR_MALLOC,
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1318 "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
1319 hdr.bpl, (img->width * hdr.bitsPerPlane) / 8);
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1320 goto error;
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1321 }
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1322
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1323 pcx.bufLen = hdr.nplanes * hdr.bpl;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1324 if ((pcx.buf = dmMalloc(pcx.bufLen)) == NULL)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1325 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1326 res = dmError(DMERR_MALLOC,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1327 "PCX: Could not allocate RLE buffer.\n");
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1328 goto error;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1329 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1330
1299
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1331 dmMsg(2,
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1332 "PCX: bufLen=%d\n",
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1333 pcx.bufLen);
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1334
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1335 // Read image data
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1336 Uint8 *dp = img->data;
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1337 for (int yc = 0; yc < img->height; yc++)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1338 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1339 // 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
1340 if (!dmPCXDecodeRLERow(fp, pcx.buf, pcx.bufLen))
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1341 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1342 res = dmError(DMERR_INVALID_DATA,
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1343 "PCX: Error decoding RLE compressed data.\n");
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1344 goto error;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1345 }
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1346
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1347 // Decode bitplanes
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1348 switch (hdr.bitsPerPlane)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1349 {
1296
228cab109c6a More work on PCX reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1295
diff changeset
1350 case 32:
228cab109c6a More work on PCX reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1295
diff changeset
1351 case 24:
228cab109c6a More work on PCX reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1295
diff changeset
1352 case 16:
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1353 case 8:
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1354 {
1286
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1355 // Actually bytes and bits per plane per pixel ..
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1356 const int bytesPerPlane = hdr.bitsPerPlane / 8;
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1357
1286
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1358 for (int nplane = 0; nplane < hdr.nplanes; nplane++)
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1359 {
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1360 Uint8 *dptr = dp + (nplane * bytesPerPlane),
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1361 *sptr = pcx.buf + (hdr.bpl * nplane);
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1362
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1363 memcpy(dptr, sptr, img->width * bytesPerPlane);
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1364 }
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1365 }
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1366 break;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1367
1280
300a51e98fc3 Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 1279
diff changeset
1368 case 1:
1814
0b7062d874ef Use dmMemset() instead of memset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1810
diff changeset
1369 dmMemset(dp, 0, img->width);
1296
228cab109c6a More work on PCX reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1295
diff changeset
1370
1280
300a51e98fc3 Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 1279
diff changeset
1371 for (int nplane = 0; nplane < hdr.nplanes; nplane++)
300a51e98fc3 Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 1279
diff changeset
1372 {
1296
228cab109c6a More work on PCX reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1295
diff changeset
1373 Uint8 *sptr = pcx.buf + (hdr.bpl * nplane);
1280
300a51e98fc3 Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 1279
diff changeset
1374
300a51e98fc3 Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 1279
diff changeset
1375 for (int xc = 0; xc < img->width; xc++)
1296
228cab109c6a More work on PCX reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1295
diff changeset
1376 {
1299
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1377 const int px = 7 - (xc & 7);
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1378 dp[xc] |= ((sptr[xc / 8] & (1 << px)) >> px) << nplane;
1296
228cab109c6a More work on PCX reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1295
diff changeset
1379 }
1280
300a51e98fc3 Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 1279
diff changeset
1380 }
300a51e98fc3 Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 1279
diff changeset
1381 break;
300a51e98fc3 Bump version and copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 1279
diff changeset
1382
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1383 default:
1285
e4bda4909d72 Make libgfx error mode a global variable, at least for now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1284
diff changeset
1384 res = dmError(DMERR_NOT_SUPPORTED,
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1385 "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
1386 hdr.bitsPerPlane);
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1387 goto error;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1388 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1389
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1390 dp += img->pitch;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1391 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1392
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1393 // Read additional VGA palette, if available
1286
b812fad6f33e Work on libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1285
diff changeset
1394 if (isPaletted)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1395 {
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1396 int ncolors;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1397 Uint8 tmpb;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1398 BOOL read;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1399
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1400 if (!dmf_read_byte(fp, &tmpb) || tmpb != 0x0C)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1401 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1402 read = FALSE;
1299
b0c0be4c76f9 Aaand some more work on PCX loader. Actually works with 4/1 planes/bpp images now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1298
diff changeset
1403 ncolors = DMPCX_PAL_COLORS;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1404 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1405 else
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1406 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1407 read = TRUE;
451
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
1408 ncolors = 256;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1409 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1410
1655
a05e3fcc60ec Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1654
diff changeset
1411 if (!dmImagePaletteAlloc(img, ncolors, -1))
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1412 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1413 res = dmError(DMERR_MALLOC,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1414 "PCX: Could not allocate palette data!\n");
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1415 goto error;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1416 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1417
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1418 if (read)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1419 {
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1420 // Okay, attempt to read the palette data
1296
228cab109c6a More work on PCX reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1295
diff changeset
1421 dmMsg(2, "PCX: Reading palette of %d colors\n", ncolors);
1655
a05e3fcc60ec Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1654
diff changeset
1422 if (!dmPaletteReadData(fp, img->pal, ncolors))
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1423 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1424 res = dmError(DMERR_FREAD,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1425 "PCX: Error reading palette.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1426 goto error;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1427 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1428 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1429 else
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1430 {
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1431 // 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
1432 // the header palette to our internal palette structure.
1296
228cab109c6a More work on PCX reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1295
diff changeset
1433 dmMsg(2, "PCX: Initializing palette from header of %d colors\n", ncolors);
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1434 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
1435 {
1279
0d3f5f44c0c4 Somewhat improve PCX read support in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 1265
diff changeset
1436 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
1437 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
1438 img->pal[i].b = hdr.colorMap[i].b;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1439 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1440 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1441 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1442
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1443 error:
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1444 dmFree(pcx.buf);
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1445 return res;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1446 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1447
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1448
1613
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
1449 //
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
1450 // IFF ILBM / PBM format
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
1451 //
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1452 #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
1453 #define IFF_ID_ILBM 0x494C424D // "ILBM"
464
358776103ceb Add support for IFF PBMs.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
1454 #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
1455 #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
1456 #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
1457 #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
1458 #define IFF_ID_CAMG 0x43414D47 // "CAMG"
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1459 #define IFF_ID_ACBM 0x4143424D // "ACBM"
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1460 #define IFF_ID_ABIT 0x41424954 // "ABIT"
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1461
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1462 #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
1463 #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
1464 #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
1465 #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
1466
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1467 #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
1468 #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
1469
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
1470 #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
1471 #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
1472 #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
1473
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1474 typedef struct
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1475 {
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1476 Uint32 id;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1477 Uint32 size;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1478 int count;
1656
2de258f2eb2e Get rid of a basically redundant function in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1655
diff changeset
1479 char idStr[6];
1895
eb03869a10d3 Clean up the IFF reader and make it more robust.
Matti Hamalainen <ccr@tnsp.org>
parents: 1894
diff changeset
1480 off_t offs;
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1481 } DMIFFChunk;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1482
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1483
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1484 typedef struct
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1485 {
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1486 Uint16 w, h;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1487 Sint16 x, y;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1488 Uint8 nplanes;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1489 Uint8 masking;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1490 Uint8 compression;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1491 Uint8 pad1;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1492 Uint16 transp;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1493 Uint8 xasp, yasp;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1494 Sint16 pagew, pageh;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1495 } DMIFFBMHD;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1496
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1497
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1498 typedef struct
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1499 {
1810
c9197a038e8e Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1808
diff changeset
1500 DMIFFChunk chFORM, chBMHD, chCMAP, chBODY;
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1501 DMIFFBMHD bmhd;
1810
c9197a038e8e Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1808
diff changeset
1502 Uint32 camg;
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1503 int ncolors;
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1504 DMColor *pal;
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1505 Uint32 idsig;
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
1506 char *idstr;
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1507 } DMIFF;
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
2068
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1510 static int fmtProbeIFF(const Uint8 *buf, const size_t len, const Uint32 id)
1613
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
1511 {
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
1512 if (len > 32 &&
2068
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1513 DM_BE32_TO_NATIVE(*(Uint32 *) (buf + 0)) == IFF_ID_FORM &&
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1514 DM_BE32_TO_NATIVE(*(Uint32 *) (buf + 8)) == id)
1613
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
1515 {
2063
bd109c0a7b88 Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2057
diff changeset
1516 if (DM_BE32_TO_NATIVE(*(Uint32 *) (buf + 12)) == IFF_ID_BMHD)
1613
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
1517 return DM_PROBE_SCORE_MAX;
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
1518 else
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
1519 return DM_PROBE_SCORE_GOOD;
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
1520 }
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
1521
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
1522 return DM_PROBE_SCORE_FALSE;
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
1523 }
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
1524
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
1525
2068
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1526 static int fmtProbeIFF_ILBM(const Uint8 *buf, const size_t len)
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1527 {
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1528 return fmtProbeIFF(buf, len, IFF_ID_ILBM);
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1529 }
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1530
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1531
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1532 static int fmtProbeIFF_PBM(const Uint8 *buf, const size_t len)
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1533 {
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1534 return fmtProbeIFF(buf, len, IFF_ID_PBM);
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1535 }
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1536
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1537
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1538 static int fmtProbeIFF_ACBM(const Uint8 *buf, const size_t len)
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1539 {
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1540 return fmtProbeIFF(buf, len, IFF_ID_ACBM);
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1541 }
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1542
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
1543
1657
2b6dbdd602b5 Split IFF chunk id string from id creation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1656
diff changeset
1544 static void dmMakeIFFChunkIDStr(DMIFFChunk *chunk)
2b6dbdd602b5 Split IFF chunk id string from id creation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1656
diff changeset
1545 {
2b6dbdd602b5 Split IFF chunk id string from id creation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1656
diff changeset
1546 chunk->idStr[0] = (chunk->id >> 24) & 0xff;
2b6dbdd602b5 Split IFF chunk id string from id creation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1656
diff changeset
1547 chunk->idStr[1] = (chunk->id >> 16) & 0xff;
2b6dbdd602b5 Split IFF chunk id string from id creation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1656
diff changeset
1548 chunk->idStr[2] = (chunk->id >> 8) & 0xff;
2b6dbdd602b5 Split IFF chunk id string from id creation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1656
diff changeset
1549 chunk->idStr[3] = (chunk->id) & 0xff;
2b6dbdd602b5 Split IFF chunk id string from id creation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1656
diff changeset
1550 chunk->idStr[4] = 0;
2b6dbdd602b5 Split IFF chunk id string from id creation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1656
diff changeset
1551 }
2b6dbdd602b5 Split IFF chunk id string from id creation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1656
diff changeset
1552
2b6dbdd602b5 Split IFF chunk id string from id creation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1656
diff changeset
1553
1654
92656ad7f706 Improve IFF reader error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1641
diff changeset
1554 static int dmReadIFFChunkHdr(DMResource *fp, DMIFFChunk *chunk)
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1555 {
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1556 if (!dmf_read_be32(fp, &chunk->id) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1557 !dmf_read_be32(fp, &chunk->size))
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1558 {
1654
92656ad7f706 Improve IFF reader error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1641
diff changeset
1559 return dmError(DMERR_FREAD,
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: 1887
diff changeset
1560 "IFF: Could not read IFF chunk header.\n");
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1561 }
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1562 else
1656
2de258f2eb2e Get rid of a basically redundant function in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1655
diff changeset
1563 {
1895
eb03869a10d3 Clean up the IFF reader and make it more robust.
Matti Hamalainen <ccr@tnsp.org>
parents: 1894
diff changeset
1564 chunk->offs = dmftell(fp);
1657
2b6dbdd602b5 Split IFF chunk id string from id creation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1656
diff changeset
1565 dmMakeIFFChunkIDStr(chunk);
1654
92656ad7f706 Improve IFF reader error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1641
diff changeset
1566 return DMERR_OK;
1656
2de258f2eb2e Get rid of a basically redundant function in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1655
diff changeset
1567 }
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1568 }
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1569
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1570
1895
eb03869a10d3 Clean up the IFF reader and make it more robust.
Matti Hamalainen <ccr@tnsp.org>
parents: 1894
diff changeset
1571 static int dmSkipIFFChunkRest(DMResource *fp, const DMIFFChunk *chunk)
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1572 {
1901
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1573 off_t read = dmftell(fp) - chunk->offs,
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1574 size = chunk->size;
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1575
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1576 if (size & 1)
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
1577 {
1901
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1578 dmMsg(3, "IFF: Chunk size %d is uneven, adjusting to %d.\n",
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1579 size, size + 1);
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1580 size++;
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1581 }
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1582
1901
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1583 if (size > read)
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1584 {
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1585 dmMsg(3, "IFF: Skipping %d bytes (%d of %d consumed)\n",
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1586 size - read, read, size);
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1587
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1588 if (dmfseek(fp, size - read, SEEK_CUR) != 0)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1589 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1590 return dmError(DMERR_FSEEK,
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: 1887
diff changeset
1591 "IFF: Failed to skip chunk end.\n");
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1592 }
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
1593 }
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1594
1901
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1595 return DMERR_OK;
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1596 }
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1597
1656
2de258f2eb2e Get rid of a basically redundant function in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1655
diff changeset
1598
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1599 static int dmCheckIFFChunk(DMIFFChunk *dest, const DMIFFChunk *chunk,
445
1d65efb29986 Simplify.
Matti Hamalainen <ccr@tnsp.org>
parents: 444
diff changeset
1600 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
1601 {
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1602 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
1603 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1604 return dmError(DMERR_INVALID_DATA,
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: 1887
diff changeset
1605 "IFF: Multiple instances of chunk %s found.\n",
1656
2de258f2eb2e Get rid of a basically redundant function in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1655
diff changeset
1606 chunk->idStr);
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1607 }
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1608
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1609 dest->count++;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1610
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1611 if (chunk->size < minSize)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1612 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1613 return dmError(DMERR_OUT_OF_DATA,
1907
571109a14967 Add some more informative messages in IFF loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1906
diff changeset
1614 "IFF: Chunk %s is too small (%d < %d).\n",
571109a14967 Add some more informative messages in IFF loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1906
diff changeset
1615 chunk->idStr, chunk->size, minSize);
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1616 }
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1617
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1618 return DMERR_OK;
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1619 }
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1620
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1621
2055
6c6a4ea67540 Make two functions static and one not.
Matti Hamalainen <ccr@tnsp.org>
parents: 2054
diff changeset
1622 BOOL dmIFFDecodeByteRun1Row(DMResource *fp, Uint8 *buf, const size_t bufLen)
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
1623 {
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1624 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
1625 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
1626 {
1905
425259977bc5 Don't use signed arithmatic in IFF ByteRun1 decoder.
Matti Hamalainen <ccr@tnsp.org>
parents: 1904
diff changeset
1627 Uint8 data, ucount;
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
1628
1905
425259977bc5 Don't use signed arithmatic in IFF ByteRun1 decoder.
Matti Hamalainen <ccr@tnsp.org>
parents: 1904
diff changeset
1629 if (!dmf_read_byte(fp, &ucount))
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
1630 return FALSE;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1631
1905
425259977bc5 Don't use signed arithmatic in IFF ByteRun1 decoder.
Matti Hamalainen <ccr@tnsp.org>
parents: 1904
diff changeset
1632 if (ucount == 0x80)
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
1633 {
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1634 if (!dmf_read_byte(fp, &data))
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
1635 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
1636 }
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1637 else
1905
425259977bc5 Don't use signed arithmatic in IFF ByteRun1 decoder.
Matti Hamalainen <ccr@tnsp.org>
parents: 1904
diff changeset
1638 if (ucount & 0x80)
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
1639 {
1905
425259977bc5 Don't use signed arithmatic in IFF ByteRun1 decoder.
Matti Hamalainen <ccr@tnsp.org>
parents: 1904
diff changeset
1640 Uint8 count = (ucount ^ 0xff) + 2;
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1641 if (!dmf_read_byte(fp, &data))
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
1642 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
1643
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1644 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
1645 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
1646 }
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1647 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
1648 {
1905
425259977bc5 Don't use signed arithmatic in IFF ByteRun1 decoder.
Matti Hamalainen <ccr@tnsp.org>
parents: 1904
diff changeset
1649 Uint8 count = ucount + 1;
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
1650 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
1651 {
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1652 if (!dmf_read_byte(fp, &data))
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
1653 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
1654
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1655 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
1656 }
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1657 }
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1658 } 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
1659
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1660 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
1661 }
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1662
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1663
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1664 static BOOL dmIFFReadOneRow(DMResource *fp, DMIFF *iff, Uint8 *buf, const size_t bufLen)
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
1665 {
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1666 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
1667 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
1668 else
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1669 return dmf_read_str(fp, buf, bufLen);
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 }
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1671
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1672
1622
009a3e3c54bf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1621
diff changeset
1673 static inline Uint8 dmDecodeBit(const Uint8 *buf, const int xc)
1619
dacf4c2f7a86 Remove slight code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents: 1618
diff changeset
1674 {
dacf4c2f7a86 Remove slight code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents: 1618
diff changeset
1675 return (buf[xc / 8] >> (7 - (xc & 7))) & 1;
dacf4c2f7a86 Remove slight code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents: 1618
diff changeset
1676 }
dacf4c2f7a86 Remove slight code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents: 1618
diff changeset
1677
dacf4c2f7a86 Remove slight code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents: 1618
diff changeset
1678
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1679 static int dmDecodeIFFBody(DMResource *fp, DMIFF *iff, DMImage *img)
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1680 {
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1681 Uint8 *buf = NULL;
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1682 size_t bufLen = 0;
1618
4c96181c9c09 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1616
diff changeset
1683 int res = DMERR_OK;
1659
99b8ab61dc1b Micro-optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1658
diff changeset
1684 const int nplanes = iff->bmhd.nplanes;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1685
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1686 if (iff->idsig == IFF_ID_ILBM)
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1687 {
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1688 bufLen = ((img->width + 15) / 16) * 2;
1906
09d46bf9fe71 Use a consistent message between IFF encoding and decoding.
Matti Hamalainen <ccr@tnsp.org>
parents: 1905
diff changeset
1689 dmMsg(2, "IFF: Line / plane row size %d bytes.\n", bufLen);
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1690
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1691 }
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1692 else
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1693 if (iff->idsig == IFF_ID_ACBM)
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1694 {
2082
a0a6f5a3fbbf Fix plane buffer size calculation for IFF ACBM to round up to nearest byte (minimum of 1).
Matti Hamalainen <ccr@tnsp.org>
parents: 2081
diff changeset
1695 bufLen = (img->width * img->height + 7) / 8;
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1696 dmMsg(2, "IFF: Plane buffer size %d bytes.\n", bufLen);
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1697 }
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1698
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1699 if (bufLen > 0 && (buf = dmMalloc(bufLen)) == NULL)
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1700 return DMERR_MALLOC;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1701
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
1702 // Decode the chunk
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1703 if (iff->idsig == IFF_ID_ACBM)
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1704 {
2081
a14286e2710e Initialize the destination image data in case of IFF ACBM loading.
Matti Hamalainen <ccr@tnsp.org>
parents: 2078
diff changeset
1705 // Initialize destination image data
a14286e2710e Initialize the destination image data in case of IFF ACBM loading.
Matti Hamalainen <ccr@tnsp.org>
parents: 2078
diff changeset
1706 dmMemset(img->data, 0, img->size);
a14286e2710e Initialize the destination image data in case of IFF ACBM loading.
Matti Hamalainen <ccr@tnsp.org>
parents: 2078
diff changeset
1707
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1708 for (int plane = 0; plane < nplanes; plane++)
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1709 {
2069
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
1710 // Decompress or read data
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
1711 if (!dmIFFReadOneRow(fp, iff, buf, bufLen))
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1712 {
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1713 res = dmError(DMERR_FREAD,
2069
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
1714 "IFF: Error in reading ACBM image plane #%d.\n",
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1715 plane);
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1716 goto out;
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1717 }
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1718
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1719 for (int yc = 0; yc < img->height; yc++)
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1720 {
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1721 Uint8 *dp = img->data + (yc * img->pitch);
2067
430c010d97c1 Fix hardcoding in ACBM decoding.
Matti Hamalainen <ccr@tnsp.org>
parents: 2065
diff changeset
1722 Uint8 *sp = buf + (yc * img->width) / 8;
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1723 for (int xc = 0; xc < img->width; xc++)
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1724 {
2067
430c010d97c1 Fix hardcoding in ACBM decoding.
Matti Hamalainen <ccr@tnsp.org>
parents: 2065
diff changeset
1725 dp[xc] |= dmDecodeBit(sp, xc) << plane;
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1726 }
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1727 }
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1728 }
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1729 }
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1730 else
1618
4c96181c9c09 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1616
diff changeset
1731 for (int yc = 0; yc < img->height; 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
1732 {
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1733 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
1734
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1735 if (iff->idsig == IFF_ID_ILBM)
447
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1736 {
1907
571109a14967 Add some more informative messages in IFF loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1906
diff changeset
1737 // Clear planar decoding buffer
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1738 dmMemset(dp, 0, img->pitch);
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1739
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1740 for (int plane = 0; plane < nplanes; plane++)
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 {
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1742 // Decompress or read data
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1743 if (!dmIFFReadOneRow(fp, iff, buf, bufLen))
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1744 {
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1745 res = dmError(DMERR_FREAD,
2069
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
1746 "IFF: Error in reading ILBM image plane #%d @ %d.\n",
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1747 plane, yc);
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1748 goto out;
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1749 }
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1750
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1751 // Decode bitplane
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1752 for (int xc = 0; xc < img->width; xc++)
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1753 dp[xc] |= dmDecodeBit(buf, xc) << plane;
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
1754 }
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1755
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1756 // Read mask data
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1757 if (iff->bmhd.masking == IFF_MASK_HAS_MASK)
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1758 {
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1759 // Decompress or read data
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1760 if (!dmIFFReadOneRow(fp, iff, buf, bufLen))
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1761 {
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1762 res = dmError(DMERR_FREAD,
2069
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
1763 "IFF: Error in reading ILBM mask plane.\n");
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1764 goto out;
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1765 }
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
1766
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1767 // Decode mask
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1768 for (int xc = 0; xc < img->width; xc++)
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1769 {
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1770 const Uint8 data = dmDecodeBit(buf, xc);
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1771
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1772 // Black out any pixels with mask bit 0
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1773 if (!data)
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1774 dp[xc] = img->ctransp < 0 ? 0 : img->ctransp;
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1775 }
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1776 }
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1777 }
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1778 else
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1779 if (iff->idsig == IFF_ID_PBM)
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
1780 {
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1781 if (!dmIFFReadOneRow(fp, iff, dp, img->width))
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
1782 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1783 res = dmError(DMERR_FREAD,
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1784 "IFF: Error reading PBM image row #%d.\n", yc);
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1785 goto out;
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
1786 }
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1787 }
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1788 }
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
1789
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1790 out:
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
1791 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
1792 return res;
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1793 }
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1794
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1795
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: 1887
diff changeset
1796 int dmReadIFFImage(DMResource *fp, DMImage **pimg)
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1797 {
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1798 DMIFFChunk chunk;
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1799 DMIFF iff;
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1800 BOOL parsed = FALSE;
1627
d0e626e039bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1626
diff changeset
1801 int res = DMERR_OK;
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1802
1167
848a88ce7a57 Use dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1152
diff changeset
1803 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
1804
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1805 // Read IFF FORM header
1654
92656ad7f706 Improve IFF reader error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1641
diff changeset
1806 if ((res = dmReadIFFChunkHdr(fp, &chunk)) != DMERR_OK)
92656ad7f706 Improve IFF reader error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1641
diff changeset
1807 return res;
92656ad7f706 Improve IFF reader error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1641
diff changeset
1808
92656ad7f706 Improve IFF reader error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1641
diff changeset
1809 if (chunk.id != IFF_ID_FORM || chunk.size < 32)
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1810 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1811 return dmError(DMERR_NOT_SUPPORTED,
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: 1887
diff changeset
1812 "IFF: Not a IFF file (%08X vs %08X / %d).\n",
1265
4e074b9b4789 Improve IFF FORM validity error message.
Matti Hamalainen <ccr@tnsp.org>
parents: 1264
diff changeset
1813 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
1814 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1815
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: 1887
diff changeset
1816 // Check IFF ILBM/PBM signature
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1817 if (!dmf_read_be32(fp, &iff.idsig) ||
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1818 (iff.idsig != IFF_ID_ILBM &&
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1819 iff.idsig != IFF_ID_PBM &&
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1820 iff.idsig != IFF_ID_ACBM))
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1821 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1822 return dmError(DMERR_INVALID_DATA,
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1823 "IFF: Not a IFF ILBM/PBM/ACBM file.\n");
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1824 }
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1825
2023
362fb8295f0c Be more informative about the actual sub-format of the IFF image (PBM/ILBM).
Matti Hamalainen <ccr@tnsp.org>
parents: 2022
diff changeset
1826 dmMsg(3, "IFF: FORM is %s format image, with size %d bytes.\n",
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
1827 iff.idsig == IFF_ID_ILBM ? "ILBM" :
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
1828 (iff.idsig == IFF_ID_PBM ? "PBM" : "ACBM"),
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1829 chunk.size);
1907
571109a14967 Add some more informative messages in IFF loader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1906
diff changeset
1830
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1831 while (!parsed && !dmfeof(fp))
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1832 {
1895
eb03869a10d3 Clean up the IFF reader and make it more robust.
Matti Hamalainen <ccr@tnsp.org>
parents: 1894
diff changeset
1833 // Read chunk header
1654
92656ad7f706 Improve IFF reader error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1641
diff changeset
1834 if ((res = dmReadIFFChunkHdr(fp, &chunk)) != DMERR_OK)
92656ad7f706 Improve IFF reader error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1641
diff changeset
1835 return res;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1836
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1837 switch (chunk.id)
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1838 {
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1839 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
1840 // 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
1841 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
1842 return res;
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1843
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
1844 // Read BMHD data
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1845 if (!dmf_read_be16(fp, &iff.bmhd.w) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1846 !dmf_read_be16(fp, &iff.bmhd.h) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1847 !dmf_read_be16(fp, (Uint16 *) &iff.bmhd.x) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1848 !dmf_read_be16(fp, (Uint16 *) &iff.bmhd.y) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1849 !dmf_read_byte(fp, &iff.bmhd.nplanes) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1850 !dmf_read_byte(fp, &iff.bmhd.masking) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1851 !dmf_read_byte(fp, &iff.bmhd.compression) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1852 !dmf_read_byte(fp, &iff.bmhd.pad1) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1853 !dmf_read_be16(fp, &iff.bmhd.transp) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1854 !dmf_read_byte(fp, &iff.bmhd.xasp) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1855 !dmf_read_byte(fp, &iff.bmhd.yasp) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1856 !dmf_read_be16(fp, (Uint16 *) &iff.bmhd.pagew) ||
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1857 !dmf_read_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
1858 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1859 return dmError(DMERR_FREAD,
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: 1887
diff changeset
1860 "IFF: 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
1861 }
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1862
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: 1887
diff changeset
1863 dmMsg(1, "IFF: BMHD %d x %d @ %d, %d : nplanes=%d, comp=%d, mask=%d, transp=%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
1864 iff.bmhd.w, iff.bmhd.h, iff.bmhd.x, iff.bmhd.y,
1655
a05e3fcc60ec Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1654
diff changeset
1865 iff.bmhd.nplanes, iff.bmhd.compression, iff.bmhd.masking,
a05e3fcc60ec Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1654
diff changeset
1866 iff.bmhd.transp);
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1867
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
1868 // 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
1869 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
1870 (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
1871 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
1872 (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
1873 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
1874 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
1875 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1876 return dmError(DMERR_NOT_SUPPORTED,
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: 1887
diff changeset
1877 "IFF: 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
1878 }
2069
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
1879
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
1880 if (iff.idsig == IFF_ID_ACBM &&
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
1881 iff.bmhd.compression != IFF_COMP_NONE)
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
1882 {
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
1883 dmMsg(1, "IFF: ACBM image with compression != none (%d). Ignoring.\n",
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
1884 iff.bmhd.compression);
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
1885
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
1886 iff.bmhd.compression = IFF_COMP_NONE;
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
1887 }
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1888 break;
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1889
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1890
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1891 case IFF_ID_CMAP:
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1892 // 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
1893 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
1894 return res;
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1895
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1896 // Check for sanity
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1897 if (chunk.size % 3 != 0)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1898 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1899 // Non-fatal
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1900 dmError(DMERR_INVALID_DATA,
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: 1887
diff changeset
1901 "IFF: CMAP chunk size not divisible by 3, possibly broken file.\n");
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1902 }
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1903
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1904 iff.ncolors = chunk.size / 3;
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: 1887
diff changeset
1905 dmMsg(2, "IFF: CMAP %d entries (%d bytes)\n",
1624
9a8395b56d1a Remove unused message argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1623
diff changeset
1906 iff.ncolors, chunk.size);
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1907
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1908 if (iff.bmhd.nplanes > 0 && iff.ncolors != 1 << iff.bmhd.nplanes)
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: 1887
diff changeset
1909 dmMsg(2, "IFF: 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
1910
451
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
1911 // Read palette
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
1912 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
1913 {
461
c7a3aacbd55e Implement transparent color in IFF ILBM reader and ctransp setting in PNG reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
1914 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
1915 (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
1916 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1917 return dmError(DMERR_MALLOC,
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: 1887
diff changeset
1918 "IFF: Could not allocate palette data.\n");
451
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
1919 }
1655
a05e3fcc60ec Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1654
diff changeset
1920 if (!dmPaletteReadData(fp, iff.pal, iff.ncolors))
451
fdc91f2a0d27 Modularize palette reading and handling code.
Matti Hamalainen <ccr@tnsp.org>
parents: 449
diff changeset
1921 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1922 return dmError(DMERR_FREAD,
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: 1887
diff changeset
1923 "IFF: 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
1924 }
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1925 }
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1926
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1927 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
1928 parsed = TRUE;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1929 break;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1930
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1931 case IFF_ID_BODY:
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1932 case IFF_ID_ABIT:
1621
9aaa8ee24626 Fix a comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 1620
diff changeset
1933 // Check for multiple occurences of BODY
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1934 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
1935 return res;
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1936
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
1937 // Check for sanity
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1938 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
1939 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1940 return dmError(DMERR_INVALID_DATA,
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1941 "IFF: %s chunk before BMHD?\n", chunk.idStr);
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1942 }
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1943
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
1944 dmMsg(2, "IFF: %s chunk size %d bytes\n", chunk.idStr, chunk.size);
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1945
1626
1793fc1496da Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents: 1625
diff changeset
1946 // Allocate image
1793fc1496da Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents: 1625
diff changeset
1947 if ((*pimg = dmImageAlloc(iff.bmhd.w, iff.bmhd.h,
1801
0562dd55a1f6 s/DM_IFMT_/DM_COLFMT_/g
Matti Hamalainen <ccr@tnsp.org>
parents: 1800
diff changeset
1948 iff.bmhd.nplanes <= 8 ? DM_COLFMT_PALETTE : DM_COLFMT_RGBA,
1626
1793fc1496da Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents: 1625
diff changeset
1949 // XXX TODO? When/if we ever handle < 8bit indexed correctly, we can use the actual bpp
1793fc1496da Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents: 1625
diff changeset
1950 //iff->bmhd.nplanes <= 8 ? iff->bmhd.nplanes : -1
1793fc1496da Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents: 1625
diff changeset
1951 -1
1793fc1496da Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents: 1625
diff changeset
1952 )) == NULL)
1793fc1496da Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents: 1625
diff changeset
1953 return DMERR_MALLOC;
1793fc1496da Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents: 1625
diff changeset
1954
1628
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
1955 // Set image aspect ratio
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
1956 if (iff.bmhd.xasp > 0 && iff.bmhd.yasp > 0)
a549d33d543a Add image aspect ratio information.
Matti Hamalainen <ccr@tnsp.org>
parents: 1627
diff changeset
1957 (*pimg)->aspect = (float) iff.bmhd.yasp / (float) iff.bmhd.xasp;
1626
1793fc1496da Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents: 1625
diff changeset
1958
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
1959 // Decode the body
1903
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1960 if ((res = dmDecodeIFFBody(fp, &iff, *pimg)) != DMERR_OK)
8ad98bc7402c Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1902
diff changeset
1961 return res;
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1962
1895
eb03869a10d3 Clean up the IFF reader and make it more robust.
Matti Hamalainen <ccr@tnsp.org>
parents: 1894
diff changeset
1963 if ((res = dmSkipIFFChunkRest(fp, &chunk)) != DMERR_OK)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1964 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
1965
444
7d588807f91d Clean up the IFF parser a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
1966 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
1967 parsed = TRUE;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1968 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
1969
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
1970 case IFF_ID_CAMG:
1609
c29adf5ce240 Convert libgfx file format routines to use DMResource instead of stdio FILE.
Matti Hamalainen <ccr@tnsp.org>
parents: 1583
diff changeset
1971 if (!dmf_read_be32(fp, &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
1972 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1973 return dmError(DMERR_FREAD,
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: 1887
diff changeset
1974 "IFF: 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
1975 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1976
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: 1887
diff changeset
1977 dmMsg(2, "IFF: 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
1978
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
1979 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
1980 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1981 return dmError(DMERR_NOT_SUPPORTED,
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: 1887
diff changeset
1982 "IFF: 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
1983 }
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
1984 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
1985
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
1986
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1987 default:
1901
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1988 dmMsg(3, "Unknown chunk ID '%s', size %d\n",
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1989 chunk.idStr, chunk.size);
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1990
1901
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1991 if (dmfseek(fp, chunk.size, SEEK_CUR) != 0)
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1992 {
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1993 return dmError(DMERR_FSEEK,
636d3c8bcd35 Fix uneven chunk size handling in IFF reader.
Matti Hamalainen <ccr@tnsp.org>
parents: 1900
diff changeset
1994 "IFF: 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
1995 }
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1996 break;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
1997 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1998
1895
eb03869a10d3 Clean up the IFF reader and make it more robust.
Matti Hamalainen <ccr@tnsp.org>
parents: 1894
diff changeset
1999 if ((res = dmSkipIFFChunkRest(fp, &chunk)) != DMERR_OK)
eb03869a10d3 Clean up the IFF reader and make it more robust.
Matti Hamalainen <ccr@tnsp.org>
parents: 1894
diff changeset
2000 return res;
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
2001 }
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
2002
1948
5790b52c339e Check that we have a CMAP/palette for IFF images that should have it.
Matti Hamalainen <ccr@tnsp.org>
parents: 1945
diff changeset
2003 // Check if we should have a palette
2019
e809e323fdde Check for NULL pointer.
Matti Hamalainen <ccr@tnsp.org>
parents: 2016
diff changeset
2004 if (*pimg != NULL && (*pimg)->format == DM_COLFMT_PALETTE)
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
2005 {
1948
5790b52c339e Check that we have a CMAP/palette for IFF images that should have it.
Matti Hamalainen <ccr@tnsp.org>
parents: 1945
diff changeset
2006 // Check that we DO have a palette ..
5790b52c339e Check that we have a CMAP/palette for IFF images that should have it.
Matti Hamalainen <ccr@tnsp.org>
parents: 1945
diff changeset
2007 if (iff.pal == NULL || iff.ncolors == 0)
5790b52c339e Check that we have a CMAP/palette for IFF images that should have it.
Matti Hamalainen <ccr@tnsp.org>
parents: 1945
diff changeset
2008 {
5790b52c339e Check that we have a CMAP/palette for IFF images that should have it.
Matti Hamalainen <ccr@tnsp.org>
parents: 1945
diff changeset
2009 return dmError(DMERR_INVALID_DATA,
5790b52c339e Check that we have a CMAP/palette for IFF images that should have it.
Matti Hamalainen <ccr@tnsp.org>
parents: 1945
diff changeset
2010 "IFF: A paletted/indexed color image with no CMAP. Bailing out.\n");
5790b52c339e Check that we have a CMAP/palette for IFF images that should have it.
Matti Hamalainen <ccr@tnsp.org>
parents: 1945
diff changeset
2011 }
5790b52c339e Check that we have a CMAP/palette for IFF images that should have it.
Matti Hamalainen <ccr@tnsp.org>
parents: 1945
diff changeset
2012
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
2013 // 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
2014 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
2015 {
834
9623b9b548d4 Reallocation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
2016 void *ptmp;
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
2017 if (iff.idsig != IFF_ID_ILBM)
1625
c8afa3e6c413 Print an error/warning about PBM files that have half-brite set.
Matti Hamalainen <ccr@tnsp.org>
parents: 1624
diff changeset
2018 {
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: 1887
diff changeset
2019 dmErrorMsg("IFF: Non-planar PBM file with Halfbrite enabled! This might not work.\n");
1625
c8afa3e6c413 Print an error/warning about PBM files that have half-brite set.
Matti Hamalainen <ccr@tnsp.org>
parents: 1624
diff changeset
2020 }
834
9623b9b548d4 Reallocation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
2021
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
2022 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
2023 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 930
diff changeset
2024 return dmError(DMERR_NOT_SUPPORTED,
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: 1887
diff changeset
2025 "IFF: 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
2026 }
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
2027
834
9623b9b548d4 Reallocation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
2028 if ((ptmp = dmRealloc(iff.pal, sizeof(DMColor) * iff.ncolors * 2)) == NULL)
9623b9b548d4 Reallocation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
2029 {
9623b9b548d4 Reallocation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
2030 dmFree(iff.pal);
9623b9b548d4 Reallocation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
2031 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
2032 return DMERR_MALLOC;
834
9623b9b548d4 Reallocation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
2033 }
9623b9b548d4 Reallocation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
2034 else
9623b9b548d4 Reallocation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
2035 iff.pal = ptmp;
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
2036
1627
d0e626e039bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1626
diff changeset
2037 for (int i = 0; i < iff.ncolors; i++)
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
2038 {
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
2039 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
2040 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
2041 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
2042 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
2043 }
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
2044 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
2045
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
2046 (*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
2047 (*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
2048 }
0e27860ddcfe Finish initial implementation of IFF ILBM loader. And whoa .. it seems to be working.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
2049
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
2050 return res;
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
2051 }
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
2052
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
2053
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2054 static int dmWriteIFFChunkHdr(DMResource *fp, DMIFFChunk *chunk, const Uint32 id)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2055 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2056 chunk->offs = dmftell(fp);
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2057 chunk->id = id;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2058 dmMakeIFFChunkIDStr(chunk);
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2059
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2060 if (!dmf_write_be32(fp, chunk->id) ||
1942
f171b929b631 Do not reference uninitialized value in IFF writer, although in this case
Matti Hamalainen <ccr@tnsp.org>
parents: 1940
diff changeset
2061 !dmf_write_be32(fp, 0))
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2062 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2063 return dmError(DMERR_FREAD,
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2064 "IFF: Could not write IFF '%s' chunk header.\n",
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2065 chunk->idStr);
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2066 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2067 else
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2068 return DMERR_OK;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2069 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2070
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2071
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2072 static int dmWriteIFFChunkFinish(DMResource *fp, DMIFFChunk *chunk)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2073 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2074 off_t curr = dmftell(fp);
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2075 if (curr < 0)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2076 return dmferror(fp);
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2077
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2078 chunk->size = curr - chunk->offs - (sizeof(Uint32) * 2);
1900
8c6040d5b930 Fix IFF chunk padding.
Matti Hamalainen <ccr@tnsp.org>
parents: 1899
diff changeset
2079 if (chunk->size & 1)
8c6040d5b930 Fix IFF chunk padding.
Matti Hamalainen <ccr@tnsp.org>
parents: 1899
diff changeset
2080 {
2020
57a2527ff63d Fix IFF chunk padding.
Matti Hamalainen <ccr@tnsp.org>
parents: 2019
diff changeset
2081 dmMsg(3, "Padding chunk ID '%s', size %d ++\n",
57a2527ff63d Fix IFF chunk padding.
Matti Hamalainen <ccr@tnsp.org>
parents: 2019
diff changeset
2082 chunk->idStr, chunk->size);
57a2527ff63d Fix IFF chunk padding.
Matti Hamalainen <ccr@tnsp.org>
parents: 2019
diff changeset
2083
1900
8c6040d5b930 Fix IFF chunk padding.
Matti Hamalainen <ccr@tnsp.org>
parents: 1899
diff changeset
2084 if (!dmf_write_byte(fp, 0))
8c6040d5b930 Fix IFF chunk padding.
Matti Hamalainen <ccr@tnsp.org>
parents: 1899
diff changeset
2085 return dmferror(fp);
8c6040d5b930 Fix IFF chunk padding.
Matti Hamalainen <ccr@tnsp.org>
parents: 1899
diff changeset
2086
2020
57a2527ff63d Fix IFF chunk padding.
Matti Hamalainen <ccr@tnsp.org>
parents: 2019
diff changeset
2087 curr++;
1900
8c6040d5b930 Fix IFF chunk padding.
Matti Hamalainen <ccr@tnsp.org>
parents: 1899
diff changeset
2088 }
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2089
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2090 if (dmfseek(fp, chunk->offs, SEEK_SET) < 0)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2091 return dmferror(fp);
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2092
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2093 if (!dmf_write_be32(fp, chunk->id) ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2094 !dmf_write_be32(fp, chunk->size))
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2095 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2096 return dmError(DMERR_FREAD,
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2097 "IFF: Could not write IFF '%s' chunk header.\n",
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2098 chunk->idStr);
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2099 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2100
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2101 if (dmfseek(fp, curr, SEEK_SET) < 0)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2102 return dmferror(fp);
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2103
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2104 return DMERR_OK;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2105 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2106
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2107
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2108 enum
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2109 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2110 DMODE_LIT,
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2111 DMODE_RLE,
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2112 };
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2113
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2114
2055
6c6a4ea67540 Make two functions static and one not.
Matti Hamalainen <ccr@tnsp.org>
parents: 2054
diff changeset
2115 static BOOL dmIFFEncodeByteRun1LIT(DMResource *fp,
2050
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2116 const Uint8 *buf, const size_t offs,
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2117 const size_t count)
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2118 {
2050
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2119 if (count <= 0)
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2120 return TRUE;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2121
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2122 Uint8 tmp = count - 1;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2123
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2124 return
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2125 dmf_write_byte(fp, tmp) &&
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2126 dmf_write_str(fp, buf + offs, count);
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2127 }
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2128
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2129
2055
6c6a4ea67540 Make two functions static and one not.
Matti Hamalainen <ccr@tnsp.org>
parents: 2054
diff changeset
2130 static BOOL dmIFFEncodeByteRun1RLE(DMResource *fp,
2050
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2131 const Uint8 *buf, const size_t offs,
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2132 const size_t count)
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2133 {
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2134 if (count <= 0)
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2135 return TRUE;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2136
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2137 Uint8
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2138 tmp = ((Uint8) count - 2) ^ 0xff,
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2139 data = buf[offs];
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2140
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2141 return
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2142 dmf_write_byte(fp, tmp) &&
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2143 dmf_write_byte(fp, data);
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2144 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2145
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2146
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2147 BOOL dmIFFEncodeByteRun1Row(DMResource *fp, const Uint8 *buf, const size_t bufLen)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2148 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2149 int prev = -1, mode = DMODE_LIT;
2050
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2150 size_t offs, l_offs, r_offs;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2151 BOOL ret = TRUE;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2152
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2153 for (offs = l_offs = r_offs = 0; offs < bufLen; offs++)
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2154 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2155 Uint8 data = buf[offs];
2050
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2156 BOOL flush = FALSE;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2157 int pmode = mode;
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2158
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2159 if (data == prev)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2160 {
2050
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2161 if (mode == DMODE_LIT &&
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2162 offs - r_offs >= 2)
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2163 {
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2164 ret = dmIFFEncodeByteRun1LIT(fp, buf, l_offs, r_offs - l_offs);
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2165 mode = DMODE_RLE;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2166 }
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2167 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2168 else
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2169 {
2050
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2170 if (mode != DMODE_LIT)
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2171 {
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2172 ret = dmIFFEncodeByteRun1RLE(fp, buf, r_offs, offs - r_offs);
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2173 mode = DMODE_LIT;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2174 l_offs = offs;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2175 }
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2176 r_offs = offs;
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2177 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2178
2050
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2179 if (!ret)
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2180 goto out;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2181
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2182 // NOTE! RLE and LIT max are both 128, checked against DP2e
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2183 flush =
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2184 (pmode == DMODE_RLE && offs - r_offs >= 128) ||
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2185 (pmode == DMODE_LIT && offs - l_offs >= 128);
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2186
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2187 // Check for last byte of input
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2188 if (offs == bufLen - 1)
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2189 {
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2190 offs++;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2191 flush = TRUE;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2192 pmode = mode;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2193 }
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2194
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2195 if (flush)
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2196 {
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2197 if (pmode == DMODE_RLE)
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2198 ret = dmIFFEncodeByteRun1RLE(fp, buf, r_offs, offs - r_offs);
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2199 else
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2200 ret = dmIFFEncodeByteRun1LIT(fp, buf, l_offs, offs - l_offs);
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2201
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2202 r_offs = l_offs = offs;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2203 mode = DMODE_LIT;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2204
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2205 if (!ret)
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2206 goto out;
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2207 }
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2208
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2209 prev = data;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2210 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2211
2050
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2212 out:
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2213 return ret;
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2214 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2215
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2216
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2217 static BOOL dmIFFWriteOneRow(DMResource *fp, DMIFF *iff, const Uint8 *buf, const size_t bufLen)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2218 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2219 if (iff->bmhd.compression == IFF_COMP_BYTERUN1)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2220 return dmIFFEncodeByteRun1Row(fp, buf, bufLen);
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2221 else
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2222 return dmf_write_str(fp, buf, bufLen);
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2223 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2224
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2225
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2226 int dmWriteIFFImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2227 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2228 DMIFF iff;
1904
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2229 Uint8 *buf = NULL;
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2230 size_t bufLen;
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2231 int res = DMERR_OK;
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2232
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2233 // XXX: Non-paletted IFF not supported!
2065
451980580189 Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 2064
diff changeset
2234 if (spec->format != DM_COLFMT_PALETTE)
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2235 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2236 return dmError(DMERR_NOT_SUPPORTED,
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2237 "Non-paletted IFF is not supported.\n");
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2238 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2239
2065
451980580189 Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 2064
diff changeset
2240 switch (spec->fmtid)
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2241 {
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2242 case DM_IMGFMT_IFF_ILBM: iff.idsig = IFF_ID_ILBM; iff.idstr = "ILBM"; break;
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2243 case DM_IMGFMT_IFF_PBM : iff.idsig = IFF_ID_PBM; iff.idstr = "PBM"; break;
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2244 case DM_IMGFMT_IFF_ACBM: iff.idsig = IFF_ID_ACBM; iff.idstr = "ACBM"; break;
2065
451980580189 Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 2064
diff changeset
2245 default:
451980580189 Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 2064
diff changeset
2246 return dmError(DMERR_NOT_SUPPORTED,
451980580189 Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 2064
diff changeset
2247 "Invalid IFF format.\n");
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2248 }
2026
b137d324e13f Force 8 bitplanes for IFF PBM images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2025
diff changeset
2249
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2250 // Setup headers
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2251 iff.bmhd.x = 0;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2252 iff.bmhd.y = 0;
1904
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2253 iff.bmhd.w = img->width * spec->scaleX;
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2254 iff.bmhd.h = img->height * spec->scaleY;
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2255 iff.bmhd.pagew = img->width * spec->scaleX;
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2256 iff.bmhd.pageh = img->height * spec->scaleY;
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2257 iff.bmhd.pad1 = 0;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2258 iff.bmhd.xasp = 1; // XXX TODO: compute the xasp/yasp from the img->aspect
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2259 iff.bmhd.yasp = 1;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2260
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2261 iff.camg = 0; // XXX TODO: when/if HAM support
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2262 iff.bmhd.masking = (img->ctransp < 0) ? IFF_MASK_NONE : spec->mask;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2263 iff.bmhd.transp = (img->ctransp >= 0 && spec->mask == IFF_MASK_TRANSP) ? img->ctransp : 0xffff;
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2264 iff.bmhd.nplanes = (iff.idsig == IFF_ID_PBM && spec->nplanes < 8) ? 8 : spec->nplanes;
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2265
2069
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2266 // Apparently ACBM can't/should not use compression .. even though
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2267 // some files in the wild have bmhd.compression != 0 (but are not
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2268 // actually compressed.) To be more compliant with the spec,
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2269 iff.bmhd.compression = (spec->compression && iff.idsig != IFF_ID_ACBM) ? IFF_COMP_BYTERUN1 : IFF_COMP_NONE;
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2270
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2271 dmMsg(2, "IFF: nplanes=%d, comp=%d, mask=%d\n",
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2272 iff.bmhd.nplanes, iff.bmhd.compression, iff.bmhd.masking);
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2273
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2274 // Write IFF FORM header
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2275 if ((res = dmWriteIFFChunkHdr(fp, &iff.chFORM, IFF_ID_FORM)) != DMERR_OK)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2276 goto out;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2277
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2278 // Write IFF ILBM/PBM signature
2053
6dfbe976d740 Implement basic initial support for IFF ACBM format images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2050
diff changeset
2279 if (!dmf_write_be32(fp, iff.idsig))
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2280 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2281 res = dmError(DMERR_FWRITE,
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2282 "IFF: Error writing %s signature.\n",
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2283 iff.idstr);
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2284 goto out;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2285 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2286
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2287 // Write BMHD chunk and data
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2288 if ((res = dmWriteIFFChunkHdr(fp, &iff.chBMHD, IFF_ID_BMHD)) != DMERR_OK ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2289 !dmf_write_be16(fp, iff.bmhd.w) ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2290 !dmf_write_be16(fp, iff.bmhd.h) ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2291 !dmf_write_be16(fp, iff.bmhd.x) ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2292 !dmf_write_be16(fp, iff.bmhd.y) ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2293 !dmf_write_byte(fp, iff.bmhd.nplanes) ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2294 !dmf_write_byte(fp, iff.bmhd.masking) ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2295 !dmf_write_byte(fp, iff.bmhd.compression) ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2296 !dmf_write_byte(fp, iff.bmhd.pad1) ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2297 !dmf_write_be16(fp, iff.bmhd.transp) ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2298 !dmf_write_byte(fp, iff.bmhd.xasp) ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2299 !dmf_write_byte(fp, iff.bmhd.yasp) ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2300 !dmf_write_be16(fp, iff.bmhd.pagew) ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2301 !dmf_write_be16(fp, iff.bmhd.pageh))
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2302 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2303 res = dmError(DMERR_FWRITE,
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2304 "IFF: Error writing BMHD chunk.\n");
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2305 goto out;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2306 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2307
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2308 if ((res = dmWriteIFFChunkFinish(fp, &iff.chBMHD)) != DMERR_OK)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2309 goto out;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2310
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2311 //
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2312 // CMAP
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2313 //
2065
451980580189 Refactor how paletted/indexed formats are handled in libgfx.
Matti Hamalainen <ccr@tnsp.org>
parents: 2064
diff changeset
2314 if (img->ncolors > 0 && spec->format == DM_COLFMT_PALETTE)
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2315 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2316 if ((res = dmWriteIFFChunkHdr(fp, &iff.chCMAP, IFF_ID_CMAP)) != DMERR_OK)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2317 goto out;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2318
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2319 for (int i = 0; i < img->ncolors; i++)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2320 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2321 DMColor *col = &img->pal[i];
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2322 if (!dmf_write_byte(fp, col->r) ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2323 !dmf_write_byte(fp, col->g) ||
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2324 !dmf_write_byte(fp, col->b))
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2325 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2326 res = dmError(DMERR_FWRITE,
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2327 "IFF: Could not write CMAP palette entry #%d.\n", i);
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2328 goto out;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2329 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2330 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2331
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2332 if ((res = dmWriteIFFChunkFinish(fp, &iff.chCMAP)) != DMERR_OK)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2333 goto out;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2334
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2335 dmMsg(2, "IFF: CMAP %d entries (%d bytes)\n",
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2336 img->ncolors, iff.chCMAP.size);
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2337 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2338
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2339 //
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2340 // CAMG
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2341 //
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2342 if ((res = dmWriteIFFChunkHdr(fp, &iff.chCMAP, IFF_ID_CAMG)) != DMERR_OK)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2343 goto out;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2344
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2345 if (!dmf_write_be32(fp, iff.camg))
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2346 {
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2347 return dmError(DMERR_FREAD,
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2348 "IFF: Error writing CAMG chunk.\n");
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2349 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2350
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2351 if ((res = dmWriteIFFChunkFinish(fp, &iff.chCMAP)) != DMERR_OK)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2352 goto out;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2353
2050
416af5a842ec Fixes to the ByteRun1 encoder. Could use some cleanups now, tho.
Matti Hamalainen <ccr@tnsp.org>
parents: 2049
diff changeset
2354
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2355 //
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2356 // Encode the body
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2357 //
2069
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2358 if ((res = dmWriteIFFChunkHdr(fp, &iff.chBODY,
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2359 (iff.idsig == IFF_ID_ACBM) ? IFF_ID_ABIT : IFF_ID_BODY)) != DMERR_OK)
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2360 goto out;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2361
1904
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2362 // Allocate encoding buffer
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2363 if (iff.idsig == IFF_ID_ILBM)
2082
a0a6f5a3fbbf Fix plane buffer size calculation for IFF ACBM to round up to nearest byte (minimum of 1).
Matti Hamalainen <ccr@tnsp.org>
parents: 2081
diff changeset
2364 bufLen = ((img->width * spec->scaleX + 15) / 16) * 2;
1904
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2365 else
2069
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2366 if (iff.idsig == IFF_ID_ACBM)
2082
a0a6f5a3fbbf Fix plane buffer size calculation for IFF ACBM to round up to nearest byte (minimum of 1).
Matti Hamalainen <ccr@tnsp.org>
parents: 2081
diff changeset
2367 bufLen = (img->width * spec->scaleX * img->height * spec->scaleY + 7) / 8;
2069
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2368 else
1904
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2369 bufLen = img->width * spec->scaleX;
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2370
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2371 dmMsg(2, "IFF: Line/plane row size %d bytes.\n", bufLen);
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2372
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2373 if ((buf = dmMalloc(bufLen)) == NULL)
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2374 return DMERR_MALLOC;
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2375
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2376 // Encode the body
2069
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2377 if (iff.idsig == IFF_ID_ACBM)
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2378 {
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2379 for (int plane = 0; plane < iff.bmhd.nplanes; plane++)
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2380 {
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2381 // Encode bitplane
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2382 dmMemset(buf, 0, bufLen);
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2383
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2384 for (int yc = 0; yc < img->height * spec->scaleY; yc++)
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2385 {
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2386 Uint8 *sp = img->data + (yc * img->pitch);
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2387 Uint8 *dp = buf + (yc * img->width * spec->scaleX) / 8;
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2388 for (int xc = 0; xc < img->width * spec->scaleX; xc++)
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2389 dp[xc / 8] |= ((sp[xc / spec->scaleX] >> plane) & 1) << (7 - (xc & 7));
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2390 }
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2391
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2392 if (!dmIFFWriteOneRow(fp, &iff, buf, bufLen))
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2393 {
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2394 res = dmError(DMERR_FWRITE,
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2395 "IFF: Error writing ACBM image plane %d.\n",
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2396 plane);
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2397 goto out;
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2398 }
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2399 }
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2400 }
83a3d05b5c1d Fixes to ACBM support.
Matti Hamalainen <ccr@tnsp.org>
parents: 2068
diff changeset
2401 else
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2402 {
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2403 for (int yc = 0; yc < img->height; yc++)
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2404 for (int yscale = 0; yscale < spec->scaleY; yscale++)
1904
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2405 {
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2406 const Uint8 *sp = img->data + (yc * img->pitch);
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2407
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2408 if (iff.idsig == IFF_ID_ILBM)
1904
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2409 {
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2410 for (int plane = 0; plane < spec->nplanes; plane++)
1904
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2411 {
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2412 // Encode bitplane
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2413 dmMemset(buf, 0, bufLen);
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2414
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2415 for (int xc = 0; xc < img->width * spec->scaleX; xc++)
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2416 buf[xc / 8] |= ((sp[xc / spec->scaleX] >> plane) & 1) << (7 - (xc & 7));
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2417
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2418 // Compress / write data
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2419 if (!dmIFFWriteOneRow(fp, &iff, buf, bufLen))
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2420 {
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2421 res = dmError(DMERR_FWRITE,
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2422 "IFF: Error writing ILBM image plane #%d @ row %d.\n",
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2423 plane, yc);
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2424 goto out;
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2425 }
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2426 }
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2427
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2428 // Write mask data, if any
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2429 if (iff.bmhd.masking == IFF_MASK_HAS_MASK)
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2430 {
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2431 dmMemset(buf, 0, bufLen);
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2432
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2433 for (int xc = 0; xc < img->width * spec->scaleX; xc++)
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2434 buf[xc / 8] |= (sp[xc / spec->scaleX] == img->ctransp) << (7 - (xc & 7));
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2435
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2436 if (!dmIFFWriteOneRow(fp, &iff, buf, bufLen))
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2437 {
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2438 res = dmError(DMERR_FWRITE,
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2439 "IFF: Error writing ILBM mask plane %d.\n", yc);
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2440 goto out;
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2441 }
1904
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2442 }
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2443 }
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2444 else
1904
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2445 {
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2446 for (int xc = 0; xc < img->width * spec->scaleX; xc++)
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2447 buf[xc] = sp[xc / spec->scaleX];
1904
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2448
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2449 if (!dmIFFWriteOneRow(fp, &iff, buf, bufLen))
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2450 {
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2451 res = dmError(DMERR_FWRITE,
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2452 "IFF: Error writing PBM image row #%d.\n", yc);
1904
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2453 goto out;
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2454 }
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2455 }
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2456 }
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2457 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2458
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2459 if ((res = dmWriteIFFChunkFinish(fp, &iff.chBODY)) != DMERR_OK)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2460 goto out;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2461
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2462 // Finish the FORM chunk
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2463 if ((res = dmWriteIFFChunkFinish(fp, &iff.chFORM)) != DMERR_OK)
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2464 goto out;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2465
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2466 out:
1904
5930ff7879b5 Cleanup IFF writer a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1903
diff changeset
2467 dmFree(buf);
1896
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2468 return res;
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2469 }
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2470
f80b2dc77c30 Work begins on IFF ILBM/PBM image writer. It is pretty broken, some things
Matti Hamalainen <ccr@tnsp.org>
parents: 1895
diff changeset
2471
1613
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
2472 //
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
2473 // List of formats
70b04c16aa40 Move format probe functions near to their other functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1609
diff changeset
2474 //
1616
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2475 const DMImageFormat dmImageFormatList[] =
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2476 {
1616
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2477 #ifdef DM_USE_LIBPNG
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2478 {
1616
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2479 "png", "Portable Network Graphics",
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2480 DM_IMGFMT_PNG, DM_FMT_RDWR,
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
2481 fmtProbePNG, dmReadPNGImage, dmWritePNGImage,
1616
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2482 },
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2483 #endif
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2484 {
1616
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2485 "ppm", "Portable PixMap",
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2486 DM_IMGFMT_PPM, DM_FMT_WR,
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
2487 NULL, NULL, dmWritePPMImage,
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2488 },
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2489 {
1616
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2490 "pcx", "Z-Soft Paintbrush",
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2491 DM_IMGFMT_PCX, DM_FMT_RDWR,
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
2492 fmtProbePCX, dmReadPCXImage, dmWritePCXImage,
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2493 },
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2494 {
2071
3dac7a781317 s/"lbm"/"ilbm"/
Matti Hamalainen <ccr@tnsp.org>
parents: 2069
diff changeset
2495 "ilbm", "IFF ILBM (interleaved/old DP2)",
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2496 DM_IMGFMT_IFF_ILBM, DM_FMT_RDWR,
2068
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
2497 fmtProbeIFF_ILBM, dmReadIFFImage, dmWriteIFFImage,
2064
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2498 },
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2499 {
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2500 "pbm", "IFF PBM (DP2e)",
3617ef01c1de Separate ILBM and PBM subformats of IFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 2063
diff changeset
2501 DM_IMGFMT_IFF_PBM, DM_FMT_RDWR,
2068
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
2502 fmtProbeIFF_PBM, dmReadIFFImage, dmWriteIFFImage,
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
2503 },
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
2504 {
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
2505 "acbm", "IFF ACBM (Amiga Basic)",
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
2506 DM_IMGFMT_IFF_ACBM, DM_FMT_RDWR,
e4dc8fbaa5ad Improve IFF probing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2067
diff changeset
2507 fmtProbeIFF_ACBM, dmReadIFFImage, dmWriteIFFImage,
443
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
2508 },
f7c9d1619c74 Beginnings of IFF ILBM reader. Not functional, only chunk parsing,
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
2509 {
1616
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2510 "raw", "Plain bitplaned (planar or non-planar) RAW",
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2511 DM_IMGFMT_RAW, DM_FMT_WR,
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
2512 NULL, NULL, dmWriteRAWImage,
566
d400e32b62d9 Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents: 487
diff changeset
2513 },
d400e32b62d9 Add a slightly different raw output format.
Matti Hamalainen <ccr@tnsp.org>
parents: 487
diff changeset
2514 {
1616
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2515 "araw", "IFFMaster Amiga RAW",
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2516 DM_IMGFMT_ARAW, DM_FMT_WR,
1886
1af79412f249 Remove the stdio FILE support from libgfx API, now only DMResource is supported.
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
2517 NULL, NULL, dmWriteRAWImage,
2047
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
2518 },
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
2519 {
2057
02fa60b27af5 Correct description of 'cdump' format.
Matti Hamalainen <ccr@tnsp.org>
parents: 2055
diff changeset
2520 "cdump", "'C' dump (image data only)",
2047
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
2521 DM_IMGFMT_CDUMP, DM_FMT_WR,
3829c292df02 Add 'cdump' image output format, mainly for debugging purposes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2026
diff changeset
2522 NULL, NULL, dmWriteCDumpImage,
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2523 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2524 };
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2525
1616
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2526 const int ndmImageFormatList = sizeof(dmImageFormatList) / sizeof(dmImageFormatList[0]);
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2527
1616
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2528
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2529 int dmImageProbeGeneric(const Uint8 *buf, const size_t len, const DMImageFormat **pfmt, int *index)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2530 {
1616
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2531 int scoreMax = DM_PROBE_SCORE_FALSE, scoreIndex = -1;
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2532
1616
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2533 for (int i = 0; i < ndmImageFormatList; i++)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2534 {
1616
36d073c45327 Refactor the format handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
2535 const DMImageFormat *fmt = &dmImageFormatList[i];
442
a67600e186d0 Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
2536 if (fmt->probe != NULL)
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2537 {
442
a67600e186d0 Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
2538 int score = fmt->probe(buf, len);
a67600e186d0 Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
2539 if (score > scoreMax)
a67600e186d0 Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
2540 {
a67600e186d0 Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
2541 scoreMax = score;
a67600e186d0 Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
2542 scoreIndex = i;
a67600e186d0 Fix probing to handle NULL probe functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
2543 }
435
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2544 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2545 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2546
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2547 if (scoreIndex >= 0)
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2548 {
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2549 *pfmt = &dmImageFormatList[scoreIndex];
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2550 *index = scoreIndex;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2551 return scoreMax;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2552 }
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2553 else
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2554 return DM_PROBE_SCORE_FALSE;
e4a3f183e463 Modularize some more.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2555 }