Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: sharp.overlayWith (alpha compositing) #207

Merged
merged 1 commit into from
May 5, 2015
Merged

Conversation

gasi
Copy link

@gasi gasi commented Apr 30, 2015

Adds sharp.overlaysWith for compositing an image on top of another image while handling alpha channels correctly.

I left all commits intact for better understanding but feel free to merge as one squash commit, @lovell.

This is now ready for review.

Tests and usage examples: test/unit/overlay.js

TODO

  • Test with non-PNGs. Non-4-channel images are not supported.
  • Add support for specifying top and left position as in sharp.overlaysWith(filename, top, left). This might be useful for watermarking. Punted.
  • Benchmark.
  • Investigate (slight) discrepancy to ImageMagick convert -composite output. Not visible to human eye (at least not mine 😉) but detectable by compare ….

References

See: #97.

@@ -30,6 +30,8 @@ Please select the `master` branch as the destination for your Pull Request so yo

Please squash your changes into a single commit using a command like `git rebase -i upstream/master`.

To test C++ changes, you can compile the module using `npm run compile`.
Copy link
Author

Choose a reason for hiding this comment

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

This was useful to me and hope it will be to others 😄

Copy link
Owner

Choose a reason for hiding this comment

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

This will compile but not test the changes. I usually use npm i to do both.

Copy link
Author

Choose a reason for hiding this comment

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

Oh, you’re right, I didn’t know npm install runs node-gyp on the package itself. I always tried npm rebuild but that only recompiles dependencies. However, you said it ‘will […] test the changes’. This doesn’t happen for me (see below). Am I missing something?

npm i

> [email protected] install /Users/dani/workspace/sharp
> node-gyp rebuild

  CXX(target) Release/obj.target/sharp/src/common.o
  CC(target) Release/obj.target/sharp/src/composite.o
  CXX(target) Release/obj.target/sharp/src/utilities.o
  CXX(target) Release/obj.target/sharp/src/metadata.o
  CXX(target) Release/obj.target/sharp/src/resize.o
  CXX(target) Release/obj.target/sharp/src/sharp.o
  SOLINK_MODULE(target) Release/sharp.node
  SOLINK_MODULE(target) Release/sharp.node: Finished

Copy link
Owner

Choose a reason for hiding this comment

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

Sorry, my mistake. Judging by the command line history npm i && npm t will do both.

Copy link
Author

Choose a reason for hiding this comment

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

No worries, I documented npm install and npm test 👍

@lovell
Copy link
Owner

lovell commented Apr 30, 2015

Thanks again Daniel for all your work on this very useful and much-requested feature.

I'm not sure I want to go down the route of using *magick for image comparisons. My experience of managing CI environments warns me otherwise (I'm looking at you, AppVeyor) and, more importantly, I believe libvips can do this with minimum effort.

I'll spend some time this afternoon to see what I can do using libvips to get #122 off the ground.

@lovell
Copy link
Owner

lovell commented Apr 30, 2015

Commit c159948 on the knife branch adds a fixtures.assertSimilar method you can use instead of *magick's "compare".

By way of example, all of the tests for the extract method now employ it.

@gasi
Copy link
Author

gasi commented May 1, 2015

Commit c159948 on the knife branch adds a fixtures.assertSimilar method you can use instead of *magick's "compare".

Awesome! Thanks for providing this and I agree it’s more elegant without the GraphicsMagick dependency. However, I have two observations:

  1. I found a case where the similarity test should fail: c9301c1
  2. Isn’t it valuable for some operations like alpha compositing to do a comparison using no tolerance? If you agree, I could add that as parameter to assertSimilar.

@@ -4,6 +4,7 @@
"maxparams": 4,
"maxcomplexity": 13,
"globals": {
"before": true,
Copy link
Author

Choose a reason for hiding this comment

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

Although I don’t use this anymore, I figure it might be valuable for the future 😄

/*
Composite images `src` and `dst`.
*/
int Composite(VipsObject *context, VipsImage *src, VipsImage *dst, VipsImage **out);
Copy link
Author

Choose a reason for hiding this comment

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

Getting used to your 140 line length limit. I usually operate at 80 out of habit 😉

Copy link
Owner

Choose a reason for hiding this comment

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

People who use an 80 limit do so to leave room at the side of their monitor for Twitter.

Copy link
Author

Choose a reason for hiding this comment

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

Haha, good one 😜 On a more serious note: I do it because I like to do three-way merges on my laptop 👍

Copy link
Author

Choose a reason for hiding this comment

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

@gasi
Copy link
Author

gasi commented May 2, 2015

@lovell I made another pass on this today and it should be ready to merge given the restrictions we talked about (only RGBA images can be composited). I might be able to add more functionality at some point but for now, I think this might be already valuable for some people 😄

vips_object_local(context, joined);

// Return a reference to the output image:
g_object_ref(joined);
Copy link
Owner

Choose a reason for hiding this comment

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

I think this g_object_ref can be removed as vips_object_local will reference joined.

@lovell
Copy link
Owner

lovell commented May 2, 2015

Thanks again for all your work on this Daniel. Other than the possible reference leak, this is definitely ready to merge into the knife WIP branch. Are you able to squash to a single commit?

@gasi gasi changed the title API: sharp.overlaysWith (alpha compositing) API: sharp.overlayWith (alpha compositing) May 4, 2015
Composites an overlay image with alpha channel into the input image (which
must have alpha channel) using ‘over’ alpha compositing blend mode. This API
requires both images to have the same dimensions.

References:
- http://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
- libvips/ruby-vips#28 (comment)

See lovell#97.
@gasi
Copy link
Author

gasi commented May 4, 2015

Other than the possible reference leak, this is definitely ready to merge into the knife WIP branch. Are you able to squash to a single commit?

Thanks, I simplified the out reference as you suggested and squashed everything into a single commit 😄 It’s been a pleasure collaborating with you and I hope I’ll be able to contribute more in the future.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.45%) to 92.56% when pulling 6c682e8 on gasi:overlays into de67ab6 on lovell:knife.

@gasi
Copy link
Author

gasi commented May 4, 2015

I forgot to add overlayWith documentation to README. Will do that later today.

lovell added a commit that referenced this pull request May 5, 2015
Initial version of overlayWith method for alpha compositing
@lovell lovell merged commit 0f8bb4e into lovell:knife May 5, 2015
@lovell
Copy link
Owner

lovell commented May 5, 2015

No worries on the docs. I intend to add the ability to overlay onto non-alpha images too and will update the README after that. Thanks again @gasi for all your work on this new feature!

@gasi
Copy link
Author

gasi commented May 5, 2015

Thanks, @lovell, I appreciate it 👍

@gasi gasi deleted the overlays branch May 7, 2015 05:32
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.

4 participants