babylon.tools.tga.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**
  2. * Based on jsTGALoader - Javascript loader for TGA file
  3. * By Vincent Thibault
  4. * @blog http://blog.robrowser.com/javascript-tga-loader.html
  5. */
  6. module BABYLON.Internals {
  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_rgb = false;
  60. var use_grey = false;
  61. // Get some informations.
  62. switch (header.image_type) {
  63. case TGATools._TYPE_RLE_INDEXED:
  64. use_rle = true;
  65. case TGATools._TYPE_INDEXED:
  66. use_pal = true;
  67. break;
  68. case TGATools._TYPE_RLE_RGB:
  69. use_rle = true;
  70. case TGATools._TYPE_RGB:
  71. use_rgb = true;
  72. break;
  73. case TGATools._TYPE_RLE_GREY:
  74. use_rle = true;
  75. case TGATools._TYPE_GREY:
  76. use_grey = true;
  77. break;
  78. }
  79. var pixel_data;
  80. var numAlphaBits = header.flags & 0xf;
  81. var pixel_size = header.pixel_size >> 3;
  82. var pixel_total = header.width * header.height * pixel_size;
  83. // Read palettes
  84. var palettes;
  85. if (use_pal) {
  86. palettes = data.subarray(offset, offset += header.colormap_length * (header.colormap_size >> 3));
  87. }
  88. // Read LRE
  89. if (use_rle) {
  90. pixel_data = new Uint8Array(pixel_total);
  91. var c, count, i;
  92. var localOffset = 0;
  93. var pixels = new Uint8Array(pixel_size);
  94. while (offset < pixel_total && localOffset < pixel_total) {
  95. c = data[offset++];
  96. count = (c & 0x7f) + 1;
  97. // RLE pixels
  98. if (c & 0x80) {
  99. // Bind pixel tmp array
  100. for (i = 0; i < pixel_size; ++i) {
  101. pixels[i] = data[offset++];
  102. }
  103. // Copy pixel array
  104. for (i = 0; i < count; ++i) {
  105. pixel_data.set(pixels, localOffset + i * pixel_size);
  106. }
  107. localOffset += pixel_size * count;
  108. }
  109. // Raw pixels
  110. else {
  111. count *= pixel_size;
  112. for (i = 0; i < count; ++i) {
  113. pixel_data[localOffset + i] = data[offset++];
  114. }
  115. localOffset += count;
  116. }
  117. }
  118. }
  119. // RAW Pixels
  120. else {
  121. pixel_data = data.subarray(
  122. offset,
  123. offset += (use_pal ? header.width * header.height : pixel_total)
  124. );
  125. }
  126. // Load to texture
  127. var x_start, y_start, x_step, y_step, y_end, x_end;
  128. switch ((header.flags & TGATools._ORIGIN_MASK) >> TGATools._ORIGIN_SHIFT) {
  129. default:
  130. case TGATools._ORIGIN_UL:
  131. x_start = 0;
  132. x_step = 1;
  133. x_end = header.width;
  134. y_start = 0;
  135. y_step = 1;
  136. y_end = header.height;
  137. break;
  138. case TGATools._ORIGIN_BL:
  139. x_start = 0;
  140. x_step = 1;
  141. x_end = header.width;
  142. y_start = header.height - 1;
  143. y_step = -1;
  144. y_end = -1;
  145. break;
  146. case TGATools._ORIGIN_UR:
  147. x_start = header.width - 1;
  148. x_step = -1;
  149. x_end = -1;
  150. y_start = 0;
  151. y_step = 1;
  152. y_end = header.height;
  153. break;
  154. case TGATools._ORIGIN_BR:
  155. x_start = header.width - 1;
  156. x_step = -1;
  157. x_end = -1;
  158. y_start = header.height - 1;
  159. y_step = -1;
  160. y_end = -1;
  161. break;
  162. }
  163. // Load the specify method
  164. var func = '_getImageData' + (use_grey ? 'Grey' : '') + (header.pixel_size) + 'bits';
  165. var imageData = TGATools[func](header, palettes, pixel_data, y_start, y_step, y_end, x_start, x_step, x_end);
  166. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, header.width, header.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, imageData);
  167. }
  168. 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 {
  169. var image = pixel_data, colormap = palettes;
  170. var width = header.width, height = header.height;
  171. var color, i = 0, x, y;
  172. var imageData = new Uint8Array(width * height * 4);
  173. for (y = y_start; y !== y_end; y += y_step) {
  174. for (x = x_start; x !== x_end; x += x_step, i++) {
  175. color = image[i];
  176. imageData[(x + width * y) * 4 + 3] = 255;
  177. imageData[(x + width * y) * 4 + 2] = colormap[(color * 3) + 0];
  178. imageData[(x + width * y) * 4 + 1] = colormap[(color * 3) + 1];
  179. imageData[(x + width * y) * 4 + 0] = colormap[(color * 3) + 2];
  180. }
  181. }
  182. return imageData;
  183. }
  184. 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 {
  185. var image = pixel_data;
  186. var width = header.width, height = header.height;
  187. var color, i = 0, x, y;
  188. var imageData = new Uint8Array(width * height * 4);
  189. for (y = y_start; y !== y_end; y += y_step) {
  190. for (x = x_start; x !== x_end; x += x_step, i += 2) {
  191. color = image[i + 0] + (image[i + 1] << 8); // Inversed ?
  192. imageData[(x + width * y) * 4 + 0] = (color & 0x7C00) >> 7;
  193. imageData[(x + width * y) * 4 + 1] = (color & 0x03E0) >> 2;
  194. imageData[(x + width * y) * 4 + 2] = (color & 0x001F) >> 3;
  195. imageData[(x + width * y) * 4 + 3] = (color & 0x8000) ? 0 : 255;
  196. }
  197. }
  198. return imageData;
  199. }
  200. 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 {
  201. var image = pixel_data;
  202. var width = header.width, height = header.height;
  203. var i = 0, x, y;
  204. var imageData = new Uint8Array(width * height * 4);
  205. for (y = y_start; y !== y_end; y += y_step) {
  206. for (x = x_start; x !== x_end; x += x_step, i += 3) {
  207. imageData[(x + width * y) * 4 + 3] = 255;
  208. imageData[(x + width * y) * 4 + 2] = image[i + 0];
  209. imageData[(x + width * y) * 4 + 1] = image[i + 1];
  210. imageData[(x + width * y) * 4 + 0] = image[i + 2];
  211. }
  212. }
  213. return imageData;
  214. }
  215. 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 {
  216. var image = pixel_data;
  217. var width = header.width, height = header.height;
  218. var i = 0, x, y;
  219. var imageData = new Uint8Array(width * height * 4);
  220. for (y = y_start; y !== y_end; y += y_step) {
  221. for (x = x_start; x !== x_end; x += x_step, i += 4) {
  222. imageData[(x + width * y) * 4 + 2] = image[i + 0];
  223. imageData[(x + width * y) * 4 + 1] = image[i + 1];
  224. imageData[(x + width * y) * 4 + 0] = image[i + 2];
  225. imageData[(x + width * y) * 4 + 3] = image[i + 3];
  226. }
  227. }
  228. return imageData;
  229. }
  230. 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 {
  231. var image = pixel_data;
  232. var width = header.width, height = header.height;
  233. var color, i = 0, x, y;
  234. var imageData = new Uint8Array(width * height * 4);
  235. for (y = y_start; y !== y_end; y += y_step) {
  236. for (x = x_start; x !== x_end; x += x_step, i++) {
  237. color = image[i];
  238. imageData[(x + width * y) * 4 + 0] = color;
  239. imageData[(x + width * y) * 4 + 1] = color;
  240. imageData[(x + width * y) * 4 + 2] = color;
  241. imageData[(x + width * y) * 4 + 3] = 255;
  242. }
  243. }
  244. return imageData;
  245. }
  246. 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 {
  247. var image = pixel_data;
  248. var width = header.width, height = header.height;
  249. var i = 0, x, y;
  250. var imageData = new Uint8Array(width * height * 4);
  251. for (y = y_start; y !== y_end; y += y_step) {
  252. for (x = x_start; x !== x_end; x += x_step, i += 2) {
  253. imageData[(x + width * y) * 4 + 0] = image[i + 0];
  254. imageData[(x + width * y) * 4 + 1] = image[i + 0];
  255. imageData[(x + width * y) * 4 + 2] = image[i + 0];
  256. imageData[(x + width * y) * 4 + 3] = image[i + 1];
  257. }
  258. }
  259. return imageData;
  260. }
  261. }
  262. }