TexFile.h 11 KB

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