CCPNGReader.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /****************************************************************************
  2. Copyright (c) 2011 Devon Govett
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. cc.PNGReader = cc.Class.extend({
  22. ctor:function(data){
  23. var chunkSize, colors, delayDen, delayNum, frame, i, index, key, section, ccshort, text, _i, _j, _ref;
  24. this.data = data;
  25. this.pos = 8;
  26. this.palette = [];
  27. this.imgData = [];
  28. this.transparency = {};
  29. this.animation = null;
  30. this.text = {};
  31. frame = null;
  32. while (true) {
  33. chunkSize = this.readUInt32();
  34. section = ((function() {
  35. var _i, _results;
  36. _results = [];
  37. for (i = _i = 0; _i < 4; i = ++_i) {
  38. _results.push(String.fromCharCode(this.data[this.pos++]));
  39. }
  40. return _results;
  41. }).call(this)).join('');
  42. switch (section) {
  43. case 'IHDR':
  44. this.width = this.readUInt32();
  45. this.height = this.readUInt32();
  46. this.bits = this.data[this.pos++];
  47. this.colorType = this.data[this.pos++];
  48. this.compressionMethod = this.data[this.pos++];
  49. this.filterMethod = this.data[this.pos++];
  50. this.interlaceMethod = this.data[this.pos++];
  51. break;
  52. case 'acTL':
  53. this.animation = {
  54. numFrames: this.readUInt32(),
  55. numPlays: this.readUInt32() || Infinity,
  56. frames: []
  57. };
  58. break;
  59. case 'PLTE':
  60. this.palette = this.read(chunkSize);
  61. break;
  62. case 'fcTL':
  63. if (frame) {
  64. this.animation.frames.push(frame);
  65. }
  66. this.pos += 4;
  67. frame = {
  68. width: this.readUInt32(),
  69. height: this.readUInt32(),
  70. xOffset: this.readUInt32(),
  71. yOffset: this.readUInt32()
  72. };
  73. delayNum = this.readUInt16();
  74. delayDen = this.readUInt16() || 100;
  75. frame.delay = 1000 * delayNum / delayDen;
  76. frame.disposeOp = this.data[this.pos++];
  77. frame.blendOp = this.data[this.pos++];
  78. frame.data = [];
  79. break;
  80. case 'IDAT':
  81. case 'fdAT':
  82. if (section === 'fdAT') {
  83. this.pos += 4;
  84. chunkSize -= 4;
  85. }
  86. data = (frame != null ? frame.data : void 0) || this.imgData;
  87. for (i = _i = 0; 0 <= chunkSize ? _i < chunkSize : _i > chunkSize; i = 0 <= chunkSize ? ++_i : --_i) {
  88. data.push(this.data[this.pos++]);
  89. }
  90. break;
  91. case 'tRNS':
  92. this.transparency = {};
  93. switch (this.colorType) {
  94. case 3:
  95. this.transparency.indexed = this.read(chunkSize);
  96. ccshort = 255 - this.transparency.indexed.length;
  97. if (ccshort > 0) {
  98. for (i = _j = 0; 0 <= ccshort ? _j < ccshort : _j > ccshort; i = 0 <= ccshort ? ++_j : --_j) {
  99. this.transparency.indexed.push(255);
  100. }
  101. }
  102. break;
  103. case 0:
  104. this.transparency.grayscale = this.read(chunkSize)[0];
  105. break;
  106. case 2:
  107. this.transparency.rgb = this.read(chunkSize);
  108. }
  109. break;
  110. case 'tEXt':
  111. text = this.read(chunkSize);
  112. index = text.indexOf(0);
  113. key = String.fromCharCode.apply(String, text.slice(0, index));
  114. this.text[key] = String.fromCharCode.apply(String, text.slice(index + 1));
  115. break;
  116. case 'IEND':
  117. if (frame) {
  118. this.animation.frames.push(frame);
  119. }
  120. this.colors = (function() {
  121. switch (this.colorType) {
  122. case 0:
  123. case 3:
  124. case 4:
  125. return 1;
  126. case 2:
  127. case 6:
  128. return 3;
  129. }
  130. }).call(this);
  131. this.hasAlphaChannel = (_ref = this.colorType) === 4 || _ref === 6;
  132. colors = this.colors + (this.hasAlphaChannel ? 1 : 0);
  133. this.pixelBitlength = this.bits * colors;
  134. this.colorSpace = (function() {
  135. switch (this.colors) {
  136. case 1:
  137. return 'DeviceGray';
  138. case 3:
  139. return 'DeviceRGB';
  140. }
  141. }).call(this);
  142. if(Uint8Array != Array)
  143. this.imgData = new Uint8Array(this.imgData);
  144. return;
  145. default:
  146. this.pos += chunkSize;
  147. }
  148. this.pos += 4;
  149. if (this.pos > this.data.length) {
  150. throw new Error("Incomplete or corrupt PNG file");
  151. }
  152. }
  153. },
  154. read:function(bytes){
  155. var i, _i, _results;
  156. _results = [];
  157. for (i = _i = 0; 0 <= bytes ? _i < bytes : _i > bytes; i = 0 <= bytes ? ++_i : --_i) {
  158. _results.push(this.data[this.pos++]);
  159. }
  160. return _results;
  161. },
  162. readUInt32:function(){
  163. var b1, b2, b3, b4;
  164. b1 = this.data[this.pos++] << 24;
  165. b2 = this.data[this.pos++] << 16;
  166. b3 = this.data[this.pos++] << 8;
  167. b4 = this.data[this.pos++];
  168. return b1 | b2 | b3 | b4;
  169. },
  170. readUInt16:function(){
  171. var b1, b2;
  172. b1 = this.data[this.pos++] << 8;
  173. b2 = this.data[this.pos++];
  174. return b1 | b2;
  175. },
  176. decodePixels:function(data){
  177. var ccbyte, c, col, i, left, length, p, pa, paeth, pb, pc, pixelBytes, pixels, pos, row, scanlineLength, upper, upperLeft, _i, _j, _k, _l, _m;
  178. if (data == null) {
  179. data = this.imgData;
  180. }
  181. if (data.length === 0) {
  182. return new Uint8Array(0);
  183. }
  184. var inflate = new Zlib.Inflate(data,{index:0, verify:false});
  185. data = inflate.decompress();
  186. pixelBytes = this.pixelBitlength / 8;
  187. scanlineLength = pixelBytes * this.width;
  188. pixels = new Uint8Array(scanlineLength * this.height);
  189. length = data.length;
  190. row = 0;
  191. pos = 0;
  192. c = 0;
  193. while (pos < length) {
  194. switch (data[pos++]) {
  195. case 0:
  196. for (i = _i = 0; _i < scanlineLength; i = _i += 1) {
  197. pixels[c++] = data[pos++];
  198. }
  199. break;
  200. case 1:
  201. for (i = _j = 0; _j < scanlineLength; i = _j += 1) {
  202. ccbyte = data[pos++];
  203. left = i < pixelBytes ? 0 : pixels[c - pixelBytes];
  204. pixels[c++] = (ccbyte + left) % 256;
  205. }
  206. break;
  207. case 2:
  208. for (i = _k = 0; _k < scanlineLength; i = _k += 1) {
  209. ccbyte = data[pos++];
  210. col = (i - (i % pixelBytes)) / pixelBytes;
  211. upper = row && pixels[(row - 1) * scanlineLength + col * pixelBytes + (i % pixelBytes)];
  212. pixels[c++] = (upper + ccbyte) % 256;
  213. }
  214. break;
  215. case 3:
  216. for (i = _l = 0; _l < scanlineLength; i = _l += 1) {
  217. ccbyte = data[pos++];
  218. col = (i - (i % pixelBytes)) / pixelBytes;
  219. left = i < pixelBytes ? 0 : pixels[c - pixelBytes];
  220. upper = row && pixels[(row - 1) * scanlineLength + col * pixelBytes + (i % pixelBytes)];
  221. pixels[c++] = (ccbyte + Math.floor((left + upper) / 2)) % 256;
  222. }
  223. break;
  224. case 4:
  225. for (i = _m = 0; _m < scanlineLength; i = _m += 1) {
  226. ccbyte = data[pos++];
  227. col = (i - (i % pixelBytes)) / pixelBytes;
  228. left = i < pixelBytes ? 0 : pixels[c - pixelBytes];
  229. if (row === 0) {
  230. upper = upperLeft = 0;
  231. } else {
  232. upper = pixels[(row - 1) * scanlineLength + col * pixelBytes + (i % pixelBytes)];
  233. upperLeft = col && pixels[(row - 1) * scanlineLength + (col - 1) * pixelBytes + (i % pixelBytes)];
  234. }
  235. p = left + upper - upperLeft;
  236. pa = Math.abs(p - left);
  237. pb = Math.abs(p - upper);
  238. pc = Math.abs(p - upperLeft);
  239. if (pa <= pb && pa <= pc) {
  240. paeth = left;
  241. } else if (pb <= pc) {
  242. paeth = upper;
  243. } else {
  244. paeth = upperLeft;
  245. }
  246. pixels[c++] = (ccbyte + paeth) % 256;
  247. }
  248. break;
  249. default:
  250. throw new Error("Invalid filter algorithm: " + data[pos - 1]);
  251. }
  252. row++;
  253. }
  254. return pixels;
  255. },
  256. copyToImageData:function(imageData,pixels){
  257. var alpha, colors, data, i, input, j, k, length, palette, v, _ref;
  258. colors = this.colors;
  259. palette = null;
  260. alpha = this.hasAlphaChannel;
  261. if (this.palette.length) {
  262. palette = (_ref = this._decodedPalette) != null ? _ref : this._decodedPalette = this.decodePalette();
  263. colors = 4;
  264. alpha = true;
  265. }
  266. data = imageData.data || imageData;
  267. length = data.length;
  268. input = palette || pixels;
  269. i = j = 0;
  270. if (colors === 1) {
  271. while (i < length) {
  272. k = palette ? pixels[i / 4] * 4 : j;
  273. v = input[k++];
  274. data[i++] = v;
  275. data[i++] = v;
  276. data[i++] = v;
  277. data[i++] = alpha ? input[k++] : 255;
  278. j = k;
  279. }
  280. } else {
  281. while (i < length) {
  282. k = palette ? pixels[i / 4] * 4 : j;
  283. data[i++] = input[k++];
  284. data[i++] = input[k++];
  285. data[i++] = input[k++];
  286. data[i++] = alpha ? input[k++] : 255;
  287. j = k;
  288. }
  289. }
  290. },
  291. decodePalette:function(){
  292. var c, i, palette, pos, ret, transparency, _i, _ref, _ref1;
  293. palette = this.palette;
  294. transparency = this.transparency.indexed || [];
  295. ret = new Uint8Array((transparency.length || 0) + palette.length);
  296. pos = 0;
  297. c = 0;
  298. for (i = _i = 0, _ref = palette.length; _i < _ref; i = _i += 3) {
  299. ret[pos++] = palette[i];
  300. ret[pos++] = palette[i + 1];
  301. ret[pos++] = palette[i + 2];
  302. ret[pos++] = (_ref1 = transparency[c++]) != null ? _ref1 : 255;
  303. }
  304. return ret;
  305. },
  306. render: function (canvas) {
  307. var ctx, data;
  308. canvas.width = this.width;
  309. canvas.height = this.height;
  310. ctx = canvas.getContext("2d");
  311. data = ctx.createImageData(this.width, this.height);
  312. this.copyToImageData(data, this.decodePixels());
  313. return ctx.putImageData(data, 0, 0);
  314. }
  315. });