File: xwd.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 (654 lines) | stat: -rw-r--r-- 19,835 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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/* xwd.c:
 *
 * XWD file reader.  unfortunately the bozo who thought up this format didn't
 * define anything at all that we can use as an identifier or even to tell
 * what kind of machine dumped the format.  what this does is read the
 * header and look at several fields to decide if this *might* be an XWD
 * file and if it is what byte order machine wrote it.
 *
 * jim frost 07.24.90
 *
 * Copyright 1990 Jim Frost.  See included file "copyright.h" for complete
 * copyright information.
 */

#include "copyright.h"
#include "xli.h"
#include "imagetypes.h"
#include "xwd.h"

/* this reads the header and does the magic to determine if it is indeed
 * an XWD file.
 */

static int isXWD(char *name, ZFILE * zf, XWDHeader * header, int verbose)
{
	GenericXWDHeader gh;
	int a;
	int type = NOT_XWD;

	if (zread(zf, (byte *) & gh, sizeofGenericXWDHeader) != sizeofGenericXWDHeader)
		return (0);

	/* first try -- see if XWD version number matches in either MSB or LSB order
	 */

	if (memToVal(gh.file_version, 4) != XWD_VERSION) {
		if (memToValLSB(gh.file_version, 4) != XWD_VERSION)
			return (0);
		else
			type = XWD_LSB;
	} else
		type = XWD_MSB;

	/* convert fields to fill out header.  things we don't care about
	 * are commented out.
	 */

	if (type == XWD_MSB) {
		header->header_size = memToVal(gh.header_size, 4);
		header->file_version = memToVal(gh.file_version, 4);
		header->pixmap_format = memToVal(gh.pixmap_format, 4);
		header->pixmap_depth = memToVal(gh.pixmap_depth, 4);
		header->pixmap_width = memToVal(gh.pixmap_width, 4);
		header->pixmap_height = memToVal(gh.pixmap_height, 4);
		header->xoffset = memToVal(gh.xoffset, 4);
		header->byte_order = memToVal(gh.byte_order, 4);
		header->bitmap_unit = memToVal(gh.bitmap_unit, 4);
		header->bitmap_bit_order = memToVal(gh.bitmap_bit_order, 4);
		header->bitmap_pad = memToVal(gh.bitmap_pad, 4);
		header->bits_per_pixel = memToVal(gh.bits_per_pixel, 4);
		header->bytes_per_line = memToVal(gh.bytes_per_line, 4);
		header->visual_class = memToVal(gh.visual_class, 4);
		/*header->red_mask= memToVal(gh.red_mask, 4); */
		/*header->green_mask= memToVal(gh.green_mask, 4); */
		/*header->blue_mask= memToVal(gh.blue_mask, 4); */
		/*header->bits_per_rgb= memToVal(gh.bits_per_rgb, 4); */
		header->colormap_entries = memToVal(gh.colormap_entries, 4);
		header->ncolors = memToVal(gh.ncolors, 4);
		/*header->window_width= memToVal(gh.window_width, 4); */
		/*header->window_height= memToVal(gh.window_height, 4); */
		/*header->window_x= memToVal(gh.window_x, 4); */
		/*header->window_y= memToVal(gh.window_y, 4); */
		/*header->window_bdrwidth= memToVal(gh.window_bdrwidth, 4); */
	} else {
		header->header_size = memToValLSB(gh.header_size, 4);
		header->file_version = memToValLSB(gh.file_version, 4);
		header->pixmap_format = memToValLSB(gh.pixmap_format, 4);
		header->pixmap_depth = memToValLSB(gh.pixmap_depth, 4);
		header->pixmap_width = memToValLSB(gh.pixmap_width, 4);
		header->pixmap_height = memToValLSB(gh.pixmap_height, 4);
		header->xoffset = memToValLSB(gh.xoffset, 4);
		header->byte_order = memToValLSB(gh.byte_order, 4);
		header->bitmap_unit = memToValLSB(gh.bitmap_unit, 4);
		header->bitmap_bit_order = memToValLSB(gh.bitmap_bit_order, 4);
		header->bitmap_pad = memToValLSB(gh.bitmap_pad, 4);
		header->bits_per_pixel = memToValLSB(gh.bits_per_pixel, 4);
		header->bytes_per_line = memToValLSB(gh.bytes_per_line, 4);
		header->visual_class = memToValLSB(gh.visual_class, 4);
		/*header->red_mask= memToValLSB(gh.red_mask, 4); */
		/*header->green_mask= memToValLSB(gh.green_mask, 4); */
		/*header->blue_mask= memToValLSB(gh.blue_mask, 4); */
		/*header->bits_per_rgb= memToValLSB(gh.bits_per_rgb, 4); */
		header->colormap_entries = memToValLSB(gh.colormap_entries, 4);
		header->ncolors = memToValLSB(gh.ncolors, 4);
		/*header->window_width= memToValLSB(gh.window_width, 4); */
		/*header->window_height= memToValLSB(gh.window_height, 4); */
		/*header->window_x= memToValLSB(gh.window_x, 4); */
		/*header->window_y= memToValLSB(gh.window_y, 4); */
		/*header->window_bdrwidth= memToValLSB(gh.window_bdrwidth, 4); */
	}

	/* if header size isn't either 100 or 104 bytes, this isn't an XWD file
	 */

	if (header->header_size < sizeofGenericXWDHeader)
		return (0);

	for (a = header->header_size - sizeofGenericXWDHeader; a; a--)
		zgetc(zf);

	/* look at a variety of the XImage fields to see if they are sane.  if
	 * they are, this passes our tests.
	 */

	switch (header->pixmap_format) {
	case XYBitmap:
	case XYPixmap:
	case ZPixmap:
		break;
	default:
		return (0);
	}

	switch (header->visual_class) {
	case StaticGray:
	case GrayScale:
	case StaticColor:
	case PseudoColor:

		/* the following are unsupported but recognized
		 */

	case TrueColor:
	case DirectColor:
		break;
	default:
		return (0);
	}

	if (verbose) {
		printf("%s is a %dx%d XWD image in ",
		       name, header->pixmap_width, header->pixmap_height);
		switch (header->pixmap_format) {
		case XYBitmap:
			printf("XYBitmap");
			break;
		case XYPixmap:
			printf("%d bit XYPixmap", header->pixmap_depth);
			break;
		case ZPixmap:
			printf("%d bit ZPixmap", header->pixmap_depth);
			break;
		}
		if (type == XWD_LSB)
			printf(" LSB header, ");
		else
			printf(" MSB header, ");
		if (header->pixmap_format != ZPixmap ||
				header->pixmap_depth == 1) {
			if (header->bitmap_bit_order == LSBFirst) {
				printf(" LSB bit, %d units,",
					header->bitmap_unit);
			} else {
				printf(" MSB bit, %d units,",
					header->bitmap_unit);
			}
		}
		if (header->byte_order == LSBFirst)
			printf(" LSB byte order");
		else
			printf(" MSB byte order");
		printf("\n");
	}
	/* if it got this far, we're pretty damned certain we've got the right
	 * file type and know what order it's in.
	 */

	return (type);
}

