File: picture.c

package info (click to toggle)
xpuzzles 8.8.7%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: ansic: 153,465; makefile: 6,283; sh: 935; perl: 80
file content (338 lines) | stat: -rw-r--r-- 8,488 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
/*
 * @(#)picture.c
 *
 * Taken from xlock, many authors...
 *
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of the author not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 *
 * This program is distributed in the hope that it will be "useful",
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Intrinsic.h>
#include <X11/Xutil.h>
#include "file.h"
#include "picture.h"

#if defined(__cplusplus) || defined(c_plusplus)
#define CLASS c_class
#else
#define CLASS class
#endif

Bool
fixedColors(Visual *visual, int depth, Bool install)
{
#ifdef FORCEFIXEDCOLORS
	/* pretending a fixed colourmap */
	return TRUE;
#else
	/* get information about the default visual */
	return (!(depth > 1 && install &&
		(visual->CLASS != StaticGray) &&
		(visual->CLASS != StaticColor) &&
		(visual->CLASS != TrueColor)));
#endif
}

static int
XbmReadFileToImage(char *filename,
		int *width, int *height, char **bits)
{
	FILE *file;
	int c, c1;
	int i, j, k = 0;
	char *pix;
	char line[256], name[256];
	unsigned char hex[256];

	if ((file = fopen(filename, "r")) == NULL) {
		/*(void) fprintf(stderr, "could not read file \"%s\"\n", filename); */
		return BitmapOpenFailed;
	}
	/* read width: skip lines until we hit a #define */
	for (;;) {
		if (!fgets(line, 256, file)) {
			/* not a xbm file */
			(void) fclose(file);
			return BitmapFileInvalid;
		}
		if (strncmp(line, "#define", (size_t) 7) == 0 && sscanf(line,
				"#define %s %d", name, width) == 2 &&
				strcmp(name, "_width"))
			break;
	}

	/* read height: skip lines until we hit another #define */
	for (;;) {
		if (!fgets(line, 256, file)) {
			(void) fclose(file);
			(void) fprintf(stderr, "EOF reached in header info.\n");
			return BitmapFileInvalid;
		}
		if (strncmp(line, "#define", (size_t) 7) == 0 && sscanf(line,
				"#define %s %d", name, height) == 2 &&
				strcmp(name, "_height"))
			break;
	}
	/* scan forward until we see the first '0x' */
	c = getc(file);
	c1 = getc(file);
	while (c1 != EOF && !(c == '0' && c1 == 'x')) {
		c = c1;
		c1 = getc(file);
	}
	if (c1 == EOF) {
		(void) fclose(file);
		(void) fprintf(stderr, "No bitmap data found\n");
		return BitmapFileInvalid;
	}
	if (*width < 1 || *height < 1 || *width > 10000 || *height > 10000) {
		(void) fclose(file);
		(void) fprintf(stderr, "Not an xbm file");
		return BitmapFileInvalid;
	}
	*bits = (char *) calloc((size_t) ((*width + 7) / 8) * (*height),
		(size_t) 8);
	if (!*bits) {
		(void) fclose(file);
		(void) fprintf(stderr, "Could not malloc bits\n");
		return BitmapNoMemory;
	}
	/* initialize the 'hex' array for zippy ASCII-hex -> int conversion */

	for (i = 0; i < 256; i++)
		hex[i] = 255;	/* flag 'undefined' chars */
	for (i = '0'; i <= '9'; i++)
		hex[i] = i - '0';
	for (i = 'a'; i <= 'f'; i++)
		hex[i] = i + 10 - 'a';
	for (i = 'A'; i <= 'F'; i++)
		hex[i] = i + 10 - 'A';

	/* read the image data */

	for (i = 0, pix = *bits; i < *height; i++)
		for (j = 0; j < (*width + 7) / 8; j++, pix++) {
			/* Get next byte from file. We are already positioned at it */
			c = getc(file);
			c1 = getc(file);
			if (c < 0 || c1 < 0) {
				/* EOF: break out of loop */
				c = c1 = '0';
				i = *height;
				j = *width;
				(void) fclose(file);
				(void) fprintf(stderr,
					"The file would appear to be truncated.\n");
				return BitmapFileInvalid;
			}
			if (hex[c1] == 255) {
				if (hex[c] == 255)
					k = 0;	/* no digits after the '0x' ... */
				else
					k = hex[c];
			} else
				k = (hex[c] << 4) + hex[c1];

			/* advance to next '0x' */
			c = getc(file);
			c1 = getc(file);
			while (c1 != EOF && !(c == '0' && c1 == 'x')) {
				c = c1;
				c1 = getc(file);
			}
			*pix = k;
		}
	(void) fclose(file);
	return BitmapSuccess;
}
#ifdef HAVE_XPM
#include <X11/xpm.h>
#endif

static XImage bpicture =
{
	0, 0,			/* width, height */
	0, XYBitmap, 0,		/* xoffset, format, data */
	LSBFirst, 8,		/* byte-order, bitmap-unit */
	LSBFirst, 8, 1		/* bitmap-bit-order, bitmap-pad, depth */
};

Bool
getImage(Display *display, Window window, Visual *visual, Colormap colormap,
	int depth, XImage **picture, char *filename, Bool install,
	int *graphicsFormat, Colormap *ncm)
{
	static char *bitmap_local = (char *) NULL;
	Bool rtn = True;

#ifdef HAVE_XPM
	XpmAttributes attrib;

#endif
	if (!fixedColors(visual, depth, install)) {
		*ncm = XCreateColormap(display, window, visual, AllocNone);
	} else {
		*ncm = None;
	}
#ifdef HAVE_XPM
	attrib.visual = visual;
	if (*ncm == None) {
		attrib.colormap = colormap;
	} else {
		attrib.colormap = *ncm;
	}
	attrib.depth = depth;
	attrib.closeness = 40000;
	attrib.valuemask = XpmVisual | XpmColormap | XpmDepth | XpmCloseness;
#endif
	*graphicsFormat = 0;

	if (bitmap_local != NULL) {
		free(bitmap_local);
		bitmap_local = (char *) NULL;
	}
	if (filename && strlen(filename)) {
		if ((bitmap_local = (char *) malloc(256)) == NULL) {
			(void) fprintf(stderr , "no memory for \"%s\"\n" ,
				filename);
			return False;
		}
		(void) strncpy(bitmap_local, filename, 255);
	}
	bitmap_local = findFile(bitmap_local);
	if (bitmap_local && strlen(bitmap_local)) {
#ifdef HAVE_XPM
#ifndef USE_MONOXPM
		if (depth > 1)
#endif
		{
			if (*graphicsFormat <= 0) {
				if (XpmSuccess == XpmReadFileToImage(display,
						bitmap_local, picture,
						(XImage **) NULL, &attrib))
					*graphicsFormat = IS_XPMFILE;
			}
		}
#endif
		if (*graphicsFormat <= 0) {
			if (!bpicture.data) {
				if (BitmapSuccess == XbmReadFileToImage(findFile(bitmap_local),
						&bpicture.width, &bpicture.height,
						&bpicture.data)) {
					bpicture.bytes_per_line = (bpicture.width + 7) / 8;
					*graphicsFormat = IS_XBMFILE;
					*picture = &bpicture;
				}
			} else {
				*graphicsFormat = IS_XBMDONE;
				*picture = &bpicture;
			}
		}
		if (*graphicsFormat <= 0) {
			(void) fprintf(stderr,
				"\"%s\" is in an unrecognized format or not compatible with screen\n",
				bitmap_local);
			rtn = False;
		}
#if 0
		if (bitmap_local != NULL) {
			free(bitmap_local);
			bitmap_local = (char *) NULL;
		}
#endif
	}
	return rtn;
}

void
destroyImage(XImage **picture, int *graphicsFormat)
{
	switch (*graphicsFormat) {
	case IS_XBM:
		bpicture.data = (char *) NULL;
		break;
	case IS_XBMFILE:
		if (bpicture.data) {
			free(bpicture.data);
			bpicture.data = (char *) NULL;
		}
		break;
	case IS_XPM:
	case IS_XPMFILE:
	case IS_RASTERFILE:
	case IS_MAGICKFILE:
		if (picture && *picture)
			(void) XDestroyImage(*picture);
		break;
	}
	*graphicsFormat = -1;
	*picture = (XImage *) NULL;
}

#if 0
static void
pickPixmap(Display * display, Drawable drawable, char *name,
	int default_width, int default_height, unsigned char *default_bits,
	int *width, int *height, Pixmap * pixmap,
	int *graphicsFormat) {
	int x_hot, y_hot; /* dummy */

	name = findFile(name);
	if (name && *name) {
		if (BitmapSuccess == XReadBitmapFile(display, drawable, name,
				(unsigned int *) width,
				(unsigned int *) height,
				pixmap, &x_hot, &y_hot)) {
			*graphicsFormat = IS_XBMFILE;
		}
		if (*graphicsFormat <= 0)
			(void) fprintf(stderr,
				"\"%s\" not xbm format\n", name);
	}
	if (*graphicsFormat <= 0) {
		*width = default_width;
		*height = default_height;
		*graphicsFormat = IS_XBM;
		*pixmap = XCreateBitmapFromData(display, drawable,
			(char *) default_bits, *width, *height);
	}
}

static void
getPixmap(Display *display, Drawable drawable, char *filename,
	int default_width, int default_height, unsigned char *default_bits,
	int *width, int *height, Pixmap * pixmap, int *graphicsFormat)
{
	static char *bitmap_local = (char *) NULL;

	if (bitmap_local) {
		free(bitmap_local);
		bitmap_local = (char *) NULL;
	}
	if (filename && strlen(filename)) {
		if ((bitmap_local = (char *) malloc(256)) == NULL) {
			(void) fprintf(stderr , "no memory for \"%s\"\n" ,
				filename);
			return;
		}
		(void) strncpy(bitmap_local, filename, 255);
	}
	pickPixmap(display, drawable, bitmap_local,
		default_width, default_height, default_bits,
		width, height, pixmap, graphicsFormat);
}
#endif