File: fbm.c

package info (click to toggle)
xli 1.17.0+20061110-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,540 kB
  • sloc: ansic: 25,840; makefile: 11
file content (402 lines) | stat: -rw-r--r-- 10,662 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
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
/*
 * fbm.c:
 *
 * adapted from code by Michael Mauldin, (mlm) at Carnegie-Mellon
 * University, (fbm tools) and Kirk L. Johnson, (tuna@athena.mit.edu),
 * (gif.c).
 *
 * fbmin.c
 * Mark Majhor
 * August 1990
 *
 * routines for reading FBM files
 *
 * Copyright 1990 Mark Majhor (see the included file
 * "mrmcpyrght.h" for complete copyright information)
 */
# include "xli.h"
# include <ctype.h>
# include <string.h>
#define FBM_C
# include "fbm.h"
# include "imagetypes.h"

/****
 **
 ** local variables
 **
 ****/

static BYTE file_open = 0;	/* status flags */
static BYTE image_open = 0;

static ZFILE *ins;		/* input stream */
static FBMFILEHDR phdr;		/* header structure */

/****
 **
 ** global variables
 **
 ****/

static int  fbmin_img_width;           /* image width */
static int  fbmin_img_height;          /* image height */
static int  fbmin_img_depth;	       /* Depth (1 for B+W, 3 for RGB) */
static int  fbmin_img_bits;	       /* Bits per pixel */
static int  fbmin_img_rowlen;	       /* length of one row of data */
static int  fbmin_img_plnlen;	       /* length of one plane of data */
static int  fbmin_img_clrlen;	       /* length of the colormap */
static double  fbmin_img_aspect;       /* image aspect ratio */
static int  fbmin_img_physbits;	       /* physical bits per pixel */
static char *fbmin_img_title;	       /* name of image */
static char *fbmin_img_credit;	       /* credit for image */

static int fbmin_image_test(void);

static char *
get_err_string(int errno)
{
  int i;
  for (i=0;fbm_err_strings[i].err_no != 0;i++)
  {
    if (fbm_err_strings[i].err_no == errno)
		return fbm_err_strings[i].name;
  }
  return "";
}

/*
 * open FBM image in the input stream; returns FBMIN_SUCCESS if
 * successful. (might also return various FBMIN_ERR codes.)
 */
static int fbmin_open_image(ZFILE *s)
{
  char *hp;		/* header pointer */

  /* make sure there isn't already a file open */
  if (file_open)
    return(FBMIN_ERR_FAO);

  /* remember that we've got this file open */
  file_open = 1;
  ins = s;

  /* read in the fbm file header */
  hp = (char *) &phdr;
  if (zread(ins, (byte *)hp, sizeof(phdr)) != sizeof(phdr))
    return FBMIN_ERR_BAD_SIG;	/* can't be FBM file */

  if (strncmp(FBM_MAGIC, phdr.magic, sizeof(FBM_MAGIC)) != 0)
    return FBMIN_ERR_BAD_SIG;

  /* Now extract relevant features of FBM file header */
  fbmin_img_width    = atoi(phdr.cols);
  fbmin_img_height   = atoi(phdr.rows);
  fbmin_img_depth    = atoi(phdr.planes);
  fbmin_img_bits     = atoi(phdr.bits);
  fbmin_img_rowlen   = atoi(phdr.rowlen);
  fbmin_img_plnlen   = atoi(phdr.plnlen);
  fbmin_img_clrlen   = atoi(phdr.clrlen);
  fbmin_img_aspect   = atof(phdr.aspect);
  fbmin_img_physbits = atoi(phdr.physbits);
  fbmin_img_title    = phdr.title;
  fbmin_img_credit   = phdr.credits;

  if (fbmin_image_test() != FBMIN_SUCCESS)
    return FBMIN_ERR_BAD_SD;

  return FBMIN_SUCCESS;
}

/*
 * close an open FBM file
 */

static int fbmin_close_file(void)
{
  /* make sure there's a file open */
  if (!file_open)
    return FBMIN_ERR_NFO;

  /* mark file (and image) as closed */
  file_open  = 0;
  image_open = 0;

  /* done! */
  return FBMIN_SUCCESS;
}
    
