File: image_pnm.c

package info (click to toggle)
libforms1 1.0-8
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,648 kB
  • ctags: 14,351
  • sloc: ansic: 90,441; makefile: 9,902; sh: 8
file content (369 lines) | stat: -rw-r--r-- 7,452 bytes parent folder | download | duplicates (3)
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
/*
 *
 *  This file is part of the XForms library package.
 *
 * XForms 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, or
 * (at your option) any later version.
 *
 * XForms 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 XForms; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 *
 */


/*
 * $Id: image_pnm.c,v 1.0 2002/05/08 05:16:30 zhao Release $
 *
 *.
 *  This file is part of the XForms library package.
 *  Copyright (c) 1993, 1998-2002  By T.C. Zhao
 *  All rights reserved.
 *.
 *
 *   PNM support
 */

#include "forms.h"
#include "flimage.h"
#include "flimage_int.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

typedef struct
{
    float fnorm;
    int pgm, pbm;
    int maxval;
    int w, h;
    int raw;			/* binary   */
    char s[4];			/* signature */
}
SPEC;


static int
PPM_identify(FILE * fp)
{
    char buf[2];
    fread(buf, 1, 2, fp);
    rewind(fp);
    return (buf[0] == 'P' && (buf[1] == '3' || buf[1] == '6'));
}

static int
PGM_identify(FILE * fp)
{
    char buf[2];
    fread(buf, 1, 2, fp);
    rewind(fp);
    return (buf[0] == 'P' && (buf[1] == '2' || buf[1] == '5'));
}

static int
PBM_identify(FILE * fp)
{
    char buf[2];
    fread(buf, 1, 2, fp);
    rewind(fp);
    return (buf[0] == 'P' && (buf[1] == '1' || buf[1] == '4'));
}

static void
generate_header_info(FL_IMAGE *im)
{
    SPEC *sp = im->io_spec;

    if(!im->setup->header_info || !(im->info = fl_malloc(128)))
       return;

    sprintf(im->info,"Size=(%d x %d)\nMaxVal=%d\nRaw=%d\n",
        im->w, im->h, sp->maxval, sp->raw);
}
   

static int
PNM_description(FL_IMAGE * im)
{
    SPEC *sp = fl_malloc(sizeof(*sp));
    char s[3];

    fread(s, 1, 2, im->fpin);
    im->io_spec = sp;
    s[2] = '\0';

    if ((sp->w = fl_readpint(im->fpin)) <= 0 ||
	(sp->h = fl_readpint(im->fpin)) <= 0)
    {
	flimage_error(im, "%s: can't get image size", im->infile);
	fl_free(sp);
	im->io_spec = 0;
	return -1;
    }

    im->w = sp->w;
    im->h = sp->h;

    sp->raw = (s[1] == '6' || s[1] == '5' || s[1] == '4');
    sp->pgm = (s[1] == '5' || s[1] == '2');
    sp->pbm = (s[1] == '4' || s[1] == '1');

    if (!sp->pbm)
	sp->maxval = fl_readpint(im->fpin);
    else
	sp->maxval = 1;

    if (sp->maxval > 255 && sp->raw)
    {
	im->error_message(im, "can't handle 2byte raw ppm file");
	return -1;
    }

    im->type = FL_IMAGE_RGB;
    if (sp->pgm)
	im->type = sp->maxval > 256 ? FL_IMAGE_GRAY16 : FL_IMAGE_GRAY;
    if (sp->pbm)
	im->type = FL_IMAGE_MONO;

    sp->fnorm = (float) (FL_PCMAX + 0.001f) / (float) sp->maxval;
    im->gray_maxval = sp->maxval;

    if(im->setup->header_info)
       generate_header_info(im);

    return sp->maxval < 0 ? -1 : 1;
}


