File: macimf.c

package info (click to toggle)
xconq 7.1.0-7
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 7,056 kB
  • ctags: 7,960
  • sloc: ansic: 88,493; perl: 2,057; sh: 1,766; makefile: 1,110; csh: 81; awk: 47; lisp: 39
file content (814 lines) | stat: -rw-r--r-- 25,830 bytes parent folder | download
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/* Image families for the Mac interface to Xconq.
   Copyright (C) 1992, 1993, 1994, 1995 Stanley T. Shebs.

Xconq is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.  See the file COPYING.  */

/* Note!  This file does not use the standard "conq.h" header, so can't assume
   all the usual definitions. */
 
#include "config.h"
#include "misc.h"
#include "lisp.h"
#include "imf.h"

#ifdef THINK_C
#include <MacHeaders>
#else /* assume MPW */
#include <Types.h>
#include <Memory.h>
#include <Resources.h>
#include <Quickdraw.h>
#endif /* THINK_C */

#include "macimf.h"

extern int hasColorQD;

CTabHandle interp_ctab(Obj *palette);
CTabHandle synth_ctab(void);
void convert_ppat(Image *img, PixPatHandle colrpat);
void convert_cicn(Image *img, CIconHandle colricon, int *hasmono, int *hasmask);
Obj *convert_ctab(CTabHandle ctabhandle);

MacImage *
init_mac_image(Image *img)
{
	int j;
	MacImage *macimg;

	macimg = (MacImage *) xmalloc(sizeof(MacImage));
	for (j = 0; j < 8; ++j)
	  SET_IMG_PAT (macimg, j, '\0');
	macimg->generic = img;
	return macimg;
}

MacImage *
get_mac_image(Image *img)
{
	MacImage *macimg;

	if (img->hook)
	  return (MacImage *) img->hook;
	macimg = init_mac_image(img);
	img->hook = (char *) macimg;
	return macimg;
}

/* This tries to fill in the given image family from various resources.  The order
   should be "best first", so as to improve search time. */

void
mac_load_imf(imf)
ImageFamily *imf;
{
    int w, h, i, rsize, startlineno = 0, endlineno = 0;
    char tmpstrbuf[100];
    Obj *imfspec;
    Image *img;
    MacImage *macimg;
    Handle imfhandle, pathandle, sicnhandle, iconhandle, maskhandle;
    PicHandle pichandle;
    PixPatHandle ppathandle;
	PixMapHandle pmhandle;
    CIconHandle cicnhandle;
    PixMap pmap;
    Rect bounds;
    Str255 namestr, maskstr, im16x16str, resname;
	short resid;
	ResType restype;

	/* Can't do anything without a name for the image family. */
	if (imf == NULL || imf->name == NULL)
	  return;
	c2p(imf->name, namestr);
	/* The name of the mask is always formed by appending " mask". */
	sprintf(tmpstrbuf, "%s mask", imf->name);
	c2p(tmpstrbuf, maskstr);
	/* The name of the 16x16 cicn is always formed by appending " 16x16". */
	sprintf(tmpstrbuf, "%s 16x16", imf->name);
	c2p(tmpstrbuf, im16x16str);
	/* Look for and load the image family specification resource first. */
	imfhandle = (Handle) GetNamedResource('XCif', namestr);
	if (imfhandle != nil) {
		imfspec = read_form_from_string(copy_string(*imfhandle), &startlineno, &endlineno);
		interp_imf_contents(imf, imfspec);
	}
	pichandle = (PicHandle) GetNamedResource('PICT', namestr);
	if (pichandle != nil) {
		img = get_img(imf, 16, 16); /* should get real bounds */
		if (img != NULL) {
			img->minw = img->minh = 8;
			img->maxw = img->maxh = 9999;
			macimg = get_mac_image(img);
			/* (should distinguish mono and color picts somehow) */
			macimg->monopict = pichandle;
			/* Look for a mask too. */
			pichandle = (PicHandle) GetNamedResource('PICT', maskstr);
			if (pichandle != nil) {
				macimg->maskpict = pichandle;
			}
			img->istile = 0;
		}
	}
	/* (should also be able to pick up picts with rows and columns of images, but
	    would need separate resource to identify which is which) */
	/* Pick up cicns, if we can do color. */
	if (hasColorQD) {
		cicnhandle = (CIconHandle) GetNamedResource('cicn', namestr);
		if (cicnhandle != nil) {
			HLock((Handle) cicnhandle);
			/* Need to get id so we can use special trap. */
			GetResInfo((Handle) cicnhandle, &resid, &restype, resname);
			cicnhandle = GetCIcon(resid);
			pmap = (*cicnhandle)->iconPMap;
			bounds = pmap.bounds;
			w = bounds.right - bounds.left;  h = bounds.bottom - bounds.top;
			img = get_img(imf, w, h);
			if (img != NULL) {
				img->minw = img->minh = 8;
				img->maxw = img->maxh = 128;
				macimg = get_mac_image(img);
				macimg->colricon = (Handle) cicnhandle;
				/* Mask is built in, don't need to load separately. */
			}
			HUnlock((Handle) cicnhandle);
		}
		cicnhandle = (CIconHandle) GetNamedResource('cicn', im16x16str);
		if (cicnhandle != nil) {
			HLock((Handle) cicnhandle);
			/* Need to get id so we can use special trap. */
			GetResInfo((Handle) cicnhandle, &resid, &restype, resname);
			cicnhandle = GetCIcon(resid);
			pmap = (*cicnhandle)->iconPMap;
			bounds = pmap.bounds;
			w = bounds.right - bounds.left;  h = bounds.bottom - bounds.top;
			img = get_img(imf, w, h);
			if (img != NULL) {
				img->minw = img->minh = 8;
				img->maxw = img->maxh = 128;
				macimg = get_mac_image(img);
				macimg->colricon = (Handle) cicnhandle;
				/* Mask is built in, don't need to load separately. */
			}
			HUnlock((Handle) cicnhandle);
		}
	} else {
		/* (should at least try to get the mono part, without using CQD traps) */
	}
	/* Pick up ICONs. */
	iconhandle = (Handle) GetNamedResource('ICON', namestr);
	if (iconhandle != nil) {
		img = get_img(imf, 32, 32);
		if (img != NULL) {
			img->minw = img->minh = 8;
			img->maxw = img->maxh = 128;
			macimg = get_mac_image(img);
			macimg->monoicon = iconhandle;
			/* Look for a mask too. */
			iconhandle = (Handle) GetNamedResource('ICON', maskstr);
			if (iconhandle != nil) {
				macimg->maskicon = iconhandle;
			}
		}
	}
	/* Pick up SICNs. */
	sicnhandle = (Handle) GetNamedResource('SICN', namestr);
	if (sicnhandle != nil) {
		img = get_img(imf, 16, 16);
		if (img != NULL) {
			img->minw = img->minh = 8;
			img->maxw = img->maxh = 64;
			macimg = get_mac_image(img);
			/* Image itself is just the first 32 bytes, mask is second 32 if present. */
			macimg->monosicn = sicnhandle;
			rsize = SizeResource(sicnhandle);
			if (rsize >= 64) {
				maskhandle = (Handle) NewHandle(32);
				for (i = 0; i < 32; ++i) {
					(*maskhandle)[i] = (*sicnhandle)[i+32];
				}
				macimg->masksicn = maskhandle;
			} else {
				/* Mask could be separate resource, so look for it. */
				iconhandle = GetNamedResource('SICN', maskstr);
				if (iconhandle != nil) {
					macimg->masksicn = sicnhandle;
				} else {
					/* no mask to be found */
				}
			}
			img->istile = 0;
		}
	}
	/* Pick up color patterns, if we're capable of doing color. */
	if (hasColorQD) {
		ppathandle = (PixPatHandle) GetNamedResource('ppat', namestr);
		if (ppathandle != nil) {
			/* Need to get the id of the ppat so we can use special trap. */
			GetResInfo((Handle) ppathandle, &resid, &restype, resname);
			ppathandle = GetPixPat(resid);
			pmhandle = (*ppathandle)->patMap;
			switch ((*ppathandle)->patType) {
				case 0:
					/* (should put something in monodata?) */
					w = h = 8;
					break;
				case 1:
					/* Compute the actual size of the pattern. */
					bounds = (*pmhandle)->bounds;
					w = bounds.right - bounds.left;  h = bounds.bottom - bounds.top;
			}
			img = get_img(imf, w, h);
			if (img != NULL) {
				/* Indicate that we can use this pattern for any size area. */
				img->minw = img->minh = 1;
				img->maxw = img->maxh = 9999;
				img->istile = TRUE;
				macimg = get_mac_image(img);
				macimg->colrpat = ppathandle;
				if (w == 8 && h == 8) {
					/* Set the monopat. */
					for (i = 0; i < 8; ++i)
					  SET_IMG_PAT(macimg, i, QD_PAT_ADDR((*ppathandle)->pat1Data)[i]);
					macimg->patdefined = 1;
				}
			}
			/* If the pattern is larger than 8x8, we need to make an 8x8 image for
			   the mono pattern. */
			if (!(w == 8 && h == 8)) {
				img = get_img(imf, 8, 8);
				if (img != NULL) {
					/* Indicate that we can use this pattern for any size area. */
					img->minw = img->minh = 1;
					img->maxw = img->maxh = 9999;
					img->istile = TRUE;
					macimg = get_mac_image(img);
					/* Set the monopat. */
					for (i = 0; i < 8; ++i)
					  SET_IMG_PAT(macimg, i, QD_PAT_ADDR((*ppathandle)->pat1Data)[i]);
					macimg->patdefined = 1;
				}
			}
		}
	}
	/* Load a pattern, which can be used for any size area, but whose "natural" size
	   is always 8x8. */
	pathandle = GetNamedResource('PAT ', namestr);
	if (pathandle != nil) {
		img = get_img(imf, 8, 8);
		if (img != NULL) {
			img->minw = img->minh = 1;
			img->maxw = img->maxh = 9999;
			img->istile = TRUE;
			macimg = get_mac_image(img);
			if (macimg->patdefined) {
				int allzeros = TRUE;

				for (i = 0; i < 8; ++i) {
					if (((char *) &(macimg->monopat))[i] != 0) {
						allzeros = FALSE;
						break;
					}
				}
				/* A mono pattern of all zeros is a default pat from a ppat; overwrite
				   it silently. */
				if (!allzeros) {
					for (i = 0; i < 8; ++i) {
						if (((char *) &(macimg->monopat))[i] != ((char *) *pathandle)[i]) {
							run_warning("ppat/PAT mismatch for \"%s\", overwriting ppat",
										imf->name);
							break;
						}
					}
				}
				for (i = 0; i < 8; ++i)
				  SET_IMG_PAT(macimg, i, ((char *) *pathandle)[i]);
			} else {
				/* Set the monopat. */
				for (i = 0; i < 8; ++i)
				  SET_IMG_PAT(macimg, i, ((char *) *pathandle)[i]);
				macimg->patdefined = 1;
			}
		}
	}
}

