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
|
/*
* Convert a GEM .img file to PBM
*
* Author: Diomidis D. Spinellis
* (C) Copyright 1988 Diomidis D. Spinellis.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation.
*
* This file is provided AS IS with no warranties of any kind. The author
* shall have no liability with respect to the infringement of copyrights,
* trade secrets or any patents by this file or any part thereof. In no
* event will the author be liable for any lost revenue or profits or
* other special, indirect and consequential damages.
*
* Comments and additions should be sent to the author:
*
* Diomidis D. Spinellis
* 1 Myrsinis Str.
* GR-145 62 Kifissia
* GREECE
*
* 92/07/11 Johann Haider
* Changed to read from stdin if file is omitted
* Changed to handle line length not a multiple of 8
*
* 94/01/31 Andreas Schwab (schwab@ls5.informatik.uni-dortmund.de)
* Changed to remove architecture dependency and conform to
* PBM coding standard.
* Added more tests for garbage.
*
* 2000/04/30 John Elliott <jce@seasip.demon.co.uk> Added ability to
* read 4-plane color IMG files. Therefore changed from PBM to PPM.
* Bryan changed it further to use the PNM facilities so it outputs
* both PBM and PPM in the Netpbm tradition. Name changed from
* gemtopbm to gemtopnm.
*/
#include <assert.h>
#include "pnm.h"
#define MAXVAL 3
#define LIGHT 2
#define DARK 1
#define BLACK 0
char pattern[8];
static void getinit ARGS ((FILE *file, int *colsP, int *rowsP, int *padrightP,
int *patlenP, int *planesP));
int
main(argc, argv)
int argc;
char *argv[];
{
int debug = 0;
FILE *f;
int row;
int rows, cols, padright, patlen, planes;
/* attributes of input image */
int type; /* The format type (PBM/PPM) of the output image */
bit *bitrow[4];
/* One row of input, one or four planes. (If one, only [0] is defined)*/
xel * xelrow; /* One row of output */
const char * const usage = "[-debug] [gem IMG file]";
int argn;
/* Process multiple planes by maintaining a separate row of bits for each
* plane. In a single-plane image, all we have to do is write out the
* first plane; in a multiple-plane image, we combine them just before writing
* out the row.
*/
pnm_init( &argc, argv );
argn = 1;
while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0')
{
if (pm_keymatch(argv[1], "-debug", 2))
debug = 1;
else
pm_usage (usage);
++argn;
}
if (argc == argn)
f = stdin;
else {
f = pm_openr (argv[argn]);
++argn;
}
if (argn != argc)
pm_usage (usage);
getinit (f, &cols, &rows, &padright, &patlen, &planes);
if (planes == 1)
type = PBM_TYPE;
else
type = PPM_TYPE;
pnm_writepnminit( stdout, cols, rows, MAXVAL, type, 0 );
{
/* allocate input row data structure */
int plane;
for (plane = 0; plane < planes; plane++)
bitrow[plane] = malloc (cols + padright);
}
xelrow = pnm_allocrow(cols+padright); /* Output row */
for (row = 0; row < rows; ) {
int linerep;
int plane;
linerep = 1;
for (plane = 0; plane < planes; plane++) {
int col;
col = 0;
while (col < cols) {
int c;
switch (c = getc(f)) {
case 0x80: /* Bit String */
{
int j;
c = getc(f); /* Byte count */
if (debug)
pm_message("bit string of %d bytes", c);
if (col + c * 8 > cols + padright)
pm_error ("bad byte count");
for (j = 0; j < c; ++j) {
int cc, k;
cc = getc(f);
for (k = 0x80; k; k >>= 1) {
bitrow[plane][col] = (k & cc) ? 0 : 1;
++col;
}
}
}
break;
case 0: /* Pattern run */
{
int j, l;
c = getc(f); /* Repeat count */
if (debug)
pm_message("pattern run of %d repetitions", c);
/* line repeat */
if (c == 0) {
c = getc(f);
if (c != 0x00ff)
pm_error( "badly formed line repeat" );
linerep = getc(f);
break;
}
fread (pattern, 1, patlen, f);
if (col + c * patlen * 8 > cols + padright)
pm_error ("bad pattern repeat count");
for (j = 0; j < c; ++j)
for (l = 0; l < patlen; ++l) {
int k;
for (k = 0x80; k; k >>= 1) {
bitrow[plane][col] = (k & pattern[l]) ? 0 : 1;
++col;
}
}
}
break;
default: /* Solid run */
{
int l, j;
if (debug)
pm_message("solid run of %d bytes %s", c & 0x7f,
c & 0x80 ? "on" : "off" );
/* each byte had eight bits DSB */
l = (c & 0x80) ? 0: 1;
c = (c & 0x7f) * 8;
if (col + c > cols + padright)
pm_error ("bad solid run repeat count");
for (j = 0; j < c; ++j) {
bitrow[plane][col] = l;
++col;
}
}
break;
case EOF: /* End of file */
pm_error( "end of file reached" );
}
}
if ( debug )
pm_message( "EOL plane %d row %d", plane, row );
if (col != cols + padright)
pm_error( "EOL beyond edge" );
}
if (planes == 4) {
/* Construct a pixel from the 4 planes of bits for this row */
int col;
for (col = 0; col < cols; col++) {
int r, g, b, i;
const int r_bit = !bitrow[0][col];
const int g_bit = !bitrow[1][col];
const int b_bit = !bitrow[2][col];
i = bitrow[3][col];
/* Deal with weird GEM palette - white/black/gray are
encoded oddly
*/
if (r_bit == g_bit && g_bit == b_bit) {
/* It's black, white, or gray */
if (r_bit && i) r = LIGHT;
else if (r_bit) r = BLACK;
else if (i) r = MAXVAL;
else r = DARK;
g = b = r;
} else {
/* It's one of the twelve colored colors */
if (!i) {
/* Low intensity */
r = r_bit * LIGHT;
g = g_bit * LIGHT;
b = b_bit * LIGHT;
} else {
/* Normal intensity */
r = r_bit * MAXVAL;
g = g_bit * MAXVAL;
b = b_bit * MAXVAL;
}
}
PPM_ASSIGN(xelrow[col], r, g, b);
}
} else {
int col;
for (col = 0; col < cols; col++)
PNM_ASSIGN1(xelrow[col], bitrow[0][col]);
}
while (linerep--) {
pnm_writepnmrow( stdout, xelrow, cols, MAXVAL, type, 0 );
++row;
}
}
pnm_freerow(xelrow);
pm_close( f );
pm_close( stdout );
exit(0);
}
static void
getinit (file, colsP, rowsP, padrightP, patlenP, planesP)
FILE *file;
int *colsP;
int *rowsP;
int *padrightP;
int *patlenP;
int *planesP;
{
short s;
short headlen;
if (pm_readbigshort (file, &s) == -1) /* Image file version */
pm_error ("EOF / read error");
if (s != 1)
pm_error ("unknown version number (%d)", (int) s);
if (pm_readbigshort (file, &headlen) == -1) /* Header length in words */
pm_error ("EOF / read error");
if (headlen < 8)
pm_error ("short header (%d)", (int) headlen);
if (pm_readbigshort (file, &s) == -1) /* Number of planes */
pm_error ("EOF / read error");
if (s != 4 && s != 1)
pm_error ("This program can interpret IMGs with only 1 or 4 planes");
*planesP = s;
if (pm_readbigshort (file, &s) == -1) /* Pattern definition length (bytes) */
pm_error ("EOF / read error");
if (s < 1 || s > 8)
pm_error ("illegal pattern length (%d)", (int) s);
*patlenP = (int) s;
if (pm_readbigshort (file, &s) == -1 /* Pixel height (microns) */
|| pm_readbigshort (file, &s) == -1 /* Pixel height (microns) */
|| pm_readbigshort (file, &s) == -1) /* Scan line width */
pm_error ("EOF / read error");
*colsP = (int) s;
if (pm_readbigshort (file, &s) == -1) /* Number of scan line items */
pm_error ("EOF / read error");
*rowsP = (int) s;
*padrightP = 7 - ((*colsP + 7) & 7);
headlen -= 8;
while (headlen-- > 0)
{
(void) getc (file);
(void) getc (file);
}
}
|