From a9aa55c32d6a546f2e4b8060bbd69f3b5db470be Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Wed, 14 Jul 2021 19:22:31 +0100 Subject: [PATCH] Ensure pipelineColourspace is applied to all inputs #2704 --- src/operations.cc | 12 ++++++++++++ src/operations.h | 5 +++++ src/pipeline.cc | 13 +++++-------- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/operations.cc b/src/operations.cc index 79fa5c79..17622efc 100644 --- a/src/operations.cc +++ b/src/operations.cc @@ -282,4 +282,16 @@ namespace sharp { return image.linear(a, b); } } + + /* + * Ensure the image is in a given colourspace + */ + VImage EnsureColourspace(VImage image, VipsInterpretation colourspace) { + if (colourspace != VIPS_INTERPRETATION_LAST && image.interpretation() != colourspace) { + image = image.colourspace(colourspace, + VImage::option()->set("source_space", image.interpretation())); + } + return image; + } + } // namespace sharp diff --git a/src/operations.h b/src/operations.h index 7e538a50..baff4c93 100644 --- a/src/operations.h +++ b/src/operations.h @@ -97,6 +97,11 @@ namespace sharp { */ VImage Modulate(VImage image, double const brightness, double const saturation, int const hue); + /* + * Ensure the image is in a given colourspace + */ + VImage EnsureColourspace(VImage image, VipsInterpretation colourspace); + } // namespace sharp #endif // SRC_OPERATIONS_H_ diff --git a/src/pipeline.cc b/src/pipeline.cc index 8531cc85..7654c1e2 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -67,13 +67,7 @@ class PipelineWorker : public Napi::AsyncWorker { vips::VImage image; sharp::ImageType inputImageType; std::tie(image, inputImageType) = sharp::OpenInput(baton->input); - - if (baton->colourspaceInput != VIPS_INTERPRETATION_LAST) { - if (image.interpretation() != baton->colourspaceInput) { - image = image.colourspace(baton->colourspaceInput, - VImage::option()->set("source_space", image.interpretation())); - } - } + image = sharp::EnsureColourspace(image, baton->colourspaceInput); // Calculate angle of rotation VipsAngle rotation; @@ -419,6 +413,7 @@ class PipelineWorker : public Napi::AsyncWorker { for (unsigned int i = 0; i < baton->joinChannelIn.size(); i++) { std::tie(joinImage, joinImageType) = sharp::OpenInput(baton->joinChannelIn[i]); + joinImage = sharp::EnsureColourspace(joinImage, baton->colourspaceInput); image = image.bandjoin(joinImage); } image = image.copy(VImage::option()->set("interpretation", baton->colourspace)); @@ -560,7 +555,8 @@ class PipelineWorker : public Napi::AsyncWorker { for (Composite *composite : baton->composite) { VImage compositeImage; sharp::ImageType compositeImageType = sharp::ImageType::UNKNOWN; - std::tie(compositeImage, compositeImageType) = OpenInput(composite->input); + std::tie(compositeImage, compositeImageType) = sharp::OpenInput(composite->input); + compositeImage = sharp::EnsureColourspace(compositeImage, baton->colourspaceInput); // Verify within current dimensions if (compositeImage.width() > image.width() || compositeImage.height() > image.height()) { throw vips::VError("Image to composite must have same dimensions or smaller"); @@ -669,6 +665,7 @@ class PipelineWorker : public Napi::AsyncWorker { VImage booleanImage; sharp::ImageType booleanImageType = sharp::ImageType::UNKNOWN; std::tie(booleanImage, booleanImageType) = sharp::OpenInput(baton->boolean); + booleanImage = sharp::EnsureColourspace(booleanImage, baton->colourspaceInput); image = sharp::Boolean(image, booleanImage, baton->booleanOp); }