TGALoader.js 12 KB

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