UNPKG

amos-tool

Version:

amos ui tool

83 lines (78 loc) 4.5 kB
"use strict"; /** * A handy class to calculate color values. * * @version 1.0 * @author Robert Eisele <robert@xarg.org> * @copyright Copyright (c) 2010, Robert Eisele * @see http://www.xarg.org/2010/03/generate-client-side-png-files-using-javascript/ * @license http://www.opensource.org/licenses/bsd-license.php BSD License * */ function write(t, i) { for (var s = 2; s < arguments.length; s++) for (var e = 0; e < arguments[s].length; e++) t[i++] = arguments[s].charAt(e); } function byte2(t) { return String.fromCharCode(t >> 8 & 255, 255 & t); } function byte4(t) { return String.fromCharCode(t >> 24 & 255, t >> 16 & 255, t >> 8 & 255, 255 & t); } function byte2lsb(t) { return String.fromCharCode(255 & t, t >> 8 & 255); } module.exports = function(t, i, s) { this.width = t, this.height = i, this.depth = s, this.pix_size = i * (t + 1), this.data_size = 2 + this.pix_size + 5 * Math.floor((65534 + this.pix_size) / 65535) + 4, this.ihdr_offs = 0, this.ihdr_size = 25, this.plte_offs = this.ihdr_offs + this.ihdr_size, this.plte_size = 8 + 3 * s + 4, this.trns_offs = this.plte_offs + this.plte_size, this.trns_size = 8 + s + 4, this.idat_offs = this.trns_offs + this.trns_size, this.idat_size = 8 + this.data_size + 4, this.iend_offs = this.idat_offs + this.idat_size, this.iend_size = 12, this.buffer_size = this.iend_offs + this.iend_size, this.buffer = new Array, this.palette = new Object, this.pindex = 0; for (var e = new Array, h = 0; h < this.buffer_size; h++) this.buffer[h] = "\0"; write(this.buffer, this.ihdr_offs, byte4(this.ihdr_size - 12), "IHDR", byte4(t), byte4(i), "\b"), write(this.buffer, this.plte_offs, byte4(this.plte_size - 12), "PLTE"), write(this.buffer, this.trns_offs, byte4(this.trns_size - 12), "tRNS"), write(this.buffer, this.idat_offs, byte4(this.idat_size - 12), "IDAT"), write(this.buffer, this.iend_offs, byte4(this.iend_size - 12), "IEND"); var r = 30912; r += 31 - r % 31, write(this.buffer, this.idat_offs + 8, byte2(r)); for (h = 0; (h << 16) - 1 < this.pix_size; h++) { var f, o; h + 65535 < this.pix_size ? (f = 65535, o = "\0") : (f = this.pix_size - (h << 16) - h, o = ""), write(this.buffer, this.idat_offs + 8 + 2 + (h << 16) + (h << 2), o, byte2lsb(f), byte2lsb(~f)); } for (h = 0; h < 256; h++) { for (var n = h, d = 0; d < 8; d++) n = 1 & n ? -306674912 ^ n >> 1 & 2147483647 : n >> 1 & 2147483647; e[h] = n; } this.index = function(t, i) { var s = i * (this.width + 1) + t + 1; return this.idat_offs + 8 + 2 + 5 * Math.floor(s / 65535 + 1) + s; }, this.color = function(t, i, s, e) { var h = (((e = e >= 0 ? e : 255) << 8 | t) << 8 | i) << 8 | s; if (void 0 === this.palette[h]) { if (this.pindex == this.depth) return "\0"; var r = this.plte_offs + 8 + 3 * this.pindex; this.buffer[r + 0] = String.fromCharCode(t), this.buffer[r + 1] = String.fromCharCode(i), this.buffer[r + 2] = String.fromCharCode(s), this.buffer[this.trns_offs + 8 + this.pindex] = String.fromCharCode(e), this.palette[h] = String.fromCharCode(this.pindex++); } return this.palette[h]; }, this.getBase64 = function() { var t, i, s, e, h, r, f, o = this.getDump(), n = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", d = o.length, a = 0, _ = ""; do { e = (t = o.charCodeAt(a)) >> 2, h = (3 & t) << 4 | (i = o.charCodeAt(a + 1)) >> 4, s = o.charCodeAt(a + 2), r = d < a + 2 ? 64 : (15 & i) << 2 | s >> 6, f = d < a + 3 ? 64 : 63 & s, _ += n.charAt(e) + n.charAt(h) + n.charAt(r) + n.charAt(f); } while ((a += 3) < d); return _; }, this.getDump = function() { for (var t = 65521, i = 1, s = 0, h = 5552, r = 0; r < this.height; r++) for (var f = -1; f < this.width; f++) s += i += this.buffer[this.index(f, r)].charCodeAt(0), 0 == (h -= 1) && (i %= t, s %= t, h = 5552); function o(t, i, s) { for (var h = -1, r = 4; r < s - 4; r += 1) h = e[255 & (h ^ t[i + r].charCodeAt(0))] ^ h >> 8 & 16777215; write(t, i + s - 4, byte4(-1 ^ h)); } return i %= t, s %= t, write(this.buffer, this.idat_offs + this.idat_size - 8, byte4(s << 16 | i)), o(this.buffer, this.ihdr_offs, this.ihdr_size), o(this.buffer, this.plte_offs, this.plte_size), o(this.buffer, this.trns_offs, this.trns_size), o(this.buffer, this.idat_offs, this.idat_size), o(this.buffer, this.iend_offs, this.iend_size), "‰PNG\r\n\n" + this.buffer.join(""); }; };