int xwdIdent(char *fullname, char *name)
{
	ZFILE *zf;
	XWDHeader header;
	int ret;

	if (!(zf = zopen(fullname)))
		return (0);
	ret = isXWD(name, zf, &header, 1);
	zclose(zf);
	return (ret);
}

static Image *loadXYBitmap(char *name, ZFILE * zf, XWDHeader header, int type)
{
	Image *image;
	int dlinelen;		/* length of scan line in data file */
	int ilinelen;		/* length of line within image structure */
	int unit;		/* # of bytes in a bitmap unit */
	int xoffset;		/* xoffset within line */
	int xunits;		/* # of units across the whole scan line */
	int trailer;		/* # of bytes in last bitmap unit on a line */
	int shift;		/* # of bits to shift last byte set */
	int x, y;		/* horizontal and vertical counters */
	byte *line;		/* input scan line */
	byte *dptr, *iptr;	/* image data pointers */

	image = newBitImage(header.pixmap_width, header.pixmap_height);
	ilinelen = (header.pixmap_width / 8) + (header.pixmap_width % 8 ? 1 : 0);
	if (header.bitmap_unit > 7)	/* supposed to be 8, 16, or 32 but appears */
		unit = header.bitmap_unit / 8;	/* to often be the byte count.  this will */
	else			/* accept either. */
		unit = header.bitmap_unit;
	xoffset = (header.xoffset / (unit * 8)) * unit;
	if (header.bytes_per_line)
		dlinelen = header.bytes_per_line;
	else
		dlinelen = unit * header.pixmap_width;
	xunits = (header.pixmap_width / (unit * 8)) +
	    (header.pixmap_width % (unit * 8) ? 1 : 0);
	trailer = unit - ((xunits * unit) - ilinelen);
	xunits--;		/* we want to use one less than the actual # of units */
	shift = (unit - trailer) * 8;
	line = (byte *) lmalloc(dlinelen);

	for (y = 0; y < header.pixmap_height; y++) {
		if (zread(zf, (byte *) line, dlinelen) != dlinelen) {
			fprintf(stderr,
				"xwdLoad: %s -  Short read (returning partial image)\n", name);
			lfree(line);
			return (image);
		}
		dptr = line + xoffset;
		iptr = image->data + (y * ilinelen);

		if (header.bitmap_bit_order == LSBFirst)
			flipBits(line, dlinelen);

		/* The byte swap conditions are a little complex as three things cause a 
		 * need to swap. If the byte_order is wrong we need to swap. If the bit
		 * order is wrong we need to swap the bits across bytes. If the whole
		 * file is byte reversed we need to swap. These swaps may cancel out,
		 * hence the xor expression below.
		 */
		if ((header.byte_order == MSBFirst) ^ (header.bitmap_bit_order == MSBFirst)
		    ^ (type == XWD_MSB))
			for (x = 0; x < xunits; x++) {	/* Don't swap bytes */
				register unsigned long temp;
				temp = memToVal(dptr, unit);
				valToMem(temp, iptr, unit);
				dptr += unit;
				iptr += unit;
		} else
			for (x = 0; x < xunits; x++) {	/* swap bytes */
				register unsigned long temp;
				temp = memToValLSB(dptr, unit);
				valToMem(temp, iptr, unit);
				dptr += unit;
				iptr += unit;
			}

		/* take care of last unit on this line
		 */

		if ((header.byte_order == MSBFirst)
		    ^ (header.bitmap_bit_order == MSBFirst) ^ (type == XWD_MSB)) {
			register unsigned long temp;
			temp = memToVal(dptr, unit);
			valToMem(temp >> shift, iptr, trailer);
		} else {
			register unsigned long temp;
			temp = memToValLSB(dptr, unit);
			valToMem(temp >> shift, iptr, trailer);
		}
	}

	lfree(line);
	return (image);
}

