pnglib.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * A handy class to calculate color values.
  3. *
  4. * @version 1.0
  5. * @author Robert Eisele <robert@xarg.org>
  6. * @copyright Copyright (c) 2010, Robert Eisele
  7. * @link http://www.xarg.org/2010/03/generate-client-side-png-files-using-javascript/
  8. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  9. *
  10. */
  11. (function() {
  12. // helper functions for that ctx
  13. function write(buffer, offs) {
  14. for (var i = 2; i < arguments.length; i++) {
  15. for (var j = 0; j < arguments[i].length; j++) {
  16. buffer[offs++] = arguments[i].charAt(j);
  17. }
  18. }
  19. }
  20. function byte2(w) {
  21. return String.fromCharCode((w >> 8) & 255, w & 255);
  22. }
  23. function byte4(w) {
  24. return String.fromCharCode((w >> 24) & 255, (w >> 16) & 255, (w >> 8) & 255, w & 255);
  25. }
  26. function byte2lsb(w) {
  27. return String.fromCharCode(w & 255, (w >> 8) & 255);
  28. }
  29. window.PNGlib = function(width,height,depth) {
  30. this.width = width;
  31. this.height = height;
  32. this.depth = depth;
  33. // pixel data and row filter identifier size
  34. this.pix_size = height * (width + 1);
  35. // deflate header, pix_size, block headers, adler32 checksum
  36. this.data_size = 2 + this.pix_size + 5 * Math.floor((0xfffe + this.pix_size) / 0xffff) + 4;
  37. // offsets and sizes of Png chunks
  38. this.ihdr_offs = 0; // IHDR offset and size
  39. this.ihdr_size = 4 + 4 + 13 + 4;
  40. this.plte_offs = this.ihdr_offs + this.ihdr_size; // PLTE offset and size
  41. this.plte_size = 4 + 4 + 3 * depth + 4;
  42. this.trns_offs = this.plte_offs + this.plte_size; // tRNS offset and size
  43. this.trns_size = 4 + 4 + depth + 4;
  44. this.idat_offs = this.trns_offs + this.trns_size; // IDAT offset and size
  45. this.idat_size = 4 + 4 + this.data_size + 4;
  46. this.iend_offs = this.idat_offs + this.idat_size; // IEND offset and size
  47. this.iend_size = 4 + 4 + 4;
  48. this.buffer_size = this.iend_offs + this.iend_size; // total PNG size
  49. this.buffer = new Array();
  50. this.palette = new Object();
  51. this.pindex = 0;
  52. var _crc32 = new Array();
  53. // initialize buffer with zero bytes
  54. for (var i = 0; i < this.buffer_size; i++) {
  55. this.buffer[i] = "\x00";
  56. }
  57. // initialize non-zero elements
  58. write(this.buffer, this.ihdr_offs, byte4(this.ihdr_size - 12), 'IHDR', byte4(width), byte4(height), "\x08\x03");
  59. write(this.buffer, this.plte_offs, byte4(this.plte_size - 12), 'PLTE');
  60. write(this.buffer, this.trns_offs, byte4(this.trns_size - 12), 'tRNS');
  61. write(this.buffer, this.idat_offs, byte4(this.idat_size - 12), 'IDAT');
  62. write(this.buffer, this.iend_offs, byte4(this.iend_size - 12), 'IEND');
  63. // initialize deflate header
  64. var header = ((8 + (7 << 4)) << 8) | (3 << 6);
  65. header+= 31 - (header % 31);
  66. write(this.buffer, this.idat_offs + 8, byte2(header));
  67. // initialize deflate block headers
  68. for (var i = 0; (i << 16) - 1 < this.pix_size; i++) {
  69. var size, bits;
  70. if (i + 0xffff < this.pix_size) {
  71. size = 0xffff;
  72. bits = "\x00";
  73. } else {
  74. size = this.pix_size - (i << 16) - i;
  75. bits = "\x01";
  76. }
  77. write(this.buffer, this.idat_offs + 8 + 2 + (i << 16) + (i << 2), bits, byte2lsb(size), byte2lsb(~size));
  78. }
  79. /* Create crc32 lookup table */
  80. for (var i = 0; i < 256; i++) {
  81. var c = i;
  82. for (var j = 0; j < 8; j++) {
  83. if (c & 1) {
  84. c = -306674912 ^ ((c >> 1) & 0x7fffffff);
  85. } else {
  86. c = (c >> 1) & 0x7fffffff;
  87. }
  88. }
  89. _crc32[i] = c;
  90. }
  91. // compute the index into a png for a given pixel
  92. this.index = function(x,y) {
  93. var i = y * (this.width + 1) + x + 1;
  94. var j = this.idat_offs + 8 + 2 + 5 * Math.floor((i / 0xffff) + 1) + i;
  95. return j;
  96. }
  97. // convert a color and build up the palette
  98. this.color = function(red, green, blue, alpha) {
  99. alpha = alpha >= 0 ? alpha : 255;
  100. var color = (((((alpha << 8) | red) << 8) | green) << 8) | blue;
  101. if (typeof this.palette[color] == "undefined") {
  102. if (this.pindex == this.depth) return "\x00";
  103. var ndx = this.plte_offs + 8 + 3 * this.pindex;
  104. this.buffer[ndx + 0] = String.fromCharCode(red);
  105. this.buffer[ndx + 1] = String.fromCharCode(green);
  106. this.buffer[ndx + 2] = String.fromCharCode(blue);
  107. this.buffer[this.trns_offs+8+this.pindex] = String.fromCharCode(alpha);
  108. this.palette[color] = String.fromCharCode(this.pindex++);
  109. }
  110. return this.palette[color];
  111. }
  112. // output a PNG string, Base64 encoded
  113. this.getBase64 = function() {
  114. var s = this.getDump();
  115. var ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  116. var c1, c2, c3, e1, e2, e3, e4;
  117. var l = s.length;
  118. var i = 0;
  119. var r = "";
  120. do {
  121. c1 = s.charCodeAt(i);
  122. e1 = c1 >> 2;
  123. c2 = s.charCodeAt(i+1);
  124. e2 = ((c1 & 3) << 4) | (c2 >> 4);
  125. c3 = s.charCodeAt(i+2);
  126. if (l < i+2) { e3 = 64; } else { e3 = ((c2 & 0xf) << 2) | (c3 >> 6); }
  127. if (l < i+3) { e4 = 64; } else { e4 = c3 & 0x3f; }
  128. r+= ch.charAt(e1) + ch.charAt(e2) + ch.charAt(e3) + ch.charAt(e4);
  129. } while ((i+= 3) < l);
  130. return r;
  131. }
  132. // output a PNG string
  133. this.getDump = function() {
  134. // compute adler32 of output pixels + row filter bytes
  135. var BASE = 65521; /* largest prime smaller than 65536 */
  136. var NMAX = 5552; /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  137. var s1 = 1;
  138. var s2 = 0;
  139. var n = NMAX;
  140. for (var y = 0; y < this.height; y++) {
  141. for (var x = -1; x < this.width; x++) {
  142. s1+= this.buffer[this.index(x, y)].charCodeAt(0);
  143. s2+= s1;
  144. if ((n-= 1) == 0) {
  145. s1%= BASE;
  146. s2%= BASE;
  147. n = NMAX;
  148. }
  149. }
  150. }
  151. s1%= BASE;
  152. s2%= BASE;
  153. write(this.buffer, this.idat_offs + this.idat_size - 8, byte4((s2 << 16) | s1));
  154. // compute crc32 of the PNG chunks
  155. function crc32(png, offs, size) {
  156. var crc = -1;
  157. for (var i = 4; i < size-4; i += 1) {
  158. crc = _crc32[(crc ^ png[offs+i].charCodeAt(0)) & 0xff] ^ ((crc >> 8) & 0x00ffffff);
  159. }
  160. write(png, offs+size-4, byte4(crc ^ -1));
  161. }
  162. crc32(this.buffer, this.ihdr_offs, this.ihdr_size);
  163. crc32(this.buffer, this.plte_offs, this.plte_size);
  164. crc32(this.buffer, this.trns_offs, this.trns_size);
  165. crc32(this.buffer, this.idat_offs, this.idat_size);
  166. crc32(this.buffer, this.iend_offs, this.iend_size);
  167. // convert PNG to string
  168. return "\211PNG\r\n\032\n"+this.buffer.join('');
  169. }
  170. }
  171. })();