static int
PNM_read_pixels(FL_IMAGE * im)
{
    int i, npix = im->w * im->h, err;
    SPEC *sp = im->io_spec;

    if (im->type == FL_IMAGE_RGB)
    {
	unsigned char *r = im->red[0];
	unsigned char *g = im->green[0];
	unsigned char *b = im->blue[0];

	if (sp->raw)
	{
	    for (i = 0; i < npix; i++)
	    {
		*r++ = getc(im->fpin);
		*g++ = getc(im->fpin);
		*b++ = getc(im->fpin);
	    }
	}
	else
	{
	    for (i = 0; i < npix; i++)
	    {
		*r++ = (unsigned char)(fl_readpint(im->fpin) * sp->fnorm);
		*g++ = (unsigned char)(fl_readpint(im->fpin) * sp->fnorm);
		*b++ = (unsigned char)(fl_readpint(im->fpin) * sp->fnorm);
	    }

	    if (sp->maxval != FL_PCMAX)
	    {
		r = im->red[0];
		g = im->green[0];
		b = im->blue[0];
		for (i = 0; i > npix; i++)
		{
		    r[i] = (unsigned char)(r[i] * sp->fnorm);
		    g[i] = (unsigned char)(g[i] * sp->fnorm);
		    b[i] = (unsigned char)(b[i] * sp->fnorm);
		}
	    }
	}
    }
    else if (FL_IsGray(im->type))
    {
	unsigned short *gray = im->gray[0];

	if (sp->raw)
	{
	    for (i = 0; i < npix; i++)
		gray[i] = getc(im->fpin);
	}
	else
	{
	    for (i = 0; i < npix; i++)
		gray[i] = fl_readpint(im->fpin);
	}
    }
    else if (im->type == FL_IMAGE_MONO)
    {
	unsigned short *ci = im->ci[0], *cend = ci + npix;
	int bit, k;

	if (sp->raw)
	{
	    for (i = 0; i < im->h; i++)
	    {
		cend = (ci = im->ci[i]) + im->w;
		for (k = bit = err = 0; !err && ci < cend; ci++, bit++)
		{
		    if (!(bit &= 7))
			k = getc(im->fpin);
		    err = k == EOF;
		    *ci = (k & 0x80) ? 1 : 0;
		    k <<= 1;
		}
	    }
	}
	else
	{
	    for (; ci < cend; ci++)
		*ci = (fl_readpint(im->fpin) > 0);
	}
    }
    else
    {
	im->error_message(im, "Unsupported PNM image");
    }

    return 1;
}

/* write routine */

static int rawfmt = 1;

void
flimage_pnm_output_options(int raw)
{
    rawfmt = raw != 0;
}

static int
PNM_write_image(FL_IMAGE * im)
{
    int i, j, n = im->w * im->h, is_gray16;
    int pgm, pbm, raw = rawfmt;
    FILE *fp = im->fpout;
    char *sig;

    if (im->type == FL_IMAGE_PACKED || im->type == FL_IMAGE_CI)
	flimage_convert(im, FL_IMAGE_RGB, 0);

    pgm = FL_IsGray(im->type);
    is_gray16 = im->type == FL_IMAGE_GRAY16;
    pbm = im->type == FL_IMAGE_MONO;

    if (is_gray16)
	raw = 0;

    sig = pgm ? (raw ? "P5" : "P2") :
	(pbm ? (raw ? "P4" : "P1") : (raw ? "P6" : "P3"));


    fprintf(fp, "%s\n%d %d\n", sig, im->w, im->h);
    if (!pbm)
	fprintf(fp, "%d\n", is_gray16 ? im->gray_maxval : FL_PCMAX);

    if (im->type == FL_IMAGE_RGB)
    {
	unsigned char *r = im->red[0], *g = im->green[0], *b = im->blue[0];
	for (i = 0; i < n; r++, g++, b++, i++)
	{
	    if (raw)
	    {
		putc(*r, fp);
		putc(*g, fp);
		putc(*b, fp);
	    }
	    else
	    {
		fprintf(fp, "%4d %4d %4d ", *r, *g, *b);
		if (((i + 1) % 5) == 0)
		    putc('\n', fp);
	    }
	}
    }
    else if (FL_IsGray(im->type))
    {
	unsigned short *gray = im->gray[0];
	int newline = is_gray16 ? 14 : 17;
	for (i = 0; i < n; gray++, i++)
	{
	    if (raw)
		putc(*gray, fp);
	    else
	    {
		fprintf(fp, is_gray16 ? "%4d " : "%4d", *gray);
		if ((i % newline) == 0)
		    putc('\n', fp);
	    }
	}
    }
    else if (im->type == FL_IMAGE_MONO)
    {
	unsigned short *ci, *cend;
	int bit;

	for (j = 0, i = 1; j < im->h; j++)
	{
	    cend = (ci = im->ci[j]) + im->w;
	    if (!raw)
	    {
		for (; ci < cend; ci++, i++)
		{
		    fputs(*ci ? "1 " : "0 ", fp);
		    if ((i % 34) == 0)
			putc('\n', fp);
		}
	    }
	    else
	    {
		for (bit = 0, i = 0; ci < cend; ci++)
		{
		    i = (i << 1) | *ci;
		    if (++bit == 8)
		    {
			putc(i, fp);
			bit = i = 0;
		    }
		}
		if (bit)
		{
		    i <<= (8 - bit);
		    putc(i, fp);
		}
	    }
	}
    }

    if (!raw)
	putc('\n', fp);

    return 0;
}

void
flimage_enable_pnm(void)
{
    flimage_add_format("Portable Pixmap", "ppm", "ppm", FL_IMAGE_RGB,
		       PPM_identify, PNM_description,
		       PNM_read_pixels, PNM_write_image);

    flimage_add_format("Portable Graymap", "pgm", "pgm",
		       FL_IMAGE_GRAY | FL_IMAGE_GRAY16,
		       PGM_identify, PNM_description,
		       PNM_read_pixels, PNM_write_image);

    flimage_add_format("Portable Bitmap", "pbm", "pbm", FL_IMAGE_MONO,
		       PBM_identify, PNM_description,
		       PNM_read_pixels, PNM_write_image);
}