-
Notifications
You must be signed in to change notification settings - Fork 0
/
input-pnm.c
530 lines (461 loc) · 13.6 KB
/
input-pnm.c
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/* input-pnm.c: import pnm and pbm files.
Copyright (C) 1999, 2000, 2001 Erik Nygren <nygren@mit.edu>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA. */
/*
* The pnm reading and writing code was written from scratch by Erik Nygren
* (nygren@mit.edu) based on the specifications in the man pages and
* does not contain any code from the netpbm or pbmplus distributions.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* Def: HAVE_CONFIG_H */
#include "types.h"
#include "bitmap.h"
#include "input-pnm.h"
#include "message.h"
#include "xstd.h"
#include <math.h>
#include <ctype.h>
/* Declare local data types
*/
typedef struct _PNMScanner
{
FILE *fd; /* The file descriptor of the file being read */
char cur; /* The current character in the input stream */
int eof; /* Have we reached end of file? */
char *inbuf; /* Input buffer - initially 0 */
int inbufsize; /* Size of input buffer */
int inbufvalidsize; /* Size of input buffer with valid data */
int inbufpos; /* Position in input buffer */
} PNMScanner;
typedef struct _PNMInfo
{
unsigned int xres, yres; /* The size of the image */
int maxval; /* For ascii image files, the max value
* which we need to normalize to */
int np; /* Number of image planes (0 for pbm) */
int asciibody; /* 1 if ascii body, 0 if raw body */
/* Routine to use to load the pnm body */
void (* loader) (PNMScanner *, struct _PNMInfo *, unsigned char *,
at_exception_type * excep);
} PNMInfo;
#define BUFLEN 512 /* The input buffer size for data returned
* from the scanner. Note that lines
* aren't allowed to be over 256 characters
* by the spec anyways so this shouldn't
* be an issue. */
#define SAVE_COMMENT_STRING "# CREATOR: The GIMP's PNM Filter Version 1.0\n"
/* Declare some local functions.
*/
static void pnm_load_ascii (PNMScanner *scan,
PNMInfo *info,
unsigned char *pixel_rgn,
at_exception_type * excep);
static void pnm_load_raw (PNMScanner *scan,
PNMInfo *info,
unsigned char *pixel_rgn,
at_exception_type * excep);
static void pnm_load_rawpbm (PNMScanner *scan,
PNMInfo *info,
unsigned char *pixel_rgn,
at_exception_type * excep);
static void pnmscanner_destroy (PNMScanner *s);
static void pnmscanner_createbuffer (PNMScanner *s,
unsigned int bufsize);
static void pnmscanner_getchar (PNMScanner *s);
static void pnmscanner_eatwhitespace (PNMScanner *s);
static void pnmscanner_gettoken (PNMScanner *s,
unsigned char *buf,
unsigned int bufsize);
static void pnmscanner_getsmalltoken (PNMScanner *s,
unsigned char *buf);
static PNMScanner * pnmscanner_create (FILE *fd);
#define pnmscanner_eof(s) ((s)->eof)
#define pnmscanner_fd(s) ((s)->fd)
static struct struct_pnm_types
{
char name;
int np;
int asciibody;
int maxval;
void (* loader) (PNMScanner *, struct _PNMInfo *, unsigned char *pixel_rgn,
at_exception_type * excep);
} pnm_types[] =
{
{ '1', 0, 1, 1, pnm_load_ascii }, /* ASCII PBM */
{ '2', 1, 1, 255, pnm_load_ascii }, /* ASCII PGM */
{ '3', 3, 1, 255, pnm_load_ascii }, /* ASCII PPM */
{ '4', 0, 0, 1, pnm_load_rawpbm }, /* RAW PBM */
{ '5', 1, 0, 255, pnm_load_raw }, /* RAW PGM */
{ '6', 3, 0, 255, pnm_load_raw }, /* RAW PPM */
{ 0 , 0, 0, 0, NULL}
};
at_bitmap_type input_pnm_reader (at_string filename,
at_input_opts_type * opts,
at_msg_func msg_func,
at_address msg_data)
{
char buf[BUFLEN]; /* buffer for random things like scanning */
PNMInfo *pnminfo;
PNMScanner * volatile scan;
int ctr;
FILE* fd;
at_bitmap_type bitmap = at_bitmap_init(NULL, 0, 0, 0);
at_exception_type excep = at_exception_new(msg_func, msg_data);
/* open the file */
fd = fopen (filename, "rb");
if (fd == NULL)
{
LOG("pnm filter: can't open file\n");
at_exception_fatal(&excep, "pnm filter: can't open file");
return (bitmap);
}
/* allocate the necessary structures */
pnminfo = (PNMInfo *) malloc (sizeof (PNMInfo));
scan = NULL;
/* set error handling */
scan = pnmscanner_create(fd);
/* Get magic number */
pnmscanner_gettoken (scan, (unsigned char *)buf, BUFLEN);
if (pnmscanner_eof(scan))
{
LOG("pnm filter: premature end of file\n");
at_exception_fatal (&excep, "pnm filter: premature end of file");
goto cleanup;
}
if (buf[0] != 'P' || buf[2])
{
LOG1("pnm filter: %s is not a valid file\n", filename);
at_exception_fatal (&excep, "pnm filter: invalid file");
goto cleanup;
}
/* Look up magic number to see what type of PNM this is */
for (ctr=0; pnm_types[ctr].name; ctr++)
if (buf[1] == pnm_types[ctr].name)
{
pnminfo->np = pnm_types[ctr].np;
pnminfo->asciibody = pnm_types[ctr].asciibody;
pnminfo->maxval = pnm_types[ctr].maxval;
pnminfo->loader = pnm_types[ctr].loader;
}
if (!pnminfo->loader)
{
LOG ("pnm filter: file not in a supported format\n");
at_exception_fatal (&excep, "pnm filter: file not in a supported format");
goto cleanup;
}
pnmscanner_gettoken(scan, (unsigned char *)buf, BUFLEN);
if (pnmscanner_eof(scan))
{
LOG ("pnm filter: premature end of file\n");
at_exception_fatal (&excep, "pnm filter: premature end of file");
goto cleanup;
}
pnminfo->xres = isdigit(*buf)?atoi(buf):0;
if (pnminfo->xres<=0)
{
LOG ("pnm filter: invalid xres while loading\n");
at_exception_fatal (&excep, "pnm filter: premature end of file");
goto cleanup;
}
pnmscanner_gettoken(scan, (unsigned char *)buf, BUFLEN);
if (pnmscanner_eof(scan))
{
LOG ("pnm filter: premature end of file\n");
at_exception_fatal (&excep, "pnm filter: premature end of file");
goto cleanup;
}
pnminfo->yres = isdigit(*buf)?atoi(buf):0;
if (pnminfo->yres<=0)
{
LOG ("pnm filter: invalid yres while loading\n");
at_exception_fatal (&excep, "pnm filter: invalid yres while loading");
goto cleanup;
}
if (pnminfo->np != 0) /* pbm's don't have a maxval field */
{
pnmscanner_gettoken(scan, (unsigned char *)buf, BUFLEN);
if (pnmscanner_eof(scan))
{
LOG ("pnm filter: premature end of file\n");
at_exception_fatal (&excep, "pnm filter: invalid yres while loading");
goto cleanup;
}
pnminfo->maxval = isdigit(*buf)?atoi(buf):0;
if ((pnminfo->maxval<=0)
|| (pnminfo->maxval>255 && !pnminfo->asciibody))
{
LOG ("pnm filter: invalid maxval while loading\n");
at_exception_fatal (&excep, "pnm filter: invalid maxval while loading");
goto cleanup;
}
}
bitmap = at_bitmap_init(NULL,
(unsigned short) pnminfo->xres,
(unsigned short) pnminfo->yres,
(pnminfo->np)?(pnminfo->np):1);
pnminfo->loader (scan, pnminfo, AT_BITMAP_BITS (bitmap), &excep);
cleanup:
/* Destroy the scanner */
pnmscanner_destroy (scan);
/* free the structures */
free (pnminfo);
/* close the file */
fclose (fd);
return (bitmap);
}
static void
pnm_load_ascii (PNMScanner *scan,
PNMInfo *info,
unsigned char *data,
at_exception_type * excep)
{
unsigned char *d;
unsigned int x;
int i, b;
int start, end, scanlines;
int np;
char buf[BUFLEN];
np = (info->np)?(info->np):1;
/* Buffer reads to increase performance */
pnmscanner_createbuffer(scan, 4096);
start = 0;
end = info->yres;
scanlines = end - start;
d = data;
for (i = 0; i < scanlines; i++)
for (x = 0; x < info->xres; x++)
{
for (b = 0; b < np; b++)
{
/* Truncated files will just have all 0's at the end of the images */
if (pnmscanner_eof(scan))
{
LOG ("pnm filter: premature end of file\n");
at_exception_fatal(excep, "pnm filter: premature end of file");
return;
}
if (info->np)
pnmscanner_gettoken(scan, (unsigned char *)buf, BUFLEN);
else
pnmscanner_getsmalltoken(scan, (unsigned char *)buf);
switch (info->maxval)
{
case 255:
d[b] = (unsigned char) (isdigit(*buf)?atoi(buf):0);
break;
case 1:
d[b] = (*buf=='0')?0xff:0x00;
break;
default:
d[b] = (unsigned char)(255.0*(((double)(isdigit(*buf)?atoi(buf):0))
/ (double)(info->maxval)));
}
}
d += np;
}
}
static void
pnm_load_raw (PNMScanner *scan,
PNMInfo *info,
unsigned char *data,
at_exception_type * excep)
{
unsigned char *d;
unsigned int x, i;
unsigned int start, end, scanlines;
FILE *fd;
fd = pnmscanner_fd(scan);
start = 0;
end = info->yres;
scanlines = end - start;
d = data;
for (i = 0; i < scanlines; i++)
{
if (info->xres*info->np
!= fread(d, 1, info->xres*info->np, fd))
{
LOG ("pnm filter: premature end of file\n");
at_exception_fatal (excep, "pnm filter: premature end of file\n");
return ;
}
if (info->maxval != 255) /* Normalize if needed */
{
for (x = 0; x < info->xres * info->np; x++)
d[x] = (unsigned char)(255.0*(double)(d[x]) / (double)(info->maxval));
}
d += info->xres * info->np;
}
}
static void
pnm_load_rawpbm (PNMScanner *scan,
PNMInfo *info,
unsigned char *data,
at_exception_type * excep)
{
unsigned char *buf;
unsigned char curbyte;
unsigned char *d;
unsigned int x, i;
unsigned int start, end, scanlines;
FILE *fd;
unsigned int rowlen, bufpos;
fd = pnmscanner_fd(scan);
rowlen = (unsigned int)ceil((double)(info->xres)/8.0);
buf = (unsigned char *)malloc(rowlen*sizeof(unsigned char));
start = 0;
end = info->yres;
scanlines = end - start;
d = data;
for (i = 0; i < scanlines; i++)
{
if (rowlen != fread(buf, 1, rowlen, fd))
{
LOG ("pnm filter: error reading file\n");
at_exception_fatal (excep, "pnm filter: error reading file");
goto cleanup;
}
bufpos = 0;
curbyte = buf[0];
for (x = 0; x < info->xres; x++)
{
if ((x % 8) == 0)
curbyte = buf[bufpos++];
d[x] = (curbyte&0x80) ? 0x00 : 0xff;
curbyte <<= 1;
}
d += info->xres;
}
cleanup:
free(buf);
}
/**************** FILE SCANNER UTILITIES **************/
/* pnmscanner_create ---
* Creates a new scanner based on a file descriptor. The
* look ahead buffer is one character initially.
*/
static PNMScanner *
pnmscanner_create (FILE *fd)
{
PNMScanner *s;
XMALLOC (s, sizeof(PNMScanner));
s->fd = fd;
s->inbuf = 0;
s->eof = !fread(&(s->cur), 1, 1, s->fd);
return(s);
}
/* pnmscanner_destroy ---
* Destroys a scanner and its resources. Doesn't close the fd.
*/
static void
pnmscanner_destroy (PNMScanner *s)
{
if (s->inbuf) free(s->inbuf);
free(s);
}
/* pnmscanner_createbuffer ---
* Creates a buffer so we can do buffered reads.
*/
static void
pnmscanner_createbuffer (PNMScanner *s,
unsigned int bufsize)
{
s->inbuf = (char *)malloc(sizeof(char)*bufsize);
s->inbufsize = bufsize;
s->inbufpos = 0;
s->inbufvalidsize = fread(s->inbuf, 1, bufsize, s->fd);
}
/* pnmscanner_gettoken ---
* Gets the next token, eating any leading whitespace.
*/
static void
pnmscanner_gettoken (PNMScanner *s,
unsigned char *buf,
unsigned int bufsize)
{
unsigned int ctr=0;
pnmscanner_eatwhitespace(s);
while (!(s->eof) && !isspace(s->cur) && (s->cur != '#') && (ctr<bufsize))
{
buf[ctr++] = s->cur;
pnmscanner_getchar(s);
}
buf[ctr] = '\0';
}
/* pnmscanner_getsmalltoken ---
* Gets the next char, eating any leading whitespace.
*/
static void
pnmscanner_getsmalltoken (PNMScanner *s,
unsigned char *buf)
{
pnmscanner_eatwhitespace(s);
if (!(s->eof) && !isspace(s->cur) && (s->cur != '#'))
{
*buf = s->cur;
pnmscanner_getchar(s);
}
}
/* pnmscanner_getchar ---
* Reads a character from the input stream
*/
static void
pnmscanner_getchar (PNMScanner *s)
{
if (s->inbuf)
{
s->cur = s->inbuf[s->inbufpos++];
if (s->inbufpos >= s->inbufvalidsize)
{
if (s->inbufsize > s->inbufvalidsize)
s->eof = 1;
else
s->inbufvalidsize = fread(s->inbuf, 1, s->inbufsize, s->fd);
s->inbufpos = 0;
}
}
else
s->eof = !fread(&(s->cur), 1, 1, s->fd);
}
/* pnmscanner_eatwhitespace ---
* Eats up whitespace from the input and returns when done or eof.
* Also deals with comments.
*/
static void
pnmscanner_eatwhitespace (PNMScanner *s)
{
int state = 0;
while (!(s->eof) && (state != -1))
{
switch (state)
{
case 0: /* in whitespace */
if (s->cur == '#')
{
state = 1; /* goto comment */
pnmscanner_getchar(s);
}
else if (!isspace(s->cur))
state = -1;
else
pnmscanner_getchar(s);
break;
case 1: /* in comment */
if (s->cur == '\n')
state = 0; /* goto whitespace */
pnmscanner_getchar(s);
break;
}
}
}