Skip to content

Commit

Permalink
simplify validate the matrix length, remove recombMatrixSize, change …
Browse files Browse the repository at this point in the history
…type to std::vector<double>
  • Loading branch information
ton11797 committed Jul 5, 2024
1 parent c66b118 commit 9d8a0f1
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 28 deletions.
1 change: 0 additions & 1 deletion lib/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ const Sharp = function (input, options) {
timeoutSeconds: 0,
linearA: [],
linearB: [],
recombMatrixSize: 3,

// Function to notify of libvips warnings
debuglog: warning => {
Expand Down
15 changes: 5 additions & 10 deletions lib/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
11 changes: 6 additions & 5 deletions src/operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<double[]> const &matrix, int recombMatrixSize) {
double *m = matrix.get();
VImage Recomb(VImage image, std::vector<double> const& matrix) {
const double* m = matrix.data();
void *matrix_data = const_cast<void*>(static_cast<const void*>(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,
Expand All @@ -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));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<double[]> const &matrix, int recombMatrixSize);
VImage Recomb(VImage image, std::vector<double> const &matrix);

/*
* Modulate brightness, saturation, hue and lightness
Expand Down
13 changes: 4 additions & 9 deletions src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<double[]>(new double[9]);
} else {
baton->recombMatrix = std::unique_ptr<double[]>(new double[16]);
}
Napi::Array recombMatrix = options.Get("recombMatrix").As<Napi::Array>();
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);
}
Expand Down
3 changes: 1 addition & 2 deletions src/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ struct PipelineBaton {
VipsForeignDzDepth tileDepth;
std::string tileId;
std::string tileBasename;
std::unique_ptr<double[]> recombMatrix;
int recombMatrixSize;
std::vector<double> recombMatrix;

PipelineBaton():
input(nullptr),
Expand Down

0 comments on commit 9d8a0f1

Please sign in to comment.