Skip to content

Add background and flatten APIs - #94

Merged
lovell merged 1 commit into
lovell:alphafrom
gasi:background-flatten
Oct 1, 2014
Merged

lovell merged 1 commit into
lovell:alphafrom
gasi:background-flatten

Conversation

@gasi

@gasi gasi commented Oct 1, 2014

Copy link
Copy Markdown

Original discussion #91. Created new pull request against lovell:alpha as 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 as color string, e.g. '#00ff00', 'hsl(120,100%,50%)', etc., or as numbers in the range of [0, 255] for r, g, and b. Defaults color to black.

flatten()

Flatten transparent images onto background with a color set using background.

Comment thread src/sharp.cc

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback 👍 Agreed, but this is the pattern the current implementation uses for all operations so I decided to stick with it for consistency. @lovell mentioned reworking the whole g_object_unref setup. What do you think, @lovell?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@lovell

lovell commented Oct 1, 2014

Copy link
Copy Markdown
Owner

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 alpha branch before merging to master.

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!

lovell added a commit that referenced this pull request Oct 1, 2014
Add `background` and `flatten` APIs
@lovell
lovell merged commit 5cdfbba into lovell:alpha Oct 1, 2014
@gasi

gasi commented Oct 1, 2014

Copy link
Copy Markdown
Author

Thanks again for all your work on this, it's very much appreciated!

You’re welcome. It was a pleasure collaborating with you and we’re excited to continue using sharp for our system. Again, if you have an iPad and would like to give Mix a spin, send me an email to [email protected] and I’ll make sure you’re on the invite list. In the meantime, you can browse other people’s ideas without an account, e.g. this hommage to sharp: https://mix.fiftythree.com/gasi/55858/remixes

lovell added a commit that referenced this pull request Oct 2, 2014
Small memory leak mop-up related to #94
@gasi
gasi deleted the background-flatten branch April 22, 2015 22:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants