1 |
greg |
1.2 |
/* Copyright 1988 Regents of the University of California */ |
2 |
greg |
1.1 |
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
greg |
1.2 |
|
7 |
|
|
/* |
8 |
greg |
1.1 |
* xraster.c - routine to load a Sun rasterfile for X windows. |
9 |
|
|
* |
10 |
|
|
* 2/18/88 |
11 |
|
|
*/ |
12 |
|
|
|
13 |
|
|
#include <stdio.h> |
14 |
|
|
|
15 |
|
|
#include <X/Xlib.h> |
16 |
|
|
|
17 |
|
|
#include "xraster.h" |
18 |
|
|
|
19 |
|
|
|
20 |
|
|
int * |
21 |
|
|
map_rcolors(xr) /* get and assign pixels */ |
22 |
|
|
register XRASTER *xr; |
23 |
|
|
{ |
24 |
|
|
register int i; |
25 |
|
|
register unsigned char *p; |
26 |
|
|
int j; |
27 |
|
|
|
28 |
|
|
if (xr->ncolors == 0) |
29 |
|
|
return(NULL); |
30 |
|
|
if (xr->pixels != NULL) |
31 |
|
|
return(xr->pixels); |
32 |
|
|
xr->pixels = (int *)malloc(xr->ncolors*sizeof(int)); |
33 |
|
|
if (xr->pixels == NULL) |
34 |
|
|
return(NULL); |
35 |
|
|
if (XGetColorCells(0, xr->ncolors, 0, &j, xr->pixels) == 0) { |
36 |
|
|
free((char *)xr->pixels); |
37 |
|
|
xr->pixels = NULL; |
38 |
|
|
return(NULL); |
39 |
|
|
} |
40 |
|
|
for (i = 0; i < xr->ncolors; i++) |
41 |
|
|
if (xr->pmap[xr->pixels[i]] == -1) |
42 |
|
|
break; |
43 |
|
|
if (i < xr->ncolors) { /* different pixels */ |
44 |
|
|
for (p = xr->data.bz, i = BZPixmapSize(xr->width,xr->height); |
45 |
|
|
i--; p++) |
46 |
|
|
*p = xr->pixels[xr->pmap[*p]]; |
47 |
|
|
for (i = 0; i < 256; i++) |
48 |
|
|
xr->pmap[i] = -1; |
49 |
|
|
for (i = 0; i < xr->ncolors; i++) { |
50 |
|
|
xr->cdefs[i].pixel = xr->pixels[i]; |
51 |
|
|
xr->pmap[xr->pixels[i]] = i; |
52 |
|
|
} |
53 |
|
|
free_rpixmap(xr); /* Pixmap invalid */ |
54 |
|
|
} |
55 |
|
|
XStoreColors(xr->ncolors, xr->cdefs); |
56 |
|
|
return(xr->pixels); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
|
60 |
|
|
Pixmap |
61 |
|
|
make_rpixmap(xr) /* make pixmap for raster */ |
62 |
|
|
register XRASTER *xr; |
63 |
|
|
{ |
64 |
|
|
Bitmap bm; |
65 |
|
|
|
66 |
|
|
if (xr->pm != 0) |
67 |
|
|
return(xr->pm); |
68 |
|
|
if (xr->ncolors > 0) { |
69 |
|
|
if (DisplayPlanes() < 2 || DisplayPlanes() > 8) |
70 |
|
|
return(0); |
71 |
|
|
xr->pm = XStorePixmapZ(xr->width, xr->height, xr->data.bz); |
72 |
|
|
} else { |
73 |
|
|
bm = XStoreBitmap(xr->width, xr->height, xr->data.m); |
74 |
|
|
if (bm == 0) |
75 |
|
|
return(0); |
76 |
|
|
xr->pm = XMakePixmap(bm, BlackPixel, WhitePixel); |
77 |
|
|
XFreeBitmap(bm); |
78 |
|
|
} |
79 |
|
|
return(xr->pm); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
|
83 |
|
|
unmap_rcolors(xr) /* free colors */ |
84 |
|
|
register XRASTER *xr; |
85 |
|
|
{ |
86 |
|
|
if (xr->pixels == NULL) |
87 |
|
|
return; |
88 |
|
|
XFreeColors(xr->pixels, xr->ncolors, 0); |
89 |
|
|
free((char *)xr->pixels); |
90 |
|
|
xr->pixels = NULL; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
|
94 |
|
|
free_rpixmap(xr) /* release Pixmap */ |
95 |
|
|
register XRASTER *xr; |
96 |
|
|
{ |
97 |
|
|
if (xr->pm == 0) |
98 |
|
|
return; |
99 |
|
|
XFreePixmap(xr->pm); |
100 |
|
|
xr->pm = 0; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
|
104 |
|
|
free_raster(xr) /* free raster data */ |
105 |
|
|
register XRASTER *xr; |
106 |
|
|
{ |
107 |
|
|
free_rpixmap(xr); |
108 |
|
|
if (xr->ncolors > 0) { |
109 |
|
|
unmap_rcolors(xr); |
110 |
|
|
free((char *)xr->data.bz); |
111 |
|
|
free((char *)xr->pmap); |
112 |
|
|
free((char *)xr->cdefs); |
113 |
|
|
} else |
114 |
|
|
free((char *)xr->data.m); |
115 |
|
|
free((char *)xr); |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
|
119 |
|
|
put_raster(w, x, y, xr) /* put raster into window */ |
120 |
|
|
Window w; |
121 |
|
|
int x, y; |
122 |
|
|
register XRASTER *xr; |
123 |
|
|
{ |
124 |
|
|
if (xr->pm != 0) { |
125 |
|
|
XPixmapPut(w, 0, 0, x, y, xr->width, xr->height, |
126 |
|
|
xr->pm, GXcopy, AllPlanes); |
127 |
|
|
} else if (xr->ncolors > 0) { |
128 |
|
|
if (DisplayPlanes() < 2 || DisplayPlanes() > 8) |
129 |
|
|
return(0); |
130 |
|
|
XPixmapBitsPutZ(w, x, y, xr->width, xr->height, |
131 |
|
|
xr->data.bz, 0, GXcopy, AllPlanes); |
132 |
|
|
} else { |
133 |
|
|
XBitmapBitsPut(w, x, y, xr->width, xr->height, |
134 |
|
|
xr->data.m, BlackPixel, WhitePixel, |
135 |
|
|
0, GXcopy, AllPlanes); |
136 |
|
|
} |
137 |
|
|
return(1); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
|
141 |
|
|
patch_raster(w, xsrc, ysrc, xdst, ydst, width, height, xr) /* piece */ |
142 |
|
|
Window w; |
143 |
|
|
int xsrc, ysrc, xdst, ydst; |
144 |
|
|
int width, height; |
145 |
|
|
register XRASTER *xr; |
146 |
|
|
{ |
147 |
|
|
register char *p; |
148 |
|
|
|
149 |
|
|
if (xsrc >= xr->width || ysrc >= xr->height) |
150 |
|
|
return(1); |
151 |
|
|
if (xsrc < 0) { |
152 |
|
|
xdst -= xsrc; width += xsrc; |
153 |
|
|
xsrc = 0; |
154 |
|
|
} |
155 |
|
|
if (ysrc < 0) { |
156 |
|
|
ydst -= ysrc; height += ysrc; |
157 |
|
|
ysrc = 0; |
158 |
|
|
} |
159 |
|
|
if (width <= 0 || height <= 0) |
160 |
|
|
return(1); |
161 |
|
|
if (xsrc + width > xr->width) |
162 |
|
|
width = xr->width - xsrc; |
163 |
|
|
if (ysrc + height > xr->height) |
164 |
|
|
height = xr->height - ysrc; |
165 |
|
|
if (xr->pm != 0) { |
166 |
|
|
XPixmapPut(w, xsrc, ysrc, xdst, ydst, width, height, |
167 |
|
|
xr->pm, GXcopy, AllPlanes); |
168 |
|
|
} else if (xr->ncolors > 0) { |
169 |
|
|
if (DisplayPlanes() < 2 || DisplayPlanes() > 8) |
170 |
|
|
return(0); |
171 |
|
|
p = (char *)xr->data.bz + BZPixmapSize(xr->width,ysrc) |
172 |
|
|
+ BZPixmapSize(xsrc,1); |
173 |
|
|
while (height--) { |
174 |
|
|
XPixmapBitsPutZ(w, xdst, ydst, width, 1, p, |
175 |
|
|
0, GXcopy, AllPlanes); |
176 |
|
|
p += BZPixmapSize(xr->width,1); |
177 |
|
|
ydst++; |
178 |
|
|
} |
179 |
|
|
} else { |
180 |
|
|
xdst -= xsrc&15; width += xsrc&15; |
181 |
|
|
xsrc &= ~15; |
182 |
|
|
p = (char *)xr->data.m + BitmapSize(xr->width,ysrc) |
183 |
|
|
+ BitmapSize(xsrc,1); |
184 |
|
|
while (height--) { |
185 |
|
|
XBitmapBitsPut(w, xdst, ydst, width, 1, |
186 |
|
|
p, BlackPixel, WhitePixel, |
187 |
|
|
0, GXcopy, AllPlanes); |
188 |
|
|
p += BitmapSize(xr->width,1); |
189 |
|
|
ydst++; |
190 |
|
|
} |
191 |
|
|
} |
192 |
|
|
return(1); |
193 |
|
|
} |