TGALoader.js 12 KB

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