File: img.c

package info (click to toggle)
xli 1.17.0%2B20061110-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,564 kB
  • sloc: ansic: 25,840; makefile: 11
file content (294 lines) | stat: -rw-r--r-- 7,717 bytes parent folder | download | duplicates (9)
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
/* #ident	"@(#)x11:contrib/clients/xloadimage/img.c 1.6 93/07/23 Labtam" */
/*
** img.c - load a GEM Bit Image file for use inside xli
**
**	Tim Northrup <tim@BRS.Com>
**
**	Version 0.1 --  4/25/91 -- Initial cut
**
**  Copyright (C) 1991 Tim Northrup
**	(see file "tgncpyrght.h" for complete copyright information)
*/

#include "tgncpyrght.h"
#include "xli.h"
#include "imagetypes.h"
#include "img.h"

static int Debug = 0;				/* Set to 1 for info */

static unsigned char *BitRow;			/* working row of bits */
static int RowRepeat;				/* number of repititons */
static int RowCount;				/* current row */
static int ColCount;				/* current column */

static int IMG_ReadHeader(ZFILE *zf, IMG_Header *h);			/* Read file header */
static void IMG_WriteByte(unsigned char c, register int cols, unsigned int bpl);			/* Write output byte */

/*
**  imgIdent
**
**	Identify passed file as a GEM Bit Image or not
**
**	Returns 1 if file is a GEM Image, 0 otherwise
*/

int imgIdent (char *fullname, char *name)
{
	ZFILE *zf;				/* Input file */
	IMG_Header header;			/* GEM Image header info */
	int ret = 0;				/* Return value */

	if ( ! (zf = zopen(fullname)))
		return(0);

	if (IMG_ReadHeader(zf,&header) == 0) {
		printf("%s is a %dx%d GEM Bit Image\n", fullname,
		       header.llen, header.lines);
		ret = 1;
	      }

	zclose(zf);
	return(ret);
}

/*
**  imgLoad
**
**	Load GEM Image file into an Image structure.
**
**	Returns pointer to allocated struct if successful, NULL otherwise
*/

Image *imgLoad (char *fullname, ImageOptions *image_ops, boolean verbose)
{
	char *name = image_ops->name;
	register int i;				/* Random index */
	ZFILE *zf;				/* Input file */
	short creps;				/* Repetition counter */
	short ictr;				/* Secondary index counter */
	unsigned int bpl;			/* Bytes per scanline */
	Image *image;				/* Allocated image struct */
	IMG_Header header;			/* GEM Image header */
	unsigned char inbuf[MAX_LITERAL], ichr;	/* Input buffer/char */

	if ( ! (zf = zopen(fullname))) {
		perror("imgLoad");
		return((Image *)NULL);
	}

	if ((i = IMG_ReadHeader(zf,&header)) != 0) {
		zclose(zf);
		switch (i) {
		case 1: /* short read in header */
		case 2: /* not a GEM image */
		  break;
		case 3:
		  fprintf(stderr,"imgLoad: %s - Color images are not supported (yet)\n",name);
		  break;
		case 4:
		  fprintf(stderr,"imgLoad: %s - image is too large\n",name);
		  break;
		default:
		  fprintf(stderr,"imgLoad: %s - Unsupported image pattern length\n",name);
		  break;
		}
		return((Image *)NULL);
	      }

	if (verbose)
	    printf("%s is a %dx%d GEM Image\n",name,header.llen,header.lines);
	znocache(zf);

	image = newBitImage(header.llen,header.lines);

	image->title = dupString(name);
	BitRow = &(image->data[0]);

	bpl = header.llen / 8;				/* bytes per line */
	ColCount = 0;					/* starting column */
	RowCount = 0;					/* starting row */
	RowRepeat = 1;					/* default repeat */

	while (RowCount < header.lines) {

		/* While the last line has not been reached ... */

		i = zgetc(zf);
		if (i == EOF) {
			fprintf(stderr,"imgLoad: %s - premature EOF\n",name);
			break;
			}

		if (i == BYTE_FLAG) {		/* scanline */
			i = zgetc(zf);
			if (i == EOF) {
				fprintf(stderr,"imgLoad: %s - premature EOF\n",name);
				break;
				}
			if (i == BYTE_SLREPEAT) {	/* repeat scanline */
				if (ColCount != 0) {
				    printf("GEM Image Failure: corrupted\n");
					fprintf(stderr,"imgLoad: %s - premature EOF\n",name);
				    break;
				    }
				i = zgetc(zf);		/* SLFLAG byte (0xFF) */
				if (i != BYTE_SLFLAG) {
					fprintf(stderr,"imgLoad: %s - corrupted\n",name);
				    break;
				    }
				RowRepeat = zgetc(zf);   /* repeat count */
				}
			else {			/* repeat pattern_len */
				creps = i;
				zread(zf,inbuf,header.patlen);
				for (i = 0 ; i < creps ; i++)
				    for (ictr = 0 ; ictr < header.patlen ; ictr++)
					IMG_WriteByte(inbuf[ictr],header.llen,bpl);
				}
			}
		else if (i == BYTE_LITERAL) {	/* literal follows */
			creps = zgetc(zf);		/* literal length */
			zread(zf,inbuf,creps);		/* literal */
			for (ictr = 0 ; ictr < creps ; ictr++)
				IMG_WriteByte(inbuf[ictr],header.llen,bpl);
			}
		else {			/* monochrome mode bits */
			creps = RUN_LENGTH(i);
			ichr = (RUN_COLOR(i) == 1) ?
					BYTE_BLACK : BYTE_WHITE;
			for (i = 0 ; i < creps ; i++)
				IMG_WriteByte(ichr,header.llen,bpl);
			}
		}

	read_trail_opt(image_ops,zf,image,verbose);
	zclose(zf);
	return(image);
}

