TGALoader.js 12 KB

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