Skip to content

Commit

Permalink
FEAT: simple premultiply function which premultiplies image's RGB c…
Browse files Browse the repository at this point in the history
…hannel with its ALPHA
  • Loading branch information
Oldes committed Aug 19, 2020
1 parent 34d6cc3 commit 07fbf25
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/core/n-image.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,30 @@ typedef struct REBCLR {
return R_RET;
}

/***********************************************************************
**
*/ REBNATIVE(premultiply)
/*
// premultiply: native [
// "Premultiplies RGB channel with its alpha channel"
// image [image!] "Image to premultiply"
// ]
***********************************************************************/
{
REBVAL *val_img = D_ARG(1);
REBINT len = VAL_IMAGE_LEN(val_img);
REBYTE *rgba = VAL_IMAGE_DATA(val_img);
REBYTE a;
// Note: could be optimized!
for (; len > 0; len--, rgba += 4) {
a = (REBINT)rgba[C_A];
rgba[C_R] = (REBYTE)(((REBINT)rgba[C_R] * a) / 255);
rgba[C_G] = (REBYTE)(((REBINT)rgba[C_G] * a) / 255);
rgba[C_B] = (REBYTE)(((REBINT)rgba[C_B] * a) / 255);
}
return R_ARG1;
}

/***********************************************************************
**
*/ REBNATIVE(image)
Expand Down

0 comments on commit 07fbf25

Please sign in to comment.