TGALoader.js 12 KB

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