/* Given an image family that already has data for it, in Lisp form,
   make platform-specific images. */

void
mac_interp_imf(imf)
ImageFamily *imf;
{
	int w, h, i, numbytes, bitrowbytes, actualw, actualh, pixelsize, rowbytes;
	int monodone, colrdone, maskdone;
	Image *img;
	MacImage *macimg;
	Handle sicnhandle, iconhandle, datahandle;
	Handle monohandle = nil, maskhandle = nil;
    PixPatHandle ppathandle;
    CIconHandle cicnhandle;
    PixMapHandle pmhandle;

	for (img = imf->images; img != NULL; img = img->next) {
		w = img->w;  h = img->h;
		actualw = img->actualw;  actualh = img->actualh;
		pixelsize = img->pixelsize;
		rowbytes = img->rowbytes;
		macimg = get_mac_image(img);
		/* Mono icons and masks are very similar; digest both here. */
		monodone = colrdone = maskdone = FALSE;
		if (w == 8 && h == 8 && img->monodata != lispnil && img->istile) {
			/* Monochrome pattern. */
			datahandle = NewHandle(8);
			memset(*datahandle, 0, 8);
			/* Read exactly 8 bytes. */
			mac_interp_bytes(img->monodata, 8, datahandle, 0);
			/* Fill in the monopat. */
			for (i = 0; i < 8; ++i)
			  SET_IMG_PAT(macimg, i, (*datahandle)[i]);
			macimg->patdefined = TRUE;
			/* Patterns have no masks, but defeat subsequent mask hacking. */
			monodone = maskdone = TRUE;
		}
		if (w == 16 && h == 16 && img->monodata != lispnil) {
			/* Shape is appropriate for a small icon, make one. */
			sicnhandle = NewHandle(32);
			memset(*sicnhandle, 0, 32);
			/* Read exactly 32 bytes. */
			mac_interp_bytes(img->monodata, 32, sicnhandle, 0);
			macimg->monosicn = sicnhandle;
			monodone = TRUE;
		}
		if (w == 16 && h == 16 && img->maskdata != lispnil) {
			/* Shape is appropriate for a small icon mask, make one. */
			sicnhandle = NewHandle(32);
			memset(*sicnhandle, 0, 32);
			/* Read exactly 32 bytes. */
			mac_interp_bytes(img->maskdata, 32, sicnhandle, 0);
			macimg->masksicn = sicnhandle;
			maskdone = TRUE;
		}
		if (w == 32 && h == 32 && img->monodata != lispnil) {
			/* Shape is appropriate for a standard icon, make one. */
			iconhandle = NewHandle(128);
			memset(*iconhandle, 0, 128);
			/* Read exactly 128 bytes. */
			mac_interp_bytes(img->monodata, 128, iconhandle, 0);
			macimg->monoicon = iconhandle;
			monodone = TRUE;
		}
		if (w == 32 && h == 32 && img->maskdata != lispnil) {
			/* Shape is appropriate for a standard icon, make one. */
			iconhandle = NewHandle(128);
			memset(*iconhandle, 0, 128);
			/* Read exactly 128 bytes. */
			mac_interp_bytes(img->maskdata, 128, iconhandle, 0);
			macimg->maskicon = iconhandle;
			maskdone = TRUE;
		}
		/* The mono and mask data are probably going to go into the cicn - save them
		   into a handle for now. */
		if (!monodone && img->monodata != lispnil) {
			if (rowbytes < 1)
			  rowbytes = ((actualw + 15) / 16) * 2;
			numbytes = actualh * (actualw <= 8 ? 2 : rowbytes);
			monohandle = NewHandle(numbytes);
			memset(*maskhandle, 0, numbytes);
			/* Read as many bytes as directed. */
			mac_interp_bytes(img->monodata, numbytes, monohandle, (actualw <= 8 ? 1 : 0));
			monodone = TRUE;
		}
		if (!maskdone && img->maskdata != lispnil) {
			if (rowbytes < 1)
			  rowbytes = ((actualw + 15) / 16) * 2;
			numbytes = actualh * (actualw <= 8 ? 2 : rowbytes);
			maskhandle = NewHandle(numbytes);
			memset(*maskhandle, 0, numbytes);
			/* Read as many bytes as directed. */
			mac_interp_bytes(img->maskdata, numbytes, maskhandle, (actualw <= 8 ? 1 : 0));
			maskdone = TRUE;
		}
		/* Limit bits per pixel to reasonable values. */
		if (pixelsize < 1 || pixelsize > 8)
		  pixelsize = 8;
		/* A sufficiently small tiling color image can be a color pattern. */
		if (hasColorQD && actualw <= 64 && actualh <= 64 && img->istile && img->colrdata != lispnil) {
			/* Make a color pattern. */
			ppathandle = NewPixPat();
			pmhandle = (*ppathandle)->patMap;
			SetRect(&((*pmhandle)->bounds), 0, 0, actualw, actualh);
			if (rowbytes < 1)
			  rowbytes = (actualw * pixelsize) / 8;
			(*pmhandle)->rowBytes = rowbytes;
			(*pmhandle)->pixelSize = pixelsize;
			(*pmhandle)->pmTable = interp_ctab(img->palette);
			numbytes = actualh * rowbytes;
			datahandle = NewHandle(numbytes);
			(*ppathandle)->patData = datahandle;
			mac_interp_bytes(img->colrdata, numbytes, datahandle, 0);
			(*ppathandle)->patXValid = -1;
			macimg->colrpat = ppathandle;
			colrdone = maskdone = TRUE;
		}
		if (hasColorQD && actualw <= 64 && actualh <= 64
		    && !img->istile
		    && (img->colrdata != lispnil
		        || (macimg->monosicn == nil
		            && macimg->masksicn == nil
		            && macimg->monoicon == nil
		            && macimg->maskicon == nil
		           ))) {
		    /* If no color data in evidence, prepare to use mono or mask instead. */
		    if (img->colrdata == lispnil) {
		    	pixelsize = 1;
		    }
			/* Make a full color icon. */
			bitrowbytes = ((actualw + 15) / 16) * 2;
			/* Allocate enough space for the icon and its data/mask bitmaps. */
			cicnhandle = (CIconHandle) NewHandle(sizeof(CIcon) + 2 * actualh * bitrowbytes);
			HLock((Handle) cicnhandle);
			(*cicnhandle)->iconPMap.baseAddr = 0;
			if (rowbytes < 1)
			  rowbytes = ((actualw + 15) / 16) * pixelsize * 2;
			(*cicnhandle)->iconPMap.rowBytes = rowbytes | 0x8000;
			SetRect(&((*cicnhandle)->iconPMap.bounds), 0, 0, actualw, actualh);
			(*cicnhandle)->iconPMap.pmVersion = 0;
			(*cicnhandle)->iconPMap.packType = 0;
			(*cicnhandle)->iconPMap.packSize = 0;
			(*cicnhandle)->iconPMap.hRes = 0;
			(*cicnhandle)->iconPMap.vRes = 0;
			(*cicnhandle)->iconPMap.pixelType = 0;
			(*cicnhandle)->iconPMap.pixelSize = pixelsize;
			(*cicnhandle)->iconPMap.cmpCount = 1;
			(*cicnhandle)->iconPMap.cmpSize = pixelsize;
			(*cicnhandle)->iconPMap.planeBytes = 0;
			(*cicnhandle)->iconPMap.pmTable = interp_ctab(img->palette);
			(*cicnhandle)->iconPMap.pmReserved = 0;
			/* Configure the monochrome icon. */
			SetRect(&((*cicnhandle)->iconBMap.bounds), 0, 0, actualw, actualh);
			(*cicnhandle)->iconBMap.rowBytes = 0;
			(*cicnhandle)->iconBMap.baseAddr = NULL;
			SetRect(&((*cicnhandle)->iconMask.bounds), 0, 0, actualw, actualh);
			/* Configure the mask bitmap. */
			(*cicnhandle)->iconMask.rowBytes = bitrowbytes;
			(*cicnhandle)->iconMask.baseAddr = NULL;
			numbytes = actualh * rowbytes;
			datahandle = NewHandle(numbytes);
			(*cicnhandle)->iconData = datahandle;
			/* Fill up the datahandle with the color data, or else use mono/mask data
			   if the color data is missing. */
			if (img->colrdata != lispnil) {
				mac_interp_bytes(img->colrdata, numbytes, datahandle, 0);
			} else if (img->monodata != lispnil) {
				mac_interp_bytes(img->monodata, numbytes, datahandle, (actualw <= 8 ? 1 : 0));
				/* We need an ersatz color table. */
				(*cicnhandle)->iconPMap.pmTable = synth_ctab();
			} else if (img->maskdata != lispnil) {
				mac_interp_bytes(img->maskdata, numbytes, datahandle, (actualw <= 8 ? 1 : 0));
				/* We need an ersatz color table. */
				(*cicnhandle)->iconPMap.pmTable = synth_ctab();
			}
			/* If a mono icon is already set up, use it as the icon's bitmap. */
			if (macimg->monoicon != nil) {
				HLock(macimg->monoicon);
				memcpy(((char *) (*cicnhandle)->iconMaskData) + actualh * bitrowbytes,
					   *(macimg->monoicon), actualh * bitrowbytes);
				(*cicnhandle)->iconBMap.rowBytes = bitrowbytes;
				HUnlock(macimg->monoicon);
			}
			/* If a mono sicn is already set up, use it as the icon's bitmap. */
			if (macimg->monosicn != nil) {
				HLock(macimg->monosicn);
				memcpy(((char *) (*cicnhandle)->iconMaskData) + actualh * bitrowbytes,
					   *(macimg->monosicn), actualh * bitrowbytes);
				(*cicnhandle)->iconBMap.rowBytes = bitrowbytes;
				HUnlock(macimg->monosicn);
			}
			/* ...or just use the monohandle that we built earlier. */
			if (monohandle != nil) {
				HLock(monohandle);
				memcpy(((char *) (*cicnhandle)->iconMaskData) + actualh * bitrowbytes,
					   *monohandle, actualh * bitrowbytes);
				(*cicnhandle)->iconBMap.rowBytes = bitrowbytes;
				HUnlock(monohandle);
			}
			/* If a mask icon is already set up, use it as the icon's mask. */
			if (macimg->maskicon != nil) {
				HLock(macimg->maskicon);
				memcpy((char *) (*cicnhandle)->iconMaskData,
					   *(macimg->maskicon), actualh * bitrowbytes);
				HUnlock(macimg->maskicon);
			}
			/* If a mask sicn is already set up, use it as the icon's mask. */
			if (macimg->masksicn != nil) {
				HLock(macimg->masksicn);
				memcpy((char *) (*cicnhandle)->iconMaskData,
					   *(macimg->masksicn), actualh * bitrowbytes);
				HUnlock(macimg->masksicn);
			}
			/* ...or just use the maskhandle that we built earlier. */
			if (maskhandle != nil) {
				HLock(maskhandle);
				memcpy((char *) (*cicnhandle)->iconMaskData,
					   *maskhandle, actualh * bitrowbytes);
				HUnlock(maskhandle);
			}
			macimg->colricon = (Handle) cicnhandle;
			HUnlock((Handle) cicnhandle);
			colrdone = TRUE;
		}
		if (!monodone && !colrdone) {
			run_warning("%dx%d image of \"%s\" could not be interpreted", w, h, imf->name);
		}
    }
#if 0 /* broken */
    /* If we have a color pattern of random size, and an 8x8 mono pattern, then we should
       glue the mono pattern into the color pattern's mono slot.  This requires searching
       all pairs of images in the family. */
	for (img = imf->images; img != NULL; img = img->next) {
		Image *img2;
		MacImage *macimg2;
		macimg = get_mac_image(img);
		if (macimg->colrpat != nil) {
			for (img2 = imf->images; img2 != NULL; img2 = img2->next) {
				macimg2 = get_mac_image(img2);
				if (macimg2->patdefined) {
					memcpy((char *) &(*(macimg->colrpat)->pat1Data), (char *) &(macimg2->monopat), 8);
				}
			}
		}
	}
#endif
}

