babylon.tga.ts 12 KB

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