Skip to content

Commit

Permalink
Prefer integer (un)premultiply for faster RGBA resize
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Jan 24, 2023
1 parent 802f560 commit 9e2207f
Show file tree
Hide file tree
Showing 14 changed files with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,12 @@ class PipelineWorker : public Napi::AsyncWorker {
image = sharp::EnsureAlpha(image, 1);
}

VipsBandFormat premultiplyFormat = image.format();
bool const shouldPremultiplyAlpha = sharp::HasAlpha(image) &&
(shouldResize || shouldBlur || shouldConv || shouldSharpen);

if (shouldPremultiplyAlpha) {
image = image.premultiply();
image = image.premultiply().cast(premultiplyFormat);
}

// Resize
Expand Down Expand Up @@ -583,13 +584,7 @@ class PipelineWorker : public Napi::AsyncWorker {

// Reverse premultiplication after all transformations
if (shouldPremultiplyAlpha) {
image = image.unpremultiply();
// Cast pixel values to integer
if (sharp::Is16Bit(image.interpretation())) {
image = image.cast(VIPS_FORMAT_USHORT);
} else {
image = image.cast(VIPS_FORMAT_UCHAR);
}
image = image.unpremultiply().cast(premultiplyFormat);
}
baton->premultiplied = shouldPremultiplyAlpha;

Expand Down
Binary file modified test/fixtures/expected/extract-alpha-16bit.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/expected/modulate-hue-angle-120.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/expected/modulate-hue-angle-150.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/expected/modulate-hue-angle-180.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/expected/modulate-hue-angle-210.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/expected/modulate-hue-angle-240.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/expected/modulate-hue-angle-270.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/expected/modulate-hue-angle-30.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/expected/modulate-hue-angle-300.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/expected/modulate-hue-angle-330.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/expected/modulate-hue-angle-360.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/expected/modulate-hue-angle-60.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/expected/modulate-hue-angle-90.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9e2207f

Please sign in to comment.