void
mac_interp_bytes(datalist, numbytes, desthandle, jump)
Obj *datalist;
int numbytes, jump;
Handle desthandle;
{
    HLock(desthandle);
    interp_bytes(datalist, numbytes, *desthandle, jump);
    HUnlock(desthandle);
}

/* Given a list of color table entries, create and return a color table. */

CTabHandle
interp_ctab(palette)
Obj *palette;
{
	int len, i;
	Obj *head, *rest, *color;
	ColorSpec *ctdata;
	CTabHandle ctabhandle;

	len = length(palette);
	if (len == 0)
	  return nil;
	ctabhandle = (CTabHandle) NewHandle(8 + len * 8);
	HLock((Handle) ctabhandle);
	(*ctabhandle)->ctFlags = 0;
	(*ctabhandle)->ctSeed = GetCTSeed();
	(*ctabhandle)->ctSize = len - 1;
	ctdata = (ColorSpec *) &((*ctabhandle)->ctTable);
	for (i = 0, rest = palette; i < len; ++i, rest = cdr(rest)) {
		head = car(rest);
		ctdata[i].value = c_number(car(head));
		color = cdr(head);
		ctdata[i].rgb.red = c_number(car(color));
		ctdata[i].rgb.green = c_number(cadr(color));
		ctdata[i].rgb.blue = c_number(car(cddr(color)));
	}
	HUnlock((Handle) ctabhandle);
	return ctabhandle;
}

