michael@0: /** michael@0: * A handy class to calculate color values. michael@0: * michael@0: * @version 1.0 michael@0: * @author Robert Eisele michael@0: * @copyright Copyright (c) 2010, Robert Eisele michael@0: * @link http://www.xarg.org/2010/03/generate-client-side-png-files-using-javascript/ michael@0: * @license http://www.opensource.org/licenses/bsd-license.php BSD License michael@0: * michael@0: */ michael@0: michael@0: (function() { michael@0: michael@0: // helper functions for that ctx michael@0: function write(buffer, offs) { michael@0: for (var i = 2; i < arguments.length; i++) { michael@0: for (var j = 0; j < arguments[i].length; j++) { michael@0: buffer[offs++] = arguments[i].charAt(j); michael@0: } michael@0: } michael@0: } michael@0: michael@0: function byte2(w) { michael@0: return String.fromCharCode((w >> 8) & 255, w & 255); michael@0: } michael@0: michael@0: function byte4(w) { michael@0: return String.fromCharCode((w >> 24) & 255, (w >> 16) & 255, (w >> 8) & 255, w & 255); michael@0: } michael@0: michael@0: function byte2lsb(w) { michael@0: return String.fromCharCode(w & 255, (w >> 8) & 255); michael@0: } michael@0: michael@0: window.PNGlib = function(width,height,depth) { michael@0: michael@0: this.width = width; michael@0: this.height = height; michael@0: this.depth = depth; michael@0: michael@0: // pixel data and row filter identifier size michael@0: this.pix_size = height * (width + 1); michael@0: michael@0: // deflate header, pix_size, block headers, adler32 checksum michael@0: this.data_size = 2 + this.pix_size + 5 * Math.floor((0xfffe + this.pix_size) / 0xffff) + 4; michael@0: michael@0: // offsets and sizes of Png chunks michael@0: this.ihdr_offs = 0; // IHDR offset and size michael@0: this.ihdr_size = 4 + 4 + 13 + 4; michael@0: this.plte_offs = this.ihdr_offs + this.ihdr_size; // PLTE offset and size michael@0: this.plte_size = 4 + 4 + 3 * depth + 4; michael@0: this.trns_offs = this.plte_offs + this.plte_size; // tRNS offset and size michael@0: this.trns_size = 4 + 4 + depth + 4; michael@0: this.idat_offs = this.trns_offs + this.trns_size; // IDAT offset and size michael@0: this.idat_size = 4 + 4 + this.data_size + 4; michael@0: this.iend_offs = this.idat_offs + this.idat_size; // IEND offset and size michael@0: this.iend_size = 4 + 4 + 4; michael@0: this.buffer_size = this.iend_offs + this.iend_size; // total PNG size michael@0: michael@0: this.buffer = new Array(); michael@0: this.palette = new Object(); michael@0: this.pindex = 0; michael@0: michael@0: var _crc32 = new Array(); michael@0: michael@0: // initialize buffer with zero bytes michael@0: for (var i = 0; i < this.buffer_size; i++) { michael@0: this.buffer[i] = "\x00"; michael@0: } michael@0: michael@0: // initialize non-zero elements michael@0: write(this.buffer, this.ihdr_offs, byte4(this.ihdr_size - 12), 'IHDR', byte4(width), byte4(height), "\x08\x03"); michael@0: write(this.buffer, this.plte_offs, byte4(this.plte_size - 12), 'PLTE'); michael@0: write(this.buffer, this.trns_offs, byte4(this.trns_size - 12), 'tRNS'); michael@0: write(this.buffer, this.idat_offs, byte4(this.idat_size - 12), 'IDAT'); michael@0: write(this.buffer, this.iend_offs, byte4(this.iend_size - 12), 'IEND'); michael@0: michael@0: // initialize deflate header michael@0: var header = ((8 + (7 << 4)) << 8) | (3 << 6); michael@0: header+= 31 - (header % 31); michael@0: michael@0: write(this.buffer, this.idat_offs + 8, byte2(header)); michael@0: michael@0: // initialize deflate block headers michael@0: for (var i = 0; (i << 16) - 1 < this.pix_size; i++) { michael@0: var size, bits; michael@0: if (i + 0xffff < this.pix_size) { michael@0: size = 0xffff; michael@0: bits = "\x00"; michael@0: } else { michael@0: size = this.pix_size - (i << 16) - i; michael@0: bits = "\x01"; michael@0: } michael@0: write(this.buffer, this.idat_offs + 8 + 2 + (i << 16) + (i << 2), bits, byte2lsb(size), byte2lsb(~size)); michael@0: } michael@0: michael@0: /* Create crc32 lookup table */ michael@0: for (var i = 0; i < 256; i++) { michael@0: var c = i; michael@0: for (var j = 0; j < 8; j++) { michael@0: if (c & 1) { michael@0: c = -306674912 ^ ((c >> 1) & 0x7fffffff); michael@0: } else { michael@0: c = (c >> 1) & 0x7fffffff; michael@0: } michael@0: } michael@0: _crc32[i] = c; michael@0: } michael@0: michael@0: // compute the index into a png for a given pixel michael@0: this.index = function(x,y) { michael@0: var i = y * (this.width + 1) + x + 1; michael@0: var j = this.idat_offs + 8 + 2 + 5 * Math.floor((i / 0xffff) + 1) + i; michael@0: return j; michael@0: } michael@0: michael@0: // convert a color and build up the palette michael@0: this.color = function(red, green, blue, alpha) { michael@0: michael@0: alpha = alpha >= 0 ? alpha : 255; michael@0: var color = (((((alpha << 8) | red) << 8) | green) << 8) | blue; michael@0: michael@0: if (typeof this.palette[color] == "undefined") { michael@0: if (this.pindex == this.depth) return "\x00"; michael@0: michael@0: var ndx = this.plte_offs + 8 + 3 * this.pindex; michael@0: michael@0: this.buffer[ndx + 0] = String.fromCharCode(red); michael@0: this.buffer[ndx + 1] = String.fromCharCode(green); michael@0: this.buffer[ndx + 2] = String.fromCharCode(blue); michael@0: this.buffer[this.trns_offs+8+this.pindex] = String.fromCharCode(alpha); michael@0: michael@0: this.palette[color] = String.fromCharCode(this.pindex++); michael@0: } michael@0: return this.palette[color]; michael@0: } michael@0: michael@0: // output a PNG string, Base64 encoded michael@0: this.getBase64 = function() { michael@0: michael@0: var s = this.getDump(); michael@0: michael@0: var ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; michael@0: var c1, c2, c3, e1, e2, e3, e4; michael@0: var l = s.length; michael@0: var i = 0; michael@0: var r = ""; michael@0: michael@0: do { michael@0: c1 = s.charCodeAt(i); michael@0: e1 = c1 >> 2; michael@0: c2 = s.charCodeAt(i+1); michael@0: e2 = ((c1 & 3) << 4) | (c2 >> 4); michael@0: c3 = s.charCodeAt(i+2); michael@0: if (l < i+2) { e3 = 64; } else { e3 = ((c2 & 0xf) << 2) | (c3 >> 6); } michael@0: if (l < i+3) { e4 = 64; } else { e4 = c3 & 0x3f; } michael@0: r+= ch.charAt(e1) + ch.charAt(e2) + ch.charAt(e3) + ch.charAt(e4); michael@0: } while ((i+= 3) < l); michael@0: return r; michael@0: } michael@0: michael@0: // output a PNG string michael@0: this.getDump = function() { michael@0: michael@0: // compute adler32 of output pixels + row filter bytes michael@0: var BASE = 65521; /* largest prime smaller than 65536 */ michael@0: var NMAX = 5552; /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ michael@0: var s1 = 1; michael@0: var s2 = 0; michael@0: var n = NMAX; michael@0: michael@0: for (var y = 0; y < this.height; y++) { michael@0: for (var x = -1; x < this.width; x++) { michael@0: s1+= this.buffer[this.index(x, y)].charCodeAt(0); michael@0: s2+= s1; michael@0: if ((n-= 1) == 0) { michael@0: s1%= BASE; michael@0: s2%= BASE; michael@0: n = NMAX; michael@0: } michael@0: } michael@0: } michael@0: s1%= BASE; michael@0: s2%= BASE; michael@0: write(this.buffer, this.idat_offs + this.idat_size - 8, byte4((s2 << 16) | s1)); michael@0: michael@0: // compute crc32 of the PNG chunks michael@0: function crc32(png, offs, size) { michael@0: var crc = -1; michael@0: for (var i = 4; i < size-4; i += 1) { michael@0: crc = _crc32[(crc ^ png[offs+i].charCodeAt(0)) & 0xff] ^ ((crc >> 8) & 0x00ffffff); michael@0: } michael@0: write(png, offs+size-4, byte4(crc ^ -1)); michael@0: } michael@0: michael@0: crc32(this.buffer, this.ihdr_offs, this.ihdr_size); michael@0: crc32(this.buffer, this.plte_offs, this.plte_size); michael@0: crc32(this.buffer, this.trns_offs, this.trns_size); michael@0: crc32(this.buffer, this.idat_offs, this.idat_size); michael@0: crc32(this.buffer, this.iend_offs, this.iend_size); michael@0: michael@0: // convert PNG to string michael@0: return "\211PNG\r\n\032\n"+this.buffer.join(''); michael@0: } michael@0: } michael@0: michael@0: })();