File: vff.c

package info (click to toggle)
xloadimage 4.1-25
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,820 kB
  • sloc: ansic: 36,084; asm: 284; makefile: 282; sh: 280
file content (400 lines) | stat: -rw-r--r-- 9,754 bytes parent folder | download | duplicates (4)
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
/* vff.c
 * Sun Visualization File Format images
 *
 * -ksp  8 June 91
 */

/*
 * Copyright (c) 1991 Southwest Research Institute
 * All rights reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appears in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  The name of Southwest Research
 * Institute may not be used to endorse or promote products derived
 * from this software without specific prior written permission.  The
 * author makes no representations about the suitability of this
 * software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 *
 * THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
 * USE OR PERFORMANCE OF THIS SOFTWARE.
 */


#include <ctype.h>
#include "image.h"

#define HEAD_BUF_SIZE	2048
#define MAX_HEAD_LINES	100

static char *header[MAX_HEAD_LINES];
static int width, height, bands, cmsize;
static unsigned char rm[256], gm[256], bm[256];

static int readHeader();
static int parseHeader();
static int readImage();
static void freeheader();


Image *
vffLoad(fullname, name, verbose)
char *fullname, *name;
unsigned int verbose;
{

     ZFILE *zf;
     char buf[4];
     Image *image = NULL;

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

     if (zread(zf, (unsigned char *)buf, 4) != 4) {
	  zclose(zf);
	  return(NULL);
     }

     if (strncmp(buf, "ncaa", 4) != 0) {
	  zclose(zf);
	  return(NULL);
     }

     if (readHeader(zf)) {
	  zclose(zf);
	  return(NULL);
     }

     if (parseHeader()) {
	  zclose(zf);
	  return(NULL);
     }

     if (bands == 1) {
	  image = newRGBImage(width, height, 8);
     } else if (bands == 2) {
	  printf("Only Sun VFF files with one or three+ bands are supported.\n");
	  zclose(zf);
	  return(NULL);
     } else if (bands >= 3) {
	  image = newTrueImage(width, height);
     }

     image->title = dupString(name);
     
     if (verbose) {
	  if (bands == 1) {
	       printf("%s is a %dx%d Sun VFF image with 1 band\n", name, width, height);
	  } else {
	       printf("%s is a %dx%d Sun VFF image with %d bands\n", name, width, height, bands);
	  }
     }

     if (readImage(zf, image)) {
	  freeImage(image);
	  zclose(zf);
	  return(NULL);
     }
     zclose(zf);
     
     return(image);
}

static int
readImage(zf, image)
ZFILE *zf;
Image *image;
{
     byte *buf;
     byte *op, *ip;
     int x, y, skip, linewidth, i;
     void memoryExhausted();

     if (bands == 1) {
	  if (cmsize == 0) {
	       /* build a grey ramp */
	       for (i = 0; i < 256; i++) {
		    *(image->rgb.red + i) = i * 256;
		    *(image->rgb.green + i) = i * 256;
		    *(image->rgb.blue + i) = i * 256;
	       }
	       image->rgb.used = 256;
	  } else {
	       if (cmsize > 256) {
		    cmsize = 256;
	       }
	       for (i = 0; i < cmsize; i++) {
		    *(image->rgb.red + i) = rm[i] * 256;
		    *(image->rgb.green + i) = gm[i] * 256;
		    *(image->rgb.blue + i) = bm[i] * 256;
	       }
	       image->rgb.used = cmsize;
	  }
	  op = image->data;
	  for (y = 0; y < height; y++) {
	       if (zread(zf, op, width) != width) {
		    printf("%s: short read line %d\n", image->title, y);
		    return(0); /* show what we got */
	       }
	       op += width;
	  }
     } else {
	  op = image->data;
	  skip = bands - 3;
	  linewidth = bands * width;
	  buf = (unsigned char *)lmalloc((unsigned)linewidth);
	  if (buf == NULL) {
	       memoryExhausted();    
	  }
	  for (y = 0; y < height; y++) {
	       ip = buf;
	       if (zread(zf, buf, linewidth) != linewidth) {
		    printf("%s: short read line %d\n", image->title, y);
		    return(0); /* show what we got */
	       }
	       for (x = 0; x < width; x++) {
		    ip += skip;
		    *(op++) = *(ip+2);
		    *(op++) = *(ip+1);
		    *(op++) = *(ip);
		    ip += 3;
	       }
	  }
	  free(buf);
     }
     
     return(0);
}

