TGALoader.js 11 KB

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