Expose unlimited option for HEIF input

This commit is contained in:
Lovell Fuller 2022-09-05 09:15:10 +01:00
parent 31c1cfb049
commit c1393daa70
5 changed files with 22 additions and 8 deletions

View File

@ -24,7 +24,7 @@ Implements the [stream.Duplex][1] class.
* `options.limitInputPixels` **([number][14] | [boolean][15])** Do not process input images where the number of pixels * `options.limitInputPixels` **([number][14] | [boolean][15])** Do not process input images where the number of pixels
(width x height) exceeds this limit. Assumes image dimensions contained in the input metadata can be trusted. (width x height) exceeds this limit. Assumes image dimensions contained in the input metadata can be trusted.
An integral Number of pixels, zero or false to remove limit, true to use default limit of 268402689 (0x3FFF x 0x3FFF). (optional, default `268402689`) An integral Number of pixels, zero or false to remove limit, true to use default limit of 268402689 (0x3FFF x 0x3FFF). (optional, default `268402689`)
* `options.unlimited` **[boolean][15]** Set this to `true` to remove safety features that help prevent memory exhaustion (SVG, PNG, JPEG). (optional, default `false`) * `options.unlimited` **[boolean][15]** Set this to `true` to remove safety features that help prevent memory exhaustion (JPEG, PNG, SVG, HEIF). (optional, default `false`)
* `options.sequentialRead` **[boolean][15]** Set this to `true` to use sequential rather than random access where possible. * `options.sequentialRead` **[boolean][15]** Set this to `true` to use sequential rather than random access where possible.
This can reduce memory usage and might improve performance on some systems. (optional, default `false`) This can reduce memory usage and might improve performance on some systems. (optional, default `false`)
* `options.density` **[number][14]** number representing the DPI for vector images in the range 1 to 100000. (optional, default `72`) * `options.density` **[number][14]** number representing the DPI for vector images in the range 1 to 100000. (optional, default `72`)

View File

@ -123,7 +123,7 @@ const debuglog = util.debuglog('sharp');
* @param {number|boolean} [options.limitInputPixels=268402689] - Do not process input images where the number of pixels * @param {number|boolean} [options.limitInputPixels=268402689] - Do not process input images where the number of pixels
* (width x height) exceeds this limit. Assumes image dimensions contained in the input metadata can be trusted. * (width x height) exceeds this limit. Assumes image dimensions contained in the input metadata can be trusted.
* An integral Number of pixels, zero or false to remove limit, true to use default limit of 268402689 (0x3FFF x 0x3FFF). * An integral Number of pixels, zero or false to remove limit, true to use default limit of 268402689 (0x3FFF x 0x3FFF).
* @param {boolean} [options.unlimited=false] - Set this to `true` to remove safety features that help prevent memory exhaustion (SVG, PNG, JPEG). * @param {boolean} [options.unlimited=false] - Set this to `true` to remove safety features that help prevent memory exhaustion (JPEG, PNG, SVG, HEIF).
* @param {boolean} [options.sequentialRead=false] - Set this to `true` to use sequential rather than random access where possible. * @param {boolean} [options.sequentialRead=false] - Set this to `true` to use sequential rather than random access where possible.
* This can reduce memory usage and might improve performance on some systems. * This can reduce memory usage and might improve performance on some systems.
* @param {number} [options.density=72] - number representing the DPI for vector images in the range 1 to 100000. * @param {number} [options.density=72] - number representing the DPI for vector images in the range 1 to 100000.

View File

@ -164,7 +164,7 @@ namespace sharp {
descriptor->limitInputPixels = static_cast<uint64_t>(AttrAsInt64(input, "limitInputPixels")); descriptor->limitInputPixels = static_cast<uint64_t>(AttrAsInt64(input, "limitInputPixels"));
// Allow switch from random to sequential access // Allow switch from random to sequential access
descriptor->access = AttrAsBool(input, "sequentialRead") ? VIPS_ACCESS_SEQUENTIAL : VIPS_ACCESS_RANDOM; descriptor->access = AttrAsBool(input, "sequentialRead") ? VIPS_ACCESS_SEQUENTIAL : VIPS_ACCESS_RANDOM;
// Remove safety features and allow unlimited SVG/PNG/JPEG input // Remove safety features and allow unlimited input
descriptor->unlimited = AttrAsBool(input, "unlimited"); descriptor->unlimited = AttrAsBool(input, "unlimited");
return descriptor; return descriptor;
} }
@ -334,6 +334,17 @@ namespace sharp {
imageType == ImageType::PDF; imageType == ImageType::PDF;
} }
/*
Does this image type support removal of safety limits?
*/
bool ImageTypeSupportsUnlimited(ImageType imageType) {
return
imageType == ImageType::JPEG ||
imageType == ImageType::PNG ||
imageType == ImageType::SVG ||
imageType == ImageType::HEIF;
}
/* /*
Open an image from the given InputDescriptor (filesystem, compressed buffer, raw pixel data) Open an image from the given InputDescriptor (filesystem, compressed buffer, raw pixel data)
*/ */
@ -362,8 +373,7 @@ namespace sharp {
vips::VOption *option = VImage::option() vips::VOption *option = VImage::option()
->set("access", descriptor->access) ->set("access", descriptor->access)
->set("fail_on", descriptor->failOn); ->set("fail_on", descriptor->failOn);
if (descriptor->unlimited && if (descriptor->unlimited && ImageTypeSupportsUnlimited(imageType)) {
(imageType == ImageType::SVG || imageType == ImageType::PNG || imageType == ImageType::JPEG)) {
option->set("unlimited", TRUE); option->set("unlimited", TRUE);
} }
if (imageType == ImageType::SVG || imageType == ImageType::PDF) { if (imageType == ImageType::SVG || imageType == ImageType::PDF) {
@ -466,8 +476,7 @@ namespace sharp {
vips::VOption *option = VImage::option() vips::VOption *option = VImage::option()
->set("access", descriptor->access) ->set("access", descriptor->access)
->set("fail_on", descriptor->failOn); ->set("fail_on", descriptor->failOn);
if (descriptor->unlimited && if (descriptor->unlimited && ImageTypeSupportsUnlimited(imageType)) {
(imageType == ImageType::SVG || imageType == ImageType::PNG || imageType == ImageType::JPEG)) {
option->set("unlimited", TRUE); option->set("unlimited", TRUE);
} }
if (imageType == ImageType::SVG || imageType == ImageType::PDF) { if (imageType == ImageType::SVG || imageType == ImageType::PDF) {

View File

@ -206,6 +206,11 @@ namespace sharp {
*/ */
bool ImageTypeSupportsPage(ImageType imageType); bool ImageTypeSupportsPage(ImageType imageType);
/*
Does this image type support removal of safety limits?
*/
bool ImageTypeSupportsUnlimited(ImageType imageType);
/* /*
Open an image from the given InputDescriptor (filesystem, compressed buffer, raw pixel data) Open an image from the given InputDescriptor (filesystem, compressed buffer, raw pixel data)
*/ */

View File

@ -680,7 +680,7 @@ describe('Input/output', function () {
}); });
}); });
describe('Switch off safety limits for PNG/SVG/JPEG input', () => { describe('Switch off safety limits for certain formats', () => {
it('Valid', () => { it('Valid', () => {
assert.doesNotThrow(() => { assert.doesNotThrow(() => {
sharp({ unlimited: true }); sharp({ unlimited: true });