TGALoader.js 12 KB

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