TexFile.h 11 KB

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