ddsfile.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. ** Command & Conquer Renegade(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. #ifndef DDSFILE_H
  19. #define DDSFILE_H
  20. #if defined(_MSC_VER)
  21. #pragma once
  22. #endif
  23. #include "always.h"
  24. #include "ww3dformat.h"
  25. #include "wwstring.h"
  26. struct IDirect3DSurface8;
  27. // ----------------------------------------------------------------------------
  28. //
  29. // This structure represents the old DX7 color key structure. It is needed
  30. // LegacyDDSURFACEDESC which is needed when loading DDS files. DO NOT MODIFY!
  31. //
  32. // ----------------------------------------------------------------------------
  33. struct LegacyDDCOLORKEY
  34. {
  35. unsigned ColorSpaceLowValue;
  36. unsigned ColorSpaceHighValue;
  37. };
  38. // ----------------------------------------------------------------------------
  39. //
  40. // This structure represents the old DX7 CAPS2 structure. It is needed
  41. // LegacyDDSURFACEDESC which is needed when loading DDS files. DO NOT MODIFY!
  42. //
  43. // ----------------------------------------------------------------------------
  44. struct LegacyDDSCAPS2
  45. {
  46. unsigned Caps;
  47. unsigned Caps2;
  48. unsigned Caps3;
  49. unsigned Caps4;
  50. };
  51. // ----------------------------------------------------------------------------
  52. //
  53. // This structure represents the old DX7 pixel format structure. It is needed
  54. // LegacyDDSURFACEDESC which is needed when loading DDS files. DO NOT MODIFY!
  55. //
  56. // ----------------------------------------------------------------------------
  57. struct LegacyDDPIXELFORMAT
  58. {
  59. unsigned Size;
  60. unsigned Flags;
  61. unsigned FourCC;
  62. union
  63. {
  64. unsigned RGBBitCount;
  65. unsigned YUVBitCount;
  66. unsigned ZBufferBitDepth;
  67. unsigned AlphaBitDepth;
  68. unsigned LuminanceBitCount;
  69. unsigned BumpBitCount;
  70. };
  71. union
  72. {
  73. unsigned RBitMask;
  74. unsigned YBitMask;
  75. unsigned StencilBitDepth;
  76. unsigned LuminanceBitMask;
  77. unsigned BumpDuBitMask;
  78. };
  79. union
  80. {
  81. unsigned GBitMask;
  82. unsigned UBitMask;
  83. unsigned ZBitMask;
  84. unsigned BumpDvBitMask;
  85. };
  86. union
  87. {
  88. unsigned BBitMask;
  89. unsigned VBitMask;
  90. unsigned StencilBitMask;
  91. unsigned BumpLuminanceBitMask;
  92. };
  93. union
  94. {
  95. unsigned RGBAlphaBitMask;
  96. unsigned YUVAlphaBitMask;
  97. unsigned LuminanceAlphaBitMask;
  98. unsigned RGBZBitMask;
  99. unsigned YUVZBitMask;
  100. };
  101. };
  102. // ----------------------------------------------------------------------------
  103. //
  104. // This structure represents the old DX7 surface description structure.
  105. // It is needed when loading DDS files. DO NOT MODIFY!
  106. //
  107. // ----------------------------------------------------------------------------
  108. struct LegacyDDSURFACEDESC2 {
  109. unsigned Size;
  110. unsigned Flags;
  111. unsigned Height;
  112. unsigned Width;
  113. union
  114. {
  115. unsigned Pitch;
  116. unsigned LinearSize;
  117. };
  118. unsigned BackBufferCount;
  119. union
  120. {
  121. unsigned MipMapCount;
  122. unsigned RefreshRate;
  123. };
  124. unsigned AlphaBitDepth;
  125. unsigned Reserved;
  126. void* Surface;
  127. union
  128. {
  129. LegacyDDCOLORKEY CKDestOverlay;
  130. unsigned EmptyFaceColor;
  131. };
  132. LegacyDDCOLORKEY CKDestBlt;
  133. LegacyDDCOLORKEY CKSrcOverlay;
  134. LegacyDDCOLORKEY CKSrcBlt;
  135. LegacyDDPIXELFORMAT PixelFormat;
  136. LegacyDDSCAPS2 Caps;
  137. unsigned TextureStage;
  138. };
  139. // ----------------------------------------------------------------------------
  140. //
  141. // Utility class for loading DDS files. Simply create an instance of the class
  142. // locally, call Load() and use the copy functions to retrieve the surface.
  143. // The class handles conversion of the surface to equal compressed formats
  144. // and all non-compressed formats. the compressed DXTn formats can't be cross-
  145. // converted except for DXT1 which can be converted to DXT2 (this feature is
  146. // needed as the NVidia cards have problems with DXT1).
  147. //
  148. // ----------------------------------------------------------------------------
  149. class DDSFileClass
  150. {
  151. unsigned Width;
  152. unsigned Height;
  153. unsigned FullWidth;
  154. unsigned FullHeight;
  155. unsigned MipLevels;
  156. unsigned long DateTime;
  157. unsigned ReductionFactor;
  158. unsigned char* DDSMemory;
  159. WW3DFormat Format;
  160. unsigned* LevelSizes;
  161. unsigned* LevelOffsets;
  162. LegacyDDSURFACEDESC2 SurfaceDesc;
  163. char Name[256];
  164. static unsigned Calculate_DXTC_Surface_Size(unsigned width, unsigned height, WW3DFormat format);
  165. public:
  166. // You can pass the name in .tga or .dds format, the class will automatically try and load .dds file.
  167. // Note that creating the object will only give you image info - call Load() to load the surfaces.
  168. DDSFileClass(const char* name,unsigned reduction_factor);
  169. ~DDSFileClass();
  170. unsigned Get_Width(unsigned level) const;
  171. unsigned Get_Height(unsigned level) const;
  172. unsigned Get_Full_Width() const { return FullWidth; } // Get the width of level 0 of non-reduced texture
  173. unsigned Get_Full_Height() const { return FullHeight; } // Get the height of level 0 of non-reduced texture
  174. unsigned long Get_Date_Time() const { return DateTime; }
  175. unsigned Get_Mip_Level_Count() const { return MipLevels; }
  176. const unsigned char* Get_Memory_Pointer(unsigned level) const;
  177. unsigned Get_Level_Size(unsigned level) const;
  178. WW3DFormat Get_Format() const { return Format; }
  179. // Copy pixels to the destination surface.
  180. void Copy_Level_To_Surface(unsigned level,IDirect3DSurface8* d3d_surface);
  181. void Copy_Level_To_Surface(
  182. unsigned level,
  183. WW3DFormat dest_format,
  184. unsigned dest_width,
  185. unsigned dest_height,
  186. unsigned char* dest_surface,
  187. unsigned dest_pitch);
  188. // Get pixel in A8R8G8B8 format. This isn't the fastest possible way of reading data from DDS.
  189. unsigned Get_Pixel(unsigned level,unsigned x,unsigned y) const;
  190. // Uncompress one 4x4 block from the compressed image.
  191. // Returns: true if block contained alpha, false is not
  192. // Note: Destination can't be DXT or paletted surface!
  193. bool Get_4x4_Block(
  194. unsigned char* dest_ptr, // Destination surface pointer
  195. unsigned dest_pitch, // Destination surface pitch, in bytes
  196. WW3DFormat dest_format, // Destination surface format, A8R8G8B8 is fastest
  197. unsigned level, // DDS mipmap level to copy from
  198. unsigned source_x, // DDS x offset to copy from, must be aligned by 4!
  199. unsigned source_y) const; // DDS y offset to copy from, must be aligned by 4!
  200. bool Load();
  201. bool Is_Available() const { return !!LevelSizes; }
  202. };
  203. // ----------------------------------------------------------------------------
  204. #endif