babylon.tools.tga.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Internals;
  4. (function (Internals) {
  5. /*
  6. * Based on jsTGALoader - Javascript loader for TGA file
  7. * By Vincent Thibault
  8. * @blog http://blog.robrowser.com/javascript-tga-loader.html
  9. */
  10. var TGATools = (function () {
  11. function TGATools() {
  12. }
  13. TGATools.GetTGAHeader = function (data) {
  14. var offset = 0;
  15. var header = {
  16. id_length: data[offset++],
  17. colormap_type: data[offset++],
  18. image_type: data[offset++],
  19. colormap_index: data[offset++] | data[offset++] << 8,
  20. colormap_length: data[offset++] | data[offset++] << 8,
  21. colormap_size: data[offset++],
  22. origin: [
  23. data[offset++] | data[offset++] << 8,
  24. data[offset++] | data[offset++] << 8
  25. ],
  26. width: data[offset++] | data[offset++] << 8,
  27. height: data[offset++] | data[offset++] << 8,
  28. pixel_size: data[offset++],
  29. flags: data[offset++]
  30. };
  31. return header;
  32. };
  33. TGATools.UploadContent = function (gl, data) {
  34. // Not enough data to contain header ?
  35. if (data.length < 19) {
  36. BABYLON.Tools.Error("Unable to load TGA file - Not enough data to contain header");
  37. return;
  38. }
  39. // Read Header
  40. var offset = 18;
  41. var header = TGATools.GetTGAHeader(data);
  42. // Assume it's a valid Targa file.
  43. if (header.id_length + offset > data.length) {
  44. BABYLON.Tools.Error("Unable to load TGA file - Not enough data");
  45. return;
  46. }
  47. // Skip not needed data
  48. offset += header.id_length;
  49. var use_rle = false;
  50. var use_pal = false;
  51. var use_rgb = false;
  52. var use_grey = false;
  53. // Get some informations.
  54. switch (header.image_type) {
  55. case TGATools._TYPE_RLE_INDEXED:
  56. use_rle = true;
  57. case TGATools._TYPE_INDEXED:
  58. use_pal = true;
  59. break;
  60. case TGATools._TYPE_RLE_RGB:
  61. use_rle = true;
  62. case TGATools._TYPE_RGB:
  63. use_rgb = true;
  64. break;
  65. case TGATools._TYPE_RLE_GREY:
  66. use_rle = true;
  67. case TGATools._TYPE_GREY:
  68. use_grey = true;
  69. break;
  70. }
  71. var pixel_data;
  72. var numAlphaBits = header.flags & 0xf;
  73. var pixel_size = header.pixel_size >> 3;
  74. var pixel_total = header.width * header.height * pixel_size;
  75. // Read palettes
  76. var palettes;
  77. if (use_pal) {
  78. palettes = data.subarray(offset, offset += header.colormap_length * (header.colormap_size >> 3));
  79. }
  80. // Read LRE
  81. if (use_rle) {
  82. pixel_data = new Uint8Array(pixel_total);
  83. var c, count, i;
  84. var localOffset = 0;
  85. var pixels = new Uint8Array(pixel_size);
  86. while (offset < pixel_total && localOffset < pixel_total) {
  87. c = data[offset++];
  88. count = (c & 0x7f) + 1;
  89. // RLE pixels
  90. if (c & 0x80) {
  91. // Bind pixel tmp array
  92. for (i = 0; i < pixel_size; ++i) {
  93. pixels[i] = data[offset++];
  94. }
  95. // Copy pixel array
  96. for (i = 0; i < count; ++i) {
  97. pixel_data.set(pixels, localOffset + i * pixel_size);
  98. }
  99. localOffset += pixel_size * count;
  100. }
  101. else {
  102. count *= pixel_size;
  103. for (i = 0; i < count; ++i) {
  104. pixel_data[localOffset + i] = data[offset++];
  105. }
  106. localOffset += count;
  107. }
  108. }
  109. }
  110. else {
  111. pixel_data = data.subarray(offset, offset += (use_pal ? header.width * header.height : pixel_total));
  112. }
  113. // Load to texture
  114. var x_start, y_start, x_step, y_step, y_end, x_end;
  115. switch ((header.flags & TGATools._ORIGIN_MASK) >> TGATools._ORIGIN_SHIFT) {
  116. default:
  117. case TGATools._ORIGIN_UL:
  118. x_start = 0;
  119. x_step = 1;
  120. x_end = header.width;
  121. y_start = 0;
  122. y_step = 1;
  123. y_end = header.height;
  124. break;
  125. case TGATools._ORIGIN_BL:
  126. x_start = 0;
  127. x_step = 1;
  128. x_end = header.width;
  129. y_start = header.height - 1;
  130. y_step = -1;
  131. y_end = -1;
  132. break;
  133. case TGATools._ORIGIN_UR:
  134. x_start = header.width - 1;
  135. x_step = -1;
  136. x_end = -1;
  137. y_start = 0;
  138. y_step = 1;
  139. y_end = header.height;
  140. break;
  141. case TGATools._ORIGIN_BR:
  142. x_start = header.width - 1;
  143. x_step = -1;
  144. x_end = -1;
  145. y_start = header.height - 1;
  146. y_step = -1;
  147. y_end = -1;
  148. break;
  149. }
  150. // Load the specify method
  151. var func = '_getImageData' + (use_grey ? 'Grey' : '') + (header.pixel_size) + 'bits';
  152. var imageData = TGATools[func](header, palettes, pixel_data, y_start, y_step, y_end, x_start, x_step, x_end);
  153. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, header.width, header.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, imageData);
  154. };
  155. TGATools._getImageData8bits = function (header, palettes, pixel_data, y_start, y_step, y_end, x_start, x_step, x_end) {
  156. var image = pixel_data, colormap = palettes;
  157. var width = header.width, height = header.height;
  158. var color, i = 0, x, y;
  159. var imageData = new Uint8Array(width * height * 4);
  160. for (y = y_start; y !== y_end; y += y_step) {
  161. for (x = x_start; x !== x_end; x += x_step, i++) {
  162. color = image[i];
  163. imageData[(x + width * y) * 4 + 3] = 255;
  164. imageData[(x + width * y) * 4 + 2] = colormap[(color * 3) + 0];
  165. imageData[(x + width * y) * 4 + 1] = colormap[(color * 3) + 1];
  166. imageData[(x + width * y) * 4 + 0] = colormap[(color * 3) + 2];
  167. }
  168. }
  169. return imageData;
  170. };
  171. TGATools._getImageData16bits = function (header, palettes, pixel_data, y_start, y_step, y_end, x_start, x_step, x_end) {
  172. var image = pixel_data;
  173. var width = header.width, height = header.height;
  174. var color, i = 0, x, y;
  175. var imageData = new Uint8Array(width * height * 4);
  176. for (y = y_start; y !== y_end; y += y_step) {
  177. for (x = x_start; x !== x_end; x += x_step, i += 2) {
  178. color = image[i + 0] + (image[i + 1] << 8); // Inversed ?
  179. imageData[(x + width * y) * 4 + 0] = (color & 0x7C00) >> 7;
  180. imageData[(x + width * y) * 4 + 1] = (color & 0x03E0) >> 2;
  181. imageData[(x + width * y) * 4 + 2] = (color & 0x001F) >> 3;
  182. imageData[(x + width * y) * 4 + 3] = (color & 0x8000) ? 0 : 255;
  183. }
  184. }
  185. return imageData;
  186. };
  187. TGATools._getImageData24bits = function (header, palettes, pixel_data, y_start, y_step, y_end, x_start, x_step, x_end) {
  188. var image = pixel_data;
  189. var width = header.width, height = header.height;
  190. var i = 0, x, y;
  191. var imageData = new Uint8Array(width * height * 4);
  192. for (y = y_start; y !== y_end; y += y_step) {
  193. for (x = x_start; x !== x_end; x += x_step, i += 3) {
  194. imageData[(x + width * y) * 4 + 3] = 255;
  195. imageData[(x + width * y) * 4 + 2] = image[i + 0];
  196. imageData[(x + width * y) * 4 + 1] = image[i + 1];
  197. imageData[(x + width * y) * 4 + 0] = image[i + 2];
  198. }
  199. }
  200. return imageData;
  201. };
  202. TGATools._getImageData32bits = function (header, palettes, pixel_data, y_start, y_step, y_end, x_start, x_step, x_end) {
  203. var image = pixel_data;
  204. var width = header.width, height = header.height;
  205. var i = 0, x, y;
  206. var imageData = new Uint8Array(width * height * 4);
  207. for (y = y_start; y !== y_end; y += y_step) {
  208. for (x = x_start; x !== x_end; x += x_step, i += 4) {
  209. imageData[(x + width * y) * 4 + 2] = image[i + 0];
  210. imageData[(x + width * y) * 4 + 1] = image[i + 1];
  211. imageData[(x + width * y) * 4 + 0] = image[i + 2];
  212. imageData[(x + width * y) * 4 + 3] = image[i + 3];
  213. }
  214. }
  215. return imageData;
  216. };
  217. TGATools._getImageDataGrey8bits = function (header, palettes, pixel_data, y_start, y_step, y_end, x_start, x_step, x_end) {
  218. var image = pixel_data;
  219. var width = header.width, height = header.height;
  220. var color, i = 0, x, y;
  221. var imageData = new Uint8Array(width * height * 4);
  222. for (y = y_start; y !== y_end; y += y_step) {
  223. for (x = x_start; x !== x_end; x += x_step, i++) {
  224. color = image[i];
  225. imageData[(x + width * y) * 4 + 0] = color;
  226. imageData[(x + width * y) * 4 + 1] = color;
  227. imageData[(x + width * y) * 4 + 2] = color;
  228. imageData[(x + width * y) * 4 + 3] = 255;
  229. }
  230. }
  231. return imageData;
  232. };
  233. TGATools._getImageDataGrey16bits = function (header, palettes, pixel_data, y_start, y_step, y_end, x_start, x_step, x_end) {
  234. var image = pixel_data;
  235. var width = header.width, height = header.height;
  236. var i = 0, x, y;
  237. var imageData = new Uint8Array(width * height * 4);
  238. for (y = y_start; y !== y_end; y += y_step) {
  239. for (x = x_start; x !== x_end; x += x_step, i += 2) {
  240. imageData[(x + width * y) * 4 + 0] = image[i + 0];
  241. imageData[(x + width * y) * 4 + 1] = image[i + 0];
  242. imageData[(x + width * y) * 4 + 2] = image[i + 0];
  243. imageData[(x + width * y) * 4 + 3] = image[i + 1];
  244. }
  245. }
  246. return imageData;
  247. };
  248. TGATools._TYPE_NO_DATA = 0;
  249. TGATools._TYPE_INDEXED = 1;
  250. TGATools._TYPE_RGB = 2;
  251. TGATools._TYPE_GREY = 3;
  252. TGATools._TYPE_RLE_INDEXED = 9;
  253. TGATools._TYPE_RLE_RGB = 10;
  254. TGATools._TYPE_RLE_GREY = 11;
  255. TGATools._ORIGIN_MASK = 0x30;
  256. TGATools._ORIGIN_SHIFT = 0x04;
  257. TGATools._ORIGIN_BL = 0x00;
  258. TGATools._ORIGIN_BR = 0x01;
  259. TGATools._ORIGIN_UL = 0x02;
  260. TGATools._ORIGIN_UR = 0x03;
  261. return TGATools;
  262. })();
  263. Internals.TGATools = TGATools;
  264. })(Internals = BABYLON.Internals || (BABYLON.Internals = {}));
  265. })(BABYLON || (BABYLON = {}));
  266. //# sourceMappingURL=babylon.tools.tga.js.map