1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/***************************************************************************
MDATOPBM: Convert Microdesign area to portable bitmap
Copyright (C) 1999 John Elliott <jce@seasip.demon.co.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
See the file mdaspec.txt for a specification of the MDA format.
******************************************************************************/
#include <string.h>
#include <stdio.h>
#include "pbm.h"
#include "mallocvar.h"
/* Simple MDA -> portable bitmap converter */
typedef unsigned char mdbyte; /* Must be exactly one byte */
static mdbyte header[128]; /* MDA file header */
static bit **data; /* PBM image */
static mdbyte *mdrow; /* MDA row after decompression (MD3 only) */
static int bInvert = 0; /* Invert image? */
static int bScale = 0; /* Scale image? */
static int bAscii = 0; /* Output ASCII PBM? */
static mdbyte
getbyte(FILE * const ifP) {
/* Read a byte from the input stream, with error trapping */
int b;
b = fgetc(ifP);
if (b == EOF)
pm_error("Unexpected end of MDA file");
return (mdbyte)b;
}
static void
renderByte(unsigned int const nInCols,
unsigned int const nOutRows,
unsigned int * const colP,
unsigned int * const xP,
unsigned int * const yP,
int const b) {
/*----------------------------------------------------------------------------
Convert a byte to 8 cells in the destination bitmap
As input
*colP = source column
*xP = destination column
*yP = destination row
b = byte to draw
As output, update *colP, *xP and *yP to point to the next bit of the row.
-----------------------------------------------------------------------------*/
int const y3 = bScale ? *yP * 2 : *yP;
if (y3 < nOutRows) {
unsigned int n;
int mask;
for (n = 0, mask = 0x80; n < 8; ++n) {
if (bInvert) data[y3][*xP] = (b & mask) ? PBM_BLACK : PBM_WHITE;
else data[y3][*xP] = (b & mask) ? PBM_WHITE : PBM_BLACK;
mask = mask >> 1;
if (bScale)
data[y3+1][*xP] = data[y3][*xP];
++(*xP);
}
++(*colP); /* Next byte */
if ((*colP) >= nInCols) {
/* Onto next line? */
*colP = 0;
*xP = 0;
++(*yP);
}
}
}
static void
md2Trans(FILE * const ifP,
unsigned int const nInRows,
unsigned int const nInCols,
unsigned int const nOutRows,
unsigned int const nOutCols) {
/*----------------------------------------------------------------------------
Convert a MicroDesign 2 area to PBM
MD2 has RLE encoding that may go over
-----------------------------------------------------------------------------*/
unsigned int x1, y1, col; /* multiple lines. */
mdbyte b;
x1 = y1 = col = 0;
while (y1 < nInRows) {
b = getbyte(ifP);
if (b == 0 || b == 0xFF) {
/* RLE sequence */
int c;
c = getbyte(ifP);
if (c == 0)
c = 256;
while (c > 0) {
renderByte(nInCols, nOutRows, &col, &x1, &y1, b);
--c;
}
} else
/* Not RLE */
renderByte(nInCols, nOutRows, &col, &x1, &y1, b);
}
}
static void
md3Trans(FILE * const ifP,
unsigned int const nInRows,
unsigned int const nInCols,
unsigned int const nOutRows,
unsigned int const nOutCols) {
/*----------------------------------------------------------------------------
Convert MD3 file. MD3 are encoded as rows, and there are three types.
-----------------------------------------------------------------------------*/
unsigned int y1;
for (y1 = 0; y1 < nInRows; ++y1) {
mdbyte b;
b = getbyte(ifP); /* Row type */
switch(b) {
case 0: { /* All the same byte */
int c;
unsigned int i;
c = getbyte(ifP);
for (i = 0; i < nInCols; ++i)
mdrow[i] = c;
} break;
case 1: /* Encoded data */
case 2: { /* Encoded as XOR with previous row */
unsigned int col;
col = 0;
while (col < nInCols) {
int c;
c = getbyte(ifP);
if (c >= 129) {
/* RLE sequence */
unsigned int i;
int d;
c = 257 - c;
d = getbyte(ifP);
for (i = 0; i < c; ++i) {
if (b == 1)
mdrow[col++] = d;
else
mdrow[col++] ^= d;
}
} else {
/* not RLE sequence */
unsigned int i;
++c;
for (i = 0; i < c; ++i) {
int d;
d = getbyte(ifP);
if (b == 1)
mdrow[col++] = d;
else
mdrow[col++] ^= d;
}
}
}
} break;
}
{
/* Row loaded. Convert it. */
unsigned int x1;
unsigned int col;
unsigned int i;
for (i = 0, x1 = 0, col = 0; i < nInCols; ++i) {
unsigned int d;
d = y1;
renderByte(nInCols, nOutRows, &col, &x1, &d, mdrow[i]);
}
}
}
}
static void
usage(const char *s) {
printf("mdatopbm v1.00, Copyright (C) 1999 "
"John Elliott <jce@seasip.demon.co.uk>\n"
"This program is redistributable under the terms of "
"the GNU General Public\n"
"License, version 2 or later.\n\n"
"Usage: %s [ -a ] [ -d ] [ -i ] [ -- ] [ infile ]\n\n"
"-a: Output an ASCII pbm file\n"
"-d: Double height (to compensate for the PCW aspect ratio)\n"
"-i: Invert colors\n"
"--: No more options (use if filename begins with a dash)\n",
s);
exit(0);
}
int
main(int argc, const char **argv) {
FILE * ifP;
int n, optstop = 0;
const char * fname;
unsigned int nInRows, nInCols;
/* Height, width of input (rows x bytes) */
unsigned int nOutCols, nOutRows;
/* Height, width of output (rows x bytes) */
pm_proginit(&argc, argv);
/* Parse options */
fname = NULL; /* initial value */
for (n = 1; n < argc; ++n) {
if (argv[n][0] == '-' && !optstop) {
if (argv[n][1] == 'a' || argv[n][1] == 'A') bAscii = 1;
if (argv[n][1] == 'd' || argv[n][1] == 'D') bScale = 1;
if (argv[n][1] == 'i' || argv[n][1] == 'I') bInvert = 1;
if (argv[n][1] == 'h' || argv[n][1] == 'H') usage(argv[0]);
if (argv[n][1] == '-' && argv[n][2] == 0 && !fname) {
/* "--" */
optstop = 1;
}
if (argv[n][1] == '-' && (argv[n][2] == 'h' || argv[n][2] == 'H'))
usage(argv[0]);
}
else if (argv[n][0] && !fname) {
/* Filename */
fname = argv[n];
}
}
if (fname)
ifP = pm_openr(fname);
else
ifP = stdin;
/* Read MDA file header */
if (fread(header, 1, 128, ifP) < 128)
pm_error("Not a .MDA file\n");
if (strncmp((char*) header, ".MDA", 4) &&
strncmp((char*) header, ".MDP", 4))
pm_error("Not a .MDA file\n");
{
short yy;
pm_readlittleshort(ifP, &yy); nInRows = yy;
pm_readlittleshort(ifP, &yy); nInCols = yy;
}
nOutCols = 8 * nInCols;
nOutRows = nInRows;
if (bScale)
nOutRows *= 2;
data = pbm_allocarray(nOutCols, nOutRows);
MALLOCARRAY_NOFAIL(mdrow, nInCols);
if (header[21] == '0')
md2Trans(ifP, nInRows, nInCols, nOutRows, nOutCols);
else
md3Trans(ifP, nInRows, nInCols, nOutRows, nOutCols);
pbm_writepbm(stdout, data, nOutCols, nOutRows, bAscii);
if (ifP != stdin)
pm_close(ifP);
fflush(stdout);
pbm_freearray(data, nOutRows);
free(mdrow);
return 0;
}
|