friendly-img-hash-js 1.0.4 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -17,6 +17,9 @@ Browser example:
17
17
  ```
18
18
  import { hash } from 'friendly-img-hash-js';
19
19
 
20
+ // Image formats support depends on your browser
21
+ // https://en.wikipedia.org/wiki/Comparison_of_web_browsers#Image_format_support
22
+
20
23
  // Construct a hash!
21
24
  const result = await hash('https://my-sweet-image-url.png');
22
25
  ```
@@ -26,6 +29,8 @@ Nodejs Example:
26
29
  ```
27
30
  import { hash } from 'friendly-img-hash-js';
28
31
 
32
+ // Image formats supported are jpeg, png, webp, bitmap, tiff, and avif
33
+
29
34
  // Construct a hash!
30
35
  const result = await hash('./some/file/on/disk.png');
31
36
 
@@ -81,7 +86,7 @@ console.log(
81
86
  Enable Discrete Cosine Transform (DCT)
82
87
 
83
88
  ```
84
- import { hash, DCT } from 'friendly-img-hash-js';
89
+ import { hash, Dct } from 'friendly-img-hash-js';
85
90
 
86
91
  const imageBytes = // get your image bytes following the examples above.
87
92
 
package/browser.js ADDED
@@ -0,0 +1,26 @@
1
+ import { hash as coreHash } from 'img-hash-js-core';
2
+
3
+ import { reduceToRgb } from './src/byte-resolver';
4
+
5
+ export {
6
+ Grayscale, Resize, Dct, Hash, Convert,
7
+ } from 'img-hash-js-core';
8
+
9
+ // Hash an image!
10
+ // Input is a string to a url, local file, etc of an image
11
+ // See defaultConfig for options.
12
+ export const hash = async (img, userConfig) => {
13
+ const image = await loadBytesFromBrowser(img);
14
+ const bytes = reduceToRgb(image.data, image.width, image.height);
15
+ return coreHash(bytes, image.width, image.height, userConfig);
16
+ };
17
+
18
+ const loadBytesFromBrowser = async (img) => {
19
+ const canvas = document.createElement('canvas');
20
+ const context = canvas.getContext('2d');
21
+ const image = await new Promise((r) => { const i = new Image(); i.onload = (() => r(i)); i.src = img; });
22
+ canvas.height = image.naturalHeight;
23
+ canvas.width = image.naturalWidth;
24
+ context.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight);
25
+ return { data: context.getImageData(0, 0, canvas.width, canvas.height), width: canvas.width, height: canvas.height };
26
+ };
package/index.js CHANGED
@@ -1,34 +1,59 @@
1
1
  import { hash as coreHash } from 'img-hash-js-core';
2
- import JIMP from 'jimp';
2
+ import fs from 'fs';
3
+ import imageType from 'image-type';
4
+ import { PNG } from 'pngjs';
5
+ import jpeg from 'jpeg-js';
6
+ import bmp from 'bmp-js';
7
+ import UTIF from 'utif2';
8
+
9
+ import { avif, webp } from '@astropub/codecs';
10
+
11
+ import { getUrlData, isUrl } from './src/url-resolver.js';
12
+ import { reduceToRgb } from './src/byte-resolver.js';
3
13
 
4
14
  export {
5
15
  Grayscale, Resize, Dct, Hash, Convert,
6
16
  } from 'img-hash-js-core';
7
17
 
18
+ const extToResolver = {
19
+ png: PNG.sync.read,
20
+ jpg: jpeg.decode,
21
+ bmp: bmp.decode,
22
+ tif: (raw) => {
23
+ const ifds = UTIF.decode(raw);
24
+ UTIF.decodeImage(raw, ifds[0]);
25
+ const rgba = UTIF.toRGBA8(ifds[0]); // Uint8Array with RGBA pixels
26
+ return { data: rgba, width: ifds[0].width, height: ifds[0].height };
27
+ },
28
+ webp: webp.decode,
29
+ avif: avif.decode,
30
+ };
31
+
8
32
  // Hash an image!
9
33
  // Input is a string to a url, local file, etc of an image
10
34
  // See defaultConfig for options.
11
35
  export const hash = async (img, userConfig) => {
12
- const image = await JIMP.read(img);
13
- const bytes = reduceToRgb(image.bitmap.data, image.bitmap.width, image.bitmap.height);
14
- return coreHash(bytes, image.bitmap.width, image.bitmap.height, userConfig);
36
+ const image = await loadBytesFromNode(img);
37
+ const bytes = reduceToRgb(image.data, image.width, image.height);
38
+ return coreHash(bytes, image.width, image.height, userConfig);
15
39
  };
