Skip to content

Commit 9c5de8c

Browse files
committed
WIP: Switch from custom VError to standard runtime_error
1 parent ef77388 commit 9c5de8c

File tree

13 files changed

+41
-44
lines changed

13 files changed

+41
-44
lines changed

docs/src/content/docs/api-output.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ instead of providing `xres` and `yres` in pixels/mm.
717717
| [options.xres] | <code>number</code> | <code>1.0</code> | horizontal resolution in pixels/mm |
718718
| [options.yres] | <code>number</code> | <code>1.0</code> | vertical resolution in pixels/mm |
719719
| [options.resolutionUnit] | <code>string</code> | <code>&quot;&#x27;inch&#x27;&quot;</code> | resolution unit options: inch, cm |
720-
| [options.bitdepth] | <code>number</code> | <code>8</code> | reduce bitdepth to 1, 2 or 4 bit |
720+
| [options.bitdepth] | <code>number</code> | <code>0</code> | reduce bitdepth to 1, 2 or 4 bit |
721721
| [options.miniswhite] | <code>boolean</code> | <code>false</code> | write 1-bit images as miniswhite |
722722

723723
**Example**

docs/src/content/docs/changelog/v0.35.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ slug: changelog/v0.35.0
2020

2121
* Upgrade to libvips v8.18.0 for upstream bug fixes.
2222

23+
* Ensure TIFF output `bitdepth` option is limited to 1, 2 or 4.
24+
2325
* Deprecate Windows 32-bit (win32-ia32) prebuilt binaries.
2426

2527
* Add AVIF/HEIF `tune` option for control over quality metrics.

