From 9d8a0f10eebb87d0269fc2a18d88b072b5f4c42d Mon Sep 17 00:00:00 2001 From: Denice Date: Fri, 5 Jul 2024 15:13:39 +0700 Subject: [PATCH] simplify validate the matrix length, remove recombMatrixSize, change type to std::vector --- lib/constructor.js | 1 - lib/operation.js | 15 +++++---------- src/operations.cc | 11 ++++++----- src/operations.h | 2 +- src/pipeline.cc | 13 ++++--------- src/pipeline.h | 3 +-- 6 files changed, 17 insertions(+), 28 deletions(-) diff --git a/lib/constructor.js b/lib/constructor.js index a5769abe1..66d814503 100644 --- a/lib/constructor.js +++ b/lib/constructor.js @@ -346,7 +346,6 @@ const Sharp = function (input, options) { timeoutSeconds: 0, linearA: [], linearB: [], - recombMatrixSize: 3, // Function to notify of libvips warnings debuglog: warning => { diff --git a/lib/operation.js b/lib/operation.js index 91eec3c35..6f0fcd098 100644 --- a/lib/operation.js +++ b/lib/operation.js @@ -795,20 +795,15 @@ function recomb (inputMatrix) { if (!Array.isArray(inputMatrix)) { throw new Error('Invalid recombination matrix'); } - - const length = inputMatrix.length; - if (length !== 3 && length !== 4) { + if (inputMatrix.length !== 3 && inputMatrix.length !== 4) { throw new Error('Invalid recombination matrix'); } - - for (const row of inputMatrix) { - if (row.length !== length) { - throw new Error('Invalid recombination matrix'); - } + const recombMatrix = inputMatrix.flat().map(Number); + if (recombMatrix.length !== 9 && recombMatrix.length !== 16) { + throw new Error('Invalid recombination matrix'); } - this.options.recombMatrix = inputMatrix.flat().map(Number); - this.options.recombMatrixSize = length; + this.options.recombMatrix = recombMatrix; return this; } diff --git a/src/operations.cc b/src/operations.cc index 44fbb434f..f41fe1093 100644 --- a/src/operations.cc +++ b/src/operations.cc @@ -183,14 +183,15 @@ namespace sharp { * Recomb with a Matrix of the given bands/channel size. * Eg. RGB will be a 3x3 matrix. */ - VImage Recomb(VImage image, std::unique_ptr const &matrix, int recombMatrixSize) { - double *m = matrix.get(); + VImage Recomb(VImage image, std::vector const& matrix) { + const double* m = matrix.data(); + void *matrix_data = const_cast(static_cast(m)); image = image.colourspace(VIPS_INTERPRETATION_sRGB); - if (recombMatrixSize == 3) { + if (matrix.size() == 9) { return image .recomb(image.bands() == 3 ? VImage::new_from_memory( - m, 9 * sizeof(double), 3, 3, 1, VIPS_FORMAT_DOUBLE + matrix_data, 9 * sizeof(double), 3, 3, 1, VIPS_FORMAT_DOUBLE ) : VImage::new_matrixv(4, 4, m[0], m[1], m[2], 0.0, @@ -199,7 +200,7 @@ namespace sharp { 0.0, 0.0, 0.0, 1.0)); } else { return image - .recomb(VImage::new_from_memory(m, 16 * sizeof(double), 4, 4, 1, VIPS_FORMAT_DOUBLE)); + .recomb(VImage::new_from_memory(matrix_data, 16 * sizeof(double), 4, 4, 1, VIPS_FORMAT_DOUBLE)); } } diff --git a/src/operations.h b/src/operations.h index 55e99812b..8c8791c6b 100644 --- a/src/operations.h +++ b/src/operations.h @@ -95,7 +95,7 @@ namespace sharp { * Recomb with a Matrix of the given bands/channel size. * Eg. RGB will be a 3x3 matrix. */ - VImage Recomb(VImage image, std::unique_ptr const &matrix, int recombMatrixSize); + VImage Recomb(VImage image, std::vector const &matrix); /* * Modulate brightness, saturation, hue and lightness diff --git a/src/pipeline.cc b/src/pipeline.cc index de149bd99..1455c5751 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -609,8 +609,8 @@ class PipelineWorker : public Napi::AsyncWorker { } // Recomb - if (baton->recombMatrix != NULL) { - image = sharp::Recomb(image, baton->recombMatrix, baton->recombMatrixSize); + if (!baton->recombMatrix.empty()) { + image = sharp::Recomb(image, baton->recombMatrix); } // Modulate @@ -1613,14 +1613,9 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) { } } if (options.Has("recombMatrix")) { - baton->recombMatrixSize = sharp::AttrAsInt32(options, "recombMatrixSize"); - if (baton->recombMatrixSize == 3) { - baton->recombMatrix = std::unique_ptr(new double[9]); - } else { - baton->recombMatrix = std::unique_ptr(new double[16]); - } Napi::Array recombMatrix = options.Get("recombMatrix").As(); - unsigned int matrixElements = baton->recombMatrixSize * baton->recombMatrixSize; + unsigned int matrixElements = recombMatrix.Length(); + baton->recombMatrix.resize(matrixElements); for (unsigned int i = 0; i < matrixElements; i++) { baton->recombMatrix[i] = sharp::AttrAsDouble(recombMatrix, i); } diff --git a/src/pipeline.h b/src/pipeline.h index 13a1ca2e9..163f84f1c 100644 --- a/src/pipeline.h +++ b/src/pipeline.h @@ -223,8 +223,7 @@ struct PipelineBaton { VipsForeignDzDepth tileDepth; std::string tileId; std::string tileBasename; - std::unique_ptr recombMatrix; - int recombMatrixSize; + std::vector recombMatrix; PipelineBaton(): input(nullptr),