File: sunraster.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 (369 lines) | stat: -rw-r--r-- 8,249 bytes parent folder | download | duplicates (5)
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
/* sunraster.c:
 *
 * sun rasterfile image type
 *
 * jim frost 09.27.89
 *
 * Copyright 1989, 1991 Jim Frost.
 * See included file "copyright.h" for complete copyright information.
 */

#include "copyright.h"
#include "image.h"
#include "sunraster.h"

/* SUPPRESS 558 */
/* SUPPRESS 560 */

static void babble(name, header)
     char           *name;
     struct rheader *header;
{
  printf("%s is a", name);
  switch (memToVal(header->type, 4)) {
  case ROLD:
    printf("n old-style");
    break;
  case RSTANDARD:
    printf(" standard");
    break;
  case RRLENCODED:
    printf(" run-length encoded");
    break;
  case RRGB:
    printf(" RGB"); /* RGB format instead of BGR */
    break;
  case RTIFF:
    printf(" TIFF");
    break;
  case RIFF:
    printf(" RIFF");
    break;
  default:
    printf(" unknown-type");
  }
  printf(" %ldx%ld", memToVal(header->width, 4), memToVal(header->height, 4));

  switch (memToVal(header->depth, 4)) {
  case 1:
    printf(" monochrome");
    break;
  case 8:
    printf(" 8 plane %s",
	   memToVal(header->maplen, 4) > 0 ? "color" : "greyscale");
    break;
  case 24:
    printf(" 24 plane color");
    break;

  case 32:
    /* isn't it nice how the sunraster.h file doesn't bother to mention that
     * 32-bit depths are allowed?
     */
    printf(" 32 plane color");
    break;
  }
  printf(" Sun rasterfile\n");
}

int sunRasterIdent(fullname, name)
     char *fullname, *name;
{ ZFILE          *zf;
  struct rheader  header;
  int             r;

  if (! (zf= zopen(fullname))) {
    perror("sunRasterIdent");
    return(0);
  }
  switch (zread(zf, (byte *)&header, sizeof(struct rheader))) {
  case -1:
    perror("sunRasterIdent");
    r= 0;
    break;

  case sizeof(struct rheader):
    if (memToVal(header.magic, 4) != RMAGICNUMBER) {
      r= 0;
      break;
    }
    babble(name, &header);
    r= 1;
    break;

  default:
    r= 0;
    break;
  }
  zclose(zf);
  return(r);
}

/* read either rl-encoded or normal image data
 */

static void sunread(zf, buf, len, enc)
     ZFILE        *zf;
     byte         *buf;
     unsigned int  len;
     unsigned int  enc;  /* true if encoded file */
{ static byte repchar, remaining= 0;

  /* rl-encoded read
   */

  if (enc) {
    while (len--)
      if (remaining) {
	remaining--;
	*(buf++)= repchar;
      }
      else {
	if (zread(zf, &repchar, 1) != 1) {
	  printf("sunRasterLoad: Bad read on image data\n");
	  exit(1);
	}
	if (repchar == RESC) {
	  if (zread(zf, &remaining, 1) != 1) {
	    printf("sunRasterLoad: Bad read on image data\n");
	    exit(1);
	  }
	  if (remaining == 0)
	    *(buf++)= RESC;
	  else {
	    if (zread(zf, &repchar, 1) != 1) {
	      printf("sunRasterLoad: Bad read on image data\n");
	      exit(1);
	    }
	    *(buf++)= repchar;
	  }
	}
	else
	  *(buf++)= repchar;
      }
  }

  /* normal read
   */

  else {
    if (zread(zf, buf, len) < len) {
      printf("sunRasterLoad: Bad read on image data\n");
      exit(1);
    }
  }
}