static int fbmin_image_test(void)
{
  if (fbmin_img_width < 1 || fbmin_img_width > 32767) {
    fprintf (stderr, "Invalid width (%d) on input\n", fbmin_img_width);
    return FBMIN_ERR_BAD_SD;
  }

  if (fbmin_img_height < 1 || fbmin_img_height > 32767) {
    fprintf (stderr, "Invalid height (%d) on input\n", fbmin_img_height);
    return (0);
  }

  if (fbmin_img_depth != 1 && fbmin_img_depth != 3) {
    fprintf (stderr, "Invalid number of planes (%d) on input %s\n",
	     fbmin_img_depth, "(must be 1 or 3)");
    return FBMIN_ERR_BAD_SD;
  }

  if (fbmin_img_bits < 1 || fbmin_img_bits > 8) {
    fprintf (stderr, "Invalid number of bits (%d) on input %s\n",
	     fbmin_img_bits, "(must be [1..8])");
    return FBMIN_ERR_BAD_SD;
  }

  if (fbmin_img_physbits != 1 && fbmin_img_physbits != 8) {
    fprintf (stderr, "Invalid number of physbits (%d) on input %s\n",
	     fbmin_img_physbits, "(must be 1 or 8)");
    return FBMIN_ERR_BAD_SD;
  }

  if (fbmin_img_rowlen < 1 || fbmin_img_rowlen > 32767) {
    fprintf (stderr, "Invalid row length (%d) on input\n",
	     fbmin_img_rowlen);
    return FBMIN_ERR_BAD_SD;
  }

  if (fbmin_img_depth > 1 && fbmin_img_plnlen < 1) { 
    fprintf (stderr, "Invalid plane length (%d) on input\n",
	     fbmin_img_plnlen);
    return FBMIN_ERR_BAD_SD;
  }

  if (fbmin_img_plnlen < (fbmin_img_height * fbmin_img_rowlen)) {
    fprintf (stderr, "Invalid plane length (%d) < (%d) * (%d) on input\n",
	     fbmin_img_plnlen,fbmin_img_height,fbmin_img_rowlen);
    return FBMIN_ERR_BAD_SD;
  }

  if (fbmin_img_aspect < 0.01 || fbmin_img_aspect > 100.0) {
    fprintf (stderr, "Invalid aspect ratio %g on input\n",
	     fbmin_img_aspect);
    return FBMIN_ERR_BAD_SD;
  }
    return FBMIN_SUCCESS;
}

/*
 * these are the routines added for interfacing to xli
 */

/*
 * tell someone what the image we're loading is.  this could be a little more
 * descriptive but I don't care
 */

static void tellAboutImage(char *name)
{
  printf("%s is a %dx%d FBM image, depth %d with %d colors\n", name,
    fbmin_img_width, fbmin_img_height, fbmin_img_bits * fbmin_img_depth,
    fbmin_img_clrlen / 3);
}