lib/constructor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ const Sharp = function (input, options) {
366366
tiffPredictor: 'horizontal',
367367
tiffPyramid: false,
368368
tiffMiniswhite: false,
369-
tiffBitdepth: 8,
369+
tiffBitdepth: 0,
370370
tiffTile: false,
371371
tiffTileHeight: 256,
372372
tiffTileWidth: 256,

lib/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,8 +1479,8 @@ declare namespace sharp {
14791479
xres?: number | undefined;
14801480
/** Vertical resolution in pixels/mm (optional, default 1.0) */
14811481
yres?: number | undefined;
1482-
/** Reduce bitdepth to 1, 2 or 4 bit (optional, default 8) */
1483-
bitdepth?: 1 | 2 | 4 | 8 | undefined;
1482+
/** Reduce bitdepth to 1, 2 or 4 bit (optional, default 0) */
1483+
bitdepth?: 1 | 2 | 4 | undefined;
14841484
/** Write 1-bit images as miniswhite (optional, default false) */
14851485
miniswhite?: boolean | undefined;
14861486
/** Resolution unit options: inch, cm (optional, default 'inch') */

lib/output.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ function trySetAnimationOptions (source, target) {
10551055
* @param {number} [options.xres=1.0] - horizontal resolution in pixels/mm
10561056
* @param {number} [options.yres=1.0] - vertical resolution in pixels/mm
10571057
* @param {string} [options.resolutionUnit='inch'] - resolution unit options: inch, cm
1058-
* @param {number} [options.bitdepth=8] - reduce bitdepth to 1, 2 or 4 bit
1058+
* @param {number} [options.bitdepth=0] - reduce bitdepth to 1, 2 or 4 bit
10591059
* @param {boolean} [options.miniswhite=false] - write 1-bit images as miniswhite
10601060
* @returns {Sharp}
10611061
* @throws {Error} Invalid options
@@ -1070,10 +1070,10 @@ function tiff (options) {
10701070
}
10711071
}
10721072
if (is.defined(options.bitdepth)) {
1073-
if (is.integer(options.bitdepth) && is.inArray(options.bitdepth, [1, 2, 4, 8])) {
1073+
if (is.integer(options.bitdepth) && is.inArray(options.bitdepth, [1, 2, 4])) {
10741074
this.options.tiffBitdepth = options.bitdepth;
10751075
} else {
1076-
throw is.invalidParameterError('bitdepth', '1, 2, 4 or 8', options.bitdepth);
1076+
throw is.invalidParameterError('bitdepth', '1, 2 or 4', options.bitdepth);
10771077
}
10781078
}
10791079
// tiling

src/common.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,11 @@ namespace sharp {
510510
option = GetOptionsForImageType(imageType, descriptor);
511511
image = VImage::new_from_buffer(descriptor->buffer, descriptor->bufferLength, nullptr, option);
512512
}
513-
} catch (vips::VError const &err) {
514-
throw vips::VError(std::string("Input buffer has corrupt header: ") + err.what());
513+
} catch (std::runtime_error const &err) {
514+
throw std::runtime_error(std::string("Input buffer has corrupt header: ") + err.what());
515515
}
516516
} else {
517-
throw vips::VError("Input buffer contains unsupported image format");
517+
throw std::runtime_error("Input buffer contains unsupported image format");
518518
}
519519
}
520520
} else {
@@ -585,10 +585,10 @@ namespace sharp {
585585
imageType = DetermineImageType(descriptor->file.data());
586586
if (imageType == ImageType::MISSING) {
587587
if (descriptor->file.find("<svg") != std::string::npos) {
588-
throw vips::VError("Input file is missing, did you mean "
588+
throw std::runtime_error("Input file is missing, did you mean "
589589
"sharp(Buffer.from('" + descriptor->file.substr(0, 8) + "...')?");
590590
}
591-
throw vips::VError("Input file is missing: " + descriptor->file);
591+
throw std::runtime_error("Input file is missing: " + descriptor->file);
592592
}
593593
if (imageType != ImageType::UNKNOWN) {
594594
try {
@@ -600,19 +600,19 @@ namespace sharp {
600600
option = GetOptionsForImageType(imageType, descriptor);
601601
image = VImage::new_from_file(descriptor->file.data(), option);
602602
}
603-
} catch (vips::VError const &err) {
604-
throw vips::VError(std::string("Input file has corrupt header: ") + err.what());
603+
} catch (std::runtime_error const &err) {
604+
throw std::runtime_error(std::string("Input file has corrupt header: ") + err.what());
605605
}
606606
} else {
607-
throw vips::VError("Input file contains unsupported image format");
607+
throw std::runtime_error("Input file contains unsupported image format");
608608
}
609609
}
610610
}
611611

612612
// Limit input images to a given number of pixels, where pixels = width * height
613613
if (descriptor->limitInputPixels > 0 &&
614614
static_cast<uint64_t>(image.width()) * image.height() > descriptor->limitInputPixels) {
615-
throw vips::VError("Input image exceeds pixel limit");
615+
throw std::runtime_error("Input image exceeds pixel limit");
616616
}
617617
return std::make_tuple(image, imageType);
618618
}
@@ -788,19 +788,19 @@ namespace sharp {
788788
: image.height();
789789
if (imageType == ImageType::JPEG) {
790790
if (image.width() > 65535 || height > 65535) {
791-
throw vips::VError("Processed image is too large for the JPEG format");
791+
throw std::runtime_error("Processed image is too large for the JPEG format");
792792
}
793793
} else if (imageType == ImageType::WEBP) {
794794
if (image.width() > 16383 || height > 16383) {
795-
throw vips::VError("Processed image is too large for the WebP format");
795+
throw std::runtime_error("Processed image is too large for the WebP format");
796796
}
797797
} else if (imageType == ImageType::GIF) {
798798
if (image.width() > 65535 || height > 65535) {
799-
throw vips::VError("Processed image is too large for the GIF format");
799+
throw std::runtime_error("Processed image is too large for the GIF format");
800800
}
801801
} else if (imageType == ImageType::HEIF) {
802802
if (image.width() > 16384 || height > 16384) {
803-
throw vips::VError("Processed image is too large for the HEIF format");
803+
throw std::runtime_error("Processed image is too large for the HEIF format");
804804
}
805805
}
806806
}

