New member Fl_Cairo_Graphics_Driver::bitmap_to_pattern() to avoid code duplication.

This commit is contained in:
ManoloFLTK 2022-06-12 09:04:22 +02:00
parent 78ca44ccca
commit c6516673ee
3 changed files with 40 additions and 41 deletions

View File

@ -154,6 +154,8 @@ public:
void cache(Fl_RGB_Image *rgb);
void uncache(Fl_RGB_Image *img, fl_uintptr_t &id_, fl_uintptr_t &mask_);
void draw_bitmap(Fl_Bitmap *bm,int XP, int YP, int WP, int HP, int cx, int cy);
static cairo_pattern_t *bitmap_to_pattern(Fl_Bitmap *bm, bool complement,
cairo_surface_t **p_surface);
void cache(Fl_Bitmap *img);
virtual void delete_bitmask(fl_uintptr_t bm);
void cache(Fl_Pixmap *pxm);

View File

@ -858,31 +858,44 @@ void Fl_Cairo_Graphics_Driver::draw_bitmap(Fl_Bitmap *bm,int XP, int YP, int WP,
}
void Fl_Cairo_Graphics_Driver::cache(Fl_Bitmap *bm) {
cairo_pattern_t *Fl_Cairo_Graphics_Driver::bitmap_to_pattern(Fl_Bitmap *bm,
bool complement, cairo_surface_t **p_surface) {
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_A1, bm->data_w());
int w_bitmap = ((bm->data_w() + 7) / 8);
uchar *BGRA = new uchar[stride * bm->data_h()];
memset(BGRA, 0, stride * bm->data_h());
uchar *r, p;
unsigned *q;
for (int j = 0; j < bm->data_h(); j++) {
r = (uchar*)bm->array + j * ((bm->data_w() + 7)/8);
q = (unsigned*)(BGRA + j * stride);
unsigned k = 0, mask32 = 1;
p = *r;
for (int i = 0; i < bm->data_w(); i++) {
if (p&1) (*q) |= mask32;
k++;
if (k % 8 != 0) p >>= 1; else p = *(++r);
if (k % 32 != 0) mask32 <<= 1; else {q++; mask32 = 1;}
for (int j = 0; j < bm->data_h(); j++) {
uchar *r = (uchar*)bm->array + j * w_bitmap;
unsigned *q = (unsigned*)(BGRA + j * stride);
unsigned k = 0, mask32 = 1;
uchar p = *r;
if (complement) p = ~p;
for (int i = 0; i < bm->data_w(); i++) {
if (p&1) (*q) |= mask32;
k++;
if (k % 8 != 0) p >>= 1;
else {
p = *(++r);
if (complement) p = ~p;
}
if (k % 32 != 0) mask32 <<= 1; else {q++; mask32 = 1;}
}
cairo_surface_t *surf = cairo_image_surface_create_for_data(BGRA, CAIRO_FORMAT_A1, bm->data_w(), bm->data_h(), stride);
if (cairo_surface_status(surf) == CAIRO_STATUS_SUCCESS) {
(void)cairo_surface_set_user_data(surf, &data_key_for_surface, BGRA, dealloc_surface_data);
cairo_pattern_t *pat = cairo_pattern_create_for_surface(surf);
cairo_surface_destroy(surf);
*Fl_Graphics_Driver::id(bm) = (fl_uintptr_t)pat;
}
cairo_surface_t *surf = cairo_image_surface_create_for_data(BGRA, CAIRO_FORMAT_A1, bm->data_w(), bm->data_h(), stride);
cairo_pattern_t *pattern = cairo_pattern_create_for_surface(surf);
if (p_surface) *p_surface = surf;
else cairo_surface_destroy(surf);
return pattern;
}
void Fl_Cairo_Graphics_Driver::cache(Fl_Bitmap *bm) {
cairo_surface_t *surf;
cairo_pattern_t *pattern = Fl_Cairo_Graphics_Driver::bitmap_to_pattern(bm, false, &surf);
uchar *BGRA = cairo_image_surface_get_data(surf);
(void)cairo_surface_set_user_data(surf, &data_key_for_surface, BGRA, dealloc_surface_data);
cairo_surface_destroy(surf);
*Fl_Graphics_Driver::id(bm) = (fl_uintptr_t)pattern;
}

View File

@ -193,32 +193,16 @@ const Fl_Image* Fl_Wayland_Window_Driver::shape() {
return shape_data_ ? shape_data_->shape_ : NULL;
}
void Fl_Wayland_Window_Driver::shape_bitmap_(Fl_Image* b) {
Fl_Bitmap *bm = (Fl_Bitmap*)b;
int i, j, w = bm->data_w(), h = bm->data_h(), w_bitmap = ((w + 7) / 8);
int bytesperrow = cairo_format_stride_for_width(CAIRO_FORMAT_A1, w);
uchar* bits = new uchar[h * bytesperrow];
// transform complement of bitmap image into CAIRO_FORMAT_A1 buffer
for (int j = 0; j < h; j++) {
uchar *r = (uchar*)bm->array + j * w_bitmap;
unsigned *q = (unsigned*)(bits + j * bytesperrow);
unsigned k = 0, mask32 = 1;
uchar p = ~*r;
for (int i = 0; i < w; i++) {
if (p&1) (*q) |= mask32;
k++;
if (k % 8 != 0) p >>= 1; else p = ~*(++r);
if (k % 32 != 0) mask32 <<= 1; else {q++; mask32 = 1;}
}
}
cairo_surface_t *mask_surf = cairo_image_surface_create_for_data(bits, CAIRO_FORMAT_A1, w, h, bytesperrow);
shape_data_->mask_pattern_ = cairo_pattern_create_for_surface(mask_surf);
cairo_surface_destroy(mask_surf);
shape_data_->mask_pattern_ = Fl_Cairo_Graphics_Driver::bitmap_to_pattern(
(Fl_Bitmap*)b, true, NULL);
shape_data_->shape_ = b;
shape_data_->lw_ = w;
shape_data_->lh_ = h;
shape_data_->lw_ = b->data_w();
shape_data_->lh_ = b->data_h();
}
void Fl_Wayland_Window_Driver::shape_alpha_(Fl_Image* img, int offset) {
int i, j, d = img->d(), w = img->data_w(), h = img->data_h();
int bytesperrow = cairo_format_stride_for_width(CAIRO_FORMAT_A1, w);