Image *fbmLoad(char *fullname, ImageOptions *image_ops, boolean verbose)
{
  ZFILE        *zf;
  char         *name = image_ops->name;
  Image *image;
  register int    x, y, j, k, width, rowpad, plnpad;
  unsigned char *pixptr, *cm;
  unsigned char *r, *g, *b;
  int retv;

  CURRFUNC("fbmLoad");
  if (! (zf= zopen(fullname)))
    return(NULL);
  if ((retv = fbmin_open_image(zf)) != FBMIN_SUCCESS) {  /* read image header */
    fbmin_close_file();
    zclose(zf);
    if(retv != FBMIN_ERR_BAD_SIG)
      fprintf (stderr, "fbmLoad: %s - aborting, '%s'\n",name, get_err_string(retv));
    return(NULL);
  }
  if (verbose)
    tellAboutImage(name);
  znocache(zf);

  if(fbmin_img_depth==1)
    if(fbmin_img_bits==1)
      image= newBitImage(fbmin_img_width, fbmin_img_height);
    else
      image = newRGBImage(fbmin_img_width, fbmin_img_height, fbmin_img_bits);
  else	/* must be 3 */
    image = newTrueImage(fbmin_img_width, fbmin_img_height);
  image->title= dupString(name);

  /* if image has a local colormap, override global colormap
   */
  if (fbmin_img_depth==1 && fbmin_img_clrlen > 0) {
    cm = (unsigned char *) lmalloc(fbmin_img_clrlen);

    if (zread(ins, cm, fbmin_img_clrlen) != fbmin_img_clrlen) {
      fprintf (stderr, "fbmLoad: %s - can't read colormap (%d bytes)\n", name, fbmin_img_clrlen);
      fbmin_close_file();
      lfree(cm);
      zclose(zf);
      freeImage(image);
      return(NULL);
    }
    /*
     * fbm color map is organized as
     * buf[3][16]
     */
    y = fbmin_img_clrlen / 3;
    r = &cm[0], g = &cm[y], b = &cm[2 * y];
    for (x = 0; x < y; x++, r++, g++, b++) {
      image->rgb.red[x]   = *r << 8;
      image->rgb.green[x] = *g << 8;
      image->rgb.blue[x]  = *b << 8;
    }
    image->rgb.used = y;

  } else
    cm = NULL;

  width = fbmin_img_width;
  rowpad = fbmin_img_rowlen - width;
  plnpad = fbmin_img_plnlen - (fbmin_img_height * fbmin_img_rowlen);

  if(fbmin_img_depth == 1) {
    if(fbmin_img_bits==1) {
      int xx,bb;
      byte *rb;
      rb = (byte *) lmalloc(width > rowpad ? width : rowpad);
      pixptr = image->data;
      for (j = 0; j < fbmin_img_height; j++, pixptr += ((width+7)/8)) {
        if (zread(ins, rb, width) != width) {
          fprintf(stderr, "fbmLoad: %s - Short read within image data\n", name);
	  if (cm != NULL)
	    lfree(cm);
          lfree(rb);
	  fbmin_close_file();
	  zclose(zf);
	  return(image);
        }
        for (xx = 0,bb=0; xx < width; xx++) {
		  bb = (bb << 1) | (rb[xx] ? 0 : 1);
		  if ((xx & 7) == 7) {
            pixptr[xx/8] = bb;
            bb = 0;
		  }
        }
        if (zread(ins, rb, rowpad) != rowpad) {
          fprintf(stderr, "fbmLoad: %s - Short read within row padding\n", name);
	  if (cm != NULL)
	    lfree(cm);
	  lfree(rb);
	  fbmin_close_file();
	  zclose(zf);
	  return(image);
        }
      }
      lfree(rb);
    }
    else {
      byte *rb;
      rb = (byte *) lmalloc(rowpad);
      pixptr = image->data;
      for (j = 0; j < fbmin_img_height; j++, pixptr += width) {
        if (zread(ins, pixptr, width) != width) {
          fprintf(stderr, "fbmLoad: %s - Short read within image data\n", name);
	  if (cm != NULL)
	    lfree(cm);
	  lfree(rb);
	  fbmin_close_file();
	  zclose(zf);
	  return(image);
        }
        if (zread(ins, rb, rowpad) != rowpad) {
          fprintf(stderr,"fbmLoad: %s - Short read within row padding\n", name);
	  if (cm != NULL)
	    lfree(cm);
	  lfree(rb);
	  fbmin_close_file();
	  zclose(zf);
	  return(image);
        }
      }
      lfree(rb);
    }
  }
  else {    /* depth == 3 case */
    int xx;
    byte *rb;
    rb = (byte *) lmalloc(width > rowpad ? (width > plnpad ? width : plnpad)
                                          :(rowpad > plnpad ? rowpad : plnpad));
    for (k = 0; k < fbmin_img_depth; k++) {
      pixptr = image->data + k;
      for (j = 0; j < fbmin_img_height; j++) {
        if (zread(ins, rb, width) != width) {
          fprintf(stderr, "fbmLoad: %s - Short read within image data\n", name);
	  if (cm != NULL)
	    lfree(cm);
	  lfree(rb);
	  fbmin_close_file();
	  zclose(zf);
	  return(image);
        }
        for (xx = 0; xx < width; xx++,pixptr+=3) {
          *pixptr = rb[xx];
        }
        if (zread(ins, rb, rowpad) != rowpad) {
          fprintf(stderr,"fbmLoad: %s - Short read within row padding\n", name);
	  if (cm != NULL)
	    lfree(cm);
	  lfree(rb);
	  fbmin_close_file();
	  zclose(zf);
	  return(image);
        }
      }
      if (zread(ins, rb, plnpad) != plnpad) {
        fprintf(stderr,"fbmLoad: %s - Short read within plane padding\n", name);
	if (cm != NULL)
	  lfree(cm);
	lfree(rb);
	fbmin_close_file();
	zclose(zf);
	return(image);
      }
    }
  lfree(rb);
  }

  if (cm != NULL)
    lfree(cm);
  fbmin_close_file();
  read_trail_opt(image_ops,zf,image,verbose);
  zclose(zf);
  return(image);
}

int fbmIdent(char *fullname, char *name)
{
  ZFILE        *zf;
  unsigned int  ret;
  int retv;

  if (! (zf= zopen(fullname)))
    return(0);
  if ((retv = fbmin_open_image(zf)) == FBMIN_SUCCESS) {
    tellAboutImage(name);
    ret = 1;
  } else {
    if (retv != FBMIN_ERR_BAD_SIG) {
      fprintf (stderr, "FBM file error\n");
      ret = 1;		/* file was identified as FBM */
    } else {
      ret = 0;
    }
  }
  fbmin_close_file();
  zclose(zf);
  return(ret);
}