2
* images.c -- $Id: images.c 685 2003-03-08 15:26:51Z travo $
3
* p_pixmap and p_rgbmap for X11
5
* Copyright (c) 1998. See accompanying LEGAL file for details.
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);
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);
25
p_ndx_cell(p_win *w, unsigned char *ndxs, int ncols, int nrows,
26
int x0, int y0, int x1, int y1)
28
x_image(w, ndxs, 0, ncols, nrows, x0, y0, x1, y1);
32
p_rgb_cell(p_win *w, unsigned char *rgbs, int ncols, int nrows,
33
int x0, int y0, int x1, int y1)
35
x_image(w, rgbs, 1, ncols, nrows, x0, y0, x1, y1);
39
x_image(p_win *w, unsigned char *bytes, int rgb, int ncols,
40
int nrows, int x0, int y0, int x1, int y1)
43
int bpp, xa, ya, xb, yb, r, dr, dy, c, dc, dx;
46
int yexpand = (height>=nrows);
47
int xexpand = (width>=ncols);
48
int drow = rgb? 3 : 1;
49
int dcol = rgb? 3*ncols : ncols;
51
Display *dpy = s->xdpy->dpy;
52
GC gc = x_getgc(s, w, FillSolid);
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;
63
r = ((unsigned int)nrows)>>1;
64
dr = height%nrows - nrows;
67
r = ((unsigned int)height)>>1;
68
dr = nrows%height - height;
69
dy = (nrows/height)*dcol;
72
c = ((unsigned int)ncols)>>1;
73
dc = width%ncols - ncols;
76
c = ((unsigned int)width)>>1;
77
dc = ncols%width - width;
78
dx = (ncols/width)*drow;
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) {
97
for (xa=xb=x0 ; xa<x1 ; xa=xb) {
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);
112
if (c>=0) cell += drow;
120
if (r>=0) bytes += dcol;
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;
131
im->data = p_malloc(bpl*height);
132
if (!im->data) { x_imzap(s); return; }
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;
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;
148
if (yexpand) dy *= bpl;
150
if (xexpand) dx *= bpp;
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);
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];
169
put_pixel(im, c, r, x_getpixel(w, pixel));
174
int c0 = ((unsigned int)c)>>1;
175
for (ya=yb=0 ; yb<y1 ; ya=yb) {
186
for (xa=xb=0 ; xb<x1 ; xa=xb) {
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);
204
if (c>=0) cell += drow;
212
if (r>=0) bytes += dcol;
218
XPutImage(dpy, w->d, gc, im, 0,0, x0, y0, width, height);
221
if (p_signalling) p_abort();
225
x_put1(XImage *im, int i, int j, p_col_t pixel)
227
unsigned char *data = (unsigned char *)im->data;
233
x_put2b(XImage *im, int i, int j, p_col_t pixel)
235
unsigned char *data = (unsigned char *)im->data;
236
data[j+i+0] = pixel>>8;
242
x_put2l(XImage *im, int i, int j, p_col_t pixel)
244
unsigned char *data = (unsigned char *)im->data;
246
data[j+i+1] = pixel>>8;
251
x_put3b(XImage *im, int i, int j, p_col_t pixel)
253
unsigned char *data = (unsigned char *)im->data;
254
data[j+i+0] = pixel>>16;
255
data[j+i+1] = pixel>>8;
261
x_put3l(XImage *im, int i, int j, p_col_t pixel)
263
unsigned char *data = (unsigned char *)im->data;
265
data[j+i+1] = pixel>>8;
266
data[j+i+2] = pixel>>16;
271
x_put4b(XImage *im, int i, int j, p_col_t pixel)
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;
282
x_put4l(XImage *im, int i, int j, p_col_t pixel)
284
unsigned char *data = (unsigned char *)im->data;
286
data[j+i+1] = pixel>>8;
287
data[j+i+2] = pixel>>16;
288
data[j+i+3] = pixel>>24;