mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 18:40:16 +02:00
Move background extraction into separate method (#1383)
This commit is contained in:
parent
db2af42ee7
commit
37d385fafa
@ -602,4 +602,40 @@ namespace sharp {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Apply the alpha channel to a given colour
|
||||
*/
|
||||
std::tuple<VImage, std::vector<double>> ApplyAlpha(VImage image, double colour[4]) {
|
||||
// Scale up 8-bit values to match 16-bit input image
|
||||
double const multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0;
|
||||
// Create alphaColour colour
|
||||
std::vector<double> alphaColour;
|
||||
if (image.bands() > 2) {
|
||||
alphaColour = {
|
||||
multiplier * colour[0],
|
||||
multiplier * colour[1],
|
||||
multiplier * colour[2]
|
||||
};
|
||||
} else {
|
||||
// Convert sRGB to greyscale
|
||||
alphaColour = { multiplier * (
|
||||
0.2126 * colour[0] +
|
||||
0.7152 * colour[1] +
|
||||
0.0722 * colour[2])
|
||||
};
|
||||
}
|
||||
// Add alpha channel to alphaColour colour
|
||||
if (colour[3] < 255.0 || HasAlpha(image)) {
|
||||
alphaColour.push_back(colour[3] * multiplier);
|
||||
}
|
||||
// Ensure alphaColour colour uses correct colourspace
|
||||
alphaColour = sharp::GetRgbaAsColourspace(alphaColour, image.interpretation());
|
||||
// Add non-transparent alpha channel, if required
|
||||
if (colour[3] < 255.0 && !HasAlpha(image)) {
|
||||
image = image.bandjoin(
|
||||
VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier));
|
||||
}
|
||||
return std::make_tuple(image, alphaColour);
|
||||
}
|
||||
|
||||
} // namespace sharp
|
||||
|
@ -255,6 +255,11 @@ namespace sharp {
|
||||
*/
|
||||
std::vector<double> GetRgbaAsColourspace(std::vector<double> const rgba, VipsInterpretation const interpretation);
|
||||
|
||||
/*
|
||||
Apply the alpha channel to a given colour
|
||||
*/
|
||||
std::tuple<VImage, std::vector<double>> ApplyAlpha(VImage image, double colour[4]);
|
||||
|
||||
} // namespace sharp
|
||||
|
||||
#endif // SRC_COMMON_H_
|
||||
|
@ -421,35 +421,8 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
// Crop/embed
|
||||
if (image.width() != baton->width || image.height() != baton->height) {
|
||||
if (baton->canvas == Canvas::EMBED) {
|
||||
// Scale up 8-bit values to match 16-bit input image
|
||||
double const multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0;
|
||||
// Create background colour
|
||||
std::vector<double> background;
|
||||
if (image.bands() > 2) {
|
||||
background = {
|
||||
multiplier * baton->background[0],
|
||||
multiplier * baton->background[1],
|
||||
multiplier * baton->background[2]
|
||||
};
|
||||
} else {
|
||||
// Convert sRGB to greyscale
|
||||
background = { multiplier * (
|
||||
0.2126 * baton->background[0] +
|
||||
0.7152 * baton->background[1] +
|
||||
0.0722 * baton->background[2])
|
||||
};
|
||||
}
|
||||
// Add alpha channel to background colour
|
||||
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(
|
||||
VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier));
|
||||
}
|
||||
std::tie(image, background) = sharp::ApplyAlpha(image, baton->background);
|
||||
|
||||
// Embed
|
||||
|
||||
@ -511,35 +484,9 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
|
||||
// Extend edges
|
||||
if (baton->extendTop > 0 || baton->extendBottom > 0 || baton->extendLeft > 0 || baton->extendRight > 0) {
|
||||
// Scale up 8-bit values to match 16-bit input image
|
||||
double const multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0;
|
||||
// Create background colour
|
||||
std::vector<double> background;
|
||||
if (image.bands() > 2) {
|
||||
background = {
|
||||
multiplier * baton->background[0],
|
||||
multiplier * baton->background[1],
|
||||
multiplier * baton->background[2]
|
||||
};
|
||||
} else {
|
||||
// Convert sRGB to greyscale
|
||||
background = { multiplier * (
|
||||
0.2126 * baton->background[0] +
|
||||
0.7152 * baton->background[1] +
|
||||
0.0722 * baton->background[2])
|
||||
};
|
||||
}
|
||||
// Add alpha channel to background colour
|
||||
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(
|
||||
VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier));
|
||||
}
|
||||
std::tie(image, background) = sharp::ApplyAlpha(image, baton->background);
|
||||
|
||||
// Embed
|
||||
baton->width = image.width() + baton->extendLeft + baton->extendRight;
|
||||
baton->height = image.height() + baton->extendTop + baton->extendBottom;
|
||||
|
Loading…
x
Reference in New Issue
Block a user