File: cmuwmrast.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 (125 lines) | stat: -rw-r--r-- 2,564 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
/*
 * 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 "xli.h"
#include "cmuwmrast.h"
#include "imagetypes.h"

/* SUPPRESS 558 */

static void babble(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(char *fullname, char *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 sizeof(struct cmuwm_header):
		if (memToVal(header.magic, sizeof(long)) != CMUWM_MAGIC) {
			r = 0;
			break;
		}
		babble(name, &header);
		r = 1;
		break;

	case -1:
		perror("cmuwmIdent");
	default:
		r = 0;
		break;
	}

	zclose(zf);
	return r;
}


Image *cmuwmLoad(char *fullname, ImageOptions * image_ops, boolean verbose)
{
	ZFILE *zf;
	char *name = image_ops->name;
	struct cmuwm_header header;
	Image *image;
	int height, width, row, linelen, r;
	long depth;
	byte *lineptr;

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

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

	default:
		zclose(zf);
		return (Image *) 0;
	}

	depth = memToVal(header.depth, sizeof(short));
	if (depth != 1) {
		fprintf(stderr,
			"cmuwmLoad: %s - raster is of depth %ld, must be 1\n",
			name, depth);
		return (Image *) 0;
	}
	image = newBitImage(width = memToVal(header.width, sizeof(long)),
			 height = memToVal(header.height, sizeof(long)));

	image->title = dupString(name);
	linelen = (width / 8) + (width % 8 ? 1 : 0);
	lineptr = image->data;
	znocache(zf);

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

		if (r != linelen) {
			printf("cmuwmLoad: %s - short raster\n", name);
			zclose(zf);
			return image;
		}

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

		lineptr += linelen;
	}

	read_trail_opt(image_ops, zf, image, verbose);

	zclose(zf);
	return image;
}