TGALoader.js 11 KB

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