Initial revision
[wmaker-crm.git] / wrlib / xpixmap.c
blob7a8e3431eae96a5a75bf766fe8b03ec04c1315d2
1 /* xpixmap.c - Make RImage from Pixmap or XImage
2 *
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.
23 #include <config.h>
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <assert.h>
32 #include "wraster.h"
36 static int
37 get_shifts(unsigned long mask)
39 int i=0;
41 while (mask) {
42 mask>>=1;
43 i++;
45 return i;
49 RImage*
50 RCreateImageFromXImage(RContext *context, XImage *image, XImage *mask)
52 RImage *img;
53 int x, y;
54 unsigned long pixel;
55 unsigned char *r, *g, *b, *a;
56 int rshift, gshift, bshift;
57 int rmask, gmask, bmask;
59 RErrorString[0] = 0;
61 assert(image!=NULL);
62 assert(image->format==ZPixmap);
63 assert(!mask || mask->format==ZPixmap);
65 img = RCreateImage(image->width, image->height, mask!=NULL);
66 if (!img) {
67 return NULL;
71 /* I don't know why, but XGetImage() for pixmaps don't set the
72 * {red,green,blue}_mask values correctly. This will not
73 * work for imageis of depth different than the root window
74 * one used in wrlib
75 */
76 if (context->depth==image->depth) {
77 rmask = context->visual->red_mask;
78 gmask = context->visual->green_mask;
79 bmask = context->visual->blue_mask;
80 } else {
81 rmask = image->red_mask;
82 gmask = image->green_mask;
83 bmask = image->blue_mask;
86 /* how many bits to shift to normalize the color into 8bpp */
87 rshift = get_shifts(rmask) - 8;
88 gshift = get_shifts(gmask) - 8;
89 bshift = get_shifts(bmask) - 8;
91 r = img->data[0];
92 g = img->data[1];
93 b = img->data[2];
94 a = img->data[3];
96 #define NORMALIZE_RED(pixel) ((rshift>0) ? ((pixel) & rmask) >> rshift \
97 : ((pixel) & rmask) << -rshift)
98 #define NORMALIZE_GREEN(pixel) ((gshift>0) ? ((pixel) & gmask) >> gshift \
99 : ((pixel) & gmask) << -gshift)
100 #define NORMALIZE_BLUE(pixel) ((bshift>0) ? ((pixel) & bmask) >> bshift \
101 : ((pixel) & bmask) << -bshift)
103 for (y = 0; y < image->height; y++) {
104 for (x = 0; x < image->width; x++) {
105 pixel = XGetPixel(image, x, y);
106 *(r++) = NORMALIZE_RED(pixel);
107 *(g++) = NORMALIZE_GREEN(pixel);
108 *(b++) = NORMALIZE_BLUE(pixel);
112 if (mask && a) {
113 for (y = 0; y < mask->height; y++) {
114 for (x = 0; x < mask->width; x++) {
115 if (XGetPixel(mask, x, y)) {
116 *(a++) = 0xff;
117 } else {
118 *(a++) = 0xff;
123 return img;
128 RImage*
129 RCreateImageFromDrawable(RContext *context, Drawable drawable, Pixmap mask)
131 RImage *image;
132 XImage *pimg, *mimg;
133 unsigned int w, h, bar;
134 int foo;
135 Window baz;
137 RErrorString[0] = 0;
139 assert(drawable!=None);
141 if (!XGetGeometry(context->dpy, drawable, &baz, &foo, &foo,
142 &w, &h, &bar, &bar)) {
143 printf("wrlib:invalid window or pixmap passed to RCreateImageFromPixmap\n");
144 return NULL;
146 pimg = XGetImage(context->dpy, drawable, 0, 0, w, h, AllPlanes,
147 ZPixmap);
149 if (!pimg) {
150 sprintf(RErrorString, "could not get image");
151 return NULL;
153 mimg = NULL;
154 if (mask) {
155 if (XGetGeometry(context->dpy, mask, &baz, &foo, &foo,
156 &w, &h, &bar, &bar)) {
157 mimg = XGetImage(context->dpy, mask, 0, 0, w, h, AllPlanes,
158 ZPixmap);
162 image = RCreateImageFromXImage(context, pimg, mimg);
164 XDestroyImage(pimg);
165 if (mimg)
166 XDestroyImage(mimg);
168 return image;