Repository navigation
Add background and flatten APIs - #94
Conversation
There was a problem hiding this comment.
@gasi I think I mentioned this in one of your earlier PRs? This is an extra copy of the image for callers who aren't flattening their images, which seems wasteful.
I think you could rewrite lines 505-525 like so:
// Flatten
// We skip non-four-band images as we haven’t tested two-channel PNGs with
// alpha channel (yet).
// See: https://github.com/lovell/sharp/pull/91#issuecomment-56496548
if (baton->flatten && sharp_image_has_alpha(shrunk_on_load) && shrunk_on_load->Bands == 4) {
VipsImage *flattened = vips_image_new();
VipsArrayDouble *background = vips_array_double_newv(
3, // vector size
baton->background_red,
baton->background_green,
baton->background_blue
);
if (vips_flatten(shrunk_on_load, &flattened, "background", background, NULL)) {
return resize_error(baton, shrunk_on_load);
};
// Free the original image
g_object_unref(shrunk_on_load);
// shrunk_on_load now refers to the flattened image
shrunk_on_load = flattened;
}and then revert to using shrunk_on_load below as before.
There was a problem hiding this comment.
This is probably my fault - there is currently an inconsistent approach with a mix of both pointer reallocation (after an unref) and use of vips_copy. The medium-to-long term plan is move to the pure C++ API when it's ready. The short-to-medium term plan is to use vips_object_local_array to "manage" references.
|
Thank you Daniel for the replacement PR - I wasn't aware the destination branch can't be changed. I'll clean things up on the This new feature should make its way into v0.7.0 along with the much-requested crop feature. Thanks again for all your work on this, it's very much appreciated! |
Add `background` and `flatten` APIs
You’re welcome. It was a pleasure collaborating with you and we’re excited to continue using |
Small memory leak mop-up related to #94
Original discussion #91. Created new pull request against
lovell:alphaas GitHub doesn’t let one change the target branch.Documentation
background(color) or background(r, g, b)
Set background color for operations such as
flatten. Pass any valid CSS color ascolorstring, e.g.'#00ff00','hsl(120,100%,50%)', etc., or as numbers in the range of[0, 255]forr,g, andb. Defaults color to black.flatten()
Flatten transparent images onto background with a color set using
background.