Compare commits

..

No commits in common. "852c7f8663709a5260b6099fe0a8937f275897e0" and "76995deefa8617cd1fd6d69cc6461e16a4fad78f" have entirely different histories.

4 changed files with 102 additions and 60 deletions

View File

@ -30,7 +30,7 @@ const inputStreamParameters = [
// Format-specific
'jp2', 'openSlide', 'pdf', 'raw', 'svg', 'tiff',
// Deprecated
'failOnError', 'openSlideLevel', 'pdfBackground', 'tiffSubifd'
'failOnError', 'level', 'pdfBackground', 'subifd'
];
/**
@ -246,14 +246,14 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
// OpenSlide specific options
if (is.object(inputOptions.openSlide) && is.defined(inputOptions.openSlide.level)) {
if (is.integer(inputOptions.openSlide.level) && is.inRange(inputOptions.openSlide.level, 0, 256)) {
inputDescriptor.openSlideLevel = inputOptions.openSlide.level;
inputDescriptor.level = inputOptions.openSlide.level;
} else {
throw is.invalidParameterError('openSlide.level', 'integer between 0 and 256', inputOptions.openSlide.level);
}
} else if (is.defined(inputOptions.level)) {
// Deprecated
if (is.integer(inputOptions.level) && is.inRange(inputOptions.level, 0, 256)) {
inputDescriptor.openSlideLevel = inputOptions.level;
inputDescriptor.level = inputOptions.level;
} else {
throw is.invalidParameterError('level', 'integer between 0 and 256', inputOptions.level);
}
@ -261,14 +261,14 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
// TIFF specific options
if (is.object(inputOptions.tiff) && is.defined(inputOptions.tiff.subifd)) {
if (is.integer(inputOptions.tiff.subifd) && is.inRange(inputOptions.tiff.subifd, -1, 100000)) {
inputDescriptor.tiffSubifd = inputOptions.tiff.subifd;
inputDescriptor.subifd = inputOptions.tiff.subifd;
} else {
throw is.invalidParameterError('tiff.subifd', 'integer between -1 and 100000', inputOptions.tiff.subifd);
}
} else if (is.defined(inputOptions.subifd)) {
// Deprecated
if (is.integer(inputOptions.subifd) && is.inRange(inputOptions.subifd, -1, 100000)) {
inputDescriptor.tiffSubifd = inputOptions.subifd;
inputDescriptor.subifd = inputOptions.subifd;
} else {
throw is.invalidParameterError('subifd', 'integer between -1 and 100000', inputOptions.subifd);
}

View File

@ -109,12 +109,12 @@ namespace sharp {
descriptor->svgHighBitdepth = AttrAsBool(input, "svgHighBitdepth");
}
// Multi-level input (OpenSlide)
if (HasAttr(input, "openSlideLevel")) {
descriptor->openSlideLevel = AttrAsUint32(input, "openSlideLevel");
if (HasAttr(input, "level")) {
descriptor->level = AttrAsUint32(input, "level");
}
// subIFD (OME-TIFF)
if (HasAttr(input, "subifd")) {
descriptor->tiffSubifd = AttrAsInt32(input, "tiffSubifd");
descriptor->subifd = AttrAsInt32(input, "subifd");
}
// // PDF background color
if (HasAttr(input, "pdfBackground")) {
@ -394,48 +394,6 @@ namespace sharp {
imageType == ImageType::HEIF;
}
/*
Format-specific options builder
*/
vips::VOption* GetOptionsForImageType(ImageType imageType, InputDescriptor *descriptor) {
vips::VOption *option = VImage::option()
->set("access", descriptor->access)
->set("fail_on", descriptor->failOn);
if (descriptor->unlimited && ImageTypeSupportsUnlimited(imageType)) {
option->set("unlimited", true);
}
if (ImageTypeSupportsPage(imageType)) {
option->set("n", descriptor->pages);
option->set("page", descriptor->page);
}
switch (imageType) {
case ImageType::SVG:
option->set("dpi", descriptor->density)
->set("stylesheet", descriptor->svgStylesheet.data())
->set("high_bitdepth", descriptor->svgHighBitdepth);
break;
case ImageType::TIFF:
option->set("tiffSubifd", descriptor->tiffSubifd);
break;
case ImageType::PDF:
option->set("dpi", descriptor->density)
->set("background", descriptor->pdfBackground);
break;
case ImageType::OPENSLIDE:
option->set("openSlideLevel", descriptor->openSlideLevel);
break;
case ImageType::JP2:
option->set("oneshot", descriptor->jp2Oneshot);
break;
case ImageType::MAGICK:
option->set("density", std::to_string(descriptor->density).data());
break;
default:
break;
}
return option;
}
/*
Open an image from the given InputDescriptor (filesystem, compressed buffer, raw pixel data)
*/
@ -462,7 +420,38 @@ namespace sharp {
imageType = DetermineImageType(descriptor->buffer, descriptor->bufferLength);
if (imageType != ImageType::UNKNOWN) {
try {
vips::VOption *option = GetOptionsForImageType(imageType, descriptor);
vips::VOption *option = VImage::option()
->set("access", descriptor->access)
->set("fail_on", descriptor->failOn);
if (descriptor->unlimited && ImageTypeSupportsUnlimited(imageType)) {
option->set("unlimited", true);
}
if (imageType == ImageType::SVG || imageType == ImageType::PDF) {
option->set("dpi", descriptor->density);
}
if (imageType == ImageType::MAGICK) {
option->set("density", std::to_string(descriptor->density).data());
}
if (ImageTypeSupportsPage(imageType)) {
option->set("n", descriptor->pages);
option->set("page", descriptor->page);
}
if (imageType == ImageType::SVG) {
option->set("stylesheet", descriptor->svgStylesheet.data());
option->set("high_bitdepth", descriptor->svgHighBitdepth);
}
if (imageType == ImageType::OPENSLIDE) {
option->set("level", descriptor->level);
}
if (imageType == ImageType::TIFF) {
option->set("subifd", descriptor->subifd);
}
if (imageType == ImageType::PDF) {
option->set("background", descriptor->pdfBackground);
}
if (imageType == ImageType::JP2) {
option->set("oneshot", descriptor->jp2Oneshot);
}
image = VImage::new_from_buffer(descriptor->buffer, descriptor->bufferLength, nullptr, option);
if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
image = SetDensity(image, descriptor->density);
@ -545,7 +534,34 @@ namespace sharp {
}
if (imageType != ImageType::UNKNOWN) {
try {
vips::VOption *option = GetOptionsForImageType(imageType, descriptor);
vips::VOption *option = VImage::option()
->set("access", descriptor->access)
->set("fail_on", descriptor->failOn);
if (descriptor->unlimited && ImageTypeSupportsUnlimited(imageType)) {
option->set("unlimited", true);
}
if (imageType == ImageType::SVG || imageType == ImageType::PDF) {
option->set("dpi", descriptor->density);
}
if (imageType == ImageType::MAGICK) {
option->set("density", std::to_string(descriptor->density).data());
}
if (ImageTypeSupportsPage(imageType)) {
option->set("n", descriptor->pages);
option->set("page", descriptor->page);
}
if (imageType == ImageType::OPENSLIDE) {
option->set("level", descriptor->level);
}
if (imageType == ImageType::TIFF) {
option->set("subifd", descriptor->subifd);
}
if (imageType == ImageType::PDF) {
option->set("background", descriptor->pdfBackground);
}
if (imageType == ImageType::JP2) {
option->set("oneshot", descriptor->jp2Oneshot);
}
image = VImage::new_from_file(descriptor->file.data(), option);
if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
image = SetDensity(image, descriptor->density);

View File

@ -50,6 +50,8 @@ namespace sharp {
bool rawPremultiplied;
int pages;
int page;
int level;
int subifd;
int createChannels;
int createWidth;
int createHeight;
@ -77,8 +79,6 @@ namespace sharp {
VipsAlign joinValign;
std::string svgStylesheet;
bool svgHighBitdepth;
int tiffSubifd;
int openSlideLevel;
std::vector<double> pdfBackground;
bool jp2Oneshot;
@ -100,6 +100,8 @@ namespace sharp {
rawPremultiplied(false),
pages(1),
page(0),
level(0),
subifd(-1),
createChannels(0),
createWidth(0),
createHeight(0),
@ -122,8 +124,6 @@ namespace sharp {
joinHalign(VIPS_ALIGN_LOW),
joinValign(VIPS_ALIGN_LOW),
svgHighBitdepth(false),
tiffSubifd(-1),
openSlideLevel(0),
pdfBackground{ 255.0, 255.0, 255.0, 255.0 },
jp2Oneshot(false) {}
};
@ -221,9 +221,14 @@ namespace sharp {
ImageType DetermineImageType(char const *file);
/*
Format-specific options builder
Does this image type support multiple pages?
*/
vips::VOption* GetOptionsForImageType(ImageType imageType, InputDescriptor *descriptor);
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)

View File

@ -241,7 +241,11 @@ class PipelineWorker : public Napi::AsyncWorker {
// factor for jpegload*, a double scale factor for webpload*,
// pdfload* and svgload*
if (jpegShrinkOnLoad > 1) {
vips::VOption *option = GetOptionsForImageType(inputImageType, baton->input)->set("shrink", jpegShrinkOnLoad);
vips::VOption *option = VImage::option()
->set("access", access)
->set("shrink", jpegShrinkOnLoad)
->set("unlimited", baton->input->unlimited)
->set("fail_on", baton->input->failOn);
if (baton->input->buffer != nullptr) {
// Reload JPEG buffer
VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength);
@ -252,8 +256,14 @@ class PipelineWorker : public Napi::AsyncWorker {
image = VImage::jpegload(const_cast<char*>(baton->input->file.data()), option);
}
} else if (scale != 1.0) {
vips::VOption *option = GetOptionsForImageType(inputImageType, baton->input)->set("scale", scale);
vips::VOption *option = VImage::option()
->set("access", access)
->set("scale", scale)
->set("fail_on", baton->input->failOn);
if (inputImageType == sharp::ImageType::WEBP) {
option->set("n", baton->input->pages);
option->set("page", baton->input->page);
if (baton->input->buffer != nullptr) {
// Reload WebP buffer
VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength);
@ -264,6 +274,11 @@ class PipelineWorker : public Napi::AsyncWorker {
image = VImage::webpload(const_cast<char*>(baton->input->file.data()), option);
}
} else if (inputImageType == sharp::ImageType::SVG) {
option->set("unlimited", baton->input->unlimited);
option->set("dpi", baton->input->density);
option->set("stylesheet", baton->input->svgStylesheet.data());
option->set("high_bitdepth", baton->input->svgHighBitdepth);
if (baton->input->buffer != nullptr) {
// Reload SVG buffer
VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength);
@ -278,6 +293,11 @@ class PipelineWorker : public Napi::AsyncWorker {
throw vips::VError("Input SVG image will exceed 32767x32767 pixel limit when scaled");
}
} else if (inputImageType == sharp::ImageType::PDF) {
option->set("n", baton->input->pages);
option->set("page", baton->input->page);
option->set("dpi", baton->input->density);
option->set("background", baton->input->pdfBackground);
if (baton->input->buffer != nullptr) {
// Reload PDF buffer
VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength);
@ -287,6 +307,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Reload PDF file
image = VImage::pdfload(const_cast<char*>(baton->input->file.data()), option);
}
sharp::SetDensity(image, baton->input->density);
}
} else {