/*
**  IMG_ReadHeader
**
**	Read in IMG file header information, skip extras.
**
**	Returns 0 if successful
**		1 if not enough file for header record
**		2 if header data is nonsensical/invalid
**		3 if image is color (unsupported)
**		4 if image is too wide ( > 8K bits!!)
**		5 if pattern length is too big ( > 255)
*/

static int IMG_ReadHeader (ZFILE *zf, IMG_Header *h)
{
	register int tlen;			/* total to read in */
	register int rlen;			/* read lengths */
	unsigned char junkbuffer[MAX_SCANLINE];	/* scrap buffer */

	tlen = zread(zf,junkbuffer,(DEF_HLEN * 2));
	if (tlen != (DEF_HLEN * 2))
		return(1);			/* not enough data */

	/* convert from big-endian to machine specific */

	h->vers   = junkbuffer[1] + (256 * junkbuffer[0]);
	h->hlen   = junkbuffer[3] + (256 * junkbuffer[2]);
	h->colors = junkbuffer[5] + (256 * junkbuffer[4]);
	h->patlen = junkbuffer[7] + (256 * junkbuffer[6]);
	h->pixw   = junkbuffer[9] + (256 * junkbuffer[8]);
	h->pixh   = junkbuffer[11] + (256 * junkbuffer[10]);
	h->llen   = junkbuffer[13] + (256 * junkbuffer[12]);
	h->lines  = junkbuffer[15] + (256 * junkbuffer[14]);

	/* sanity check; if fields don't look right, it's probably not a GEM
	 * image.
	 */

	if ((h->vers != DEF_VERSION) ||
	    (h->hlen < DEF_HLEN) ||
	    (h->colors < 0) || (h->colors > 256) ||
	    (h->pixw < 1) ||
	    (h->pixh < 1) ||
	    (h->llen < 1) || (h->llen > (MAX_SCANLINE * 8)) ||
	    (h->lines < 1) || (h->lines > 8192) ||
	    (h->patlen < 0) || (h->patlen > MAX_LITERAL))
	  return(2);

	if (Debug) {
		fprintf(stderr,"Header Information:\n");
		fprintf(stderr,"\tIMG Version:  %d\n",h->vers);
		fprintf(stderr,"\t Header Len:  %d\n",h->hlen);
		fprintf(stderr,"\t     Colors:  %d\n",h->colors);
		fprintf(stderr,"\tPattern Len:  %d\n",h->patlen);
		fprintf(stderr,"\t Pixel Size:  %d x %d (microns=1000th mm)\n",
				h->pixw,h->pixh);
		fprintf(stderr,"\t Image Size:  %d x %d (pixels)\n",
				h->llen,h->lines);
		}

	/* validity check on particular fields
	 */

	if (h->colors != 1)
		return(3);
	if (h->llen > (MAX_SCANLINE * 8))
		return(4);
	if (h->patlen > MAX_LITERAL)
		return(5);

	/* make life easier if not on an even boundary */
	if (h->llen % 8) {
		h->llen += (8 - (h->llen % 8));
		if (Debug)
			fprintf(stderr,"Image expanded to %d pixels wide\n",
				h->llen);
		}

	/* skip additional header information if present */
	if (h->hlen > DEF_HLEN) {
		tlen = ((h->hlen - DEF_HLEN) * 2);
		if (Debug)
			fprintf(stderr,"imgLoad: %d bytes of extra header skipped",tlen);
		for ( ; tlen > 0 ; tlen -= rlen) {
			rlen = (tlen > MAX_SCANLINE) ? MAX_SCANLINE : tlen;
			zread(zf,junkbuffer,rlen);
			}
		}

	return(0);
}



/*
**  IMG_WriteByte
**
**	Add byte to image; if end of scanline, may need to replicate it
**
**	Returns no value (void function)
*/

static void IMG_WriteByte (unsigned char c, register int cols, unsigned int bpl)
{
	register int i;
	register unsigned char *ptr;
	register unsigned char *ptr2;

	BitRow[ColCount] = c;

	if (++ColCount >= bpl) {			/* end of scanline */
		ptr2 = BitRow + bpl;
		RowCount++;			/* count one already out */
		while (--RowRepeat > 0) {
			for (ptr = BitRow, i = 0 ; i < bpl ; i++, ptr++)
				*ptr2++ = *ptr;
			RowCount++;
			}
		BitRow = ptr2;
		ColCount = 0;
		RowRepeat = 1;
		}

	return;
}