Allow non-RGB input to embed/extend onto bg with alpha #646

This commit is contained in:
Lovell Fuller
2016-12-11 15:58:20 +00:00
parent 61721bb086
commit d2455267a8
11 changed files with 74 additions and 25 deletions

View File

@@ -434,4 +434,20 @@ namespace sharp {
);
}
/*
Convert RGBA value to another colourspace
*/
std::vector<double> GetRgbaAsColourspace(std::vector<double> const rgba, VipsInterpretation const interpretation) {
int const bands = static_cast<int>(rgba.size());
if (bands < 3 || interpretation == VIPS_INTERPRETATION_sRGB || interpretation == VIPS_INTERPRETATION_RGB) {
return rgba;
} else {
VImage pixel = VImage::new_matrix(1, 1);
pixel.set("bands", bands);
pixel = pixel.new_from_image(rgba);
pixel = pixel.colourspace(interpretation, VImage::option()->set("source_space", VIPS_INTERPRETATION_sRGB));
return pixel(0, 0);
}
}
} // namespace sharp

View File

@@ -199,6 +199,11 @@ namespace sharp {
*/
VipsInterpretation GetInterpretation(std::string const typeStr);
/*
Convert RGBA value to another colourspace
*/
std::vector<double> GetRgbaAsColourspace(std::vector<double> const rgba, VipsInterpretation const interpretation);
} // namespace sharp
#endif // SRC_COMMON_H_

View File

@@ -470,6 +470,8 @@ class PipelineWorker : public Nan::AsyncWorker {
if (baton->background[3] < 255.0 || HasAlpha(image)) {
background.push_back(baton->background[3] * multiplier);
}
// Ensure background colour uses correct colourspace
background = sharp::GetRgbaAsColourspace(background, image.interpretation());
// Add non-transparent alpha channel, if required
if (baton->background[3] < 255.0 && !HasAlpha(image)) {
image = image.bandjoin(
@@ -538,6 +540,8 @@ class PipelineWorker : public Nan::AsyncWorker {
if (baton->background[3] < 255.0 || HasAlpha(image)) {
background.push_back(baton->background[3] * multiplier);
}
// Ensure background colour uses correct colourspace
background = sharp::GetRgbaAsColourspace(background, image.interpretation());
// Add non-transparent alpha channel, if required
if (baton->background[3] < 255.0 && !HasAlpha(image)) {
image = image.bandjoin(
@@ -689,14 +693,13 @@ class PipelineWorker : public Nan::AsyncWorker {
}
image = image.extract_band(baton->extractChannel);
}
// Convert image to sRGB, if not already
if (sharp::Is16Bit(image.interpretation())) {
image = image.cast(VIPS_FORMAT_USHORT);
}
if (image.interpretation() != baton->colourspace) {
// Need to convert image
image = image.colourspace(baton->colourspace);
// Convert colourspace, pass the current known interpretation so libvips doesn't have to guess
image = image.colourspace(baton->colourspace, VImage::option()->set("source_space", image.interpretation()));
// Transform colours from embedded profile to output profile
if (baton->withMetadata &&
sharp::HasProfile(image) &&