Skip to content

Add flatten API for transparent images - #91

Closed
gasi wants to merge 13 commits into
lovell:masterfrom
gasi:flatten
Closed

gasi wants to merge 13 commits into
lovell:masterfrom
gasi:flatten

Conversation

@gasi

@gasi gasi commented Sep 23, 2014

Copy link
Copy Markdown

Background

We at @fiftythree added the flatten API to flatten transparent PNGs (from Paper app) onto a custom background color for our new product, Mix:

Please consider this pull request as a starting point to discuss functionality and API. Happy to collaborate with you to make it as good as possible.

API

flatten(background) or flatten(r, g, b)

Flatten transparent images onto background color before processing. Defaults background color to black. Pass any valid CSS color as background, e.g. #00ff00, hsl(120,100%,50%), etc., or numbers in the range of [0, 255] for r, g, and b.

Acknowledgements

@lovell, thank you for creating sharp. It has significantly sped up our Mix image conversion pipeline and hence improved the experience for our users. If you have an iPad and would like to try Mix, let me know and I’ll add you to the invite list.

Hat tip to @julianwa for adding the 4-bands bugfix and @apitaru for finding black fringing bug and helping fixing it 😄

I’m planning to responding to your survey with more details soon: https://twitter.com/lovell/status/512151120496820224

@lovell

lovell commented Sep 23, 2014

Copy link
Copy Markdown
Owner

Thank you all for this most excellent addition. With tests and documentation it's my favourite kind of pull request.

Sorry you had to work on the C++ source - it's got far too large recently and needs refactoring into smaller methods and files. I've also got a plan to vastly simplify all that g_object_unref-ing too.

My initial thoughts, based solely on looking at the diffs:

  1. I prefer your initial separation of background and flatten methods. The background method can be re-used for Feature request: Embed Transparent #86 and Add embedColor() method #89 (and this change makes those feature requests easier to implement, thank you).
  2. As well as 4 band RGBA it's possible to have 2 band PNG images (grey+alpha) that need to be "flattenable". It's also possible to have 4 band JPEGs in CMYK colour space that should not. You can access libvips' colour space interpretation via image->Type, with relevant values to check being VIPS_INTERPRETATION_sRGB, VIPS_INTERPRETATION_B_W and VIPS_INTERPRETATION_CMYK.
  3. The presence of 2 band PNGs means the background method should probably also accept a single 0-255 Number to represent a shade of (8-bit) grey.

If possible, please can you rebase and squash your commits once you're happy with any changes.

Thanks again for adding this very useful feature!

@gasi

gasi commented Sep 25, 2014

Copy link
Copy Markdown
Author

Thanks for your feedback, @lovell. Re: C++. No worries, this a great opportunity for me to learn more about it 😄

  1. Sounds good. With your pointers to Feature request: Embed Transparent #86 and Add embedColor() method #89 it makes sense to have both. I noticed already it could work for the embed* cases.
  2. I’ll look into making it more robust by taking image->Type into account but given other stuff I have to work on, I most likely won’t be able to implement and more importantly test the 2-band PNG case as I don’t even have a test fixture for that.
  3. That’d be easy to add but again, I won’t have the bandwidth to test this (see 2.).

I will make the additions per your feedback—thanks!—and then squash the commit. Thanks again for your excellent work 👍

lovell added a commit that referenced this pull request Sep 25, 2014
Provides internal sharp_image_has_alpha method

Hopefully this helps with #91
@lovell

lovell commented Sep 25, 2014

Copy link
Copy Markdown
Owner

I've added an internally available sharp_image_has_alpha method via 9d6d270 on the alpha branch. You should be able to AND this to your if (baton->flatten && ... test.

I took the liberty of adding your transparent PNG test fixture to test this, thank you (hence the rebase conflict you're about to experience).

This should hopefully help you with (2) more than it causes rebase headaches.

Please can you update this PR to land in the alpha branch and I'll add further test fixtures before merging to master.

Thanks again for this welcome contribution!

lovell added a commit that referenced this pull request Sep 30, 2014
Provides internal sharp_image_has_alpha method

Hopefully this helps with #91
@gasi

gasi commented Oct 1, 2014

Copy link
Copy Markdown
Author

@lovell: Thanks for the valuable sharp_image_has_alpha addition and the engaging discussion. I reopened a new PR with the requested changes as #94 😄

@lovell

lovell commented Oct 1, 2014

Copy link
Copy Markdown
Owner

Replaced with #94, thank you.

@lovell lovell closed this Oct 1, 2014
@gasi
gasi deleted the flatten branch April 22, 2015 22:59
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.

2 participants