tga.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import { Logger } from "../Misc/logger.js";
  2. //private static _TYPE_NO_DATA = 0;
  3. const _TYPE_INDEXED = 1;
  4. const _TYPE_RGB = 2;
  5. const _TYPE_GREY = 3;
  6. const _TYPE_RLE_INDEXED = 9;
  7. const _TYPE_RLE_RGB = 10;
  8. const _TYPE_RLE_GREY = 11;
  9. const _ORIGIN_MASK = 0x30;
  10. const _ORIGIN_SHIFT = 0x04;
  11. const _ORIGIN_BL = 0x00;
  12. const _ORIGIN_BR = 0x01;
  13. const _ORIGIN_UL = 0x02;
  14. const _ORIGIN_UR = 0x03;
  15. /**
  16. * Gets the header of a TGA file
  17. * @param data defines the TGA data
  18. * @returns the header
  19. */
  20. export function GetTGAHeader(data) {
  21. let offset = 0;
  22. const header = {
  23. id_length: data[offset++],
  24. colormap_type: data[offset++],
  25. image_type: data[offset++],
  26. colormap_index: data[offset++] | (data[offset++] << 8),
  27. colormap_length: data[offset++] | (data[offset++] << 8),
  28. colormap_size: data[offset++],
  29. origin: [data[offset++] | (data[offset++] << 8), data[offset++] | (data[offset++] << 8)],
  30. width: data[offset++] | (data[offset++] << 8),
  31. height: data[offset++] | (data[offset++] << 8),
  32. pixel_size: data[offset++],
  33. flags: data[offset++],
  34. };
  35. return header;
  36. }
  37. /**
  38. * Uploads TGA content to a Babylon Texture
  39. * @internal
  40. */
  41. export function UploadContent(texture, data) {
  42. // Not enough data to contain header ?
  43. if (data.length < 19) {
  44. Logger.Error("Unable to load TGA file - Not enough data to contain header");
  45. return;
  46. }
  47. // Read Header
  48. let offset = 18;
  49. const header = GetTGAHeader(data);
  50. // Assume it's a valid Targa file.
  51. if (header.id_length + offset > data.length) {
  52. Logger.Error("Unable to load TGA file - Not enough data");
  53. return;
  54. }
  55. // Skip not needed data
  56. offset += header.id_length;
  57. let use_rle = false;
  58. let use_pal = false;
  59. let use_grey = false;
  60. // Get some informations.
  61. switch (header.image_type) {
  62. case _TYPE_RLE_INDEXED:
  63. use_rle = true;
  64. // eslint-disable-next-line no-fallthrough
  65. case _TYPE_INDEXED:
  66. use_pal = true;
  67. break;
  68. case _TYPE_RLE_RGB:
  69. use_rle = true;
  70. // eslint-disable-next-line no-fallthrough
  71. case _TYPE_RGB:
  72. // use_rgb = true;
  73. break;
  74. case _TYPE_RLE_GREY:
  75. use_rle = true;
  76. // eslint-disable-next-line no-fallthrough
  77. case _TYPE_GREY:
  78. use_grey = true;
  79. break;
  80. }
  81. let pixel_data;
  82. // var numAlphaBits = header.flags & 0xf;
  83. const pixel_size = header.pixel_size >> 3;
  84. const pixel_total = header.width * header.height * pixel_size;
  85. // Read palettes
  86. let palettes;
  87. if (use_pal) {
  88. palettes = data.subarray(offset, (offset += header.colormap_length * (header.colormap_size >> 3)));
  89. }
  90. // Read LRE
  91. if (use_rle) {
  92. pixel_data = new Uint8Array(pixel_total);
  93. let c, count, i;
  94. let localOffset = 0;
  95. const pixels = new Uint8Array(pixel_size);
  96. while (offset < pixel_total && localOffset < pixel_total) {
  97. c = data[offset++];
  98. count = (c & 0x7f) + 1;
  99. // RLE pixels
  100. if (c & 0x80) {
  101. // Bind pixel tmp array
  102. for (i = 0; i < pixel_size; ++i) {
  103. pixels[i] = data[offset++];
  104. }
  105. // Copy pixel array
  106. for (i = 0; i < count; ++i) {
  107. pixel_data.set(pixels, localOffset + i * pixel_size);
  108. }
  109. localOffset += pixel_size * count;
  110. }
  111. // Raw pixels
  112. else {
  113. count *= pixel_size;
  114. for (i = 0; i < count; ++i) {
  115. pixel_data[localOffset + i] = data[offset++];
  116. }
  117. localOffset += count;
  118. }
  119. }
  120. }
  121. // RAW Pixels
  122. else {
  123. pixel_data = data.subarray(offset, (offset += use_pal ? header.width * header.height : pixel_total));
  124. }
  125. // Load to texture
  126. let x_start, y_start, x_step, y_step, y_end, x_end;
  127. switch ((header.flags & _ORIGIN_MASK) >> _ORIGIN_SHIFT) {
  128. default:
  129. case _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 _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 _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 _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. const func = "_getImageData" + (use_grey ? "Grey" : "") + header.pixel_size + "bits";
  164. const imageData = TGATools[func](header, palettes, pixel_data, y_start, y_step, y_end, x_start, x_step, x_end);
  165. const engine = texture.getEngine();
  166. engine._uploadDataToTextureDirectly(texture, imageData);
  167. }
  168. /**
  169. * @internal
  170. */
  171. function _getImageData8bits(header, palettes, pixel_data, y_start, y_step, y_end, x_start, x_step, x_end) {
  172. const image = pixel_data, colormap = palettes;
  173. const width = header.width, height = header.height;
  174. let color, i = 0, x, y;
  175. const 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++) {
  178. color = image[i];
  179. imageData[(x + width * y) * 4 + 3] = 255;
  180. imageData[(x + width * y) * 4 + 2] = colormap[color * 3 + 0];
  181. imageData[(x + width * y) * 4 + 1] = colormap[color * 3 + 1];
  182. imageData[(x + width * y) * 4 + 0] = colormap[color * 3 + 2];
  183. }
  184. }
  185. return imageData;
  186. }
  187. /**
  188. * @internal
  189. */
  190. function _getImageData16bits(header, palettes, pixel_data, y_start, y_step, y_end, x_start, x_step, x_end) {
  191. const image = pixel_data;
  192. const width = header.width, height = header.height;
  193. let color, i = 0, x, y;
  194. const imageData = new Uint8Array(width * height * 4);
  195. for (y = y_start; y !== y_end; y += y_step) {
  196. for (x = x_start; x !== x_end; x += x_step, i += 2) {
  197. color = image[i + 0] + (image[i + 1] << 8); // Inversed ?
  198. const r = ((((color & 0x7c00) >> 10) * 255) / 0x1f) | 0;
  199. const g = ((((color & 0x03e0) >> 5) * 255) / 0x1f) | 0;
  200. const b = (((color & 0x001f) * 255) / 0x1f) | 0;
  201. imageData[(x + width * y) * 4 + 0] = r;
  202. imageData[(x + width * y) * 4 + 1] = g;
  203. imageData[(x + width * y) * 4 + 2] = b;
  204. imageData[(x + width * y) * 4 + 3] = color & 0x8000 ? 0 : 255;
  205. }
  206. }
  207. return imageData;
  208. }
  209. /**
  210. * @internal
  211. */
  212. function _getImageData24bits(header, palettes, pixel_data, y_start, y_step, y_end, x_start, x_step, x_end) {
  213. const image = pixel_data;
  214. const width = header.width, height = header.height;
  215. let i = 0, x, y;
  216. const imageData = new Uint8Array(width * height * 4);
  217. for (y = y_start; y !== y_end; y += y_step) {
  218. for (x = x_start; x !== x_end; x += x_step, i += 3) {
  219. imageData[(x + width * y) * 4 + 3] = 255;
  220. imageData[(x + width * y) * 4 + 2] = image[i + 0];
  221. imageData[(x + width * y) * 4 + 1] = image[i + 1];
  222. imageData[(x + width * y) * 4 + 0] = image[i + 2];
  223. }
  224. }
  225. return imageData;
  226. }
  227. /**
  228. * @internal
  229. */
  230. function _getImageData32bits(header, palettes, pixel_data, y_start, y_step, y_end, x_start, x_step, x_end) {
  231. const image = pixel_data;
  232. const width = header.width, height = header.height;
  233. let i = 0, x, y;
  234. const 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 += 4) {
  237. imageData[(x + width * y) * 4 + 2] = image[i + 0];
  238. imageData[(x + width * y) * 4 + 1] = image[i + 1];
  239. imageData[(x + width * y) * 4 + 0] = image[i + 2];
  240. imageData[(x + width * y) * 4 + 3] = image[i + 3];
  241. }
  242. }
  243. return imageData;
  244. }
  245. /**
  246. * @internal
  247. */
  248. function _getImageDataGrey8bits(header, palettes, pixel_data, y_start, y_step, y_end, x_start, x_step, x_end) {
  249. const image = pixel_data;
  250. const width = header.width, height = header.height;
  251. let color, i = 0, x, y;
  252. const imageData = new Uint8Array(width * height * 4);
  253. for (y = y_start; y !== y_end; y += y_step) {
  254. for (x = x_start; x !== x_end; x += x_step, i++) {
  255. color = image[i];
  256. imageData[(x + width * y) * 4 + 0] = color;
  257. imageData[(x + width * y) * 4 + 1] = color;
  258. imageData[(x + width * y) * 4 + 2] = color;
  259. imageData[(x + width * y) * 4 + 3] = 255;
  260. }
  261. }
  262. return imageData;
  263. }
  264. /**
  265. * @internal
  266. */
  267. function _getImageDataGrey16bits(header, palettes, pixel_data, y_start, y_step, y_end, x_start, x_step, x_end) {
  268. const image = pixel_data;
  269. const width = header.width, height = header.height;
  270. let i = 0, x, y;
  271. const imageData = new Uint8Array(width * height * 4);
  272. for (y = y_start; y !== y_end; y += y_step) {
  273. for (x = x_start; x !== x_end; x += x_step, i += 2) {
  274. imageData[(x + width * y) * 4 + 0] = image[i + 0];
  275. imageData[(x + width * y) * 4 + 1] = image[i + 0];
  276. imageData[(x + width * y) * 4 + 2] = image[i + 0];
  277. imageData[(x + width * y) * 4 + 3] = image[i + 1];
  278. }
  279. }
  280. return imageData;
  281. }
  282. /**
  283. * Based on jsTGALoader - Javascript loader for TGA file
  284. * By Vincent Thibault
  285. * @see http://blog.robrowser.com/javascript-tga-loader.html
  286. */
  287. export const TGATools = {
  288. /**
  289. * Gets the header of a TGA file
  290. * @param data defines the TGA data
  291. * @returns the header
  292. */
  293. GetTGAHeader,
  294. /**
  295. * Uploads TGA content to a Babylon Texture
  296. * @internal
  297. */
  298. UploadContent,
  299. /** @internal */
  300. _getImageData8bits,
  301. /** @internal */
  302. _getImageData16bits,
  303. /** @internal */
  304. _getImageData24bits,
  305. /** @internal */
  306. _getImageData32bits,
  307. /** @internal */
  308. _getImageDataGrey8bits,
  309. /** @internal */
  310. _getImageDataGrey16bits,
  311. };
  312. //# sourceMappingURL=tga.js.map