TGALoader.js 11 KB

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