TGALoader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * @author Daosheng Mu / https://github.com/DaoshengMu/
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. THREE.TGALoader = function ( manager ) {
  6. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  7. };
  8. // extend THREE.BinaryTextureLoader
  9. THREE.TGALoader.prototype = Object.create( THREE.BinaryTextureLoader.prototype );
  10. // reference from vthibault, https://github.com/vthibault/roBrowser/blob/master/src/Loaders/Targa.js
  11. THREE.TGALoader.prototype._parser = function ( buffer ) {
  12. // TGA Constants
  13. var TGA_TYPE_NO_DATA = 0,
  14. TGA_TYPE_INDEXED = 1,
  15. TGA_TYPE_RGB = 2,
  16. TGA_TYPE_GREY = 3,
  17. TGA_TYPE_RLE_INDEXED = 9,
  18. TGA_TYPE_RLE_RGB = 10,
  19. TGA_TYPE_RLE_GREY = 11,
  20. TGA_ORIGIN_MASK = 0x30,
  21. TGA_ORIGIN_SHIFT = 0x04,
  22. TGA_ORIGIN_BL = 0x00,
  23. TGA_ORIGIN_BR = 0x01,
  24. TGA_ORIGIN_UL = 0x02,
  25. TGA_ORIGIN_UR = 0x03;
  26. if ( buffer.length < 19 )
  27. console.error( 'THREE.TGALoader.parse: Not enough data to contain header.' );
  28. var content = new Uint8Array( buffer ),
  29. offset = 0,
  30. header = {
  31. id_length: content[ offset ++ ],
  32. colormap_type: content[ offset ++ ],
  33. image_type: content[ offset ++ ],
  34. colormap_index: content[ offset ++ ] | content[ offset ++ ] << 8,
  35. colormap_length: content[ offset ++ ] | content[ offset ++ ] << 8,
  36. colormap_size: content[ offset ++ ],
  37. origin: [
  38. content[ offset ++ ] | content[ offset ++ ] << 8,
  39. content[ offset ++ ] | content[ offset ++ ] << 8
  40. ],
  41. width: content[ offset ++ ] | content[ offset ++ ] << 8,
  42. height: content[ offset ++ ] | content[ offset ++ ] << 8,
  43. pixel_size: content[ offset ++ ],
  44. flags: content[ offset ++ ]
  45. };
  46. function tgaCheckHeader( header ) {
  47. switch ( header.image_type ) {
  48. // Check indexed type
  49. case TGA_TYPE_INDEXED:
  50. case TGA_TYPE_RLE_INDEXED:
  51. if ( header.colormap_length > 256 || header.colormap_size !== 24 || header.colormap_type !== 1) {
  52. console.error('THREE.TGALoader.parse.tgaCheckHeader: Invalid type colormap data for indexed type');
  53. }
  54. break;
  55. // Check colormap type
  56. case TGA_TYPE_RGB:
  57. case TGA_TYPE_GREY:
  58. case TGA_TYPE_RLE_RGB:
  59. case TGA_TYPE_RLE_GREY:
  60. if (header.colormap_type) {
  61. console.error('THREE.TGALoader.parse.tgaCheckHeader: Invalid type colormap data for colormap type');
  62. }
  63. break;
  64. // What the need of a file without data ?
  65. case TGA_TYPE_NO_DATA:
  66. console.error('THREE.TGALoader.parse.tgaCheckHeader: No data');
  67. // Invalid type ?
  68. default:
  69. console.error('THREE.TGALoader.parse.tgaCheckHeader: Invalid type " ' + header.image_type + '"');
  70. }
  71. // Check image width and height
  72. if ( header.width <= 0 || header.height <= 0 ) {
  73. console.error( 'THREE.TGALoader.parse.tgaCheckHeader: Invalid image size' );
  74. }
  75. // Check image pixel size
  76. if (header.pixel_size !== 8 &&
  77. header.pixel_size !== 16 &&
  78. header.pixel_size !== 24 &&
  79. header.pixel_size !== 32) {
  80. console.error('THREE.TGALoader.parse.tgaCheckHeader: Invalid pixel size "' + header.pixel_size + '"');
  81. }
  82. }
  83. // Check tga if it is valid format
  84. tgaCheckHeader( header );
  85. if ( header.id_length + offset > buffer.length ) {
  86. console.error('THREE.TGALoader.parse: No data');
  87. }
  88. // Skip the needn't data
  89. offset += header.id_length;
  90. // Get targa information about RLE compression and palette
  91. var use_rle = false,
  92. use_pal = false,
  93. use_grey = false;
  94. switch ( header.image_type ) {
  95. case TGA_TYPE_RLE_INDEXED:
  96. use_rle = true;
  97. use_pal = true;
  98. break;
  99. case TGA_TYPE_INDEXED:
  100. use_pal = true;
  101. break;
  102. case TGA_TYPE_RLE_RGB:
  103. use_rle = true;
  104. break;
  105. case TGA_TYPE_RGB:
  106. break;
  107. case TGA_TYPE_RLE_GREY:
  108. use_rle = true;
  109. use_grey = true;
  110. break;
  111. case TGA_TYPE_GREY:
  112. use_grey = true;
  113. break;
  114. }
  115. // Parse tga image buffer
  116. function tgaParse( use_rle, use_pal, header, offset, data ) {
  117. var pixel_data,
  118. pixel_size,
  119. pixel_total,
  120. palettes;
  121. pixel_size = header.pixel_size >> 3;
  122. pixel_total = header.width * header.height * pixel_size;
  123. // Read palettes
  124. if ( use_pal ) {
  125. palettes = data.subarray( offset, offset += header.colormap_length * ( header.colormap_size >> 3 ) );
  126. }
  127. // Read RLE
  128. if ( use_rle ) {
  129. pixel_data = new Uint8Array(pixel_total);
  130. var c, count, i;
  131. var shift = 0;
  132. var pixels = new Uint8Array(pixel_size);
  133. while (shift < pixel_total) {
  134. c = data[offset ++];
  135. count = (c & 0x7f) + 1;
  136. // RLE pixels.
  137. if (c & 0x80) {
  138. // Bind pixel tmp array
  139. for (i = 0; i < pixel_size; ++ i) {
  140. pixels[i] = data[offset ++];
  141. }
  142. // Copy pixel array
  143. for (i = 0; i < count; ++ i) {
  144. pixel_data.set(pixels, shift + i * pixel_size);
  145. }
  146. shift += pixel_size * count;
  147. } else {
  148. // Raw pixels.
  149. count *= pixel_size;
  150. for (i = 0; i < count; ++ i) {
  151. pixel_data[shift + i] = data[offset ++];
  152. }
  153. shift += count;
  154. }
  155. }
  156. } else {
  157. // RAW Pixels
  158. pixel_data = data.subarray(
  159. offset, offset += (use_pal ? header.width * header.height : pixel_total)
  160. );
  161. }
  162. return {
  163. pixel_data: pixel_data,
  164. palettes: palettes
  165. };
  166. }
  167. function tgaGetImageData8bits(imageData, y_start, y_step, y_end, x_start, x_step, x_end, image, palettes) {
  168. var colormap = palettes;
  169. var color, i = 0, x, y;
  170. var width = header.width;
  171. for (y = y_start; y !== y_end; y += y_step) {
  172. for (x = x_start; x !== x_end; x += x_step, i ++) {
  173. color = image[i];
  174. imageData[(x + width * y) * 4 + 3] = 255;
  175. imageData[(x + width * y) * 4 + 2] = colormap[(color * 3) + 0];
  176. imageData[(x + width * y) * 4 + 1] = colormap[(color * 3) + 1];
  177. imageData[(x + width * y) * 4 + 0] = colormap[(color * 3) + 2];
  178. }
  179. }
  180. return imageData;
  181. };
  182. function tgaGetImageData16bits(imageData, y_start, y_step, y_end, x_start, x_step, x_end, image) {
  183. var color, i = 0, x, y;
  184. var width = header.width;
  185. for (y = y_start; y !== y_end; y += y_step) {
  186. for (x = x_start; x !== x_end; x += x_step, i += 2) {
  187. color = image[i + 0] + (image[i + 1] << 8); // Inversed ?
  188. imageData[(x + width * y) * 4 + 0] = (color & 0x7C00) >> 7;
  189. imageData[(x + width * y) * 4 + 1] = (color & 0x03E0) >> 2;
  190. imageData[(x + width * y) * 4 + 2] = (color & 0x001F) >> 3;
  191. imageData[(x + width * y) * 4 + 3] = (color & 0x8000) ? 0 : 255;
  192. }
  193. }
  194. return imageData;
  195. };
  196. function tgaGetImageData24bits(imageData, y_start, y_step, y_end, x_start, x_step, x_end, image) {
  197. var i = 0, x, y;
  198. var width = header.width;
  199. for (y = y_start; y !== y_end; y += y_step) {
  200. for (x = x_start; x !== x_end; x += x_step, i += 3) {
  201. imageData[(x + width * y) * 4 + 3] = 255;
  202. imageData[(x + width * y) * 4 + 2] = image[i + 0];
  203. imageData[(x + width * y) * 4 + 1] = image[i + 1];
  204. imageData[(x + width * y) * 4 + 0] = image[i + 2];
  205. }
  206. }
  207. return imageData;
  208. };
  209. function tgaGetImageData32bits(imageData, y_start, y_step, y_end, x_start, x_step, x_end, image) {
  210. var i = 0, x, y;
  211. var width = header.width;
  212. for (y = y_start; y !== y_end; y += y_step) {
  213. for (x = x_start; x !== x_end; x += x_step, i += 4) {
  214. imageData[(x + width * y) * 4 + 2] = image[i + 0];
  215. imageData[(x + width * y) * 4 + 1] = image[i + 1];
  216. imageData[(x + width * y) * 4 + 0] = image[i + 2];
  217. imageData[(x + width * y) * 4 + 3] = image[i + 3];
  218. }
  219. }
  220. return imageData;
  221. };
  222. function tgaGetImageDataGrey8bits( imageData, y_start, y_step, y_end, x_start, x_step, x_end, image ) {
  223. var color, i = 0, x, y;
  224. var width = header.width;
  225. for (y = y_start; y !== y_end; y += y_step) {
  226. for (x = x_start; x !== x_end; x += x_step, i ++) {
  227. color = image[i];
  228. imageData[(x + width * y) * 4 + 0] = color;
  229. imageData[(x + width * y) * 4 + 1] = color;
  230. imageData[(x + width * y) * 4 + 2] = color;
  231. imageData[(x + width * y) * 4 + 3] = 255;
  232. }
  233. }
  234. return imageData;
  235. };
  236. function tgaGetImageDataGrey16bits(imageData, y_start, y_step, y_end, x_start, x_step, x_end, image) {
  237. var i = 0, x, y;
  238. var width = header.width;
  239. for (y = y_start; y !== y_end; y += y_step) {
  240. for (x = x_start; x !== x_end; x += x_step, i += 2) {
  241. imageData[(x + width * y) * 4 + 0] = image[i + 0];
  242. imageData[(x + width * y) * 4 + 1] = image[i + 0];
  243. imageData[(x + width * y) * 4 + 2] = image[i + 0];
  244. imageData[(x + width * y) * 4 + 3] = image[i + 1];
  245. }
  246. }
  247. return imageData;
  248. };
  249. function getTgaRGBA( width, height, image, palette ) {
  250. var x_start,
  251. y_start,
  252. x_step,
  253. y_step,
  254. x_end,
  255. y_end,
  256. data = new Uint8Array(width * height * 4);
  257. switch ( (header.flags & TGA_ORIGIN_MASK) >> TGA_ORIGIN_SHIFT ) {
  258. default:
  259. case TGA_ORIGIN_UL:
  260. x_start = 0;
  261. x_step = 1;
  262. x_end = width;
  263. y_start = 0;
  264. y_step = 1;
  265. y_end = height;
  266. break;
  267. case TGA_ORIGIN_BL:
  268. x_start = 0;
  269. x_step = 1;
  270. x_end = width;
  271. y_start = height - 1;
  272. y_step = -1;
  273. y_end = -1;
  274. break;
  275. case TGA_ORIGIN_UR:
  276. x_start = width - 1;
  277. x_step = -1;
  278. x_end = -1;
  279. y_start = 0;
  280. y_step = 1;
  281. y_end = height;
  282. break;
  283. case TGA_ORIGIN_BR:
  284. x_start = width - 1;
  285. x_step = -1;
  286. x_end = -1;
  287. y_start = height - 1;
  288. y_step = -1;
  289. y_end = -1;
  290. break;
  291. }
  292. if ( use_grey ) {
  293. switch ( header.pixel_size ) {
  294. case 8:
  295. tgaGetImageDataGrey8bits( data, y_start, y_step, y_end, x_start, x_step, x_end, image );
  296. break;
  297. case 16:
  298. tgaGetImageDataGrey16bits( data, y_start, y_step, y_end, x_start, x_step, x_end, image );
  299. break;
  300. default:
  301. console.error( 'THREE.TGALoader.parse.getTgaRGBA: not support this format' );
  302. break;
  303. }
  304. } else {
  305. switch ( header.pixel_size ) {
  306. case 8:
  307. tgaGetImageData8bits( data, y_start, y_step, y_end, x_start, x_step, x_end, image, palette );
  308. break;
  309. case 16:
  310. tgaGetImageData16bits( data, y_start, y_step, y_end, x_start, x_step, x_end, image );
  311. break;
  312. case 24:
  313. tgaGetImageData24bits( data, y_start, y_step, y_end, x_start, x_step, x_end, image );
  314. break;
  315. case 32:
  316. tgaGetImageData32bits( data, y_start, y_step, y_end, x_start, x_step, x_end, image );
  317. break;
  318. default:
  319. console.error( 'THREE.TGALoader.parse.getTgaRGBA: not support this format' );
  320. break;
  321. }
  322. }
  323. // Load image data according to specific method
  324. // var func = 'tgaGetImageData' + (use_grey ? 'Grey' : '') + (header.pixel_size) + 'bits';
  325. // func(data, y_start, y_step, y_end, x_start, x_step, x_end, width, image, palette );
  326. return data;
  327. }
  328. var result = tgaParse( use_rle, use_pal, header, offset, content );
  329. var rgbaData = getTgaRGBA( header.width, header.height, result.pixel_data, result.palettes );
  330. return {
  331. width: header.width,
  332. height: header.height,
  333. data: rgbaData
  334. };
  335. };