/* this is a lot like the above function but OR's planes together to
 * build the destination.  1-bit images are handled by XYBitmap.
 */

static Image *loadXYPixmap(char *name, ZFILE * zf, XWDHeader header, int type)
{
	Image *image;
	int plane;
	int dlinelen;		/* length of scan line in data file */
	int ilinelen;		/* length of line within image structure */
	int unit;		/* # of bytes in a bitmap unit */
	int unitbits;		/* # of bits in a bitmap unit */
	int unitmask;		/* mask for current bit within current unit */
	int xoffset;		/* xoffset within data */
	int xunits;		/* # of units across the whole scan line */
	int x, x2, y;		/* horizontal and vertical counters */
	int index;		/* index within image scan line */
	byte *line;		/* input scan line */
	byte *dptr, *iptr;	/* image data pointers */
	unsigned long pixvals;	/* bits for pixels in this unit */
	unsigned long mask;

	image = newRGBImage(header.pixmap_width, header.pixmap_height,
			    header.pixmap_depth);
	ilinelen = image->width * image->pixlen;
	if (header.bitmap_unit > 7)	/* supposed to be 8, 16, or 32 but appears */
		unit = header.bitmap_unit / 8;	/* to often be the byte count.  this will */
	else			/* accept either. */
		unit = header.bitmap_unit;
	unitbits = unit * 8;
	unitmask = 1 << (unitbits - 1);
	xoffset = (header.xoffset / unitbits) * unit;
	if (header.bytes_per_line)
		dlinelen = header.bytes_per_line;
	else
		dlinelen = unit * header.pixmap_width;
	xunits = (header.pixmap_width / (unit * 8)) +
	    (header.pixmap_width % (unit * 8) ? 1 : 0);
	line = (byte *) lmalloc(dlinelen);

	/* for each plane, load in the bitmap and or it into the image
	 */

	for (plane = header.pixmap_depth; plane > 0; plane--) {
		for (y = 0; y < header.pixmap_height; y++) {
			if (zread(zf, (byte *) line, dlinelen) != dlinelen) {
				fprintf(stderr,
					"xwdLoad: %s - Short read (returning partial image)\n", name);
				lfree(line);
				return (image);
			}
			dptr = line + xoffset;
			iptr = image->data + (y * ilinelen);
			index = 0;

			if (header.bitmap_bit_order == LSBFirst)
				flipBits(line, dlinelen);

			if ((header.byte_order == MSBFirst)
			    ^ (header.bitmap_bit_order == MSBFirst) ^ (type == XWD_MSB))
				for (x = 0; x < xunits; x++) {
					register unsigned long temp;
					pixvals = memToVal(dptr, unit);
					mask = unitmask;
					for (x2 = 0; x2 < unitbits; x2++) {
						if (pixvals & mask) {
							temp = memToVal(iptr + index, image->pixlen);
							valToMem(temp | (1 << plane), iptr + index, image->pixlen);
						}
						index += image->pixlen;
						if (index > ilinelen) {
							x = xunits;
							break;
						}
						if (!(mask >>= 1))
							mask = unitmask;
					}
					dptr += unit;
			} else
				for (x = 0; x < xunits; x++) {
					register unsigned long temp;
					pixvals = memToValLSB(dptr, unit);
					mask = unitmask;
					for (x2 = 0; x2 < unitbits; x2++) {
						if (pixvals & mask) {
							temp = memToVal(iptr + index, image->pixlen);
							valToMem(temp | (1 << plane), iptr + index, image->pixlen);
						}
						index += image->pixlen;
						if (index > ilinelen) {
							x = xunits;
							break;
						}
						if (!(mask >>= 1))
							mask = unitmask;
					}
					dptr += unit;
				}
		}
	}

	lfree(line);
	return (image);
}
/* this loads a ZPixmap format image.  note that this only supports depths
 * of 4, 8, 16, 24, or 32 bits as does Xlib.  You gotta 6-bit image,
 * you gotta problem.  1-bit images are handled by XYBitmap.
 */

