Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
schulzch committed Feb 8, 2015
1 parent 9d8cc8a commit ea8b437
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 50 deletions.
1 change: 1 addition & 0 deletions .npmignore
@@ -0,0 +1 @@
test/
Empty file removed LICENSE
Empty file.
62 changes: 34 additions & 28 deletions README.md
@@ -1,53 +1,59 @@
# node-phash
# pHash [![NPM version](https://badge.fury.io/js/phash.png)](http://badge.fury.io/js/phash) [![Build Status](https://travis-ci.org/aaronm67/node-phash.png?branch=master)](https://travis-ci.org/aaronm67/node-phash)

node-pash is [pHash](http://www.phash.org/) bindings for node.js.
[pHash](http://www.phash.org/) bindings for [Node.js](http://nodejs.org/).

Phash is a library that will create a "perceptual hash" of media files, so similar files will return similar hashes. Typically to compare hashes,
a simple [Hamming distance](http://en.wikipedia.org/wiki/Hamming_distance) between the two hashes is a good indicator of how similar two
media files are.
A pHash is a "perceptual hash" of a multimedia file derived from various features from its content. This can be useful to compare similar files, create database indices and so on.

Current version supports only image hashing. (no video & audio hashing)

[![Build Status](https://travis-ci.org/aaronm67/node-phash.png?branch=master)](https://travis-ci.org/aaronm67/node-phash)
Note: Currently supports only images - no video or audio.

## Installation

Install using npm:

$ npm install phash

## Functions

```js

// asynchronous hash
imageHash(filename, function(err, hash));

// synchronous hash
var hash = imageHashSync(filename);

hammingDistance(hash1, hash2);
```

## Usage

```js
```javascript
var pHash = require("phash");

pHash.imageHash("file.jpg", function(err, hash) {
if (err) {
throw err;
console.error(err);
}

// hash is the pHash of file.jpg
console.log("pHash: " + hash);
});

var hashA = pHash.imageHashSync("a.jpg");
var hashB = pHash.imageHashSync("b.png");
var hammingAB = pHash.hammingDistance(hashA,hashB);
var hammingAB = pHash.hammingDistance(hashA, hashB);
console.log("Hamming Distance A -> B: " + hammingAB);
```

## API

### pHash#imageHash

Computes a pHash asynchronously.

```javascript
imageHash(filename, function(err, hash));
```

### pHash#imageHashSync

Computes a pHash.

```javascript
var hash = imageHashSync(filename);
```

### pHash#hammingDistance

Computes the [Hamming distance](http://en.wikipedia.org/wiki/Hamming_distance) between the two pHashes.

```
hammingDistance(hashA, hashB);
```

## License

Licensed under the incredibly [permissive](http://en.wikipedia.org/wiki/Permissive_free_software_licence) [MIT License](http://creativecommons.org/licenses/MIT/). Copyright &copy; 2013 Aaron Marasco. <br>
Expand Down
1 change: 1 addition & 0 deletions binding.gyp
Expand Up @@ -5,6 +5,7 @@
'defines': [
'HAVE_IMAGE_HASH',
'cimg_use_png',
'cimg_verbosity=0',
],
'include_dirs': [
'deps/pHash',
Expand Down
1 change: 1 addition & 0 deletions deps/pHash/pHash.gyp
Expand Up @@ -6,6 +6,7 @@
'defines': [
'HAVE_IMAGE_HASH',
'cimg_use_png',
'cimg_verbosity=0',
],
'include_dirs': [
'.',
Expand Down
17 changes: 7 additions & 10 deletions lib/phash.js
Expand Up @@ -23,25 +23,22 @@
var pHash = require('../build/Release/pHashBinding');

exports.imageHashSync = function(file) {
return pHash.imageHashSync(file);
return pHash.imageHashSync(file);
};

exports.imageHash = function(file, cb) {
return pHash.imageHash(file, cb);
return pHash.imageHash(file, cb);
};

exports.hammingDistance = function(hasha,hashb) {
return pHash.hammingDistance(hasha,hashb);
exports.hammingDistance = function(hasha, hashb) {
return pHash.hammingDistance(hasha, hashb);
};

/*
Deprecated, use "imageHashSync"
*/
*Deprecated, use "imageHashSync"
*/
exports.getImageHash = exports.imageHashSync;

exports.getOldHash = function(filePath) {
return pHash.oldHash(filePath);
return pHash.oldHash(filePath);
};



23 changes: 11 additions & 12 deletions src/phash.cpp
Expand Up @@ -70,11 +70,10 @@ const string getHash(const char* file) {
try {
ulong64 hash = 0;
ph_dct_imagehash(file, hash);
return NumberToString(hash);
return NumberToString(hash);
}
catch(...) {
// something went wrong with hashing
// probably a CImg or ImageMagick IO Problem
catch (...) {
// something went wrong; probably a problem with CImg.
return "0";
}
}
Expand All @@ -91,7 +90,7 @@ void HashAfter(uv_work_t* req, int status) {
Handle<Value> argv[2];

if (request->hash == "0") {
argv[0] = String::New("Error getting image hash");
argv[0] = v8::Exception::Error(String::New("Error getting image hash"));
}
else {
argv[0] = Undefined();
Expand Down Expand Up @@ -158,13 +157,13 @@ Handle<Value> oldHash(const Arguments& args) {
}

void RegisterModule(Handle<Object> target) {
NODE_SET_METHOD(target, "imageHashSync", ImageHashSync);
NODE_SET_METHOD(target, "imageHash", ImageHashAsync);
NODE_SET_METHOD(target, "hammingDistance", HammingDistance);
// methods below are deprecated
NODE_SET_METHOD(target, "oldHash", oldHash);
NODE_SET_METHOD(target, "imagehash", ImageHashSync);
NODE_SET_METHOD(target, "imageHashSync", ImageHashSync);
NODE_SET_METHOD(target, "imageHash", ImageHashAsync);
NODE_SET_METHOD(target, "hammingDistance", HammingDistance);

// methods below are deprecated
NODE_SET_METHOD(target, "oldHash", oldHash);
NODE_SET_METHOD(target, "imagehash", ImageHashSync);
}

NODE_MODULE(pHashBinding, RegisterModule);

0 comments on commit ea8b437

Please sign in to comment.