TGALoader.js 11 KB

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