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

new_from_memory / write_to_memory #12

Closed
kleisauke opened this issue Sep 7, 2017 · 4 comments
Closed

new_from_memory / write_to_memory #12

kleisauke opened this issue Sep 7, 2017 · 4 comments

Comments

@kleisauke
Copy link
Member

It would be nice to wrap an image around a memory array or to write an image to a large memory array. This can be useful for moving images between libvips and imagemagick or to calculate the perceptual hash of an image (see below).

The new functions can be named like this:
Extension: new_from_memory, write_to_memory.
php-vips: Image::newFromMemory, ->writeToMemory.

I can open a PR at the php layer to support these new functions if this has been implemented in the extension.

If you are wondering why I need this:
For our test cases we are calculating the perceptual hash of an image (based on the dHash algorithm) to verify the similarity of expected vs actual images (using a distance threshold). It looks like this (so ->copyMemory and ->getpoint are the important ones). If there's a ->writeToMemory function it could be somewhat optimized.

@jcupitt
Copy link
Member

jcupitt commented Sep 7, 2017

Sure, this is a great idea! Are you OK doing this? I'm working on a JS binding now and I ought to get that done before I switch languages again.

dHash is rather old, fwiw, I know it from the 1980s and it's probably older than that. I would do it slightly differently: shrink to 8x8 greyscale, find image average, then set bits depending on whether the pixel is brighter or darker than the image average.

This has the same independence from brightness property you get with the difference image, but saves you computing left-right differences. You can also look at the images and guess what they represent, which you can't with a left-right diff.

@kleisauke
Copy link
Member Author

I'm OK with implementing this. You can expect a pull request on the weekend.

Wow, I didn't know that dHash already existed since 1980s. Your hash calculation sounds like an average hash. I tried to implement this and it seems not to work for sequential access. (I think this is fixable when there is a ->writeToMemory function, or when doing a ->linecache).

By the way,
You're doing a great job with those (new) FFI bindings for libvips! I'm happy to test the Node.js binding (on several environments) when you're ready. Perhaps sharp can also benefit from node-vips in the near future, lovell/sharp#241 is relevant here.

@jcupitt
Copy link
Member

jcupitt commented Sep 7, 2017

Oh heh yes, I described aHash. I suppose dHash is a little better for non-linear lightness changes.

@jcupitt
Copy link
Member

jcupitt commented Nov 23, 2017

I think this is all done now, I'll close and update pecl.

Good job!

@jcupitt jcupitt closed this as completed Nov 23, 2017
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

No branches or pull requests

2 participants