TGALoader.js 11 KB

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