rem: for (y = 0; y < mask->height; y++) { for (x = 0; x < mask->width; x++) {
add: for (y = 0; y < MIN(mask->height, image->height); y++) { for (x = 0; x < MIN(mask->width, image->width); x++) {
context: 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;#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)    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;		}	    }	}    } 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) {	for (y = 0; y < mask->height; y++) {	    for (x = 0; x < mask->width; x++) {		if (XGetPixel(mask, x, y)) {		    *(data++) = 0xff;		} else {		    *(data++) = 0;		}	    }	}    }    return img;}