TGALoader.js 12 KB

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