1 /* xpixmap.c - Make RImage from Pixmap or XImage
3 * Raster graphics library
5 * Copyright (c) 1997 Alfredo K. Kojima
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <X11/Xutil.h>
37 get_shifts(unsigned long mask
)
48 #define NORMALIZE_RED(pixel) ((rshift>0) ? ((pixel) & rmask) >> rshift \
49 : ((pixel) & rmask) << -rshift)
50 #define NORMALIZE_GREEN(pixel) ((gshift>0) ? ((pixel) & gmask) >> gshift \
51 : ((pixel) & gmask) << -gshift)
52 #define NORMALIZE_BLUE(pixel) ((bshift>0) ? ((pixel) & bmask) >> bshift \
53 : ((pixel) & bmask) << -bshift)
56 RCreateImageFromXImage(RContext
*context
, XImage
*image
, XImage
*mask
)
62 int rshift
, gshift
, bshift
;
63 int rmask
, gmask
, bmask
;
66 assert(image
->format
==ZPixmap
);
67 assert(!mask
|| mask
->format
==ZPixmap
);
69 img
= RCreateImage(image
->width
, image
->height
, mask
!=NULL
);
75 /* I don't know why, but XGetImage() for pixmaps don't set the
76 * {red,green,blue}_mask values correctly.
78 if (context
->depth
==image
->depth
) {
79 rmask
= context
->visual
->red_mask
;
80 gmask
= context
->visual
->green_mask
;
81 bmask
= context
->visual
->blue_mask
;
83 rmask
= image
->red_mask
;
84 gmask
= image
->green_mask
;
85 bmask
= image
->blue_mask
;
88 /* how many bits to shift to normalize the color into 8bpp */
89 rshift
= get_shifts(rmask
) - 8;
90 gshift
= get_shifts(gmask
) - 8;
91 bshift
= get_shifts(bmask
) - 8;
95 if (image
->depth
==1) {
96 for (y
= 0; y
< image
->height
; y
++) {
97 for (x
= 0; x
< image
->width
; x
++) {
98 pixel
= XGetPixel(image
, x
, y
);
112 for (y
= 0; y
< image
->height
; y
++) {
113 for (x
= 0; x
< image
->width
; x
++) {
114 pixel
= XGetPixel(image
, x
, y
);
115 *(data
++) = NORMALIZE_RED(pixel
);
116 *(data
++) = NORMALIZE_GREEN(pixel
);
117 *(data
++) = NORMALIZE_BLUE(pixel
);
123 #define MIN(a,b) ((a)<(b)?(a):(b))
125 data
= img
->data
+ 3; /* Skip R, G & B */
126 for (y
= 0; y
< MIN(mask
->height
, image
->height
); y
++) {
127 for (x
= 0; x
< MIN(mask
->width
, image
->width
); x
++) {
128 if (mask
->width
<= image
->width
&& XGetPixel(mask
, x
, y
)) {
135 for (; x
< image
->width
; x
++) {
140 for (; y
< image
->height
; y
++) {
141 for (x
= 0; x
< image
->width
; x
++) {
153 RCreateImageFromDrawable(RContext
*context
, Drawable drawable
, Pixmap mask
)
157 unsigned int w
, h
, bar
;
162 assert(drawable
!=None
);
164 if (!XGetGeometry(context
->dpy
, drawable
, &baz
, &foo
, &foo
,
165 &w
, &h
, &bar
, &bar
)) {
166 printf("wrlib:invalid window or pixmap passed to RCreateImageFromPixmap\n");
169 pimg
= XGetImage(context
->dpy
, drawable
, 0, 0, w
, h
, AllPlanes
,
173 RErrorCode
= RERR_XERROR
;
178 if (XGetGeometry(context
->dpy
, mask
, &baz
, &foo
, &foo
,
179 &w
, &h
, &bar
, &bar
)) {
180 mimg
= XGetImage(context
->dpy
, mask
, 0, 0, w
, h
, AllPlanes
,
185 image
= RCreateImageFromXImage(context
, pimg
, mimg
);