TGALoader.js 11 KB

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