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