1 |
/* |
2 |
* xraster.c - routines to handle images for X windows. |
3 |
*/ |
4 |
|
5 |
/* ==================================================================== |
6 |
* The Radiance Software License, Version 1.0 |
7 |
* |
8 |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
9 |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
10 |
* |
11 |
* Redistribution and use in source and binary forms, with or without |
12 |
* modification, are permitted provided that the following conditions |
13 |
* are met: |
14 |
* |
15 |
* 1. Redistributions of source code must retain the above copyright |
16 |
* notice, this list of conditions and the following disclaimer. |
17 |
* |
18 |
* 2. Redistributions in binary form must reproduce the above copyright |
19 |
* notice, this list of conditions and the following disclaimer in |
20 |
* the documentation and/or other materials provided with the |
21 |
* distribution. |
22 |
* |
23 |
* 3. The end-user documentation included with the redistribution, |
24 |
* if any, must include the following acknowledgment: |
25 |
* "This product includes Radiance software |
26 |
* (http://radsite.lbl.gov/) |
27 |
* developed by the Lawrence Berkeley National Laboratory |
28 |
* (http://www.lbl.gov/)." |
29 |
* Alternately, this acknowledgment may appear in the software itself, |
30 |
* if and wherever such third-party acknowledgments normally appear. |
31 |
* |
32 |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
33 |
* and "The Regents of the University of California" must |
34 |
* not be used to endorse or promote products derived from this |
35 |
* software without prior written permission. For written |
36 |
* permission, please contact [email protected]. |
37 |
* |
38 |
* 5. Products derived from this software may not be called "Radiance", |
39 |
* nor may "Radiance" appear in their name, without prior written |
40 |
* permission of Lawrence Berkeley National Laboratory. |
41 |
* |
42 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
43 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
44 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
45 |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
46 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
47 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
48 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
49 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
50 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
51 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
52 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
53 |
* SUCH DAMAGE. |
54 |
* ==================================================================== |
55 |
* |
56 |
* This software consists of voluntary contributions made by many |
57 |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
58 |
* information on Lawrence Berkeley National Laboratory, please see |
59 |
* <http://www.lbl.gov/>. |
60 |
*/ |
61 |
|
62 |
#include <stdio.h> |
63 |
|
64 |
#include <X/Xlib.h> |
65 |
|
66 |
#include "xraster.h" |
67 |
|
68 |
|
69 |
int * |
70 |
map_rcolors(xr) /* get and assign pixels */ |
71 |
register XRASTER *xr; |
72 |
{ |
73 |
register int i; |
74 |
register unsigned char *p; |
75 |
int j; |
76 |
|
77 |
if (xr->ncolors == 0) |
78 |
return(NULL); |
79 |
if (xr->pixels != NULL) |
80 |
return(xr->pixels); |
81 |
xr->pixels = (int *)malloc(xr->ncolors*sizeof(int)); |
82 |
if (xr->pixels == NULL) |
83 |
return(NULL); |
84 |
if (XGetColorCells(0, xr->ncolors, 0, &j, xr->pixels) == 0) { |
85 |
free((void *)xr->pixels); |
86 |
xr->pixels = NULL; |
87 |
return(NULL); |
88 |
} |
89 |
for (i = 0; i < xr->ncolors; i++) |
90 |
if (xr->pmap[xr->pixels[i]] == -1) |
91 |
break; |
92 |
if (i < xr->ncolors) { /* different pixels */ |
93 |
for (p = xr->data.bz, i = BZPixmapSize(xr->width,xr->height); |
94 |
i--; p++) |
95 |
*p = xr->pixels[xr->pmap[*p]]; |
96 |
for (i = 0; i < 256; i++) |
97 |
xr->pmap[i] = -1; |
98 |
for (i = 0; i < xr->ncolors; i++) { |
99 |
xr->cdefs[i].pixel = xr->pixels[i]; |
100 |
xr->pmap[xr->pixels[i]] = i; |
101 |
} |
102 |
free_rpixmap(xr); /* Pixmap invalid */ |
103 |
} |
104 |
XStoreColors(xr->ncolors, xr->cdefs); |
105 |
return(xr->pixels); |
106 |
} |
107 |
|
108 |
|
109 |
Pixmap |
110 |
make_rpixmap(xr) /* make pixmap for raster */ |
111 |
register XRASTER *xr; |
112 |
{ |
113 |
Bitmap bm; |
114 |
|
115 |
if (xr->pm != 0) |
116 |
return(xr->pm); |
117 |
if (xr->ncolors > 0) { |
118 |
if (DisplayPlanes() < 2 || DisplayPlanes() > 8) |
119 |
return(0); |
120 |
xr->pm = XStorePixmapZ(xr->width, xr->height, xr->data.bz); |
121 |
} else { |
122 |
bm = XStoreBitmap(xr->width, xr->height, xr->data.m); |
123 |
if (bm == 0) |
124 |
return(0); |
125 |
xr->pm = XMakePixmap(bm, BlackPixel, WhitePixel); |
126 |
XFreeBitmap(bm); |
127 |
} |
128 |
return(xr->pm); |
129 |
} |
130 |
|
131 |
|
132 |
unmap_rcolors(xr) /* free colors */ |
133 |
register XRASTER *xr; |
134 |
{ |
135 |
if (xr->pixels == NULL) |
136 |
return; |
137 |
XFreeColors(xr->pixels, xr->ncolors, 0); |
138 |
free((void *)xr->pixels); |
139 |
xr->pixels = NULL; |
140 |
} |
141 |
|
142 |
|
143 |
free_rpixmap(xr) /* release Pixmap */ |
144 |
register XRASTER *xr; |
145 |
{ |
146 |
if (xr->pm == 0) |
147 |
return; |
148 |
XFreePixmap(xr->pm); |
149 |
xr->pm = 0; |
150 |
} |
151 |
|
152 |
|
153 |
free_raster(xr) /* free raster data */ |
154 |
register XRASTER *xr; |
155 |
{ |
156 |
free_rpixmap(xr); |
157 |
if (xr->ncolors > 0) { |
158 |
unmap_rcolors(xr); |
159 |
free((void *)xr->data.bz); |
160 |
free((void *)xr->pmap); |
161 |
free((void *)xr->cdefs); |
162 |
} else |
163 |
free((void *)xr->data.m); |
164 |
free((void *)xr); |
165 |
} |
166 |
|
167 |
|
168 |
put_raster(w, x, y, xr) /* put raster into window */ |
169 |
Window w; |
170 |
int x, y; |
171 |
register XRASTER *xr; |
172 |
{ |
173 |
if (xr->pm != 0) { |
174 |
XPixmapPut(w, 0, 0, x, y, xr->width, xr->height, |
175 |
xr->pm, GXcopy, AllPlanes); |
176 |
} else if (xr->ncolors > 0) { |
177 |
if (DisplayPlanes() < 2 || DisplayPlanes() > 8) |
178 |
return(0); |
179 |
XPixmapBitsPutZ(w, x, y, xr->width, xr->height, |
180 |
xr->data.bz, 0, GXcopy, AllPlanes); |
181 |
} else { |
182 |
XBitmapBitsPut(w, x, y, xr->width, xr->height, |
183 |
xr->data.m, BlackPixel, WhitePixel, |
184 |
0, GXcopy, AllPlanes); |
185 |
} |
186 |
return(1); |
187 |
} |
188 |
|
189 |
|
190 |
patch_raster(w, xsrc, ysrc, xdst, ydst, width, height, xr) /* piece */ |
191 |
Window w; |
192 |
int xsrc, ysrc, xdst, ydst; |
193 |
int width, height; |
194 |
register XRASTER *xr; |
195 |
{ |
196 |
register char *p; |
197 |
|
198 |
if (xsrc >= xr->width || ysrc >= xr->height) |
199 |
return(1); |
200 |
if (xsrc < 0) { |
201 |
xdst -= xsrc; width += xsrc; |
202 |
xsrc = 0; |
203 |
} |
204 |
if (ysrc < 0) { |
205 |
ydst -= ysrc; height += ysrc; |
206 |
ysrc = 0; |
207 |
} |
208 |
if (width <= 0 || height <= 0) |
209 |
return(1); |
210 |
if (xsrc + width > xr->width) |
211 |
width = xr->width - xsrc; |
212 |
if (ysrc + height > xr->height) |
213 |
height = xr->height - ysrc; |
214 |
if (xr->pm != 0) { |
215 |
XPixmapPut(w, xsrc, ysrc, xdst, ydst, width, height, |
216 |
xr->pm, GXcopy, AllPlanes); |
217 |
} else if (xr->ncolors > 0) { |
218 |
if (DisplayPlanes() < 2 || DisplayPlanes() > 8) |
219 |
return(0); |
220 |
p = (char *)xr->data.bz + BZPixmapSize(xr->width,ysrc) |
221 |
+ BZPixmapSize(xsrc,1); |
222 |
while (height--) { |
223 |
XPixmapBitsPutZ(w, xdst, ydst, width, 1, p, |
224 |
0, GXcopy, AllPlanes); |
225 |
p += BZPixmapSize(xr->width,1); |
226 |
ydst++; |
227 |
} |
228 |
} else { |
229 |
xdst -= xsrc&15; width += xsrc&15; |
230 |
xsrc &= ~15; |
231 |
p = (char *)xr->data.m + BitmapSize(xr->width,ysrc) |
232 |
+ BitmapSize(xsrc,1); |
233 |
while (height--) { |
234 |
XBitmapBitsPut(w, xdst, ydst, width, 1, |
235 |
p, BlackPixel, WhitePixel, |
236 |
0, GXcopy, AllPlanes); |
237 |
p += BitmapSize(xr->width,1); |
238 |
ydst++; |
239 |
} |
240 |
} |
241 |
return(1); |
242 |
} |