Skip to content

Commit

Permalink
Fix tint op by ensuring LAB and allowing negative values #1235
Browse files Browse the repository at this point in the history
Add test cases for more tint colours and input interpretations
  • Loading branch information
lovell committed May 23, 2018
1 parent b1a9bf1 commit 54a71fc
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 9 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

Requires libvips v8.6.1.

#### v0.20.3 - TBD

* Fix tint operation by ensuring LAB interpretation and allowing negative values.
[#1235](https://github.com/lovell/sharp/issues/1235)
[@wezside](https://github.com/wezside)

#### v0.20.2 - 28<sup>th</sup> April 2018

* Add tint operation to set image chroma.
Expand Down
4 changes: 2 additions & 2 deletions lib/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ const Sharp = function (input, options) {
fastShrinkOnLoad: true,
// operations
background: [0, 0, 0, 255],
tintA: 0,
tintB: 0,
tintA: 128,
tintB: 128,
flatten: false,
negate: false,
medianSize: 0,
Expand Down
9 changes: 5 additions & 4 deletions src/operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@ namespace sharp {
if (typeBeforeTint == VIPS_INTERPRETATION_RGB) {
typeBeforeTint = VIPS_INTERPRETATION_sRGB;
}
// Create 2 band image with every pixel set to the tint chroma
std::vector<double> chromaPixel {a, b};
VImage chroma = image.new_from_image(chromaPixel);
// Extract luminance
VImage luminance = image.colourspace(VIPS_INTERPRETATION_LAB)[0];
// Create the tinted version by combining the L from the original and the chroma from the tint
VImage tinted = luminance.bandjoin(chroma).colourspace(typeBeforeTint);
std::vector<double> chroma {a, b};
VImage tinted = luminance
.bandjoin(chroma)
.copy(VImage::option()->set("interpretation", VIPS_INTERPRETATION_LAB))
.colourspace(typeBeforeTint);
// Attach original alpha channel, if any
if (HasAlpha(image)) {
// Extract original alpha channel
Expand Down
2 changes: 1 addition & 1 deletion src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ class PipelineWorker : public Nan::AsyncWorker {
}

// Tint the image
if (baton->tintA > 0 || baton->tintB > 0) {
if (baton->tintA < 128.0 || baton->tintB < 128.0) {
image = sharp::Tint(image, baton->tintA, baton->tintB);
}

Expand Down
4 changes: 2 additions & 2 deletions src/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ struct PipelineBaton {
cropOffsetLeft(0),
cropOffsetTop(0),
premultiplied(false),
tintA(0.0),
tintB(0.0),
tintA(128.0),
tintB(128.0),
flatten(false),
negate(false),
blurSigma(0.0),
Expand Down
Binary file added test/fixtures/expected/tint-blue.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/expected/tint-cmyk.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/expected/tint-green.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions test/unit/tint.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ describe('Tint', function () {
});
});

it('tints rgb image green', function (done) {
const output = fixtures.path('output.tint-green.jpg');
sharp(fixtures.inputJpg)
.resize(320, 240)
.tint('#00FF00')
.toFile(output, function (err, info) {
if (err) throw err;
assert.strictEqual(true, info.size > 0);
fixtures.assertMaxColourDistance(output, fixtures.expected('tint-green.jpg'), 10);
done();
});
});

it('tints rgb image blue', function (done) {
const output = fixtures.path('output.tint-blue.jpg');
sharp(fixtures.inputJpg)
.resize(320, 240)
.tint('#0000FF')
.toFile(output, function (err, info) {
if (err) throw err;
assert.strictEqual(true, info.size > 0);
fixtures.assertMaxColourDistance(output, fixtures.expected('tint-blue.jpg'), 10);
done();
});
});

it('tints rgb image with sepia tone', function (done) {
const output = fixtures.path('output.tint-sepia.jpg');
sharp(fixtures.inputJpg)
Expand Down Expand Up @@ -60,4 +86,17 @@ describe('Tint', function () {
done();
});
});

it('tints cmyk image red', function (done) {
const output = fixtures.path('output.tint-cmyk.jpg');
sharp(fixtures.inputJpgWithCmykProfile)
.resize(320, 240)
.tint('#FF0000')
.toFile(output, function (err, info) {
if (err) throw err;
assert.strictEqual(true, info.size > 0);
fixtures.assertMaxColourDistance(output, fixtures.expected('tint-cmyk.jpg'), 10);
done();
});
});
});

0 comments on commit 54a71fc

Please sign in to comment.