Skip to content

Commit

Permalink
fix: restore PixbufUtils.mask used by prism_core
Browse files Browse the repository at this point in the history
  • Loading branch information
knarewski committed Sep 6, 2024
1 parent a0459eb commit e5f15d9
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
73 changes: 73 additions & 0 deletions ext/morandi_native/mask.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
static GdkPixbuf *pixbuf_mask(GdkPixbuf *src, GdkPixbuf *mask)
{
int s_has_alpha, m_has_alpha;
int s_width, s_height, s_rowstride;
int d_width, d_height, d_rowstride;
int m_width, m_height, m_rowstride;
guchar *s_pix, *sp;
guchar *d_pix, *dp;
guchar *m_pix, *mp;
int i, j, pix_width, alpha, grey;
pixel_t pix;
GdkPixbuf *dest;

g_return_val_if_fail(src != NULL, NULL);

s_width = gdk_pixbuf_get_width(src);
s_height = gdk_pixbuf_get_height(src);
s_has_alpha = gdk_pixbuf_get_has_alpha(src);
s_rowstride = gdk_pixbuf_get_rowstride(src);
s_pix = gdk_pixbuf_get_pixels(src);

g_return_val_if_fail(mask != NULL, NULL);

m_width = gdk_pixbuf_get_width(mask);
m_height = gdk_pixbuf_get_height(mask);
m_has_alpha = gdk_pixbuf_get_has_alpha(mask);
m_rowstride = gdk_pixbuf_get_rowstride(mask);
m_pix = gdk_pixbuf_get_pixels(mask);

g_return_val_if_fail(m_width <= s_width, NULL);
g_return_val_if_fail(m_height <= s_height, NULL);

d_width = m_width;
d_height = m_height;
dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, d_width, d_height);

g_return_val_if_fail(dest != NULL, NULL);

d_rowstride = gdk_pixbuf_get_rowstride(dest);
d_pix = gdk_pixbuf_get_pixels(dest);

pix_width = (m_has_alpha ? 4 : 3);



for (i = 0; i < m_height; i++) {
sp = s_pix + (i * s_rowstride);
dp = d_pix + (i * d_rowstride);
mp = m_pix + (i * m_rowstride);

for (j = 0; j < m_width; j++) {
*(dp++) = *(sp++); /* red */
*(dp++) = *(sp++); /* green */
*(dp++) = *(sp++); /* blue */

if (s_has_alpha)
{
alpha = *(sp++); /* alpha */
}
else
{
alpha = 0xff;
}

pix = PIXEL(mp, pix_width, j);
grey = GIMP_RGB_TO_GREY(pix->r, pix->g, pix->b);

*(dp++) = sqrt(alpha * (255 - grey)); /* alpha */
}
}

return dest;
}
4 changes: 4 additions & 0 deletions ext/morandi_native/morandi_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ PixbufUtils_CLASS_gamma(VALUE self OPTIONAL_ATTR, VALUE __v_src OPTIONAL_ATTR, V
static VALUE
PixbufUtils_CLASS_tint(int __p_argc, VALUE *__p_argv, VALUE self);

static VALUE
PixbufUtils_CLASS_mask(VALUE self OPTIONAL_ATTR, VALUE __v_src OPTIONAL_ATTR, VALUE __v_mask OPTIONAL_ATTR);

/* Inline C code */

Expand All @@ -89,6 +91,7 @@ extern void Init_morandi_c(void);

#include "rotate.h"
#include "gamma.h"
#include "mask.h"
#include "tint.h"
#include "filter.h"

Expand Down Expand Up @@ -1081,6 +1084,7 @@ Init_morandi_native(void) {
rb_define_singleton_method(mPixbufUtils, "rotate", PixbufUtils_CLASS_rotate, 2);
rb_define_singleton_method(mPixbufUtils, "gamma", PixbufUtils_CLASS_gamma, 2);
rb_define_singleton_method(mPixbufUtils, "tint", PixbufUtils_CLASS_tint, -1);
rb_define_singleton_method(mPixbufUtils, "mask", PixbufUtils_CLASS_mask, 2);



Expand Down

0 comments on commit e5f15d9

Please sign in to comment.