CCPNGReader.js 13 KB

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