Skip to content

Commit 07fbf25

Browse files
committed
FEAT: simple premultiply function which premultiplies image's RGB channel with its ALPHA
1 parent 34d6cc3 commit 07fbf25

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/core/n-image.c

+24
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,30 @@ typedef struct REBCLR {
304304
return R_RET;
305305
}
306306

307+
/***********************************************************************
308+
**
309+
*/ REBNATIVE(premultiply)
310+
/*
311+
// premultiply: native [
312+
// "Premultiplies RGB channel with its alpha channel"
313+
// image [image!] "Image to premultiply"
314+
// ]
315+
***********************************************************************/
316+
{
317+
REBVAL *val_img = D_ARG(1);
318+
REBINT len = VAL_IMAGE_LEN(val_img);
319+
REBYTE *rgba = VAL_IMAGE_DATA(val_img);
320+
REBYTE a;
321+
// Note: could be optimized!
322+
for (; len > 0; len--, rgba += 4) {
323+
a = (REBINT)rgba[C_A];
324+
rgba[C_R] = (REBYTE)(((REBINT)rgba[C_R] * a) / 255);
325+
rgba[C_G] = (REBYTE)(((REBINT)rgba[C_G] * a) / 255);
326+
rgba[C_B] = (REBYTE)(((REBINT)rgba[C_B] * a) / 255);
327+
}
328+
return R_ARG1;
329+
}
330+
307331
/***********************************************************************
308332
**
309333
*/ REBNATIVE(image)

0 commit comments

Comments
 (0)