TGALoader.js 11 KB

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