16
40
 
17
- const reduceToRgb = (data, width, height) => {
18
- // already rgb
19
- if (data.length === (width * height * 3)) {
20
- return data;
21
- }
41
+ const loadBytesFromNode = async (img) => {
42
+ const rawFileData = await readRawFileData(img);
43
+ const format = await imageType(rawFileData);
22
44
 
23
- // rgba
24
- if (data.length === (width * height * 4)) {
25
- return data.filter((_, i) => (i + 1) % 4);
45
+ const resolver = extToResolver[format.ext];
46
+ if (!resolver) {
47
+ throw new Error(`Unknown image format ${format.ext}, try a png instead.`);
26
48
  }
27
49
 
28
- // l8
29
- if (data.length === (width * height)) {
30
- return new Array(width * height * 3).fill(0).map((_, i) => data[Math.floor(i / 3)]);
50
+ return resolver.call({}, rawFileData);
51
+ };
52
+
53
+ const readRawFileData = async (s) => {
54
+ if (isUrl(s)) {
55
+ return getUrlData(s);
31
56
  }
32
57
 
33
- throw new Error(`Unknown data length, expected ${data.length} to be a factor of width * height (${width} * ${height}).`);
58
+ return fs.readFileSync(s);
34
59
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "friendly-img-hash-js",
3
- "version": "1.0.4",
3
+ "version": "1.1.1",
4
4
  "description": "User friendly algorithms for generating image hashes.",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -37,7 +37,13 @@
37
37
  "mocha": "^10.2.0"
38
38
  },
39
39
  "dependencies": {
40
+ "@astropub/codecs": "^0.4.4",
41
+ "bmp-js": "^0.1.0",
42
+ "image-type": "^5.2.0",
40
43
  "img-hash-js-core": "^1.0.7",
41
- "jimp": "^0.22.10"
42
- }
43
- }
44
+ "jpeg-js": "^0.4.4",
45
+ "pngjs": "^7.0.0",
46
+ "utif2": "^4.1.0"
47
+ },
48
+ "browser": "browser.js"
49
+ }
@@ -0,0 +1,13 @@
1
+ diff --git a/node_modules/react-native-libwebpjs/package.json b/node_modules/react-native-libwebpjs/package.json
2
+ old mode 100644
3
+ new mode 100755
4
+ index 2bb6d59..f7cf399
5
+ --- a/node_modules/react-native-libwebpjs/package.json
6
+ +++ b/node_modules/react-native-libwebpjs/package.json
7
+ @@ -1,5 +1,6 @@
8
+ {
9
+ "name": "react-native-libwebpjs",
10
+ + "type": "module",
11
+ "version": "1.0.0",
12
+ "main": "libwebp.js",
13
+ "description": "A forked libwebpjs-v0.6.0 for React Native"
@@ -0,0 +1,18 @@
1
+ export const reduceToRgb = (data, width, height) => {
2
+ // already rgb
3
+ if (data.length === (width * height * 3)) {
4
+ return data;
5
+ }
6
+
7
+ // rgba
8
+ if (data.length === (width * height * 4)) {
9
+ return data.filter((_, i) => (i + 1) % 4);
10
+ }
11
+
12
+ // l8
13
+ if (data.length === (width * height)) {
14
+ return new Array(width * height * 3).fill(0).map((_, i) => data[Math.floor(i / 3)]);
15
+ }
16
+
17
+ throw new Error(`Unknown data length, expected ${data.length} to be a factor of width * height (${width} * ${height}).`);
18
+ };
@@ -0,0 +1,27 @@
1
+ import { URL } from 'url';
2
+ import https from 'https';
3
+
4
+ export const isUrl = (s) => {
5
+ try {
6
+ // eslint-disable-next-line no-new
7
+ new URL(s);
8
+ return true;
9
+ } catch (err) {
10
+ // Nope.
11
+ }
12
+
13
+ return false;
14
+ };
15
+
16
+ export const getUrlData = async (s) => {
17
+ const options = new URL(s);
18
+ const chunks = [];
19
+ return new Promise((r) => {
20
+ https.request(options, (res) => {
21
+ res.on('data', (chunk) => {
22
+ chunks.push(chunk);
23
+ });
24
+ res.on('end', () => r(Buffer.concat(chunks)));
25
+ }).end();
26
+ });
27
+ };