TGALoader.js 14 KB

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