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