1.2.3
by Ondrej Certik
Import upstream version 0.6.0 |
1 |
/*
|
2 |
* images.c -- $Id: images.c 685 2003-03-08 15:26:51Z travo $
|
|
3 |
* p_pixmap and p_rgbmap for X11
|
|
4 |
*
|
|
5 |
* Copyright (c) 1998. See accompanying LEGAL file for details.
|
|
6 |
*/
|
|
7 |
||
8 |
#include "config.h" |
|
9 |
#include "playx.h" |
|
10 |
||
11 |
#include "pstdlib.h" |
|
12 |
||
13 |
static void x_image(p_win *w, unsigned char *bytes, int rgb, int ncols, |
|
14 |
int nrows, int x0, int y0, int x1, int y1); |
|
15 |
||
16 |
static int x_put1(XImage *im, int i, int j, p_col_t pixel); |
|
17 |
static int x_put2b(XImage *im, int i, int j, p_col_t pixel); |
|
18 |
static int x_put2l(XImage *im, int i, int j, p_col_t pixel); |
|
19 |
static int x_put3b(XImage *im, int i, int j, p_col_t pixel); |
|
20 |
static int x_put3l(XImage *im, int i, int j, p_col_t pixel); |
|
21 |
static int x_put4b(XImage *im, int i, int j, p_col_t pixel); |
|
22 |
static int x_put4l(XImage *im, int i, int j, p_col_t pixel); |
|
23 |
||
24 |
void
|
|
25 |
p_ndx_cell(p_win *w, unsigned char *ndxs, int ncols, int nrows, |
|
26 |
int x0, int y0, int x1, int y1) |
|
27 |
{
|
|
28 |
x_image(w, ndxs, 0, ncols, nrows, x0, y0, x1, y1); |
|
29 |
}
|
|
30 |
||
31 |
void
|
|
32 |
p_rgb_cell(p_win *w, unsigned char *rgbs, int ncols, int nrows, |
|
33 |
int x0, int y0, int x1, int y1) |
|
34 |
{
|
|
35 |
x_image(w, rgbs, 1, ncols, nrows, x0, y0, x1, y1); |
|
36 |
}
|
|
37 |
||
38 |
static void |
|
39 |
x_image(p_win *w, unsigned char *bytes, int rgb, int ncols, |
|
40 |
int nrows, int x0, int y0, int x1, int y1) |
|
41 |
{
|
|
42 |
unsigned char *cell; |
|
43 |
int bpp, xa, ya, xb, yb, r, dr, dy, c, dc, dx; |
|
44 |
int width = x1-x0; |
|
45 |
int height = y1-y0; |
|
46 |
int yexpand = (height>=nrows); |
|
47 |
int xexpand = (width>=ncols); |
|
48 |
int drow = rgb? 3 : 1; |
|
49 |
int dcol = rgb? 3*ncols : ncols; |
|
50 |
p_scr *s = w->s; |
|
51 |
Display *dpy = s->xdpy->dpy; |
|
52 |
GC gc = x_getgc(s, w, FillSolid); |
|
53 |
XImage *im; |
|
54 |
||
55 |
if (s->image) x_imzap(s); |
|
56 |
s->own_image_data = 1; |
|
57 |
im = s->image = XCreateImage(dpy, DefaultVisual(dpy, s->scr_num), |
|
58 |
s->depth, ZPixmap, 0, (char *)0, |
|
59 |
width, height, BitmapPad(dpy), 0); |
|
60 |
bpp = im->bits_per_pixel; |
|
61 |
||
62 |
if (yexpand) { |
|
63 |
r = ((unsigned int)nrows)>>1; |
|
64 |
dr = height%nrows - nrows; |
|
65 |
dy = height/nrows; |
|
66 |
} else { |
|
67 |
r = ((unsigned int)height)>>1; |
|
68 |
dr = nrows%height - height; |
|
69 |
dy = (nrows/height)*dcol; |
|
70 |
}
|
|
71 |
if (xexpand) { |
|
72 |
c = ((unsigned int)ncols)>>1; |
|
73 |
dc = width%ncols - ncols; |
|
74 |
dx = width/ncols; |
|
75 |
} else { |
|
76 |
c = ((unsigned int)width)>>1; |
|
77 |
dc = ncols%width - width; |
|
78 |
dx = (ncols/width)*drow; |
|
79 |
}
|
|
80 |
||
81 |
if ((long)bpp*(long)width*(long)height > 288L*(long)ncols*(long)nrows) { |
|
82 |
/* less X protocol traffic in a series of filled rectangles */
|
|
83 |
int c0 = ((unsigned int)c)>>1; |
|
84 |
x_imzap(s); /* wasteful to have created image, |
|
85 |
* but easiest way to get bpp... */
|
|
86 |
for (ya=yb=y0 ; ya<y1 ; ya=yb) { |
|
87 |
r += dr; |
|
88 |
if (yexpand) { |
|
89 |
yb += dy; |
|
90 |
if (r>=0) yb++; |
|
91 |
else r += nrows; |
|
92 |
} else { |
|
93 |
yb++; |
|
94 |
}
|
|
95 |
cell = bytes; |
|
96 |
c = c0; |
|
97 |
for (xa=xb=x0 ; xa<x1 ; xa=xb) { |
|
98 |
c += dc; |
|
99 |
if (xexpand) { |
|
100 |
xb += dx; |
|
101 |
if (c>=0) xb++; |
|
102 |
else c += ncols; |
|
103 |
} else { |
|
104 |
xb++; |
|
105 |
}
|
|
106 |
p_color(w, rgb? P_RGB(cell[0], cell[1], cell[2]) : cell[0]); |
|
107 |
XFillRectangle(dpy, w->d, gc, xa, ya, xb-xa, yb-ya); |
|
108 |
if (xexpand) { |
|
109 |
cell += drow; |
|
110 |
} else { |
|
111 |
cell += dx; |
|
112 |
if (c>=0) cell += drow; |
|
113 |
else c += width; |
|
114 |
}
|
|
115 |
}
|
|
116 |
if (yexpand) { |
|
117 |
bytes += dcol; |
|
118 |
} else { |
|
119 |
bytes += dy; |
|
120 |
if (r>=0) bytes += dcol; |
|
121 |
else r += height; |
|
122 |
}
|
|
123 |
}
|
|
124 |
||
125 |
} else { |
|
126 |
/* less X protocol traffic in one image */
|
|
127 |
int (*put_pixel)(XImage *, int, int, p_col_t)= 0; |
|
128 |
int bpl = im->bytes_per_line; |
|
129 |
p_col_t pixel; |
|
130 |
||
131 |
im->data = p_malloc(bpl*height); |
|
132 |
if (!im->data) { x_imzap(s); return; } |
|
133 |
||
134 |
if (bpp==8) { |
|
135 |
put_pixel = x_put1, bpp = 1; |
|
136 |
} else if (im->byte_order==MSBFirst) { |
|
137 |
if (bpp==32) put_pixel = x_put4b, bpp = 4; |
|
138 |
else if (bpp==16) put_pixel = x_put2b, bpp = 2; |
|
139 |
else if (bpp==24) put_pixel = x_put3b, bpp = 3; |
|
140 |
} else { |
|
141 |
if (bpp==32) put_pixel = x_put4l, bpp = 4; |
|
142 |
else if (bpp==16) put_pixel = x_put2l, bpp = 2; |
|
143 |
else if (bpp==24) put_pixel = x_put3l, bpp = 3; |
|
144 |
}
|
|
145 |
||
146 |
if (put_pixel) { |
|
147 |
y1 = height*bpl; |
|
148 |
if (yexpand) dy *= bpl; |
|
149 |
x1 = width*bpp; |
|
150 |
if (xexpand) dx *= bpp; |
|
151 |
} else { |
|
152 |
/* generic case is slow, but still more efficient than setting
|
|
153 |
* the image parameters and letting XPutImage do the work --
|
|
154 |
* XPutImage uses XGetPixel as well as XPutPixel for this case
|
|
155 |
* (this is same as XPutPixel macro in Xutil.h) */
|
|
156 |
put_pixel = (int (*)(XImage *, int, int, p_col_t))im->f.put_pixel; |
|
157 |
if (bpp==1) p_color(w, P_FG); |
|
158 |
y1 = height; |
|
159 |
bpl = 1; |
|
160 |
x1 = width; |
|
161 |
bpp = 1; |
|
162 |
}
|
|
163 |
||
164 |
if (ncols==width && nrows==height) { |
|
165 |
for (r=0 ; r<y1 ; r+=bpl) |
|
166 |
for (c=0 ; c<x1 ; c+=bpp) { |
|
167 |
pixel = rgb? P_RGB(bytes[0], bytes[1], bytes[2]) : bytes[0]; |
|
168 |
bytes += drow; |
|
169 |
put_pixel(im, c, r, x_getpixel(w, pixel)); |
|
170 |
}
|
|
171 |
||
172 |
} else { |
|
173 |
int i, j; |
|
174 |
int c0 = ((unsigned int)c)>>1; |
|
175 |
for (ya=yb=0 ; yb<y1 ; ya=yb) { |
|
176 |
r += dr; |
|
177 |
if (yexpand) { |
|
178 |
yb += dy; |
|
179 |
if (r>=0) yb += bpl; |
|
180 |
else r += nrows; |
|
181 |
} else { |
|
182 |
yb += bpl; |
|
183 |
}
|
|
184 |
cell = bytes; |
|
185 |
c = c0; |
|
186 |
for (xa=xb=0 ; xb<x1 ; xa=xb) { |
|
187 |
c += dc; |
|
188 |
if (xexpand) { |
|
189 |
xb += dx; |
|
190 |
if (c>=0) xb += bpp; |
|
191 |
else c += ncols; |
|
192 |
} else { |
|
193 |
xb += bpp; |
|
194 |
}
|
|
195 |
pixel = rgb? P_RGB(cell[0], cell[1], cell[2]) : cell[0]; |
|
196 |
pixel = x_getpixel(w, pixel); |
|
197 |
for (j=ya ; j<yb ; j+=bpl) |
|
198 |
for (i=xa ; i<xb ; i+=bpp) |
|
199 |
put_pixel(im, i, j, pixel); |
|
200 |
if (xexpand) { |
|
201 |
cell += drow; |
|
202 |
} else { |
|
203 |
cell += dx; |
|
204 |
if (c>=0) cell += drow; |
|
205 |
else c += width; |
|
206 |
}
|
|
207 |
}
|
|
208 |
if (yexpand) { |
|
209 |
bytes += dcol; |
|
210 |
} else { |
|
211 |
bytes += dy; |
|
212 |
if (r>=0) bytes += dcol; |
|
213 |
else r += height; |
|
214 |
}
|
|
215 |
}
|
|
216 |
}
|
|
217 |
||
218 |
XPutImage(dpy, w->d, gc, im, 0,0, x0, y0, width, height); |
|
219 |
x_imzap(s); |
|
220 |
}
|
|
221 |
if (p_signalling) p_abort(); |
|
222 |
}
|
|
223 |
||
224 |
static int |
|
225 |
x_put1(XImage *im, int i, int j, p_col_t pixel) |
|
226 |
{
|
|
227 |
unsigned char *data = (unsigned char *)im->data; |
|
228 |
data[j+i] = pixel; |
|
229 |
return 0; |
|
230 |
}
|
|
231 |
||
232 |
static int |
|
233 |
x_put2b(XImage *im, int i, int j, p_col_t pixel) |
|
234 |
{
|
|
235 |
unsigned char *data = (unsigned char *)im->data; |
|
236 |
data[j+i+0] = pixel>>8; |
|
237 |
data[j+i+1] = pixel; |
|
238 |
return 0; |
|
239 |
}
|
|
240 |
||
241 |
static int |
|
242 |
x_put2l(XImage *im, int i, int j, p_col_t pixel) |
|
243 |
{
|
|
244 |
unsigned char *data = (unsigned char *)im->data; |
|
245 |
data[j+i+0] = pixel; |
|
246 |
data[j+i+1] = pixel>>8; |
|
247 |
return 0; |
|
248 |
}
|
|
249 |
||
250 |
static int |
|
251 |
x_put3b(XImage *im, int i, int j, p_col_t pixel) |
|
252 |
{
|
|
253 |
unsigned char *data = (unsigned char *)im->data; |
|
254 |
data[j+i+0] = pixel>>16; |
|
255 |
data[j+i+1] = pixel>>8; |
|
256 |
data[j+i+2] = pixel; |
|
257 |
return 0; |
|
258 |
}
|
|
259 |
||
260 |
static int |
|
261 |
x_put3l(XImage *im, int i, int j, p_col_t pixel) |
|
262 |
{
|
|
263 |
unsigned char *data = (unsigned char *)im->data; |
|
264 |
data[j+i+0] = pixel; |
|
265 |
data[j+i+1] = pixel>>8; |
|
266 |
data[j+i+2] = pixel>>16; |
|
267 |
return 0; |
|
268 |
}
|
|
269 |
||
270 |
static int |
|
271 |
x_put4b(XImage *im, int i, int j, p_col_t pixel) |
|
272 |
{
|
|
273 |
unsigned char *data = (unsigned char *)im->data; |
|
274 |
data[j+i+0] = pixel>>24; |
|
275 |
data[j+i+1] = pixel>>16; |
|
276 |
data[j+i+2] = pixel>>8; |
|
277 |
data[j+i+3] = pixel; |
|
278 |
return 0; |
|
279 |
}
|
|
280 |
||
281 |
static int |
|
282 |
x_put4l(XImage *im, int i, int j, p_col_t pixel) |
|
283 |
{
|
|
284 |
unsigned char *data = (unsigned char *)im->data; |
|
285 |
data[j+i+0] = pixel; |
|
286 |
data[j+i+1] = pixel>>8; |
|
287 |
data[j+i+2] = pixel>>16; |
|
288 |
data[j+i+3] = pixel>>24; |
|
289 |
return 0; |
|
290 |
}
|