UNPKG

lifehash

Version:

TypeScript/JavaScript implementation of LifeHash, a visual hash algorithm

142 lines (138 loc) 5.27 kB
/* Copyright 2011 Andrey Zholos Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ export class Bitmap { constructor(width, height) { this.width = width; this.height = height; this.pixel = new Array(width); for (let x = 0; x < width; x++) { this.pixel[x] = new Array(height); for (let y = 0; y < height; y++) { this.pixel[x][y] = [0, 0, 0, 0]; } } } subsample(n) { const w = ~~(this.width / n); const h = ~~(this.height / n); const px = new Array(w); for (let x = 0; x < w; x++) { px[x] = new Array(h); for (let y = 0; y < h; y++) { const q = [0, 0, 0, 0]; for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { const r = this.pixel[x * n + i][y * n + j]; q[0] += r[3] * r[0]; q[1] += r[3] * r[1]; q[2] += r[3] * r[2]; q[3] += r[3]; } } if (q[3]) { q[0] /= q[3]; q[1] /= q[3]; q[2] /= q[3]; q[3] /= n * n; } px[x][y] = q; } } this.width = w; this.height = h; this.pixel = px; } dataURL() { const sample = (v) => { return ~~(Math.max(0, Math.min(1, v)) * 255); }; const gamma = (v) => { return sample(v); }; const row = (px, w, y) => { let d = '\0'; for (let x = 0; x < w; x++) { const r = px[x][y]; d += String.fromCharCode(gamma(r[0]), gamma(r[1]), gamma(r[2]), sample(r[3])); } return d; }; const rows = (px, w, h) => { let d = ''; for (let y = 0; y < h; y++) { d += row(px, w, y); } return d; }; const adler = (d) => { let s1 = 1; let s2 = 0; for (let i = 0; i < d.length; i++) { s1 = (s1 + d.charCodeAt(i)) % 65521; s2 = (s2 + s1) % 65521; } return (s2 << 16) | s1; }; const hton = (i) => { return String.fromCharCode(i >>> 24, (i >>> 16) & 255, (i >>> 8) & 255, i & 255); }; const deflate = (d) => { let compressed = '\x78\x01'; let i = 0; do { const block = d.slice(i, i + 65535); const len = block.length; compressed += String.fromCharCode((i += block.length) === d.length ? 1 : 0, len & 255, len >>> 8, ~len & 255, (~len >>> 8) & 255); compressed += block; } while (i < d.length); return compressed + hton(adler(d)); }; const crc32 = (d) => { let c = ~0; for (let i = 0; i < d.length; i++) { for (let b = d.charCodeAt(i) | 0x100; b !== 1; b >>>= 1) { c = (c >>> 1) ^ ((c ^ b) & 1 ? 0xedb88320 : 0); } } return ~c; }; const chunk = (type, d) => { return hton(d.length) + type + d + hton(crc32(type + d)); }; const base64 = (d) => { let enc = ''; for (let i = 5, n = d.length * 8 + 5; i < n; i += 6) { enc += 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'[(((d.charCodeAt(~~(i / 8) - 1) << 8) | d.charCodeAt(~~(i / 8))) >> (7 - (i % 8))) & 63]; } for (; enc.length % 4; enc += '=') { // pad } return enc; }; const png = '\x89PNG\r\n\x1a\n' + chunk('IHDR', hton(this.width) + hton(this.height) + '\x08\x06\0\0\0') + chunk('IDAT', deflate(rows(this.pixel, this.width, this.height))) + chunk('IEND', ''); return 'data:image/png;base64,' + base64(png); } }