CCTGAlib.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * @constant
  24. * @type Number
  25. */
  26. cc.TGA_OK = 0;
  27. /**
  28. * @constant
  29. * @type Number
  30. */
  31. cc.TGA_ERROR_FILE_OPEN = 1;
  32. /**
  33. * @constant
  34. * @type Number
  35. */
  36. cc.TGA_ERROR_READING_FILE = 2;
  37. /**
  38. * @constant
  39. * @type Number
  40. */
  41. cc.TGA_ERROR_INDEXED_COLOR = 3;
  42. /**
  43. * @constant
  44. * @type Number
  45. */
  46. cc.TGA_ERROR_MEMORY = 4;
  47. /**
  48. * @constant
  49. * @type Number
  50. */
  51. cc.TGA_ERROR_COMPRESSED_FILE = 5;
  52. /**
  53. * TGA format
  54. * @param {Number} status
  55. * @param {Number} type
  56. * @param {Number} pixelDepth
  57. * @param {Number} width map width
  58. * @param {Number} height map height
  59. * @param {Array} imageData raw data
  60. * @param {Number} flipped
  61. * @constructor
  62. */
  63. cc.ImageTGA = function (status, type, pixelDepth, width, height, imageData, flipped) {
  64. this.status = status || 0;
  65. this.type = type || 0;
  66. this.pixelDepth = pixelDepth || 0;
  67. this.width = width || 0;
  68. this.height = height || 0;
  69. this.imageData = imageData || [];
  70. this.flipped = flipped || 0;
  71. };
  72. /**
  73. * load the image header field from stream. We only keep those that matter!
  74. * @param {Array} buffer
  75. * @param {Number} bufSize
  76. * @param {cc.ImageTGA} psInfo
  77. * @return {Boolean}
  78. */
  79. cc.tgaLoadHeader = function (buffer, bufSize, psInfo) {
  80. var step = 2;
  81. if (step + 1 > bufSize)
  82. return false;
  83. var binaryReader = new cc.BinaryStreamReader(buffer);
  84. binaryReader.setOffset(step);
  85. psInfo.type = binaryReader.readByte();
  86. step += 10; // . step += sizeof(unsigned char) * 2; step += sizeof(signed short) * 4;
  87. if (step + 4 + 1 > bufSize)
  88. return false;
  89. binaryReader.setOffset(step);
  90. psInfo.width = binaryReader.readUnsignedShort();
  91. psInfo.height = binaryReader.readUnsignedInteger();
  92. psInfo.pixelDepth = binaryReader.readByte();
  93. step += 5; // . step += sizeof(unsigned char); step += sizeof(signed short) * 2;
  94. if (step + 1 > bufSize)
  95. return false;
  96. var garbage = binaryReader.readByte();
  97. psInfo.flipped = 0;
  98. if (garbage & 0x20)
  99. psInfo.flipped = 1;
  100. return true;
  101. };
  102. /**
  103. * loads the image pixels. You shouldn't call this function directly.
  104. * @param {Array} buffer
  105. * @param {Number} bufSize
  106. * @param {cc.ImageTGA} psInfo
  107. * @return {Boolean}
  108. */
  109. cc.tgaLoadImageData = function (buffer, bufSize, psInfo) {
  110. var mode, total, i, aux;
  111. var step = 18; // .size_t step = (sizeof(unsigned char) + sizeof(signed short)) * 6;
  112. // mode equal the number of components for each pixel
  113. mode = 0 | (psInfo.pixelDepth / 2);
  114. // total is the number of unsigned chars we'll have to read
  115. total = psInfo.height * psInfo.width * mode;
  116. if (step + total > bufSize)
  117. return false;
  118. psInfo.imageData = cc.__getSubArray(buffer, step, step + total);
  119. // mode=3 or 4 implies that the image is RGB(A). However TGA
  120. // stores it as BGR(A) so we'll have to swap R and B.
  121. if (mode >= 3) {
  122. for (i = 0; i < total; i += mode) {
  123. aux = psInfo.imageData[i];
  124. psInfo.imageData[i] = psInfo.imageData[i + 2];
  125. psInfo.imageData[i + 2] = aux;
  126. }
  127. }
  128. return true;
  129. };
  130. /**
  131. * converts RGB to grayscale
  132. * @param {cc.ImageTGA} psInfo
  133. */
  134. cc.tgaRGBtogreyscale = function (psInfo) {
  135. var i, j;
  136. // if the image is already grayscale do nothing
  137. if (psInfo.pixelDepth === 8)
  138. return;
  139. // compute the number of actual components
  140. var mode = psInfo.pixelDepth / 8;
  141. // allocate an array for the new image data
  142. var newImageData = new Uint8Array(psInfo.height * psInfo.width);
  143. if (newImageData === null)
  144. return;
  145. // convert pixels: grayscale = o.30 * R + 0.59 * G + 0.11 * B
  146. for (i = 0, j = 0; j < psInfo.width * psInfo.height; i += mode, j++)
  147. newImageData[j] = (0.30 * psInfo.imageData[i] + 0.59 * psInfo.imageData[i + 1] + 0.11 * psInfo.imageData[i + 2]);
  148. // reassign pixelDepth and type according to the new image type
  149. psInfo.pixelDepth = 8;
  150. psInfo.type = 3;
  151. // reassigning imageData to the new array.
  152. psInfo.imageData = newImageData;
  153. };
  154. /**
  155. * releases the memory used for the image
  156. * @param {cc.ImageTGA} psInfo
  157. */
  158. cc.tgaDestroy = function (psInfo) {
  159. if (!psInfo)
  160. return;
  161. psInfo.imageData = null;
  162. psInfo = null;
  163. };
  164. /**
  165. * Load RLE image data
  166. * @param buffer
  167. * @param bufSize
  168. * @param psInfo
  169. * @returns {boolean}
  170. */
  171. cc.tgaLoadRLEImageData = function (buffer, bufSize, psInfo) {
  172. var mode, total, i, index = 0 , skip = 0, flag = 0;
  173. var aux = [], runlength = 0;
  174. var step = 18; // . size_t step = (sizeof(unsigned char) + sizeof(signed short)) * 6;
  175. // mode equal the number of components for each pixel
  176. mode = psInfo.pixelDepth / 8;
  177. // total is the number of unsigned chars we'll have to read
  178. total = psInfo.height * psInfo.width;
  179. for (i = 0; i < total; i++) {
  180. // if we have a run length pending, run it
  181. if (runlength != 0) {
  182. // we do, update the run length count
  183. runlength--;
  184. skip = (flag != 0);
  185. } else {
  186. // otherwise, read in the run length token
  187. if (step + 1 > bufSize)
  188. break;
  189. runlength = buffer[step];
  190. step += 1;
  191. // see if it's a RLE encoded sequence
  192. flag = runlength & 0x80;
  193. if (flag)
  194. runlength -= 128;
  195. skip = 0;
  196. }
  197. // do we need to skip reading this pixel?
  198. if (!skip) {
  199. // no, read in the pixel data
  200. if (step + mode > bufSize)
  201. break;
  202. aux = cc.__getSubArray(buffer, step, step + mode);
  203. step += mode;
  204. // mode=3 or 4 implies that the image is RGB(A). However TGA
  205. // stores it as BGR(A) so we'll have to swap R and B.
  206. if (mode >= 3) {
  207. var tmp = aux[0];
  208. aux[0] = aux[2];
  209. aux[2] = tmp;
  210. }
  211. }
  212. // add the pixel to our image
  213. for (var j = 0; j < mode; j++)
  214. psInfo.imageData[index + j] = aux[j];
  215. index += mode;
  216. }
  217. return true;
  218. };
  219. /**
  220. * ImageTGA Flip
  221. * @param {cc.ImageTGA} psInfo
  222. */
  223. cc.tgaFlipImage = function (psInfo) {
  224. // mode equal the number of components for each pixel
  225. var mode = psInfo.pixelDepth / 8;
  226. var rowbytes = psInfo.width * mode;
  227. for (var y = 0; y < (psInfo.height / 2); y++) {
  228. var row = cc.__getSubArray(psInfo.imageData, y * rowbytes, y * rowbytes + rowbytes);
  229. cc.__setDataToArray(cc.__getSubArray(psInfo.imageData, (psInfo.height - (y + 1)) * rowbytes, rowbytes), psInfo.imageData, y * rowbytes);
  230. cc.__setDataToArray(row, psInfo.imageData, (psInfo.height - (y + 1)) * rowbytes);
  231. }
  232. psInfo.flipped = 0;
  233. };
  234. cc.__getSubArray = function (array, start, end) {
  235. if (array instanceof Array)
  236. return array.slice(start, end);
  237. else
  238. return array.subarray(start, end);
  239. };
  240. cc.__setDataToArray = function (sourceData, destArray, startIndex) {
  241. for (var i = 0; i < sourceData.length; i++)
  242. destArray[startIndex + i] = sourceData[i];
  243. };
  244. /**
  245. * Binary Stream Reader
  246. *
  247. * @class
  248. * @param binaryData
  249. */
  250. cc.BinaryStreamReader = cc.Class.extend({
  251. _binaryData:null,
  252. _offset:0,
  253. /**
  254. * <p>The cc.BinaryStreamReader's constructor. <br/>
  255. * This function will automatically be invoked when you create a node using new construction: "var node = new cc.BinaryStreamReader()".<br/>
  256. * Override it to extend its behavior, remember to call "this._super()" in the extended "ctor" function.</p>
  257. * @param binaryData
  258. */
  259. ctor:function (binaryData) {
  260. this._binaryData = binaryData;
  261. },
  262. /**
  263. * Set the binaryData.
  264. * @param binaryData
  265. */
  266. setBinaryData:function (binaryData) {
  267. this._binaryData = binaryData;
  268. this._offset = 0;
  269. },
  270. /**
  271. * Gets the binaryData.
  272. * @returns {Object}
  273. */
  274. getBinaryData:function () {
  275. return this._binaryData;
  276. },
  277. _checkSize:function (neededBits) {
  278. if (!(this._offset + Math.ceil(neededBits / 8) < this._data.length))
  279. throw new Error("Index out of bound");
  280. },
  281. _decodeFloat:function (precisionBits, exponentBits) {
  282. var length = precisionBits + exponentBits + 1;
  283. var size = length >> 3;
  284. this._checkSize(length);
  285. var bias = Math.pow(2, exponentBits - 1) - 1;
  286. var signal = this._readBits(precisionBits + exponentBits, 1, size);
  287. var exponent = this._readBits(precisionBits, exponentBits, size);
  288. var significand = 0;
  289. var divisor = 2;
  290. var curByte = 0; //length + (-precisionBits >> 3) - 1;
  291. do {
  292. var byteValue = this._readByte(++curByte, size);
  293. var startBit = precisionBits % 8 || 8;
  294. var mask = 1 << startBit;
  295. while (mask >>= 1) {
  296. if (byteValue & mask)
  297. significand += 1 / divisor;
  298. divisor *= 2;
  299. }
  300. } while (precisionBits -= startBit);
  301. this._offset += size;
  302. return exponent == (bias << 1) + 1 ? significand ? NaN : signal ? -Infinity : +Infinity
  303. : (1 + signal * -2) * (exponent || significand ? !exponent ? Math.pow(2, -bias + 1) * significand
  304. : Math.pow(2, exponent - bias) * (1 + significand) : 0);
  305. },
  306. _readByte:function (i, size) {
  307. return this._data[this._offset + size - i - 1];
  308. },
  309. _decodeInt:function (bits, signed) {
  310. var x = this._readBits(0, bits, bits / 8), max = Math.pow(2, bits);
  311. var result = signed && x >= max / 2 ? x - max : x;
  312. this._offset += bits / 8;
  313. return result;
  314. },
  315. _shl:function (a, b) {
  316. for (++b; --b; a = ((a %= 0x7fffffff + 1) & 0x40000000) == 0x40000000 ? a * 2 : (a - 0x40000000) * 2 + 0x7fffffff + 1){};
  317. return a;
  318. },
  319. _readBits:function (start, length, size) {
  320. var offsetLeft = (start + length) % 8;
  321. var offsetRight = start % 8;
  322. var curByte = size - (start >> 3) - 1;
  323. var lastByte = size + (-(start + length) >> 3);
  324. var diff = curByte - lastByte;
  325. var sum = (this._readByte(curByte, size) >> offsetRight) & ((1 << (diff ? 8 - offsetRight : length)) - 1);
  326. if (diff && offsetLeft)
  327. sum += (this._readByte(lastByte++, size) & ((1 << offsetLeft) - 1)) << (diff-- << 3) - offsetRight;
  328. while (diff)
  329. sum += this._shl(this._readByte(lastByte++, size), (diff-- << 3) - offsetRight);
  330. return sum;
  331. },
  332. readInteger:function () {
  333. return this._decodeInt(32, true);
  334. },
  335. readUnsignedInteger:function () {
  336. return this._decodeInt(32, false);
  337. },
  338. readSingle:function () {
  339. return this._decodeFloat(23, 8);
  340. },
  341. readShort:function () {
  342. return this._decodeInt(16, true);
  343. },
  344. readUnsignedShort:function () {
  345. return this._decodeInt(16, false);
  346. },
  347. readByte:function () {
  348. var readByte = this._data[this._offset];
  349. this._offset += 1;
  350. return readByte;
  351. },
  352. readData:function (start, end) {
  353. if (this._binaryData instanceof Array) {
  354. return this._binaryData.slice(start, end);
  355. } else {
  356. //typed array
  357. return this._binaryData.subarray(start, end);
  358. }
  359. },
  360. setOffset:function (offset) {
  361. this._offset = offset;
  362. },
  363. getOffset:function () {
  364. return this._offset;
  365. }
  366. });