Skip to content

Commit

Permalink
fix input typing - added DecodedImage type, updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
seb-lewis committed Aug 21, 2022
1 parent d21501f commit 63aa9e6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
## Usage

```javascript
import { imageHash, jpeg } from "https://deno.land/x/imagehash/mod.ts";
import { imageHash, jpeg, DecodedImage } from "https://deno.land/x/imagehash/mod.ts";

serve(async (request: Request) => {
const rawImageData: Uint8Array = request.body
const hash = await imageHash(rawImageData, 10);
const hash: String = await imageHash(rawImageData, 10);

// OR

const decodedImage = jpeg.decode(rawImageData)
const hash = await imageHash(decodedImage, 10);
const decodedImage: DecodedImage = jpeg.decode(rawImageData) // decodedImage = { width: 100, height: 100, data: ... }
const hash: String = await imageHash(decodedImage, 10);

return new Response(JSON.stringify({hash}), {status: 200})

Expand All @@ -28,11 +28,9 @@ Returns: `string`
Returns the hash value as a string of hexadecimal characters.
### imgData
Type: `Uint8Array` or `jpeg.Image`
Type: `Uint8Array` or `DecodedImage`/`jpeg.Image`
The image data to hash. This can be a `Uint8Array` or a `jpeg.Image` object.
> The return value of `jpeg.decode()` is a `jpeg.Image` object
The image data to hash. This can be a `Uint8Array` or an object that matches `DecodedImage`: ```{data: `Uint8Array`, width: `number`, height: `number`})```
### bits = 10
#### (Optional)
Expand Down
30 changes: 16 additions & 14 deletions imageHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
// Updated and deno-ified version of blockhash.js (https://github.com/commonsmachinery/blockhash-js)
export * as jpeg from "./deps.ts"
import {mimeType} from "./mime-type.ts";
import {mimeType} from "./mime-type";

var one_bits = [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4];

Expand All @@ -23,16 +23,16 @@ export function hammingDistance(hash1: string, hash2: string) {
return d;
}

function median(data) {
function median(data: any) {
var mdarr = data.slice(0);
mdarr.sort(function(a, b) { return a-b; });
mdarr.sort(function(a: number, b: number) { return a-b; });
if (mdarr.length % 2 === 0) {
return (mdarr[mdarr.length/2] + mdarr[mdarr.length/2 + 1]) / 2.0;
}
return mdarr[Math.floor(mdarr.length/2)];
}

function translate_blocks_to_bits(blocks, pixels_per_block) {
function translate_blocks_to_bits(blocks: any, pixels_per_block: number) {
var half_block_value = pixels_per_block * 256 * 3 / 2;
var bandsize = blocks.length / 4;

Expand All @@ -53,17 +53,17 @@ function translate_blocks_to_bits(blocks, pixels_per_block) {
}
}

function bits_to_hexhash(bitsArray) {
function bits_to_hexhash(bitsArray: any) {
var hex = [];
for (var i = 0; i < bitsArray.length; i += 4) {
var nibble = bitsArray.slice(i, i + 4);
var nibble: any = bitsArray.slice(i, i + 4);
hex.push(parseInt(nibble.join(''), 2).toString(16));
}

return hex.join('');
}

function _fromEvenImage(data, bits) {
function _fromEvenImage(data: { width: number; height: number; data: any[]; }, bits: number) {
var blocksize_x = Math.floor(data.width / bits);
var blocksize_y = Math.floor(data.height / bits);

Expand Down Expand Up @@ -96,16 +96,18 @@ function _fromEvenImage(data, bits) {
return bits_to_hexhash(result);
}

function hashFromImage(data, bits) {
var result = [];
export type DecodedImage = { width: any; height: any; data: any; };

function hashFromImage(data: DecodedImage, bits: number) {
var result: any[] = [];

var i, j, x, y;
var block_width, block_height;
var weight_top, weight_bottom, weight_left, weight_right;
var block_top, block_bottom, block_left, block_right;
var y_mod, y_frac, y_int;
var x_mod, x_frac, x_int;
var blocks = [];
var blocks: any = [];

var even_x = data.width % bits === 0;
var even_y = data.height % bits === 0;
Expand Down Expand Up @@ -199,20 +201,20 @@ function hashFromImage(data, bits) {

/**
* It takes an image and returns a hash
* @param {Uint8Array | jpeg.Image} srcImageData - The image data to hash. This can be a Uint8Array or a jpeg.Image object.
* @param {Uint8Array} srcImageData - The image data to hash. This can be a Uint8Array or a Image object.
* @param {number} bits - The number of bits to use for the hash. The higher the number, the more accurate the hash, but
* the longer it will take to compute. (Default is 10)
* @returns A string of hexadecimal characters.
*/
export function imageHash(srcImageData: Uint8Array | jpeg.Image, bits?: number = 10): string {
export function imageHash(srcImageData: Uint8Array | DecodedImage, bits: number = 10): string {
try {
const mime = mimeType(srcImageData);
if (mime !== 'image/jpeg') throw new Error("Unsupported image type");
const imgData = (srcImageData instanceof jpeg.Image) ? srcImageData : jpeg.decode(srcImageData);
const imgData = (srcImageData instanceof Uint8Array) ? jpeg.decode(srcImageData) : srcImageData;
if (!imgData) throw new Error("Couldn't decode image");

return hashFromImage(imgData, bits);
} catch (err) {
throw new Error(err);
throw err;
}
}
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { imageHash, hammingDistance, jpeg } from "./imageHash.ts";
export { imageHash, hammingDistance, jpeg, DecodedImage } from "./imageHash.ts";

0 comments on commit 63aa9e6

Please sign in to comment.