QGearsTexFile.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 <vector>
  17. #include <boost/cstdint.hpp>
  18. #include <OgreImageCodec.h>
  19. #include <OgrePlatform.h>
  20. #include "common/TypeDefine.h"
  21. namespace QGears{
  22. /**
  23. * Handles TEX files.
  24. *
  25. * TEX files contain texture data for the PC version of Final Fantasy VII.
  26. */
  27. class TexFile{
  28. public:
  29. /**
  30. * Constructor.
  31. */
  32. TexFile();
  33. /**
  34. * Destructor.
  35. */
  36. virtual ~TexFile();
  37. /**
  38. * Reads texture data.
  39. *
  40. * @param input[in] The contents of the TEX file.
  41. */
  42. void Read(Ogre::DataStreamPtr& input);
  43. /**
  44. * Reads the TEX file header and sets the instance data.
  45. *
  46. * @param input[in] The contents of the TEX file.
  47. */
  48. void ReadHeader(Ogre::DataStreamPtr& input);
  49. /**
  50. * Reads texture data and matches it with the color palette.
  51. *
  52. * @param input[in] The contents of the TEX file.
  53. */
  54. void ReadPalleted(Ogre::DataStreamPtr& input);
  55. /**
  56. * Retrieves pixel information from the texture image.
  57. *
  58. * @return Pixel data.
  59. */
  60. Ogre::MemoryDataStream* GetPixelData() const;
  61. /**
  62. * Retrieves image data from the texture.
  63. *
  64. * @return The texture image.
  65. */
  66. Ogre::ImageCodec::ImageData* GetImageData() const;
  67. typedef Ogre::int32 TexEntry;
  68. #if OGRE_COMPILER == OGRE_COMPILER_MSVC
  69. #pragma pack (push, 1)
  70. #else
  71. #pragma pack (1)
  72. #endif
  73. /**
  74. * Bit data in a TEX file.
  75. */
  76. struct BitData{
  77. /**
  78. * Minimum colour value.
  79. */
  80. TexEntry color_min;
  81. /**
  82. * Maximum colour value.
  83. */
  84. TexEntry color_max;
  85. /**
  86. * Minimum alpha value.
  87. */
  88. TexEntry alpha_min;
  89. /**
  90. * Maximum alpha value.
  91. */
  92. TexEntry alpha_max;
  93. /**
  94. * @todo Understand and document.
  95. */
  96. TexEntry pixel_min;
  97. /**
  98. * @todo Understand and document.
  99. */
  100. TexEntry pixel_max;
  101. };
  102. /**
  103. * Texture image data.
  104. */
  105. struct ImageData{
  106. /**
  107. * Image depth, in bits.
  108. */
  109. TexEntry bit_depth;
  110. /**
  111. * Image width.
  112. */
  113. TexEntry width;
  114. /**
  115. * Image height.
  116. */
  117. TexEntry height;
  118. /**
  119. * Image pitch.
  120. */
  121. TexEntry pitch;
  122. };
  123. /**
  124. * Colour palette data.
  125. */
  126. struct PaletteData{
  127. /**
  128. * @todo Understand and document.
  129. */
  130. TexEntry flag;
  131. /**
  132. * @todo Understand and document.
  133. */
  134. TexEntry index_bits;
  135. /**
  136. * @todo Understand and document.
  137. */
  138. TexEntry index_8bit;
  139. /**
  140. * @todo Understand and document.
  141. */
  142. TexEntry total_color_count;
  143. /**
  144. * @todo Understand and document.
  145. */
  146. TexEntry colors_per_palette;
  147. };
  148. /**
  149. * RGBA colour data.
  150. */
  151. struct RGBAData{
  152. /**
  153. * Red component.
  154. */
  155. TexEntry red;
  156. /**
  157. * Green component.
  158. */
  159. TexEntry green;
  160. /**
  161. * Blue component.
  162. */
  163. TexEntry blue;
  164. /**
  165. * Alpha component.
  166. */
  167. TexEntry alpha;
  168. };
  169. /**
  170. * Pixel format structure.
  171. */
  172. struct PixelFormat{
  173. /**
  174. * Number of bits per pixel definition.
  175. */
  176. TexEntry bits_per_pixel;
  177. /**
  178. * Number of bytes per pixel definition.
  179. */
  180. TexEntry bytes_per_pixel;
  181. /**
  182. * Number of bits.
  183. */
  184. RGBAData bit_count;
  185. /**
  186. * @todo Understand and document.
  187. */
  188. RGBAData bit_mask;
  189. /**
  190. * @todo Understand and document.
  191. */
  192. RGBAData bit_shift;
  193. /**
  194. * @todo Understand and document.
  195. */
  196. RGBAData bit_count_unused;
  197. /**
  198. * @todo Understand and document.
  199. */
  200. RGBAData shades;
  201. };
  202. /**
  203. * A TEX file header.
  204. */
  205. struct Header{
  206. /**
  207. * File format version.
  208. */
  209. TexEntry version;
  210. /**
  211. * Unknown data.
  212. */
  213. TexEntry unknown_0x04;
  214. /**
  215. * Color key.
  216. */
  217. TexEntry color_key_flag;
  218. /**
  219. * Unknown data.
  220. */
  221. TexEntry unknown_0x0C;
  222. /**
  223. * Unknown data.
  224. */
  225. TexEntry unknown_0x10;
  226. /**
  227. * Bit data.
  228. */
  229. BitData bit_data;
  230. /**
  231. * The type of colour palette.
  232. */
  233. TexEntry palette_type;
  234. /**
  235. * The number of palettes.
  236. */
  237. TexEntry palette_count;
  238. /**
  239. * The number of colours between all the palettes.
  240. */
  241. TexEntry palette_total_color_count;
  242. /**
  243. * Texture image data.
  244. */
  245. ImageData image_data;
  246. /**
  247. * Unknown data.
  248. */
  249. TexEntry unknown_0x48;
  250. /**
  251. * Colour palette data.
  252. */
  253. PaletteData palette_data;
  254. /**
  255. * @todo Understand and document.
  256. */
  257. TexEntry runtime_data_ptr_palette_data;
  258. /**
  259. * Pixel format.
  260. */
  261. PixelFormat pixel_format;
  262. /**
  263. * @todo Understand and document.
  264. */
  265. TexEntry color_key_array_flag;
  266. /**
  267. * @todo Understand and document.
  268. */
  269. TexEntry runtime_data_ptr_color_key_array;
  270. /**
  271. * @todo Understand and document.
  272. */
  273. TexEntry reference_alpha;
  274. /**
  275. * @todo Understand and document.
  276. */
  277. TexEntry runtime_data_02;
  278. /**
  279. * @todo Understand and document.
  280. */
  281. TexEntry unknown_0xCC;
  282. /**
  283. * @todo Understand and document.
  284. */
  285. TexEntry runtime_data_palette_index;
  286. /**
  287. * @todo Understand and document.
  288. */
  289. TexEntry runtime_data_ptr_image_data;
  290. /**
  291. * @todo Understand and document.
  292. */
  293. TexEntry runtime_data_04;
  294. /**
  295. * Unknown data.
  296. */
  297. TexEntry unknown_06;
  298. /**
  299. * Unknown data.
  300. */
  301. TexEntry unknown_07;
  302. /**
  303. * Unknown data.
  304. */
  305. TexEntry unknown_08;
  306. /**
  307. * Unknown data.
  308. */
  309. TexEntry unknown_09;
  310. } header_;
  311. /**
  312. * Texture format.
  313. */
  314. struct TextureFormat{
  315. // reversing ff7 code
  316. // structure @ 0x3c
  317. // size 0x80
  318. /**
  319. * Texture width.
  320. */
  321. TexEntry width;
  322. /**
  323. * Texture height.
  324. */
  325. TexEntry height;
  326. /**
  327. * Texture pitch.
  328. */
  329. TexEntry pitch;
  330. /**
  331. * Unknown data.
  332. */
  333. TexEntry unknown_0x48;
  334. /**
  335. * @todo Understand and document.
  336. */
  337. TexEntry palette_flag;
  338. /**
  339. * @todo Understand and document.
  340. */
  341. TexEntry bits_per_palette_index;
  342. /**
  343. * @todo Understand and document.
  344. */
  345. TexEntry palette_index_8bit;
  346. /**
  347. * @todo Understand and document.
  348. */
  349. TexEntry palette_size;
  350. /**
  351. * The number of colours in the palette.
  352. */
  353. TexEntry palette_color_count;
  354. /**
  355. * @todo Understand and document.
  356. */
  357. TexEntry runtime_data_ptr_palette_data;
  358. /**
  359. * The format for the texture pixels.
  360. */
  361. PixelFormat pixel_format;
  362. };
  363. #if OGRE_COMPILER == OGRE_COMPILER_MSVC
  364. #pragma pack (pop)
  365. #else
  366. #pragma pack ()
  367. #endif
  368. enum PaletteType{
  369. // as in ogre, eg msb -> lsb
  370. /**
  371. * Palette type: ARGB, 8 bit per component.
  372. */
  373. PF_A8R8G8B8 = 0x00
  374. /**
  375. * Palette type: ARGB, floating values.
  376. */
  377. ,PF_FLOAT32_ARGB = 0x01
  378. /**
  379. * Palette type: BGRA, 8 bit per component.
  380. */
  381. ,PF_B8G8R8A8 = 0x02
  382. };
  383. typedef ColorA8R8G8B8 Color;
  384. typedef std::vector<Color> Palette;
  385. typedef std::vector<Color> TexImageData;
  386. /**
  387. * The texture palette.
  388. */
  389. Palette palette;
  390. /**
  391. * The texture image data.
  392. */
  393. TexImageData image_data_;
  394. private:
  395. /**
  396. * Flips the endian mode of data.
  397. *
  398. * If the data was big endian, it will be conterted to little
  399. * endian. If it was little endian, it will be converted to big
  400. * endian.
  401. *
  402. * @param data[in|out] Data to flip.
  403. * @param size[in] Size of data.
  404. * @param count[in] How much data to flip.
  405. */
  406. void FlipEndian(void * data, size_t size, size_t count) const;
  407. /**
  408. * Flips the endian mode of data.
  409. *
  410. * If the data was big endian, it will be conterted to little
  411. * endian. If it was little endian, it will be converted to big
  412. * endian.
  413. *
  414. * @param data[in|out] Data to flip.
  415. * @param size[in] Size of data.
  416. */
  417. void FlipEndian(void * data, size_t size) const;
  418. /**
  419. * @todo Understand and document.
  420. */
  421. static const Ogre::uint8 USE_REFERENCE_ALPHA;
  422. };
  423. }