TGALoader.js 12 KB

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