CTabHandle
synth_ctab()
{
	int len;
	ColorSpec *ctdata;
	CTabHandle ctabhandle;

	len = 2;
	ctabhandle = (CTabHandle) NewHandle(8 + len * 8);
	HLock((Handle) ctabhandle);
	(*ctabhandle)->ctFlags = 0;
	(*ctabhandle)->ctSeed = GetCTSeed();
	(*ctabhandle)->ctSize = len - 1;
	ctdata = (ColorSpec *) &((*ctabhandle)->ctTable);
	ctdata[0].value = 0;
	ctdata[0].rgb.red = 65535;
	ctdata[0].rgb.green = 65535;
	ctdata[0].rgb.blue = 65535;
	ctdata[1].value = 1;
	ctdata[1].rgb.red = 0;
	ctdata[1].rgb.green = 0;
	ctdata[1].rgb.blue = 0;
	HUnlock((Handle) ctabhandle);
	return ctabhandle;
}

void
make_generic_image_data(ImageFamily *imf)
{
	int i, hasmono, hasmask;
	Image *img;
	MacImage *macimg;

	for (img = imf->images; img != NULL; img = img->next) {
		hasmono = hasmask = FALSE;
		macimg = get_mac_image(img);
		if (hasColorQD && macimg->colricon != nil) {
			convert_cicn(img, (CIconHandle) macimg->colricon, &hasmono, &hasmask);
		}
		if (hasColorQD && macimg->colrpat != nil) {
			convert_ppat(img, macimg->colrpat);
		}
		if (macimg->monoicon != nil && !hasmono) {
			img->rawmonodata = xmalloc(128);
			for (i = 0; i < 128; ++i)
			  img->rawmonodata[i] = ((char *) *(macimg->monoicon))[i];
		}
		if (macimg->maskicon != nil && !hasmask) {
			img->rawmaskdata = xmalloc(128);
			for (i = 0; i < 128; ++i)
			  img->rawmaskdata[i] = ((char *) *(macimg->maskicon))[i];
		}
		if (macimg->monosicn != nil && !hasmono) {
			img->rawmonodata = xmalloc(32);
			for (i = 0; i < 32; ++i)
			  img->rawmonodata[i] = ((char *) *(macimg->monosicn))[i];
		}
		if (macimg->masksicn != nil && !hasmask) {
			img->rawmaskdata = xmalloc(32);
			for (i = 0; i < 32; ++i)
			  img->rawmaskdata[i] = ((char *) *(macimg->masksicn))[i];
		}
		if (macimg->patdefined) {
			img->rawmonodata = xmalloc(8);
			for (i = 0; i < 8; ++i)
			  img->rawmonodata[i] = ((char *) &(macimg->monopat))[i];
		}
	}
}

