TGALoader.js 11 KB

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