friendly-img-hash-js 1.0.1 → 1.0.2

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.
Files changed (2) hide show
  1. package/index.js +21 -1
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -10,5 +10,25 @@ export {
10
10
  // See defaultConfig for options.
11
11
  export const hash = async (img, userConfig) => {
12
12
  const image = await JIMP.read(img);
13
- return coreHash(image.bitmap.data, image.bitmap.width, image.bitmap.height, userConfig);
13
+ const bytes = reduceToRgb(image.bitmap.data, image.bitmap.width, image.bitmap.height);
14
+ return coreHash(bytes, image.bitmap.width, image.bitmap.height, userConfig);
15
+ };
16
+
17
+ const reduceToRgb = (data, width, height) => {
18
+ // already rgb
19
+ if (data.length === (width * height * 3)) {
20
+ return data;
21
+ }
22
+
23
+ // rgba
24
+ if (data.length === (width * height * 4)) {
25
+ return data.filter((_, i) => (i + 1) % 4);
26
+ }
27
+
28
+ // l8
29
+ if (data.length === (width * height)) {
30
+ return new Array(width * height * 3).fill(0).map((_, i) => data[Math.floor(i / 3)]);
31
+ }
32
+
33
+ throw new Error(`Unknown data length, expected ${data.length} to be a factor of width * height (${width} * ${height}).`);
14
34
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "friendly-img-hash-js",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "User friendly algorithms for generating image hashes.",
5
5
  "main": "index.js",
6
6
  "type": "module",