static int
parseHeader()
{
     int bits;
     int line;

     /* KISS ? */

     for (line = 0; ((line < MAX_HEAD_LINES) && (header[line] != NULL)); line++) {
	  if (strncmp(header[line], "type", 4) == 0) {
	       if (strncmp(header[line], "type raster", 11) != 0) {
		    printf("Only Sun VFF files of type `raster' are supported.\n");
		    freeheader();
		    return(1);
	       }
	  }
	  if (strncmp(header[line], "bands", 5) == 0) {
	       sscanf(header[line], "bands %d", &bands);
	  }
	  if (strncmp(header[line], "bits", 4) == 0) {
	       sscanf(header[line], "bits %d", &bits); /* only check one band */
	       if (bits != 8) {
		    printf("Only Sun VFF files with 8-bit bands are supported.\n");
		    freeheader();
		    return(1);    
	       }
	  }
	  if (strncmp(header[line],"components", 10) == 0) {
	       printf("Only Sun VFF files of type `raster' are supported.\n");
	       freeheader();
	       return(1);
	  }
	  if ((strncmp(header[line], "colormap", 8) == 0) &&
	      (strncmp(header[line], "colormapsize", 12) != 0)) {
	      {
		   char *cp;
		   int i, r, g, b;

		   /* ship colormap */
		   cp = header[line]+8;
		   /* find the first entry */
		   while (*cp && !isxdigit(*cp)) {
			cp++;
		   }
		   for (i = 0; i < 256; i++) {
			if (sscanf(cp, "%02x%02x%02x", &b, &g, &r) != 3)
			     break;
			bm[i] = b;
			gm[i] = g;
			rm[i] = r;
			cp += 6;
			while (*cp && !isxdigit(*cp)) {
			     cp++;
			}
		   }
		   cmsize = i;
	      }
 
	  }
	  if (strncmp(header[line], "size", 4) == 0) {
	       sscanf(header[line], "size %d %d", &width, &height);
	  }
     }
     freeheader();
     return(0);
}

static void
freeheader()
{
     int line;

     for (line = 0; ((line < MAX_HEAD_LINES) && (header[line] != NULL)); line++) {
	  free(header[line]);
    }
}

static int
readHeader(zf)
ZFILE *zf;
{
     char buf[HEAD_BUF_SIZE];
     int count, c, lines;
     char *cp;
     
     cp = buf;
     count = 0;
     lines = 0;

     while (lines < MAX_HEAD_LINES) {
	  while ((((c = zgetc(zf)) != '\f')) && (c != EOF) && (c != '=')) {
	       switch(c) {
	       case '\n':
	       case ' ':
	       case '\t':
	       case '\r':
		    continue;
	       
	       case ';':
		    printf("Malformed Sun VFF header line: a `;' was found before a `='.\n" );
		    return(1);
		    default:
		    if (count < HEAD_BUF_SIZE) {
			 *cp = (char)c;
			 cp++;
			 count++;
		    } else {
			 printf("Malformed Sun VFF header line: a line longer then %d bytes was found.\n", HEAD_BUF_SIZE);
			 return(0); /* go ahead and try to parse what we got (geometry file?) */
		    }
		    break;     
	       }
	  }


	  if (c == '=') {
	       if (count < HEAD_BUF_SIZE) {
		    *cp = ' ';
		    cp++;
		    count++;
	       } else {
		    printf("Malformed Sun VFF header line: a line longer then %d bytes was found.\n", HEAD_BUF_SIZE);
		    return(0);
	       
	       }
	
	       while ((c = zgetc(zf)) != ';') {
		    switch(c) {
		    case EOF:
			 printf("Malformed Sun VFF header line: a `EOF' was found before a `;'.\n" );
			 return(1);
		    case '\f':
			 printf("Malformed Sun VFF header line: a `\f' was found before a `;'.\n" );
			 return(1);
		    case '\n':
			 printf("Malformed Sun VFF header line: a `\\n' was found before a `;'.\n" );
			 return(1);
		    case '\\':
			 if ((c = zgetc(zf)) == EOF) {
			      printf("Malformed Sun VFF header line: a `EOF' was found after a `\\'.\n" );
			      return(1);
			 }
			 default:
			 if (count < HEAD_BUF_SIZE) {
			      *cp = (char)c;
			      cp++;
			      count++;
			 } else {
			      printf("Malformed Sun VFF header line: a line longer then %d bytes was found.\n", HEAD_BUF_SIZE);
			      return(0);
			 }

		    }
	       }
	       if (count < HEAD_BUF_SIZE) {
		    *cp = '\0';
		    cp++;
		    count++;
	       } else {
		    printf("Malformed Sun VFF header line: a line longer then %d bytes was found.\n", HEAD_BUF_SIZE-1);
		    return(0);
	       }

	       if ((header[lines] = (char *)lmalloc((unsigned)count+1)) == NULL) {
		    memoryExhausted();    
	       }
	       bcopy(buf, header[lines], count);
	       count = 0;
	       cp = buf;
	       lines++;
	  } else if (c == EOF) {
	       printf("Malformed Sun VFF header line: EOF found before the end of the header.\n");
	       return(1);  
	  } else if (c == '\f') {
	       /* read the \n after the \f */
	       c = zgetc(zf);
	       if (c == EOF) {
		    printf("Malformed Sun VFF header line: EOF found before the end of the header.\n");
		    return(1);
	       }
	       return(0);
	  }
     }
     printf("Malformed Sun VFF header: more then %d lines found.\n", MAX_HEAD_LINES);
     return(0); /* so that we try and see what was wrong */
}

int vffIdent(fullname, name)
char *fullname, *name;
{

     ZFILE *zf;
     char buf[4];

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

     if (zread(zf, (unsigned char *)buf, 4) != 4) {
	  zclose(zf);
	  return(0);
     }

     if (strncmp(buf, "ncaa", 4) != 0) {
	  zclose(zf);
	  return(0);
     }

     if (readHeader(zf)) {
	  zclose(zf);
	  return(0);
     }

     if (parseHeader()) {
	  zclose(zf);
	  return(0);
     }

     if (bands == 1) {
	  printf("%s is a %dx%d Sun VFF image with 1 band\n", name, width, height);
     } else {
	  printf("%s is a %dx%d Sun VFF image with %d bands\n", name, width, height, bands);
     }
     return(1);
}