Image *sunRasterLoad(fullname, name, verbose)
     char         *fullname, *name;
     unsigned int  verbose;
{ ZFILE          *zf;
  struct rheader  header;
  unsigned int    mapsize;
  byte           *map;
  byte           *mapred, *mapgreen, *mapblue;
  unsigned int    depth;
  unsigned int    linelen;   /* length of raster line in bytes */
  unsigned int    fill;      /* # of fill bytes per raster line */
  unsigned int    enc;
  unsigned int    rgb_format;
  byte            fillchar;
  Image          *image;
  byte           *lineptr;
  unsigned int    x, y;

  if (! (zf= zopen(fullname))) {
    perror("sunRasterLoad");
    return(NULL);
  }
  switch (zread(zf, (byte *)&header, sizeof(struct rheader))) {
  case -1:
    perror("sunRasterLoad");
    zclose(zf);
    exit(1);

  case sizeof(struct rheader):
    if (memToVal(header.magic, 4) != RMAGICNUMBER) {
      zclose(zf);
      return(NULL);
    }
    if (verbose)
      babble(name, &header);
    break;

  default:
    zclose(zf);
    return(NULL);
  }

  znocache(zf); /* turn off caching; we don't need it anymore */

  /* get an image to put the data in
   */

  depth= memToVal(header.depth, 4);
  switch(depth) {
  case 1:
    image= newBitImage(memToVal(header.width, 4),
		       memToVal(header.height, 4));
    break;
  case 8:
    image= newRGBImage(memToVal(header.width, 4),
		       memToVal(header.height, 4),
		       memToVal(header.depth, 4));
    break;
  case 24:
  case 32:
    image= newTrueImage(memToVal(header.width, 4),
		       memToVal(header.height, 4));
    break;
  default:
    printf("sunRasterLoad: Bad depth %d (only 1, 8, 24 are valid)\n", depth);
    exit(1);
  }

  /* figure out which format it is
   */

  enc= 0;
  rgb_format= 0;
  switch (memToVal(header.type, 4)) {
  case ROLD:
  case RSTANDARD:
    break;
  case RRLENCODED:
    enc= 1;
    break;
  case RRGB:
    rgb_format= 1;
    break;
  case RTIFF: /* sorry, don't even know what these are */
  case RIFF:
  default:
    fprintf(stderr, "%s: Unsupported Sun Rasterfile image type (sorry)\n",
	    name);
    return(NULL);
  }

  /* set up the colormap
   */

  if (depth == 1)
    linelen= (image->width / 8) + (image->width % 8 ? 1 : 0);
  else
    linelen= image->width * image->pixlen;
  fill= (linelen % 2 ? 1 : 0);
  /*
   *  Handle color...
   */
  if ((mapsize= memToVal(header.maplen, 4))) {
    map= lmalloc(mapsize);
    if (zread(zf, map, mapsize) < mapsize) {
      printf("sunRasterLoad: Bad read on colormap\n");
      exit(1);
    }
    mapsize /= 3;
    mapred= map;
    mapgreen= mapred + mapsize;
    mapblue= mapgreen + mapsize;
    if (image->rgb.size == 0)
	newRGBMapData(&image->rgb, mapsize);
    else if (mapsize > image->rgb.size)
	mapsize= image->rgb.size;
    for (y= 0; y < mapsize; y++) {
      *(image->rgb.red + y)= (*(mapred++) << 8);
      *(image->rgb.green + y)= (*(mapgreen++) << 8);
      *(image->rgb.blue + y)= (*(mapblue++) << 8);
    }
    lfree(map);
    image->rgb.used= mapsize;
  }

  /*
   *  Handle 8-bit greyscale via a simple ramp function...
   */
  else if (depth == 8) {
    mapsize = 256*3;
    map= lmalloc(mapsize);
    for (y = 0; y < 256; y += 1) {
      map[y] = map[256+y] = map[2*256+y] = y;
    }
    mapsize /= 3;
    mapred= map;
    mapgreen= mapred + mapsize;
    mapblue= mapgreen + mapsize;
    if (image->rgb.size == 0)
	newRGBMapData(&image->rgb, mapsize);
    for (y= 0; y < mapsize; y++) {
      *(image->rgb.red + y)= (*(mapred++) << 8);
      *(image->rgb.green + y)= (*(mapgreen++) << 8);
      *(image->rgb.blue + y)= (*(mapblue++) << 8);
    }
    lfree(map);
    image->rgb.used= mapsize;
  }
  /* 24-bit and 32-bit handle themselves.  currently we don't support
   * a colormap for them.
   */

  lineptr= image->data;

  /* if it's a 32-bit image, we read the line and then strip off the
   * top byte of each pixel to get truecolor format
   */

  if (depth >= 24) {
    byte *buf, *bp;

    buf= lmalloc(image->width * (depth == 24 ? 3 : 4));
    for (y= 0; y < image->height; y++) {
      sunread(zf, buf, image->width * (depth == 24 ? 3 : 4), enc);
      bp= buf;
      if (depth == 24) {
	if (rgb_format)
	  for (x= 0; x < image->width; x++) {
	    *(lineptr++)= *(bp++); /* red */
	    *(lineptr++)= *(bp++); /* green */
	    *(lineptr++)= *(bp++); /* blue */
	  }
	else
	  for (x= 0; x < image->width; x++) {
	    *(lineptr++)= *(bp + 2); /* red */
	    *(lineptr++)= *(bp + 1); /* green */
	    *(lineptr++)= *bp;       /* blue */
	    bp += 3;
	  }
      }
      else {
	if (rgb_format)
	  for (x= 0; x < image->width; x++) {
	    bp++;                  /* skip byte */
	    *(lineptr++)= *(bp++); /* red */
	    *(lineptr++)= *(bp++); /* green */
	    *(lineptr++)= *(bp++); /* blue */
	  }
	else
	  for (x= 0; x < image->width; x++) {
	    *(lineptr++)= *(bp + 3); /* red */
	    *(lineptr++)= *(bp + 2); /* green */
	    *(lineptr++)= *(bp + 1); /* blue */
	    bp += 4;
	  }
      }
      if (fill)
	sunread(zf, &fillchar, fill, enc);
    }
    lfree(buf);
  }
  else {
    for (y= 0; y < image->height; y++) {
      sunread(zf, lineptr, linelen, enc);
      lineptr += linelen;
      if (fill)
	sunread(zf, &fillchar, fill, enc);
    }
  }
  zclose(zf);
  image->title= dupString(name);
  return(image);
}