TexFile.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include "common/TypeDefine.h"
  17. class TexFile{
  18. public:
  19. TexFile(std::string path);
  20. ~TexFile();
  21. /**
  22. * Saves a fragment of the TEX file as a PNG file.
  23. *
  24. * @param[in] file_name Full path of the destination file.
  25. * @param[in] x X coordinate of the image to extract from the TEX file.
  26. * @param[in] y Y coordinate of the image to extract from the TEX file.
  27. * @param[in] w Width of the image to extract from the TEX file.
  28. * @param[in] h Height of the image to extract from the TEX file.
  29. * @param[in] palette Index of the color palette to use. For non paletted files it's
  30. * ignored.
  31. */
  32. void SavePng(
  33. std::string file_name, unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  34. unsigned int palette = 0
  35. );
  36. /**
  37. * Saves the TEX file as a PNG file.
  38. *
  39. * @param[in] file_name Full path of the destination file.
  40. * @param[in] palette Index of the color palette to use. For non paletted files it's
  41. * ignored.
  42. */
  43. void SavePng(std::string file_name, unsigned int palette = 0);
  44. /**
  45. * Saves two fragments of the TEX file as a single PNG image.
  46. *
  47. * The second fragment will be at the right of the first one. They must have the same
  48. * height.
  49. *
  50. * @param[in] file_name Full path of the destination file.
  51. * @param[in] x1 X coordinate of the first image to extract from the TEX file.
  52. * @param[in] x2 X coordinate of the second image to extract from the TEX file.
  53. * @param[in] y1 Y coordinate of the first image to extract from the TEX file.
  54. * @param[in] y2 Y coordinate of the first image to extract from the TEX file.
  55. * @param[in] w1 Width of the first image to extract from the TEX file.
  56. * @param[in] w2 Width of the second image to extract from the TEX file.
  57. * @param[in] h Height of the images to extract from the TEX file.
  58. * @param[in] palette Index of the color palette to use. For non paletted files it's
  59. * ignored.
  60. */
  61. void SavePng(
  62. std::string file_name, unsigned int x1, unsigned int x2, unsigned int y1, unsigned int y2,
  63. unsigned int w1, unsigned int w2, unsigned int h, unsigned int palette
  64. );
  65. private:
  66. // HEADER
  67. /**
  68. * File format version. Always 1. 4 bytes.
  69. */
  70. u32 version_;
  71. /**
  72. * Unknown data. 4 bytes.
  73. */
  74. u32 unknown_0_;
  75. /**
  76. * Color key flag. 4 bytes.
  77. */
  78. u32 colour_key_;
  79. /**
  80. * Unknown data. 4 bytes.
  81. */
  82. u32 unknown_1_;
  83. /**
  84. * Unknown data. 4 bytes.
  85. */
  86. u32 unknown_2_;
  87. /**
  88. * Minimum bits per color. 4 bytes.
  89. *
  90. * Ignored by V-Geas.
  91. */
  92. u32 min_bpc_;
  93. /**
  94. * Maximum bits per color. 4 bytes.
  95. *
  96. * Ignored by V-Geas.
  97. */
  98. u32 max_bpc_;
  99. /**
  100. * Minimum alpha bits. 4 bytes.
  101. *
  102. * Ignored by V-Gears.
  103. */
  104. u32 min_alpha_bits_;
  105. /**
  106. * Minimum alpha bits. 4 bytes.
  107. *
  108. * Ignored by V-Gears.
  109. */
  110. u32 max_alpha_bits_;
  111. /**
  112. * Minimum bits per pixel. 4 bytes.
  113. *
  114. * Ignored by V-Gears.
  115. */
  116. u32 min_bpp_;
  117. /**
  118. * Maximum bits per pixel. 4 bytes.
  119. *
  120. * Ignored by V-Gears.
  121. */
  122. u32 max_bpp_;
  123. /**
  124. * Unknown data. 4 bytes.
  125. */
  126. u32 unknown_3_;
  127. /**
  128. * Number of colour palettes. 4 bytes.
  129. */
  130. u32 palette_count_;
  131. /**
  132. * Number of colours per palette. 4 bytes.
  133. */
  134. u32 palette_colour_count_;
  135. /**
  136. * Bit depth. 4 Bytes.
  137. *
  138. * Can be ignores, always follow {@see bpp}.
  139. */
  140. u32 bit_depth_;
  141. /**
  142. * Image width. 4 bytes
  143. */
  144. u32 width_;
  145. /**
  146. * Image height. 4 bytes
  147. */
  148. u32 height_;
  149. /**
  150. * Pitch or bytes per row. 4 bytes.
  151. *
  152. * Ignored, use {@see bytes_per_pixel} * {@see width}.
  153. */
  154. u32 pitch_;
  155. /**
  156. * Unknown data. 4 bytes.
  157. */
  158. u32 unknown_4_;
  159. /**
  160. * Indicates a paletted image. 4 bytes.
  161. */
  162. u32 has_palette_;
  163. /**
  164. * Bits per index. 4 bytes.
  165. *
  166. * For paletted images only. 0 for non-paletted.
  167. */
  168. u32 bits_per_index_;
  169. /**
  170. * Indexed to 8 bit flag.
  171. *
  172. * Ignored by OG and V-Gears.
  173. */
  174. u32 indexed_to_8_bit_;
  175. /**
  176. * Palette size. 4 bytes.
  177. *
  178. * It must match {@see palette_count} * {@see colours_per_palette}.
  179. */
  180. u32 palette_size_;
  181. /**
  182. * Number of colours per palette. 4 bytes.
  183. *
  184. * Ignore, use {@see palette_colour_count}
  185. */
  186. u32 colours_per_palette_;
  187. /**
  188. * Runtime data.
  189. *
  190. * V-gears ignores it.
  191. */
  192. u32 run_0_;
  193. /**
  194. * Bits per pixel.
  195. */
  196. u32 bits_per_pixel_;
  197. /**
  198. * Bytes per pixel.
  199. */
  200. u32 bytes_per_pixel_;
  201. // Pixel format 0 for non-paletted images.
  202. /**
  203. * Number of red bits.
  204. */
  205. u32 bits_red_;
  206. /**
  207. * Number of green bits
  208. */
  209. u32 bits_green_;
  210. /**
  211. * Number of blue bits
  212. */
  213. u32 bits_blue_;
  214. /**
  215. * Number of alpha bits
  216. */
  217. u32 bits_alpha_;
  218. /**
  219. * Red shift. 4 bytes.
  220. */
  221. u32 bitmask_red_;
  222. /**
  223. * Green bitmask. 4 bytes.
  224. */
  225. u32 bitmask_green_;
  226. /**
  227. * Blue bitmask. 4 bytes.
  228. */
  229. u32 bitmask_blue_;
  230. /**
  231. * Alpha bitmask. 4 bytes.
  232. */
  233. u32 bitmask_alpha_;
  234. /**
  235. * Red shift. 4 bytes.
  236. */
  237. u32 shift_red_;
  238. /**
  239. * Green shift. 4 bytes.
  240. */
  241. u32 shift_green_;
  242. /**
  243. * Blue shift. 4 bytes.
  244. */
  245. u32 shift_blue_;
  246. /**
  247. * Alpha shift. 4 bytes.
  248. */
  249. u32 shift_alpha_;
  250. /**
  251. * Red bits. Always 8. 4 bytes.
  252. *
  253. * Unused, use {@see bits_red}.
  254. */
  255. u32 bits_red_2_;
  256. /**
  257. * Green bits. Always 8. 4 bytes.
  258. *
  259. * Unused, use {@see bits_green}.
  260. */
  261. u32 bits_green_2_;
  262. /**
  263. * Blue bits. Always 8. 4 bytes.
  264. *
  265. * Unused, use {@see bits_blue}.
  266. */
  267. u32 bits_blue_2_;
  268. /**
  269. * Alpha bits. Always 8. 4 bytes.
  270. *
  271. * Unused, use {@see bits_alpha}.
  272. */
  273. u32 bits_alpha_2_;
  274. /**
  275. * Max value for red colour. 4 bytes.
  276. */
  277. u32 max_red_;
  278. /**
  279. * Max value for green colour. 4 bytes.
  280. */
  281. u32 max_green_;
  282. /**
  283. * Max value for blue colour. 4 bytes.
  284. */
  285. u32 max_blue_;
  286. /**
  287. * Max alpha value. 4 bytes.
  288. */
  289. u32 max_alpha_;
  290. // Pixel format end.
  291. /**
  292. * Colour key array flag. 4 bytes.
  293. *
  294. * Indicates the presence of a color key array,
  295. */
  296. u32 colour_k_array_;
  297. /**
  298. * Runtime data. 4 bytes.
  299. *
  300. * V-Gears ignores it.
  301. */
  302. u32 run_1_;
  303. /**
  304. * Alpha reference value. 4 bytes.
  305. *
  306. * Only applies to paltted images, if the alpha value sampled from the palee is 0xFE, this
  307. * value should be replaced with the reference alpha.
  308. */
  309. u32 ref_alpha_;
  310. /**
  311. * Runtime data. 4 bytes.
  312. *
  313. * V-Gears ignores it.
  314. */
  315. u32 run_2_;
  316. /**
  317. * Unknown data. 4 bytes.
  318. */
  319. u32 unknown_5_;
  320. /**
  321. * Palette index. 4 bytes.
  322. *
  323. * Runtime data.
  324. */
  325. u32 palette_index_;
  326. /**
  327. * Runtime data. 4 bytes.
  328. *
  329. * V-Gears ignores it.
  330. */
  331. u32 run_3_;
  332. /**
  333. * Runtime data. 4 bytes.
  334. *
  335. * V-Gears ignores it.
  336. */
  337. u32 run_4_;
  338. /**
  339. * Unknown data. 4 bytes.
  340. */
  341. u32 unknown_6_;
  342. /**
  343. * Unknown data. 4 bytes.
  344. */
  345. u32 unknown_7_;
  346. /**
  347. * Unknown data. 4 bytes.
  348. */
  349. u32 unknown_8_;
  350. /**
  351. * Unknown data. 4 bytes.
  352. */
  353. u32 unknown_9_;
  354. // Palette data.
  355. /**
  356. * Palette data. 4 * {@see palette_size} bytes.
  357. *
  358. * Used only for paletted images. Blocks of 32-bit BGRA colour format.
  359. */
  360. std::vector<std::vector<Ogre::ColourValue>> palettes_;
  361. // Pixel data.
  362. /**
  363. * Pixel data for non paletted images. {@see width} * {@see height}.
  364. */
  365. std::vector<Ogre::ColourValue> pixel_colour_;
  366. /**
  367. * Pixel data for paletted images. {@see width} * {@see height} * {@see bytes_per pixels}.
  368. *
  369. * In paletted images, every byte is a color reference in the palette. For non-paletted
  370. * images, a pixel format section.
  371. */
  372. std::vector<u8> pixel_ref_;
  373. // Colour key array
  374. /**
  375. * Colour key array. {@see palette_count} bytes.
  376. */
  377. std::vector<u8> colour_key_array_;
  378. };