-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Composites an overlay image with alpha channel into the input image (which must have alpha channel) using ‘over’ alpha compositing blend mode. This API requires both images to have the same dimensions. References: - http://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending - libvips/ruby-vips#28 (comment) See #97.
- Loading branch information
Showing
17 changed files
with
312 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
"maxparams": 4, | ||
"maxcomplexity": 13, | ||
"globals": { | ||
"before": true, | ||
"describe": true, | ||
"it": true | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#include <stdio.h> | ||
#include <vips/vips.h> | ||
|
||
|
||
const int ALPHA_BAND_INDEX = 3; | ||
const int NUM_COLOR_BANDS = 3; | ||
|
||
|
||
/* | ||
Composite images `src` and `dst` | ||
*/ | ||
int Composite(VipsObject *context, VipsImage *src, VipsImage *dst, VipsImage **out) { | ||
if (src->Bands != 4 || dst->Bands != 4) | ||
return -1; | ||
|
||
// Extract RGB bands: | ||
VipsImage *srcRGB; | ||
VipsImage *dstRGB; | ||
if (vips_extract_band(src, &srcRGB, 0, "n", NUM_COLOR_BANDS, NULL) || | ||
vips_extract_band(dst, &dstRGB, 0, "n", NUM_COLOR_BANDS, NULL)) | ||
return -1; | ||
|
||
vips_object_local(context, srcRGB); | ||
vips_object_local(context, dstRGB); | ||
|
||
// Extract alpha bands: | ||
VipsImage *srcAlpha; | ||
VipsImage *dstAlpha; | ||
if (vips_extract_band(src, &srcAlpha, ALPHA_BAND_INDEX, NULL) || | ||
vips_extract_band(dst, &dstAlpha, ALPHA_BAND_INDEX, NULL)) | ||
return -1; | ||
|
||
vips_object_local(context, srcAlpha); | ||
vips_object_local(context, dstAlpha); | ||
|
||
// Compute normalized input alpha channels: | ||
VipsImage *srcAlphaNormalized; | ||
VipsImage *dstAlphaNormalized; | ||
if (vips_linear1(srcAlpha, &srcAlphaNormalized, 1.0 / 255.0, 0.0, NULL) || | ||
vips_linear1(dstAlpha, &dstAlphaNormalized, 1.0 / 255.0, 0.0, NULL)) | ||
return -1; | ||
|
||
vips_object_local(context, srcAlphaNormalized); | ||
vips_object_local(context, dstAlphaNormalized); | ||
|
||
// | ||
// Compute normalized output alpha channel: | ||
// | ||
// References: | ||
// - http://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending | ||
// - https://github.com/jcupitt/ruby-vips/issues/28#issuecomment-9014826 | ||
// | ||
// out_a = src_a + dst_a * (1 - src_a) | ||
// ^^^^^^^^^^^ | ||
// t0 | ||
// ^^^^^^^^^^^^^^^^^^^ | ||
// t1 | ||
VipsImage *t0; | ||
VipsImage *t1; | ||
VipsImage *outAlphaNormalized; | ||
if (vips_linear1(srcAlphaNormalized, &t0, -1.0, 1.0, NULL) || | ||
vips_multiply(dstAlphaNormalized, t0, &t1, NULL) || | ||
vips_add(srcAlphaNormalized, t1, &outAlphaNormalized, NULL)) | ||
return -1; | ||
|
||
vips_object_local(context, t0); | ||
vips_object_local(context, t1); | ||
vips_object_local(context, outAlphaNormalized); | ||
|
||
// | ||
// Compute output RGB channels: | ||
// | ||
// Wikipedia: | ||
// out_rgb = (src_rgb * src_a + dst_rgb * dst_a * (1 - src_a)) / out_a | ||
// | ||
// `vips_ifthenelse` with `blend=TRUE`: http://bit.ly/1KoSsga | ||
// out = (cond / 255) * in1 + (1 - cond / 255) * in2 | ||
// | ||
// Substitutions: | ||
// | ||
// cond --> src_a | ||
// in1 --> src_rgb | ||
// in2 --> dst_rgb * dst_a (premultiplied destination RGB) | ||
// | ||
// Finally, manually divide by `out_a` to unpremultiply the RGB channels. | ||
// Failing to do so results in darker than expected output with low | ||
// opacity images. | ||
// | ||
VipsImage *dstRGBPremultiplied; | ||
if (vips_multiply(dstRGB, dstAlphaNormalized, &dstRGBPremultiplied, NULL)) | ||
return -1; | ||
|
||
vips_object_local(context, dstRGBPremultiplied); | ||
|
||
VipsImage *outRGBPremultiplied; | ||
if (vips_ifthenelse(srcAlpha, srcRGB, dstRGBPremultiplied, | ||
&outRGBPremultiplied, "blend", TRUE, NULL)) | ||
return -1; | ||
|
||
vips_object_local(context, outRGBPremultiplied); | ||
|
||
// Unpremultiply RGB channels: | ||
VipsImage *outRGB; | ||
if (vips_divide(outRGBPremultiplied, outAlphaNormalized, &outRGB, NULL)) | ||
return -1; | ||
|
||
vips_object_local(context, outRGB); | ||
|
||
// Denormalize output alpha channel: | ||
VipsImage *outAlpha; | ||
if (vips_linear1(outAlphaNormalized, &outAlpha, 255.0, 0.0, NULL)) | ||
return -1; | ||
|
||
vips_object_local(context, outAlpha); | ||
|
||
// Combine RGB and alpha channel into output image: | ||
VipsImage *joined; | ||
if (vips_bandjoin2(outRGB, outAlpha, &joined, NULL)) | ||
return -1; | ||
|
||
// Return a reference to the output image: | ||
*out = joined; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef SRC_COMPOSITE_H_ | ||
#define SRC_COMPOSITE_H_ | ||
|
||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
/* | ||
Composite images `src` and `dst`. | ||
*/ | ||
int Composite(VipsObject *context, VipsImage *src, VipsImage *dst, VipsImage **out); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // SRC_COMPOSITE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.