Skip to content

Rewrite to support different image libs #4

Open
@xemle

Description

I had problems with different sharp versions (which was solved recently in 1.1.0, thanks). While solving this on my side I came up by splitting the problem into

  • Read and resize image - this could be done by sharp, jimp or canvas
  • Calculate the dct2d - this might be useful for other things if the inverse function is supported
  • Calculate phash - the origin task
  • Encoding of the result

I did some rewrite and came up with https://github.com/xemle/sharp-phash/tree/rewrite-phash

This drops the sharp dependency and the resize of the image up to the customer/client. This is the tricky part of the rewrite for a lib called sharp-phash ¯_(ツ)_/¯

The dct2d was rewritten and aligned with the used variable names of the source https://www.mathworks.com/help/images/ref/dct2.html including the idct2d function based on https://www.mathworks.com/help/images/ref/idct2.html

Old Browsers do not support 64 bit numbers due lack of BigInt support. The hash result are 32 bit high and low values with converters of toBin, toHex and toDec (requires BigInt support).

Since I store and transfer the hash result via JSON documents the result is encoded in 16 byte hex string to save space. The comparison is done char-wise by a 16x16 lookup table. Further, since one major use case is to find similar images, the difference algorithm supports a max bit difference to skip further comparisons if the image differs too much.

RFC

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Rewrite to support different image libs · Issue #4 · btd/sharp-phash