static Image *loadZPixmap(char *name, ZFILE * zf, XWDHeader header, int type)
{
	Image *image;
	int dlinelen;		/* length of scan line in data file */
	int ilinelen;		/* length of scan line in image file */
	int depth;		/* depth rounded up to 8-bit value */
	int pixlen;		/* length of pixel in bytes */
	int x, y;		/* horizontal and vertical counters */
	byte *line;		/* input scan line */
	byte *dptr, *iptr;	/* image data pointers */
	unsigned long pixmask;	/* bit mask within pixel */
	unsigned long pixel;	/* pixel we're working on */

	image = newRGBImage(header.pixmap_width, header.pixmap_height,
			    header.pixmap_depth);

	/* for pixmaps that aren't simple depths, we round to a depth of 8.  this
	 * is what Xlib does, be it right nor not.
	 */

	if ((header.pixmap_depth != 4) && (header.pixmap_depth % 8))
		depth = header.pixmap_depth + 8 - (header.pixmap_depth % 8);
	else
		depth = header.pixmap_depth;

	pixmask = ((unsigned long) 0xffffffff) >> (32 - header.pixmap_depth);
	pixlen = image->pixlen;
	if (header.bytes_per_line)
		dlinelen = header.bytes_per_line;
	else
		dlinelen = depth * header.pixmap_width;
	ilinelen = image->width * image->pixlen;

	line = (byte *) lmalloc(dlinelen);

	for (y = 0; y < header.pixmap_height; y++) {
		if (zread(zf, (byte *) line, dlinelen) != dlinelen) {
			fprintf(stderr,
				"xwdLoad: %s - Short read (returning partial image)\n", name);
			lfree(line);
			return (image);
		}
		dptr = line;
		iptr = image->data + (y * ilinelen);

		switch (depth) {
		case 4:
			/* The protocol documents says we should take the order of */
			/* 4 bit pixels within a byte from the byte_order field - GWG */
			if (header.byte_order == LSBFirst) {	/* nybbles are reversed */
				for (x = 0; x < header.pixmap_width; x++) {
					pixel = memToVal(dptr, 1);
					valToMem(pixel & pixmask, iptr++, 1);
					if (++x < header.pixmap_width)
						valToMem((pixel >> 4) & pixmask, iptr++, 1);
				}
			} else {	/* MSB first */
				for (x = 0; x < header.pixmap_width; x++) {
					pixel = memToVal(dptr, 1);
					valToMem((pixel >> 4) & pixmask, iptr++, 1);
					if (++x < header.pixmap_width)
						valToMem(pixel & pixmask, iptr++, 1);
				}
			}
			break;
		case 8:
			for (x = 0; x < header.pixmap_width; x++) {
				pixel = ((unsigned long) *(dptr++)) & pixmask;	/* loader isn't needed */
				valToMem(pixel, iptr++, 1);
			}
			break;
		case 16:
			if ((header.byte_order == MSBFirst) ^ (type != XWD_MSB)) {
				for (x = 0; x < header.pixmap_width; x++) {
					pixel = memToVal(dptr, 2);
					valToMem(pixel & pixmask, iptr, 2);
					dptr += 2;
					iptr += 2;
				}
			} else {
				for (x = 0; x < header.pixmap_width; x++) {
					pixel = memToValLSB(dptr, 2);
					valToMem(pixel & pixmask, iptr, 2);
					dptr += 2;
					iptr += 2;
				}
			}
			break;
		case 24:
			if ((header.byte_order == MSBFirst) ^ (type != XWD_MSB)) {
				for (x = 0; x < header.pixmap_width; x++) {
					pixel = memToVal(dptr, 3);
					valToMem(pixel & pixmask, iptr, 3);
					dptr += 3;
					iptr += 3;
				}
			} else {
				for (x = 0; x < header.pixmap_width; x++) {
					pixel = memToValLSB(dptr, 3);
					valToMem(pixel & pixmask, iptr, 3);
					dptr += 3;
					iptr += 3;
				}
			}
			break;
		case 32:
			if ((header.byte_order == MSBFirst) ^ (type != XWD_MSB)) {
				for (x = 0; x < header.pixmap_width; x++) {
					pixel = memToVal(dptr, 4);
					valToMem(pixel & pixmask, iptr, 4);
					dptr += 4;
					iptr += 4;
				}
			} else {
				for (x = 0; x < header.pixmap_width; x++) {
					pixel = memToValLSB(dptr, 4);
					valToMem(pixel & pixmask, iptr, 4);
					dptr += 4;
					iptr += 4;
				}
			}
			break;
		default:
			fprintf(stderr,
				"xwdLoad: %s - ZPixmaps of depth %d are not supported (sorry).\n",
				name, header.pixmap_depth);
			lfree(line);
			freeImage(image);
			return (NULL);
		}
	}

	lfree(line);
	return (image);
}

