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