Prefer integer (un)premultiply for faster RGBA resize

This commit is contained in:
Lovell Fuller 2023-01-24 15:24:58 +00:00
parent 802f560b9b
commit 9e2207f376
14 changed files with 3 additions and 8 deletions

View File

@ -367,11 +367,12 @@ class PipelineWorker : public Napi::AsyncWorker {
image = sharp::EnsureAlpha(image, 1); image = sharp::EnsureAlpha(image, 1);
} }
VipsBandFormat premultiplyFormat = image.format();
bool const shouldPremultiplyAlpha = sharp::HasAlpha(image) && bool const shouldPremultiplyAlpha = sharp::HasAlpha(image) &&
(shouldResize || shouldBlur || shouldConv || shouldSharpen); (shouldResize || shouldBlur || shouldConv || shouldSharpen);
if (shouldPremultiplyAlpha) { if (shouldPremultiplyAlpha) {
image = image.premultiply(); image = image.premultiply().cast(premultiplyFormat);
} }
// Resize // Resize
@ -583,13 +584,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Reverse premultiplication after all transformations // Reverse premultiplication after all transformations
if (shouldPremultiplyAlpha) { if (shouldPremultiplyAlpha) {
image = image.unpremultiply(); image = image.unpremultiply().cast(premultiplyFormat);
// Cast pixel values to integer
if (sharp::Is16Bit(image.interpretation())) {
image = image.cast(VIPS_FORMAT_USHORT);
} else {
image = image.cast(VIPS_FORMAT_UCHAR);
}
} }
baton->premultiplied = shouldPremultiplyAlpha; baton->premultiplied = shouldPremultiplyAlpha;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 262 B

After

Width:  |  Height:  |  Size: 255 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 301 KiB