TGALoader.js 11 KB

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