windowmaker-wl/wrlib/convert_wl.c

59 lines
1.6 KiB
C

/* convert_wl.c - Wayland backend for RImage → pixman_image_t conversion */
#include <config.h>
#include <pixman-1/pixman.h>
#include <string.h>
#include "wraster.h"
int wl_convert_image(RContext *context, RImage *image, Pixmap *pixmap)
{
(void)context;
unsigned int w = image->width;
unsigned int h = image->height;
pixman_image_t *img = pixman_image_create_bits(PIXMAN_a8r8g8b8, w, h, NULL, 0);
uint32_t *dst = pixman_image_get_data(img);
unsigned char *src = image->data;
int has_alpha = (image->format == RRGBAFormat);
for (unsigned int i = 0; i < w * h; i++) {
unsigned char r = src[0], g = src[1], b = src[2];
unsigned char a = has_alpha ? src[3] : 0xFF;
dst[i] = ((uint32_t)a << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
src += has_alpha ? 4 : 3;
}
*pixmap = (Pixmap)(uintptr_t)img;
return True;
}
int wl_convert_image_mask(RContext *context, RImage *image,
Pixmap *pixmap, Pixmap *mask, int threshold)
{
if (!wl_convert_image(context, image, pixmap))
return False;
if (image->format == RRGBFormat) {
*mask = (Pixmap)0;
return True;
}
/* Create 8-bit alpha mask from image alpha channel */
unsigned int w = image->width;
unsigned int h = image->height;
pixman_image_t *m = pixman_image_create_bits(PIXMAN_a8, w, h, NULL, 0);
uint8_t *mdst = (uint8_t *)pixman_image_get_data(m);
int stride = pixman_image_get_stride(m);
unsigned char *src = image->data;
for (unsigned int y = 0; y < h; y++) {
for (unsigned int x = 0; x < w; x++) {
unsigned char a = src[(y * w + x) * 4 + 3];
mdst[y * stride + x] = (a >= threshold) ? 0xFF : 0x00;
}
}
*mask = (Pixmap)(uintptr_t)m;
return True;
}