/* Generify a color pattern. */

void
convert_ppat(Image *img, PixPatHandle pat)
{
	int w, h, i, numbytes;
	Rect bounds;
	PixMapHandle pmhandle = (*pat)->patMap;
	CTabHandle ctabhandle = (*pmhandle)->pmTable;
	Handle patdata = (*pat)->patData;

	switch ((*pat)->patType) {
		case 0:
			/* (should put something in monodata?) */
			break;
		case 1:
			/* Compute the actual size of the pattern. */
			bounds = (*pmhandle)->bounds;
			w = bounds.right - bounds.left;  h = bounds.bottom - bounds.top;
			if (w != img->w || h != img->h) {
				img->actualw = w;  img->actualh = h;
			}
			img->rowbytes = (*pmhandle)->rowBytes & 0x3fff;
			img->pixelsize = (*pmhandle)->pixelSize;
			img->palette = convert_ctab(ctabhandle);
			numbytes = h * img->rowbytes;
			img->rawcolrdata = xmalloc(numbytes);
			for (i = 0; i < numbytes; ++i)
			  img->rawcolrdata[i] = (*patdata)[i];
			break;
		case 2:
			run_warning("Type 2 (RGB) pattern, ignoring");
			break;
	}
}

/* Generify the data in a color icon. */

