Skip to content

Commit

Permalink
Ensure AVIF output is always 8-bit #3358
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Sep 14, 2022
1 parent fbd4970 commit 28b87db
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs/changelog.md
Expand Up @@ -13,6 +13,9 @@ Requires libvips v8.13.1
* Ensure auto-rotation works with shrink-on-load and extract (regression in 0.31.0).
[#3352](https://github.com/lovell/sharp/issues/3352)

* Ensure AVIF output is always 8-bit.
[#3358](https://github.com/lovell/sharp/issues/3358)

### v0.31.0 - 5th September 2022

* Drop support for Node.js 12, now requires Node.js >= 14.15.0.
Expand Down
6 changes: 4 additions & 2 deletions src/pipeline.cc
Expand Up @@ -902,12 +902,13 @@ class PipelineWorker : public Napi::AsyncWorker {
} else if (baton->formatOut == "heif" ||
(baton->formatOut == "input" && inputImageType == sharp::ImageType::HEIF)) {
// Write HEIF to buffer
image = sharp::RemoveAnimationProperties(image);
image = sharp::RemoveAnimationProperties(image).cast(VIPS_FORMAT_UCHAR);
VipsArea *area = reinterpret_cast<VipsArea*>(image.heifsave_buffer(VImage::option()
->set("strip", !baton->withMetadata)
->set("Q", baton->heifQuality)
->set("compression", baton->heifCompression)
->set("effort", baton->heifEffort)
->set("bitdepth", 8)
->set("subsample_mode", baton->heifChromaSubsampling == "4:4:4"
? VIPS_FOREIGN_SUBSAMPLE_OFF : VIPS_FOREIGN_SUBSAMPLE_ON)
->set("lossless", baton->heifLossless)));
Expand Down Expand Up @@ -1073,12 +1074,13 @@ class PipelineWorker : public Napi::AsyncWorker {
} else if (baton->formatOut == "heif" || (mightMatchInput && isHeif) ||
(willMatchInput && inputImageType == sharp::ImageType::HEIF)) {
// Write HEIF to file
image = sharp::RemoveAnimationProperties(image);
image = sharp::RemoveAnimationProperties(image).cast(VIPS_FORMAT_UCHAR);
image.heifsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
->set("strip", !baton->withMetadata)
->set("Q", baton->heifQuality)
->set("compression", baton->heifCompression)
->set("effort", baton->heifEffort)
->set("bitdepth", 8)
->set("subsample_mode", baton->heifChromaSubsampling == "4:4:4"
? VIPS_FOREIGN_SUBSAMPLE_OFF : VIPS_FOREIGN_SUBSAMPLE_ON)
->set("lossless", baton->heifLossless));
Expand Down
24 changes: 24 additions & 0 deletions test/unit/avif.js
Expand Up @@ -103,4 +103,28 @@ describe('AVIF', () => {
width: 10
});
});

it('should cast to uchar', async () => {
const data = await sharp(inputJpg)
.resize(32)
.sharpen()
.avif({ effort: 0 })
.toBuffer();
const { size, ...metadata } = await sharp(data)
.metadata();
assert.deepStrictEqual(metadata, {
channels: 3,
compression: 'av1',
depth: 'uchar',
format: 'heif',
hasAlpha: false,
hasProfile: false,
height: 26,
isProgressive: false,
pagePrimary: 0,
pages: 1,
space: 'srgb',
width: 32
});
});
});

0 comments on commit 28b87db

Please sign in to comment.