TGALoader.js 12 KB

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