src/metadata.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MetadataWorker : public Napi::AsyncWorker {
3131
sharp::ImageType imageType = sharp::ImageType::UNKNOWN;
3232
try {
3333
std::tie(image, imageType) = OpenInput(baton->input);
34-
} catch (vips::VError const &err) {
34+
} catch (std::runtime_error const &err) {
3535
(baton->err).append(err.what());
3636
}
3737
if (imageType != sharp::ImageType::UNKNOWN) {

src/operations.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "./operations.h"
1515

1616
using vips::VImage;
17-
using vips::VError;
1817

1918
namespace sharp {
2019
/*
@@ -287,7 +286,7 @@ namespace sharp {
287286
*/
288287
VImage Trim(VImage image, std::vector<double> background, double threshold, bool const lineArt, int const margin) {
289288
if (image.width() < 3 && image.height() < 3) {
290-
throw VError("Image to trim must be at least 3x3 pixels");
289+
throw std::runtime_error("Image to trim must be at least 3x3 pixels");
291290
}
292291
if (background.size() == 0) {
293292
// Top-left pixel provides the default background colour if none is given
@@ -361,7 +360,7 @@ namespace sharp {
361360
VImage Linear(VImage image, std::vector<double> const a, std::vector<double> const b) {
362361
size_t const bands = static_cast<size_t>(image.bands());
363362
if (a.size() > bands) {
364-
throw VError("Band expansion using linear is unsupported");
363+
throw std::runtime_error("Band expansion using linear is unsupported");
365364
}
366365
bool const uchar = !Is16Bit(image.interpretation());
367366
if (image.has_alpha() && a.size() != bands && (a.size() == 1 || a.size() == bands - 1 || bands - 1 == 1)) {

src/pipeline.cc

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class PipelineWorker : public Napi::AsyncWorker {
274274
}
275275
sharp::SetDensity(image, baton->input->density);
276276
if (image.width() > 32767 || image.height() > 32767) {
277-
throw vips::VError("Input SVG image will exceed 32767x32767 pixel limit when scaled");
277+
throw std::runtime_error("Input SVG image will exceed 32767x32767 pixel limit when scaled");
278278
}
279279
} else if (inputImageType == sharp::ImageType::PDF) {
280280
if (baton->input->buffer != nullptr) {
@@ -290,7 +290,7 @@ class PipelineWorker : public Napi::AsyncWorker {
290290
}
291291
} else {
292292
if (inputImageType == sharp::ImageType::SVG && (image.width() > 32767 || image.height() > 32767)) {
293-
throw vips::VError("Input SVG image exceeds 32767x32767 pixel limit");
293+
throw std::runtime_error("Input SVG image exceeds 32767x32767 pixel limit");
294294
}
295295
}
296296
if (baton->input->autoOrient) {
@@ -675,7 +675,7 @@ class PipelineWorker : public Napi::AsyncWorker {
675675

676676
// Verify within current dimensions
677677
if (compositeImage.width() > image.width() || compositeImage.height() > image.height()) {
678-
throw vips::VError("Image to composite must have same dimensions or smaller");
678+
throw std::runtime_error("Image to composite must have same dimensions or smaller");
679679
}
680680
// Check if overlay is tiled
681681
if (composite->tile) {
@@ -1086,20 +1086,19 @@ class PipelineWorker : public Napi::AsyncWorker {
10861086
// Get raw image data
10871087
baton->bufferOut = static_cast<char*>(image.write_to_memory(&baton->bufferOutLength));
10881088
if (baton->bufferOut == nullptr) {
1089-
(baton->err).append("Could not allocate enough memory for raw output");
1090-
return Error();
1089+
throw std::runtime_error("Could not allocate enough memory for raw output");
10911090
}
10921091
baton->formatOut = "raw";
10931092
} else {
10941093
// Unsupported output format
1095-
(baton->err).append("Unsupported output format ");
1094+
auto unsupported = std::string("Unsupported output format ");
10961095
if (baton->formatOut == "input") {
1097-
(baton->err).append("when trying to match input format of ");
1098-
(baton->err).append(ImageTypeId(inputImageType));
1096+
unsupported.append("when trying to match input format of ");
1097+
unsupported.append(ImageTypeId(inputImageType));
10991098
} else {
1100-
(baton->err).append(baton->formatOut);
1099+
unsupported.append(baton->formatOut);
11011100
}
1102-
return Error();
1101+
throw std::runtime_error(unsupported);
11031102
}
11041103
} else {
11051104
// File output
@@ -1274,7 +1273,7 @@ class PipelineWorker : public Napi::AsyncWorker {
12741273
return Error();
12751274
}
12761275
}
1277-
} catch (vips::VError const &err) {
1276+
} catch (std::runtime_error const &err) {
12781277
char const *what = err.what();
12791278
if (what && what[0]) {
12801279
(baton->err).append(what);
@@ -1306,7 +1305,6 @@ class PipelineWorker : public Napi::AsyncWorker {
13061305
}
13071306
warning = sharp::VipsWarningPop();
13081307
}
1309-
13101308
if (baton->err.empty()) {
13111309
int width = baton->width;
13121310
int height = baton->height;
@@ -1407,7 +1405,7 @@ class PipelineWorker : public Napi::AsyncWorker {
14071405

14081406
void MultiPageUnsupported(int const pages, std::string op) {
14091407
if (pages > 1) {
1410-
throw vips::VError(op + " is not supported for multi-page images");
1408+
throw std::runtime_error(op + " is not supported for multi-page images");
14111409
}
14121410
}
14131411

src/pipeline.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ struct PipelineBaton {
365365
tiffBigtiff(false),
366366
tiffPredictor(VIPS_FOREIGN_TIFF_PREDICTOR_HORIZONTAL),
367367
tiffPyramid(false),
368-
tiffBitdepth(8),
368+
tiffBitdepth(0),
369369
tiffMiniswhite(false),
370370
tiffTile(false),
371371
tiffTileHeight(256),

0 commit comments

Comments
 (0)