Skip to content

Commit e286e2b

Browse files
committed
Build format-specific input options in a single function
1 parent 76995de commit e286e2b

File tree

3 files changed

+45
-90
lines changed

3 files changed

+45
-90
lines changed

src/common.cc

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,45 @@ namespace sharp {
394394
imageType == ImageType::HEIF;
395395
}
396396

397+
/*
398+
Format-specific options builder
399+
*/
400+
vips::VOption* GetOptionsForImageType(ImageType imageType, InputDescriptor *descriptor) {
401+
vips::VOption *option = VImage::option()
402+
->set("access", descriptor->access)
403+
->set("fail_on", descriptor->failOn);
404+
if (descriptor->unlimited && ImageTypeSupportsUnlimited(imageType)) {
405+
option->set("unlimited", true);
406+
}
407+
if (imageType == ImageType::SVG || imageType == ImageType::PDF) {
408+
option->set("dpi", descriptor->density);
409+
}
410+
if (imageType == ImageType::MAGICK) {
411+
option->set("density", std::to_string(descriptor->density).data());
412+
}
413+
if (ImageTypeSupportsPage(imageType)) {
414+
option->set("n", descriptor->pages);
415+
option->set("page", descriptor->page);
416+
}
417+
if (imageType == ImageType::SVG) {
418+
option->set("stylesheet", descriptor->svgStylesheet.data());
419+
option->set("high_bitdepth", descriptor->svgHighBitdepth);
420+
}
421+
if (imageType == ImageType::OPENSLIDE) {
422+
option->set("level", descriptor->level);
423+
}
424+
if (imageType == ImageType::TIFF) {
425+
option->set("subifd", descriptor->subifd);
426+
}
427+
if (imageType == ImageType::PDF) {
428+
option->set("background", descriptor->pdfBackground);
429+
}
430+
if (imageType == ImageType::JP2) {
431+
option->set("oneshot", descriptor->jp2Oneshot);
432+
}
433+
return option;
434+
}
435+
397436
/*
398437
Open an image from the given InputDescriptor (filesystem, compressed buffer, raw pixel data)
399438
*/
@@ -420,38 +459,7 @@ namespace sharp {
420459
imageType = DetermineImageType(descriptor->buffer, descriptor->bufferLength);
421460
if (imageType != ImageType::UNKNOWN) {
422461
try {
423-
vips::VOption *option = VImage::option()
424-
->set("access", descriptor->access)
425-
->set("fail_on", descriptor->failOn);
426-
if (descriptor->unlimited && ImageTypeSupportsUnlimited(imageType)) {
427-
option->set("unlimited", true);
428-
}
429-
if (imageType == ImageType::SVG || imageType == ImageType::PDF) {
430-
option->set("dpi", descriptor->density);
431-
}
432-
if (imageType == ImageType::MAGICK) {
433-
option->set("density", std::to_string(descriptor->density).data());
434-
}
435-
if (ImageTypeSupportsPage(imageType)) {
436-
option->set("n", descriptor->pages);
437-
option->set("page", descriptor->page);
438-
}
439-
if (imageType == ImageType::SVG) {
440-
option->set("stylesheet", descriptor->svgStylesheet.data());
441-
option->set("high_bitdepth", descriptor->svgHighBitdepth);
442-
}
443-
if (imageType == ImageType::OPENSLIDE) {
444-
option->set("level", descriptor->level);
445-
}
446-
if (imageType == ImageType::TIFF) {
447-
option->set("subifd", descriptor->subifd);
448-
}
449-
if (imageType == ImageType::PDF) {
450-
option->set("background", descriptor->pdfBackground);
451-
}
452-
if (imageType == ImageType::JP2) {
453-
option->set("oneshot", descriptor->jp2Oneshot);
454-
}
462+
vips::VOption *option = GetOptionsForImageType(imageType, descriptor);
455463
image = VImage::new_from_buffer(descriptor->buffer, descriptor->bufferLength, nullptr, option);
456464
if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
457465
image = SetDensity(image, descriptor->density);
@@ -534,34 +542,7 @@ namespace sharp {
534542
}
535543
if (imageType != ImageType::UNKNOWN) {
536544
try {
537-
vips::VOption *option = VImage::option()
538-
->set("access", descriptor->access)
539-
->set("fail_on", descriptor->failOn);
540-
if (descriptor->unlimited && ImageTypeSupportsUnlimited(imageType)) {
541-
option->set("unlimited", true);
542-
}
543-
if (imageType == ImageType::SVG || imageType == ImageType::PDF) {
544-
option->set("dpi", descriptor->density);
545-
}
546-
if (imageType == ImageType::MAGICK) {
547-
option->set("density", std::to_string(descriptor->density).data());
548-
}
549-
if (ImageTypeSupportsPage(imageType)) {
550-
option->set("n", descriptor->pages);
551-
option->set("page", descriptor->page);
552-
}
553-
if (imageType == ImageType::OPENSLIDE) {
554-
option->set("level", descriptor->level);
555-
}
556-
if (imageType == ImageType::TIFF) {
557-
option->set("subifd", descriptor->subifd);
558-
}
559-
if (imageType == ImageType::PDF) {
560-
option->set("background", descriptor->pdfBackground);
561-
}
562-
if (imageType == ImageType::JP2) {
563-
option->set("oneshot", descriptor->jp2Oneshot);
564-
}
545+
vips::VOption *option = GetOptionsForImageType(imageType, descriptor);
565546
image = VImage::new_from_file(descriptor->file.data(), option);
566547
if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
567548
image = SetDensity(image, descriptor->density);

src/common.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,9 @@ namespace sharp {
221221
ImageType DetermineImageType(char const *file);
222222

223223
/*
224-
Does this image type support multiple pages?
224+
Format-specific options builder
225225
*/
226-
bool ImageTypeSupportsPage(ImageType imageType);
227-
228-
/*
229-
Does this image type support removal of safety limits?
230-
*/
231-
bool ImageTypeSupportsUnlimited(ImageType imageType);
226+
vips::VOption* GetOptionsForImageType(ImageType imageType, InputDescriptor *descriptor);
232227

233228
/*
234229
Open an image from the given InputDescriptor (filesystem, compressed buffer, raw pixel data)

src/pipeline.cc

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,7 @@ class PipelineWorker : public Napi::AsyncWorker {
241241
// factor for jpegload*, a double scale factor for webpload*,
242242
// pdfload* and svgload*
243243
if (jpegShrinkOnLoad > 1) {
244-
vips::VOption *option = VImage::option()
245-
->set("access", access)
246-
->set("shrink", jpegShrinkOnLoad)
247-
->set("unlimited", baton->input->unlimited)
248-
->set("fail_on", baton->input->failOn);
244+
vips::VOption *option = GetOptionsForImageType(inputImageType, baton->input)->set("shrink", jpegShrinkOnLoad);
249245
if (baton->input->buffer != nullptr) {
250246
// Reload JPEG buffer
251247
VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength);
@@ -256,14 +252,8 @@ class PipelineWorker : public Napi::AsyncWorker {
256252
image = VImage::jpegload(const_cast<char*>(baton->input->file.data()), option);
257253
}
258254
} else if (scale != 1.0) {
259-
vips::VOption *option = VImage::option()
260-
->set("access", access)
261-
->set("scale", scale)
262-
->set("fail_on", baton->input->failOn);
255+
vips::VOption *option = GetOptionsForImageType(inputImageType, baton->input)->set("scale", scale);
263256
if (inputImageType == sharp::ImageType::WEBP) {
264-
option->set("n", baton->input->pages);
265-
option->set("page", baton->input->page);
266-
267257
if (baton->input->buffer != nullptr) {
268258
// Reload WebP buffer
269259
VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength);
@@ -274,11 +264,6 @@ class PipelineWorker : public Napi::AsyncWorker {
274264
image = VImage::webpload(const_cast<char*>(baton->input->file.data()), option);
275265
}
276266
} else if (inputImageType == sharp::ImageType::SVG) {
277-
option->set("unlimited", baton->input->unlimited);
278-
option->set("dpi", baton->input->density);
279-
option->set("stylesheet", baton->input->svgStylesheet.data());
280-
option->set("high_bitdepth", baton->input->svgHighBitdepth);
281-
282267
if (baton->input->buffer != nullptr) {
283268
// Reload SVG buffer
284269
VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength);
@@ -293,11 +278,6 @@ class PipelineWorker : public Napi::AsyncWorker {
293278
throw vips::VError("Input SVG image will exceed 32767x32767 pixel limit when scaled");
294279
}
295280
} else if (inputImageType == sharp::ImageType::PDF) {
296-
option->set("n", baton->input->pages);
297-
option->set("page", baton->input->page);
298-
option->set("dpi", baton->input->density);
299-
option->set("background", baton->input->pdfBackground);
300-
301281
if (baton->input->buffer != nullptr) {
302282
// Reload PDF buffer
303283
VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength);
@@ -307,7 +287,6 @@ class PipelineWorker : public Napi::AsyncWorker {
307287
// Reload PDF file
308288
image = VImage::pdfload(const_cast<char*>(baton->input->file.data()), option);
309289
}
310-
311290
sharp::SetDensity(image, baton->input->density);
312291
}
313292
} else {

0 commit comments

Comments
 (0)