void
convert_cicn(Image *img, CIconHandle cicnhandle, int *hasmono, int *hasmask)
{
	int w, h, i, rowbytes, numbytes;
	Rect bounds;
	char *baseaddr;
	PixMap pmap = (*cicnhandle)->iconPMap;
	CTabHandle ctabhandle = pmap.pmTable;
	Handle datahandle;

	*hasmono = *hasmask = FALSE;
	HLock((Handle) cicnhandle);
	bounds = pmap.bounds;
	w = bounds.right - bounds.left;  h = bounds.bottom - bounds.top;
	if (w != img->w || h != img->h) {
		img->actualw = w;  img->actualh = h;
	}
	img->rowbytes = pmap.rowBytes & 0x3fff;
	img->pixelsize = pmap.pixelSize;
	img->palette = convert_ctab(ctabhandle);
	numbytes = h * img->rowbytes;
	img->rawcolrdata = xmalloc(numbytes);
	datahandle = (*cicnhandle)->iconData;
	for (i = 0; i < numbytes; ++i)
	  img->rawcolrdata[i] = (*datahandle)[i];
	/* Convert the cicn's monochrome icon if defined. */
	rowbytes = (*cicnhandle)->iconBMap.rowBytes;
	if (rowbytes > 0) {
		baseaddr = ((char *) (*cicnhandle)->iconMaskData) + h * rowbytes;
		numbytes = h * rowbytes;
		img->rawmonodata = xmalloc(numbytes);
		for (i = 0; i < numbytes; ++i) {
			img->rawmonodata[i] = baseaddr[i];
			/* For small icons, jump over a not-strictly-necessary padding char. */
			if (w <= 8 && rowbytes == 2) ++i;
		}
		*hasmono = TRUE;
	}
	/* Write the cicn's mask if one is defined. */
	rowbytes = (*cicnhandle)->iconMask.rowBytes;
	if (rowbytes > 0) {
		baseaddr = (char *) (*cicnhandle)->iconMaskData;
		numbytes = h * rowbytes;
		img->rawmaskdata = xmalloc(numbytes);
		for (i = 0; i < numbytes; ++i) {
			img->rawmaskdata[i] = baseaddr[i];
			/* For small icons, jump over a not-strictly-necessary padding char. */
			if (w <= 8 && rowbytes == 2) ++i;
		}
		*hasmask = TRUE;
	}
	HUnlock((Handle) cicnhandle);
}

