1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
/* xpixmap.c - Make RImage from Pixmap or XImage
*
* Raster graphics library
*
* Copyright (c) 1997-2003 Alfredo K. Kojima
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <config.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "wraster.h"
#include "wr_i18n.h"
static int get_shifts(unsigned long mask)
{
int i = 0;
while (mask) {
mask >>= 1;
i++;
}
return i;
}
#define NORMALIZE_RED(pixel) ((rshift>0) ? ((pixel) & rmask) >> rshift \
: ((pixel) & rmask) << -rshift)
#define NORMALIZE_GREEN(pixel) ((gshift>0) ? ((pixel) & gmask) >> gshift \
: ((pixel) & gmask) << -gshift)
#define NORMALIZE_BLUE(pixel) ((bshift>0) ? ((pixel) & bmask) >> bshift \
: ((pixel) & bmask) << -bshift)
RImage *RCreateImageFromXImage(RContext * context, XImage * image, XImage * mask)
{
RImage *img;
int x, y;
unsigned long pixel;
unsigned char *data;
int rshift, gshift, bshift;
int rmask, gmask, bmask;
assert(image != NULL);
assert(image->format == ZPixmap);
assert(!mask || mask->format == ZPixmap);
img = RCreateImage(image->width, image->height, mask != NULL);
if (!img) {
return NULL;
}
/* I don't know why, but XGetImage() for pixmaps don't set the
* {red,green,blue}_mask values correctly.
*/
if (context->depth == image->depth) {
rmask = context->visual->red_mask;
gmask = context->visual->green_mask;
bmask = context->visual->blue_mask;
} else {
rmask = image->red_mask;
gmask = image->green_mask;
bmask = image->blue_mask;
}
/* how many bits to shift to normalize the color into 8bpp */
rshift = get_shifts(rmask) - 8;
gshift = get_shifts(gmask) - 8;
bshift = get_shifts(bmask) - 8;
data = img->data;
if (image->depth == 1) {
for (y = 0; y < image->height; y++) {
for (x = 0; x < image->width; x++) {
pixel = XGetPixel(image, x, y);
if (pixel) {
*data++ = 0;
*data++ = 0;
*data++ = 0;
} else {
*data++ = 0xff;
*data++ = 0xff;
*data++ = 0xff;
}
if (mask)
data++;
}
}
} else {
for (y = 0; y < image->height; y++) {
for (x = 0; x < image->width; x++) {
pixel = XGetPixel(image, x, y);
*(data++) = NORMALIZE_RED(pixel);
*(data++) = NORMALIZE_GREEN(pixel);
*(data++) = NORMALIZE_BLUE(pixel);
if (mask)
data++;
}
}
}
#define MIN(a,b) ((a)<(b)?(a):(b))
if (mask) {
data = img->data + 3; /* Skip R, G & B */
for (y = 0; y < MIN(mask->height, image->height); y++) {
for (x = 0; x < MIN(mask->width, image->width); x++) {
if (mask->width <= image->width && XGetPixel(mask, x, y)) {
*data = 0xff;
} else {
*data = 0;
}
data += 4;
}
for (; x < image->width; x++) {
*data = 0;
data += 4;
}
}
for (; y < image->height; y++) {
for (x = 0; x < image->width; x++) {
*data = 0;
data += 4;
}
}
}
return img;
}
RImage *RCreateImageFromDrawable(RContext * context, Drawable drawable, Pixmap mask)
{
RImage *image;
XImage *pimg, *mimg;
unsigned int w, h, bar;
int foo;
Window baz;
assert(drawable != None);
if (!XGetGeometry(context->dpy, drawable, &baz, &foo, &foo, &w, &h, &bar, &bar)) {
fprintf(stderr, _("wrlib: invalid window or pixmap passed to RCreateImageFromDrawable\n"));
return NULL;
}
pimg = XGetImage(context->dpy, drawable, 0, 0, w, h, AllPlanes, ZPixmap);
if (!pimg) {
RErrorCode = RERR_XERROR;
return NULL;
}
mimg = NULL;
if (mask) {
if (XGetGeometry(context->dpy, mask, &baz, &foo, &foo, &w, &h, &bar, &bar)) {
mimg = XGetImage(context->dpy, mask, 0, 0, w, h, AllPlanes, ZPixmap);
}
}
image = RCreateImageFromXImage(context, pimg, mimg);
XDestroyImage(pimg);
if (mimg)
XDestroyImage(mimg);
return image;
}
|