Skip to content

Commit

Permalink
Limit HEIF output dimensions to 16384x16384
Browse files Browse the repository at this point in the history
This is a slightly breaking change to sync with the behaviour of a
forthcoming libvips patch release. It also matches the libavif
limit. Having an arbitrary limit is safer than no limit.
  • Loading branch information
lovell committed Jul 10, 2023
1 parent d2f0fa8 commit 5522060
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Requires libvips v8.14.2

### v0.32.2 - TBD

* Limit HEIF output dimensions to 16384x16384, matches libvips.

* Ensure exceptions are not thrown when terminating.
[#3569](https://github.com/lovell/sharp/issues/3569)

Expand Down
4 changes: 4 additions & 0 deletions src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,10 @@ namespace sharp {
if (image.width() > 65535 || height > 65535) {
throw vips::VError("Processed image is too large for the GIF format");
}
} else if (imageType == ImageType::HEIF) {
if (image.width() > 16384 || height > 16384) {
throw vips::VError("Processed image is too large for the HEIF format");
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ class PipelineWorker : public Napi::AsyncWorker {
} else if (baton->formatOut == "heif" ||
(baton->formatOut == "input" && inputImageType == sharp::ImageType::HEIF)) {
// Write HEIF to buffer
sharp::AssertImageTypeDimensions(image, sharp::ImageType::HEIF);
image = sharp::RemoveAnimationProperties(image).cast(VIPS_FORMAT_UCHAR);
VipsArea *area = reinterpret_cast<VipsArea*>(image.heifsave_buffer(VImage::option()
->set("strip", !baton->withMetadata)
Expand Down Expand Up @@ -1130,6 +1131,7 @@ class PipelineWorker : public Napi::AsyncWorker {
} else if (baton->formatOut == "heif" || (mightMatchInput && isHeif) ||
(willMatchInput && inputImageType == sharp::ImageType::HEIF)) {
// Write HEIF to file
sharp::AssertImageTypeDimensions(image, sharp::ImageType::HEIF);
image = sharp::RemoveAnimationProperties(image).cast(VIPS_FORMAT_UCHAR);
image.heifsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
->set("strip", !baton->withMetadata)
Expand Down
14 changes: 14 additions & 0 deletions test/unit/avif.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,18 @@ describe('AVIF', () => {
width: 32
});
});

it('Invalid width - too large', async () =>
assert.rejects(
() => sharp({ create: { width: 16385, height: 16, channels: 3, background: 'red' } }).avif().toBuffer(),
/Processed image is too large for the HEIF format/
)
);

it('Invalid height - too large', async () =>
assert.rejects(
() => sharp({ create: { width: 16, height: 16385, channels: 3, background: 'red' } }).avif().toBuffer(),
/Processed image is too large for the HEIF format/
)
);
});

0 comments on commit 5522060

Please sign in to comment.