Skip to content

Commit a1c8a6d

Browse files
committed
Add support for libvips JPEG 2000 one-shot load option
1 parent 03e1b19 commit a1c8a6d

File tree

6 files changed

+33
-5
lines changed

6 files changed

+33
-5
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ where the overall height is the `pageHeight` multiplied by the number of `pages`
4747
| [options.subifd] | <code>number</code> | <code>-1</code> | subIFD (Sub Image File Directory) to extract for OME-TIFF, defaults to main image. |
4848
| [options.level] | <code>number</code> | <code>0</code> | level to extract from a multi-level input (OpenSlide), zero based. |
4949
| [options.pdfBackground] | <code>string</code> \| <code>Object</code> | | Background colour to use when PDF is partially transparent. Parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha. Requires the use of a globally-installed libvips compiled with support for PDFium, Poppler, ImageMagick or GraphicsMagick. |
50+
| [options.jp2Oneshot] | <code>boolean</code> | <code>false</code> | Set to `true` to load JPEG 2000 images using [oneshot mode](https://github.com/libvips/libvips/issues/4205)|
5051
| [options.animated] | <code>boolean</code> | <code>false</code> | Set to `true` to read all frames/pages of an animated image (GIF, WebP, TIFF), equivalent of setting `pages` to `-1`. |
5152
| [options.raw] | <code>Object</code> | | describes raw pixel input image data. See `raw()` for pixel ordering. |
5253
| [options.raw.width] | <code>number</code> | | integral number of pixels wide. |

lib/constructor.js

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ const debuglog = util.debuglog('sharp');
156156
* @param {number} [options.subifd=-1] - subIFD (Sub Image File Directory) to extract for OME-TIFF, defaults to main image.
157157
* @param {number} [options.level=0] - level to extract from a multi-level input (OpenSlide), zero based.
158158
* @param {string|Object} [options.pdfBackground] - Background colour to use when PDF is partially transparent. Parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha. Requires the use of a globally-installed libvips compiled with support for PDFium, Poppler, ImageMagick or GraphicsMagick.
159+
* @param {boolean} [options.jp2Oneshot=false] - Set to `true` to load JPEG 2000 images using [oneshot mode](https://github.com/libvips/libvips/issues/4205)
159160
* @param {boolean} [options.animated=false] - Set to `true` to read all frames/pages of an animated image (GIF, WebP, TIFF), equivalent of setting `pages` to `-1`.
160161
* @param {Object} [options.raw] - describes raw pixel input image data. See `raw()` for pixel ordering.
161162
* @param {number} [options.raw.width] - integral number of pixels wide.
@@ -320,6 +321,7 @@ const Sharp = function (input, options) {
320321
jp2TileWidth: 512,
321322
jp2Lossless: false,
322323
jp2ChromaSubsampling: '4:4:4',
324+
jp2Oneshot: false,
323325
webpQuality: 80,
324326
webpAlphaQuality: 100,
325327
webpLossless: false,

lib/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,8 @@ declare namespace sharp {
994994
/** Background colour to use when PDF is partially transparent. Requires the use of a globally-installed libvips compiled with support for PDFium, Poppler, ImageMagick or GraphicsMagick. */
995995
pdfBackground?: Colour | Color | undefined;
996996
/** Set to `true` to read all frames/pages of an animated image (equivalent of setting `pages` to `-1`). (optional, default false) */
997+
jp2Oneshot?: boolean | undefined;
998+
/** Set to `true` to load JPEG 2000 images using [oneshot mode(https://github.com/libvips/libvips/issues/4205) **/
997999
animated?: boolean | undefined;
9981000
/** Describes raw pixel input image data. See raw() for pixel ordering. */
9991001
raw?: CreateRaw | undefined;

lib/input.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ const align = {
2828
* @private
2929
*/
3030
function _inputOptionsFromObject (obj) {
31-
const { raw, density, limitInputPixels, ignoreIcc, unlimited, sequentialRead, failOn, failOnError, animated, page, pages, subifd, pdfBackground, autoOrient } = obj;
32-
return [raw, density, limitInputPixels, ignoreIcc, unlimited, sequentialRead, failOn, failOnError, animated, page, pages, subifd, pdfBackground, autoOrient].some(is.defined)
33-
? { raw, density, limitInputPixels, ignoreIcc, unlimited, sequentialRead, failOn, failOnError, animated, page, pages, subifd, pdfBackground, autoOrient }
31+
const { raw, density, limitInputPixels, ignoreIcc, unlimited, sequentialRead, failOn, failOnError, animated, page, pages, subifd, pdfBackground, autoOrient, jp2Oneshot } = obj;
32+
return [raw, density, limitInputPixels, ignoreIcc, unlimited, sequentialRead, failOn, failOnError, animated, page, pages, subifd, pdfBackground, autoOrient, jp2Oneshot].some(is.defined)
33+
? { raw, density, limitInputPixels, ignoreIcc, unlimited, sequentialRead, failOn, failOnError, animated, page, pages, subifd, pdfBackground, autoOrient, jp2Oneshot }
3434
: undefined;
3535
}
3636

@@ -45,7 +45,8 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
4545
limitInputPixels: Math.pow(0x3FFF, 2),
4646
ignoreIcc: false,
4747
unlimited: false,
48-
sequentialRead: true
48+
sequentialRead: true,
49+
jp2Oneshot: false
4950
};
5051
if (is.string(input)) {
5152
// filesystem
@@ -251,6 +252,15 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
251252
if (is.defined(inputOptions.pdfBackground)) {
252253
this._setBackgroundColourOption('pdfBackground', inputOptions.pdfBackground);
253254
}
255+
256+
if (is.defined(inputOptions.jp2Oneshot)) {
257+
if (is.bool(inputOptions.jp2Oneshot)) {
258+
inputDescriptor.jp2Oneshot = inputOptions.jp2Oneshot;
259+
} else {
260+
throw is.invalidParameterError('jp2Oneshot', 'boolean', inputOptions.jp2Oneshot);
261+
}
262+
}
263+
254264
// Create new image
255265
if (is.defined(inputOptions.create)) {
256266
if (

src/common.cc

+11
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ namespace sharp {
113113
if (HasAttr(input, "pdfBackground")) {
114114
descriptor->pdfBackground = AttrAsVectorOfDouble(input, "pdfBackground");
115115
}
116+
// Use JPEG 2000 oneshot mode?
117+
if (HasAttr(input, "jp2Oneshot")) {
118+
descriptor->jp2Oneshot = AttrAsBool(input, "jp2Oneshot");
119+
}
120+
116121
// Create new image
117122
if (HasAttr(input, "createChannels")) {
118123
descriptor->createChannels = AttrAsUint32(input, "createChannels");
@@ -434,6 +439,9 @@ namespace sharp {
434439
if (imageType == ImageType::PDF) {
435440
option->set("background", descriptor->pdfBackground);
436441
}
442+
if (imageType == ImageType::JP2 && descriptor->jp2Oneshot) {
443+
option->set("oneshot", 1);
444+
}
437445
image = VImage::new_from_buffer(descriptor->buffer, descriptor->bufferLength, nullptr, option);
438446
if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
439447
image = SetDensity(image, descriptor->density);
@@ -541,6 +549,9 @@ namespace sharp {
541549
if (imageType == ImageType::PDF) {
542550
option->set("background", descriptor->pdfBackground);
543551
}
552+
if (imageType == ImageType::JP2 && descriptor->jp2Oneshot) {
553+
option->set("oneshot", 1);
554+
}
544555
image = VImage::new_from_file(descriptor->file.data(), option);
545556
if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
546557
image = SetDensity(image, descriptor->density);

src/common.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ namespace sharp {
7878
VipsAlign joinHalign;
7979
VipsAlign joinValign;
8080
std::vector<double> pdfBackground;
81+
bool jp2Oneshot;
8182

8283
InputDescriptor():
8384
autoOrient(false),
@@ -120,7 +121,8 @@ namespace sharp {
120121
joinBackground{ 0.0, 0.0, 0.0, 255.0 },
121122
joinHalign(VIPS_ALIGN_LOW),
122123
joinValign(VIPS_ALIGN_LOW),
123-
pdfBackground{ 255.0, 255.0, 255.0, 255.0 } {}
124+
pdfBackground{ 255.0, 255.0, 255.0, 255.0 },
125+
jp2Oneshot(false) {}
124126
};
125127

126128
// Convenience methods to access the attributes of a Napi::Object

0 commit comments

Comments
 (0)