Add tint operation to set image chroma

This commit is contained in:
Rik Heywood
2018-04-11 20:05:48 +01:00
committed by Lovell Fuller
parent bdac5b5807
commit dbac4b9a63
15 changed files with 172 additions and 23 deletions

View File

@@ -152,6 +152,32 @@ namespace sharp {
return dst.bandjoin(mask.cast(dst.format()));
}
/*
* Tint an image using the specified chroma, preserving the original image luminance
*/
VImage Tint(VImage image, double const a, double const b) {
// Get original colourspace
VipsInterpretation typeBeforeTint = image.interpretation();
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);
// Attach original alpha channel, if any
if (HasAlpha(image)) {
// Extract original alpha channel
VImage alpha = image[image.bands() - 1];
// Join alpha channel to normalised image
tinted = tinted.bandjoin(alpha);
}
return tinted;
}
/*
* Stretch luminance to cover full dynamic range.
*/

View File

@@ -46,6 +46,11 @@ namespace sharp {
*/
VImage Cutout(VImage src, VImage dst, const int gravity);
/*
* Tint an image using the specified chroma, preserving the original image luminance
*/
VImage Tint(VImage image, double const a, double const b);
/*
* Stretch luminance to cover full dynamic range.
*/

View File

@@ -682,6 +682,11 @@ class PipelineWorker : public Nan::AsyncWorker {
image = sharp::Bandbool(image, baton->bandBoolOp);
}
// Tint the image
if (baton->tintA > 0 || baton->tintB > 0) {
image = sharp::Tint(image, baton->tintA, baton->tintB);
}
// Extract an image channel (aka vips band)
if (baton->extractChannel > -1) {
if (baton->extractChannel >= image.bands()) {
@@ -1167,6 +1172,9 @@ NAN_METHOD(pipeline) {
for (unsigned int i = 0; i < 4; i++) {
baton->background[i] = AttrTo<double>(background, i);
}
// Tint chroma
baton->tintA = AttrTo<double>(options, "tintA");
baton->tintB = AttrTo<double>(options, "tintB");
// Overlay options
if (HasAttr(options, "overlay")) {
baton->overlay = CreateInputDescriptor(AttrAs<v8::Object>(options, "overlay"), buffersToPersist);

View File

@@ -70,6 +70,8 @@ struct PipelineBaton {
std::string kernel;
bool fastShrinkOnLoad;
double background[4];
double tintA;
double tintB;
bool flatten;
bool negate;
double blurSigma;
@@ -155,6 +157,8 @@ struct PipelineBaton {
cropOffsetLeft(0),
cropOffsetTop(0),
premultiplied(false),
tintA(0.0),
tintB(0.0),
flatten(false),
negate(false),
blurSigma(0.0),