Image *xwdLoad(char *fullname, ImageOptions * image_ops, boolean verbose)
{
	ZFILE *zf;
	char *name = image_ops->name;
	XWDHeader header;
	int cmaplen;
	XWDColor *cmap;
	byte *cmapptr;		/* Pointer into structure elements */
	Image *image;
	int a;
	int type;

	if (!(zf = zopen(fullname))) {
		perror("xwdLoad");
		return (NULL);
	}
	if (!(type = isXWD(name, zf, &header, verbose))) {
		zclose(zf);
		return (NULL);
	}
	znocache(zf);

	/* complain if we don't understand the visual
	 */

	switch (header.visual_class) {
	case StaticGray:
	case GrayScale:
	case StaticColor:
	case PseudoColor:
		break;
	case TrueColor:
	case DirectColor:
		fprintf(stderr,
			"xwdLoad: %d - Unsupported visual type, sorry\n",
			header.visual_class);
		zclose(zf);
		return (NULL);
	}

	if ((header.pixmap_width == 0) || (header.pixmap_height == 0)) {
		fprintf(stderr, "Zero-size image -- header might be corrupted.\n");
		zclose(zf);
		return (NULL);
	}
	/* read in colormap
	 */

	cmaplen = header.ncolors * sizeofXWDColor;
	cmap = (XWDColor *) lmalloc(cmaplen);
	if (zread(zf, (byte *) cmap, cmaplen) != cmaplen) {
		fprintf(stderr, "Short read in colormap!\n");
		lfree((byte *) cmap);
		zclose(zf);
		return (NULL);
	}
	/* any depth 1 image is basically a XYBitmap so we fake it here
	 */

	if (header.pixmap_depth == 1)
		header.pixmap_format = XYBitmap;

	/* we can't realistically support images of more than depth 16 with the
	 * RGB image format so this nukes them for the time being.
	 */

	if (header.pixmap_depth > 16) {
		fprintf(stderr,
			"%s: Sorry, cannot load images deeper than 16 bits (yet)\n",
			name);
		lfree((byte *) cmap);
		zclose(zf);
		return (NULL);
	}
	/* load the colormap.
	 */

	if (header.pixmap_format == XYBitmap) {
		image = loadXYBitmap(name, zf, header, type);
		image->rgb.used = 2;
		if (type == XWD_MSB)
			for (a = 0, cmapptr = (byte *) cmap;
			     a < 2;
			     a++, cmapptr += sizeofXWDColor) {
				image->rgb.red[a] = memToVal(cmapptr + 4, 2);
				image->rgb.green[a] = memToVal(cmapptr + 6, 2);
				image->rgb.blue[a] = memToVal(cmapptr + 8, 2);
		} else
			for (a = 0, cmapptr = (byte *) cmap;
			     a < 2;
			     a++, cmapptr += sizeofXWDColor) {
				image->rgb.red[a] = memToValLSB(cmapptr + 4, 2);
				image->rgb.green[a] = memToValLSB(cmapptr + 6, 2);
				image->rgb.blue[a] = memToValLSB(cmapptr + 8, 2);
			}
	} else {		/* color format */
		if (header.pixmap_format == XYPixmap)
			image = loadXYPixmap(name, zf, header, type);
		else		/*  ZPixmap */
			image = loadZPixmap(name, zf, header, type);
		if (image == NULL) {
			zclose(zf);
			return NULL;
		}
		image->rgb.used = header.ncolors;
		if (type == XWD_MSB)
			for (a = 0, cmapptr = (byte *) cmap;
			     a < header.ncolors;
			     a++, cmapptr += sizeofXWDColor) {
				int pixelval = memToVal(cmapptr, 4);
				image->rgb.red[pixelval] = memToVal(cmapptr + 4, 2);
				image->rgb.green[pixelval] = memToVal(cmapptr + 6, 2);
				image->rgb.blue[pixelval] = memToVal(cmapptr + 8, 2);
		} else
			for (a = 0, cmapptr = (byte *) cmap;
			     a < header.ncolors;
			     a++, cmapptr += sizeofXWDColor) {
				int pixelval = memToValLSB(cmapptr, 4);
				image->rgb.red[pixelval] = memToValLSB(cmapptr + 4, 2);
				image->rgb.green[pixelval] = memToValLSB(cmapptr + 6, 2);
				image->rgb.blue[pixelval] = memToValLSB(cmapptr + 8, 2);
			}
	}
	read_trail_opt(image_ops, zf, image, verbose);
	zclose(zf);
	image->title = dupString(name);
	lfree((byte *) cmap);
	return (image);
}