/* Write a color table. */

Obj *
convert_ctab(CTabHandle ctabhandle)
{
	int c, ctsize;
	ColorSpec cspec;
	Obj *rslt = lispnil, *tmp, *rest, *restprev, *tmp2;

	if (ctabhandle == nil)
	  return lispnil;
	ctsize = (*ctabhandle)->ctSize;
	if (ctsize >= 0) {
		HLock((Handle) ctabhandle);
		for (c = 0; c <= ctsize; ++c) {
			cspec = (*ctabhandle)->ctTable[c];
			tmp = cons(new_number(cspec.value),
					   cons(new_number(cspec.rgb.red),
					   		cons(new_number(cspec.rgb.green),
					   			 cons(new_number(cspec.rgb.blue),
					   			 	  lispnil))));
			/* Splice the new entry into the list so that all are sorted by index. */
			restprev = lispnil;
			for (rest = rslt;
				 cspec.value > (numberp(car(car(rest))) ? c_number(car(car(rest))) : 1000000);
				 rest = cdr(rest)) {
				restprev = rest;
			}
			tmp2 = cons(tmp, rest);
			if (restprev != lispnil)
			  set_cdr(restprev, tmp2);
			else
			  rslt = tmp2;
		}
		HUnlock((Handle) ctabhandle);
	}
	return rslt;
}

void
mac_load_image_color(imc)
ImageColor *imc;
{
	Handle imchandle;
    Str255 namestr;

	if (imc->name == NULL)
	  return;
	c2p(imc->name, namestr);
	imchandle = GetNamedResource('XCic', namestr);
	if (imchandle != nil) {
		imc->r = ((short *) (*imchandle))[0];
		imc->g = ((short *) (*imchandle))[1];
		imc->b = ((short *) (*imchandle))[2];
		imc->defined = 1;
	} else {
		/* Not found. */
		imc->r = imc->g = imc->b = 0;
		imc->defined = 0;
	}
}