File: cmuwmraster.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 (155 lines) | stat: -rw-r--r-- 2,763 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
/*
 *
 * handle CMU Window Manager (ITC) raster image type
 *
 * dan lovinger (dl2n+@andrew.cmu.edu) 07.11.90
 *
 * the format is essentially a byte-reversed sun raster w/o encoding
 *
 * Copyright 1989 Jim Frost.  See included file "copyright.h" for complete
 * copyright information.
 */

#include "copyright.h"
#include "image.h"
#include "cmuwmraster.h"

/* SUPPRESS 558 */

void babble(name, headerp)
char *name;
struct cmuwm_header *headerp;
{
    printf("%s is a %ldx%ld %ld plane CMU WM raster\n",
	   name,
	   memToVal(headerp->width, sizeof(long)),
	   memToVal(headerp->height, sizeof(long)),
	   memToVal(headerp->depth, sizeof(short)));
}

int cmuwmIdent(fullname, name)
char *fullname, *name;
{
    ZFILE *zf;
    struct cmuwm_header header;
    int r;

    if (!(zf = zopen(fullname)))
      {
	  perror("cmuwmIdent");
	  return(0);
      }

    switch (zread(zf, (byte *)&header, sizeof(struct cmuwm_header)))
      {
      case -1:
	  perror("cmuwmIdent");
	  r =0;
	  break;

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

      default:
	  r = 0;
	  break;
      }

    zclose(zf);

    return r;
}

Image* cmuwmLoad(fullname, name, verbose)
char *fullname, *name;
unsigned int verbose;
{
    ZFILE *zf;
    struct cmuwm_header header;
    Image *image;
    int height, width, row, linelen, r;
    byte *lineptr;

    if (!(zf= zopen(fullname))) 
      {
	  perror("cmuwmLoad");
	  return(NULL);
      }

    switch (zread(zf, (byte *)&header, sizeof(struct cmuwm_header))) 
      {
      case -1:
	  perror("cmuwmLoad");
	  zclose(zf);
	  exit(1);

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

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

    if (memToVal(header.depth, sizeof(short)) != 1)
      {
	  fprintf(stderr,"CMU WM raster %s is of depth %d, must be 1",
		  name,
		  (int) header.depth);
	  return(NULL);
      }

    image = newBitImage(width = memToVal(header.width, sizeof(long)),
			height = memToVal(header.height, sizeof(long)));

    linelen = (width / 8) + (width % 8 ? 1 : 0);
    lineptr = image->data;

    for (row = 0; row < height; row++)
      {
	  r = zread(zf, lineptr, linelen);

	  if (r == -1)
	    {
		perror("cmuwmLoad");
		exit(1);
	    }
	  
	  if (r != linelen)
	    {
		printf("cmuwmLoad: short raster\n");
		exit(1);
	    }

	  for (r = 0; r < linelen; r++)
	    {
		lineptr[r] ^= 0xff;
	    }

	  lineptr += linelen;
      }

    zclose(zf);

    image->title = dupString(name);

    return image;
}