ddsfile.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program 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. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // 08/06/02 KM Added cube map and volume texture support
  19. #ifndef DDSFILE_H
  20. #define DDSFILE_H
  21. #if defined(_MSC_VER)
  22. #pragma once
  23. #endif
  24. #include "always.h"
  25. #include "ww3dformat.h"
  26. #include "wwstring.h"
  27. #include "vector3.h"
  28. struct IDirect3DSurface8;
  29. struct IDirect3DVolume8;
  30. // ----------------------------------------------------------------------------
  31. //
  32. // This structure represents the old DX7 color key structure. It is needed
  33. // LegacyDDSURFACEDESC which is needed when loading DDS files. DO NOT MODIFY!
  34. //
  35. // ----------------------------------------------------------------------------
  36. struct LegacyDDCOLORKEY
  37. {
  38. unsigned ColorSpaceLowValue;
  39. unsigned ColorSpaceHighValue;
  40. };
  41. // ----------------------------------------------------------------------------
  42. //
  43. // This structure represents the old DX7 CAPS2 structure. It is needed
  44. // LegacyDDSURFACEDESC which is needed when loading DDS files. DO NOT MODIFY!
  45. //
  46. // ----------------------------------------------------------------------------
  47. struct LegacyDDSCAPS2
  48. {
  49. unsigned Caps;
  50. unsigned Caps2;
  51. unsigned Caps3;
  52. unsigned Caps4;
  53. };
  54. // ----------------------------------------------------------------------------
  55. //
  56. // This structure represents the old DX7 pixel format structure. It is needed
  57. // LegacyDDSURFACEDESC which is needed when loading DDS files. DO NOT MODIFY!
  58. //
  59. // ----------------------------------------------------------------------------
  60. struct LegacyDDPIXELFORMAT
  61. {
  62. unsigned Size;
  63. unsigned Flags;
  64. unsigned FourCC;
  65. union
  66. {
  67. unsigned RGBBitCount;
  68. unsigned YUVBitCount;
  69. unsigned ZBufferBitDepth;
  70. unsigned AlphaBitDepth;
  71. unsigned LuminanceBitCount;
  72. unsigned BumpBitCount;
  73. };
  74. union
  75. {
  76. unsigned RBitMask;
  77. unsigned YBitMask;
  78. unsigned StencilBitDepth;
  79. unsigned LuminanceBitMask;
  80. unsigned BumpDuBitMask;
  81. };
  82. union
  83. {
  84. unsigned GBitMask;
  85. unsigned UBitMask;
  86. unsigned ZBitMask;
  87. unsigned BumpDvBitMask;
  88. };
  89. union
  90. {
  91. unsigned BBitMask;
  92. unsigned VBitMask;
  93. unsigned StencilBitMask;
  94. unsigned BumpLuminanceBitMask;
  95. };
  96. union
  97. {
  98. unsigned RGBAlphaBitMask;
  99. unsigned YUVAlphaBitMask;
  100. unsigned LuminanceAlphaBitMask;
  101. unsigned RGBZBitMask;
  102. unsigned YUVZBitMask;
  103. };
  104. };
  105. // ----------------------------------------------------------------------------
  106. //
  107. // This structure represents the old DX7 surface description structure.
  108. // It is needed when loading DDS files. DO NOT MODIFY!
  109. //
  110. // ----------------------------------------------------------------------------
  111. struct LegacyDDSURFACEDESC2 {
  112. unsigned Size;
  113. unsigned Flags;
  114. unsigned Height;
  115. unsigned Width;
  116. union
  117. {
  118. unsigned Pitch;
  119. unsigned LinearSize;
  120. };
  121. union
  122. {
  123. unsigned BackBufferCount;
  124. unsigned Depth; // added depth for volume textures
  125. };
  126. union
  127. {
  128. unsigned MipMapCount;
  129. unsigned RefreshRate;
  130. };
  131. unsigned AlphaBitDepth;
  132. unsigned Reserved;
  133. void* Surface;
  134. union
  135. {
  136. LegacyDDCOLORKEY CKDestOverlay;
  137. unsigned EmptyFaceColor;
  138. };
  139. LegacyDDCOLORKEY CKDestBlt;
  140. LegacyDDCOLORKEY CKSrcOverlay;
  141. LegacyDDCOLORKEY CKSrcBlt;
  142. LegacyDDPIXELFORMAT PixelFormat;
  143. LegacyDDSCAPS2 Caps;
  144. unsigned TextureStage;
  145. };
  146. enum DDSType
  147. {
  148. DDS_TEXTURE,
  149. DDS_CUBEMAP,
  150. DDS_VOLUME
  151. };
  152. // ----------------------------------------------------------------------------
  153. //
  154. // Utility class for loading DDS files. Simply create an instance of the class
  155. // locally, call Load() and use the copy functions to retrieve the surface.
  156. // The class handles conversion of the surface to equal compressed formats
  157. // and all non-compressed formats. the compressed DXTn formats can't be cross-
  158. // converted except for DXT1 which can be converted to DXT2 (this feature is
  159. // needed as the NVidia cards have problems with DXT1).
  160. //
  161. // ----------------------------------------------------------------------------
  162. class DDSFileClass
  163. {
  164. unsigned Width;
  165. unsigned Height;
  166. unsigned Depth;
  167. unsigned FullWidth;
  168. unsigned FullHeight;
  169. unsigned FullDepth;
  170. unsigned MipLevels;
  171. unsigned long DateTime;
  172. unsigned ReductionFactor;
  173. unsigned char* DDSMemory;
  174. WW3DFormat Format;
  175. DDSType Type;
  176. unsigned* LevelSizes;
  177. unsigned* LevelOffsets;
  178. unsigned CubeFaceSize;
  179. LegacyDDSURFACEDESC2 SurfaceDesc;
  180. char Name[256];
  181. static unsigned Calculate_DXTC_Surface_Size(unsigned width, unsigned height, WW3DFormat format);
  182. public:
  183. // You can pass the name in .tga or .dds format, the class will automatically try and load .dds file.
  184. // Note that creating the object will only give you image info - call Load() to load the surfaces.
  185. DDSFileClass(const char* name,unsigned reduction_factor);
  186. ~DDSFileClass();
  187. unsigned Get_Width(unsigned level) const;
  188. unsigned Get_Height(unsigned level) const;
  189. unsigned Get_Depth(unsigned level) const;
  190. unsigned Get_Full_Width() const { return FullWidth; } // Get the width of level 0 of non-reduced texture
  191. unsigned Get_Full_Height() const { return FullHeight; } // Get the height of level 0 of non-reduced texture
  192. unsigned Get_Full_Depth() const { return FullDepth; }
  193. unsigned long Get_Date_Time() const { return DateTime; }
  194. unsigned Get_Mip_Level_Count() const { return MipLevels; }
  195. const unsigned char* Get_Memory_Pointer(unsigned level) const;
  196. unsigned Get_Level_Size(unsigned level) const;
  197. WW3DFormat Get_Format() const { return Format; }
  198. DDSType Get_Type() const { return Type; }
  199. // Copy pixels to the destination surface.
  200. void Copy_Level_To_Surface(unsigned level,IDirect3DSurface8* d3d_surface,const Vector3& hsv_shift=Vector3(0.0f,0.0f,0.0f));
  201. void Copy_Level_To_Surface(
  202. unsigned level,
  203. WW3DFormat dest_format,
  204. unsigned dest_width,
  205. unsigned dest_height,
  206. unsigned char* dest_surface,
  207. unsigned dest_pitch,
  208. const Vector3& hsv_shift=Vector3(0.0f,0.0f,0.0f));
  209. // cube map
  210. const unsigned char* Get_CubeMap_Memory_Pointer(unsigned face, unsigned level) const;
  211. void Copy_CubeMap_Level_To_Surface
  212. (
  213. unsigned face,
  214. unsigned level,
  215. WW3DFormat dest_format,
  216. unsigned width,
  217. unsigned height,
  218. unsigned char* surf,
  219. unsigned pitch,
  220. const Vector3& hsv_shift=Vector3(0.0f,0.0f,0.0f)
  221. );
  222. // volume texture
  223. const unsigned char* Get_Volume_Memory_Pointer(unsigned level) const;
  224. void Copy_Volume_Level_To_Surface
  225. (
  226. unsigned level,
  227. unsigned depth,
  228. WW3DFormat dest_format,
  229. unsigned width,
  230. unsigned height,
  231. unsigned char* vol,
  232. unsigned row_pitch,
  233. unsigned slice_pitch,
  234. const Vector3& hsv_shift=Vector3(0.0f,0.0f,0.0f)
  235. );
  236. // Get pixel in A8R8G8B8 format. This isn't the fastest possible way of reading data from DDS.
  237. unsigned Get_Pixel(unsigned level,unsigned x,unsigned y) const;
  238. // Uncompress one 4x4 block from the compressed image.
  239. // Returns: true if block contained alpha, false is not
  240. // Note: Destination can't be DXT or paletted surface!
  241. bool Get_4x4_Block(
  242. unsigned char* dest_ptr, // Destination surface pointer
  243. unsigned dest_pitch, // Destination surface pitch, in bytes
  244. WW3DFormat dest_format, // Destination surface format, A8R8G8B8 is fastest
  245. unsigned level, // DDS mipmap level to copy from
  246. unsigned source_x, // DDS x offset to copy from, must be aligned by 4!
  247. unsigned source_y, // DDS y offset to copy from, must be aligned by 4!
  248. const Vector3& hsv_shift=Vector3(0.0f,0.0f,0.0f)) const;
  249. bool Load();
  250. bool Is_Available() const { return !!LevelSizes; }
  251. };
  252. // ----------------------------------------------------------------------------
  253. #endif