TGALoader.js 11 KB

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