LCOV - code coverage report
Current view: top level - source/lib/tex - tex_dds.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 203 243 83.5 %
Date: 2023-01-19 00:18:29 Functions: 20 22 90.9 %

          Line data    Source code
       1             : /* Copyright (C) 2022 Wildfire Games.
       2             :  *
       3             :  * Permission is hereby granted, free of charge, to any person obtaining
       4             :  * a copy of this software and associated documentation files (the
       5             :  * "Software"), to deal in the Software without restriction, including
       6             :  * without limitation the rights to use, copy, modify, merge, publish,
       7             :  * distribute, sublicense, and/or sell copies of the Software, and to
       8             :  * permit persons to whom the Software is furnished to do so, subject to
       9             :  * the following conditions:
      10             :  *
      11             :  * The above copyright notice and this permission notice shall be included
      12             :  * in all copies or substantial portions of the Software.
      13             :  *
      14             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      15             :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      16             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      17             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      18             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      19             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      20             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      21             :  */
      22             : 
      23             : /*
      24             :  * DDS (DirectDraw Surface) codec.
      25             :  */
      26             : 
      27             : #include "precompiled.h"
      28             : 
      29             : #include "lib/byte_order.h"
      30             : #include "lib/bits.h"
      31             : #include "lib/timer.h"
      32             : #include "lib/allocators/shared_ptr.h"
      33             : #include "tex_codec.h"
      34             : 
      35             : 
      36             : // NOTE: the convention is bottom-up for DDS, but there's no way to tell.
      37             : 
      38             : 
      39             : //-----------------------------------------------------------------------------
      40             : // S3TC decompression
      41             : //-----------------------------------------------------------------------------
      42             : 
      43             : // note: this code may not be terribly efficient. it's only used to
      44             : // emulate hardware S3TC support - if that isn't available, performance
      45             : // will suffer anyway due to increased video memory usage.
      46             : 
      47             : 
      48             : // for efficiency, we precalculate as much as possible about a block
      49             : // and store it here.
      50             : class S3tcBlock
      51             : {
      52             : public:
      53         521 :     S3tcBlock(size_t dxt, const u8* RESTRICT block)
      54         521 :         : m_Dxt(dxt)
      55             :     {
      56             :         // (careful, 'dxt != 1' doesn't work - there's also DXT1a)
      57         521 :         const u8* a_block = block;
      58         521 :         const u8* c_block = (dxt == 3 || dxt == 5)? block+8 : block;
      59             : 
      60         521 :         PrecalculateAlpha(dxt, a_block);
      61         521 :         PrecalculateColor(dxt, c_block);
      62         521 :     }
      63             : 
      64        8336 :     void WritePixel(size_t pixel_idx, u8* RESTRICT out) const
      65             :     {
      66        8336 :         ENSURE(pixel_idx < 16);
      67             : 
      68             :         // pixel index -> color selector (2 bit) -> color
      69        8336 :         const size_t c_selector = access_bit_tbl(c_selectors, pixel_idx, 2);
      70       33344 :         for(int i = 0; i < 3; i++)
      71       25008 :             out[i] = (u8)c[c_selector][i];
      72             : 
      73             :         // if no alpha, done
      74        8336 :         if(m_Dxt == 1)
      75        4240 :             return;
      76             : 
      77             :         size_t a;
      78        4096 :         if(m_Dxt == 3)
      79             :         {
      80             :             // table of 4-bit alpha entries
      81           0 :             a = access_bit_tbl(a_bits, pixel_idx, 4);
      82           0 :             a |= a << 4; // expand to 8 bits (replicate high into low!)
      83             :         }
      84        4096 :         else if(m_Dxt == 5)
      85             :         {
      86             :             // pixel index -> alpha selector (3 bit) -> alpha
      87        4096 :             const size_t a_selector = access_bit_tbl(a_bits, pixel_idx, 3);
      88        4096 :             a = dxt5_a_tbl[a_selector];
      89             :         }
      90             :         // (m_Dxt == DXT1A)
      91             :         else
      92           0 :             a = c[c_selector][A];
      93        4096 :         out[A] = (u8)(a & 0xFF);
      94             :     }
      95             : 
      96             : private:
      97             :     // pixel colors are stored as size_t[4]. size_t rather than u8 protects from
      98             :     // overflow during calculations, and padding to an even size is a bit
      99             :     // more efficient (even though we don't need the alpha component).
     100             :     enum RGBA { R, G, B, A };
     101             : 
     102         690 :     static inline void mix_2_3(size_t dst[4], size_t c0[4], size_t c1[4])
     103             :     {
     104         690 :         for(int i = 0; i < 3; i++) dst[i] = (c0[i]*2 + c1[i] + 1)/3;
     105         690 :     }
     106             : 
     107         176 :     static inline void mix_avg(size_t dst[4], size_t c0[4], size_t c1[4])
     108             :     {
     109         176 :         for(int i = 0; i < 3; i++) dst[i] = (c0[i]+c1[i])/2;
     110         176 :     }
     111             : 
     112             :     template<typename T>
     113       12432 :     static inline size_t access_bit_tbl(T tbl, size_t idx, size_t bit_width)
     114             :     {
     115       12432 :         size_t val = (tbl >> (idx*bit_width)) & bit_mask<T>(bit_width);
     116       12432 :         return val;
     117             :     }
     118             : 
     119             :     // extract a range of bits and expand to 8 bits (by replicating
     120             :     // MS bits - see http://www.mindcontrol.org/~hplus/graphics/expand-bits.html ;
     121             :     // this is also the algorithm used by graphics cards when decompressing S3TC).
     122             :     // used to convert 565 to 32bpp RGB.
     123        3126 :     static inline size_t unpack_to_8(u16 c, size_t bits_below, size_t num_bits)
     124             :     {
     125        3126 :         const size_t num_filler_bits = 8-num_bits;
     126        3126 :         const size_t field = (size_t)bits(c, bits_below, bits_below+num_bits-1);
     127        3126 :         const size_t filler = field >> (num_bits-num_filler_bits);
     128        3126 :         return (field << num_filler_bits) | filler;
     129             :     }
     130             : 
     131         521 :     void PrecalculateAlpha(size_t dxt, const u8* RESTRICT a_block)
     132             :     {
     133             :         // read block contents
     134         521 :         const u8 a0 = a_block[0], a1 = a_block[1];
     135         521 :         a_bits = read_le64(a_block);    // see below
     136             : 
     137         521 :         if(dxt == 5)
     138             :         {
     139             :             // skip a0,a1 bytes (data is little endian)
     140         256 :             a_bits >>= 16;
     141             : 
     142         256 :             const bool is_dxt5_special_combination = (a0 <= a1);
     143         256 :             u8* a = dxt5_a_tbl; // shorthand
     144         256 :             if(is_dxt5_special_combination)
     145             :             {
     146         182 :                 a[0] = a0;
     147         182 :                 a[1] = a1;
     148         182 :                 a[2] = (4*a0 + 1*a1 + 2)/5;
     149         182 :                 a[3] = (3*a0 + 2*a1 + 2)/5;
     150         182 :                 a[4] = (2*a0 + 3*a1 + 2)/5;
     151         182 :                 a[5] = (1*a0 + 4*a1 + 2)/5;
     152         182 :                 a[6] = 0;
     153         182 :                 a[7] = 255;
     154             :             }
     155             :             else
     156             :             {
     157          74 :                 a[0] = a0;
     158          74 :                 a[1] = a1;
     159          74 :                 a[2] = (6*a0 + 1*a1 + 3)/7;
     160          74 :                 a[3] = (5*a0 + 2*a1 + 3)/7;
     161          74 :                 a[4] = (4*a0 + 3*a1 + 3)/7;
     162          74 :                 a[5] = (3*a0 + 4*a1 + 3)/7;
     163          74 :                 a[6] = (2*a0 + 5*a1 + 3)/7;
     164          74 :                 a[7] = (1*a0 + 6*a1 + 3)/7;
     165             :             }
     166             :         }
     167         521 :     }
     168             : 
     169             : 
     170         521 :     void PrecalculateColor(size_t dxt, const u8* RESTRICT c_block)
     171             :     {
     172             :         // read block contents
     173             :         // .. S3TC reference colors (565 format). the color table is generated
     174             :         //    from some combination of these, depending on their ordering.
     175             :         u16 rc[2];
     176        1563 :         for(int i = 0; i < 2; i++)
     177        1042 :             rc[i] = read_le16(c_block + 2*i);
     178             :         // .. table of 2-bit color selectors
     179         521 :         c_selectors = read_le32(c_block+4);
     180             : 
     181         521 :         const bool is_dxt1_special_combination = (dxt == 1 || dxt == DXT1A) && rc[0] <= rc[1];
     182             : 
     183             :         // c0 and c1 are the values of rc[], converted to 32bpp
     184        1563 :         for(int i = 0; i < 2; i++)
     185             :         {
     186        1042 :             c[i][R] = unpack_to_8(rc[i], 11, 5);
     187        1042 :             c[i][G] = unpack_to_8(rc[i],  5, 6);
     188        1042 :             c[i][B] = unpack_to_8(rc[i],  0, 5);
     189             :         }
     190             : 
     191             :         // c2 and c3 are combinations of c0 and c1:
     192         521 :         if(is_dxt1_special_combination)
     193             :         {
     194         176 :             mix_avg(c[2], c[0], c[1]);          // c2 = (c0+c1)/2
     195         176 :             for(int i = 0; i < 3; i++) c[3][i] = 0;  // c3 = black
     196         176 :             c[3][A] = (dxt == DXT1A)? 0 : 255;      // (transparent iff DXT1a)
     197             :         }
     198             :         else
     199             :         {
     200         345 :             mix_2_3(c[2], c[0], c[1]);          // c2 = 2/3*c0 + 1/3*c1
     201         345 :             mix_2_3(c[3], c[1], c[0]);          // c3 = 1/3*c0 + 2/3*c1
     202             :         }
     203         521 :     }
     204             : 
     205             :     // the 4 color choices for each pixel (RGBA)
     206             :     size_t c[4][4]; // c[i][RGBA_component]
     207             : 
     208             :     // (DXT5 only) the 8 alpha choices
     209             :     u8 dxt5_a_tbl[8];
     210             : 
     211             :     // alpha block; interpretation depends on dxt.
     212             :     u64 a_bits;
     213             : 
     214             :     // table of 2-bit color selectors
     215             :     u32 c_selectors;
     216             : 
     217             :     size_t m_Dxt;
     218             : };
     219             : 
     220             : 
     221             : struct S3tcDecompressInfo
     222             : {
     223             :     size_t dxt;
     224             :     size_t s3tc_block_size;
     225             :     size_t out_Bpp;
     226             :     u8* out;
     227             : };
     228             : 
     229          10 : static void s3tc_decompress_level(size_t UNUSED(level), size_t level_w, size_t level_h,
     230             :     const u8* RESTRICT level_data, size_t level_data_size, void* RESTRICT cbData)
     231             : {
     232          10 :     S3tcDecompressInfo* di = (S3tcDecompressInfo*)cbData;
     233          10 :     const size_t dxt             = di->dxt;
     234          10 :     const size_t s3tc_block_size = di->s3tc_block_size;
     235             : 
     236             :     // note: 1x1 images are legitimate (e.g. in mipmaps). they report their
     237             :     // width as such for glTexImage, but the S3TC data is padded to
     238             :     // 4x4 pixel block boundaries.
     239          10 :     const size_t blocks_w = DivideRoundUp(level_w, size_t(4));
     240          10 :     const size_t blocks_h = DivideRoundUp(level_h, size_t(4));
     241          10 :     const u8* s3tc_data = level_data;
     242          10 :     ENSURE(level_data_size % s3tc_block_size == 0);
     243             : 
     244          50 :     for(size_t block_y = 0; block_y < blocks_h; block_y++)
     245             :     {
     246         561 :         for(size_t block_x = 0; block_x < blocks_w; block_x++)
     247             :         {
     248         521 :             S3tcBlock block(dxt, s3tc_data);
     249         521 :             s3tc_data += s3tc_block_size;
     250             : 
     251         521 :             size_t pixel_idx = 0;
     252        2605 :             for(int y = 0; y < 4; y++)
     253             :             {
     254             :                 // this is ugly, but advancing after x, y and block_y loops
     255             :                 // is no better.
     256        2084 :                 u8* out = (u8*)di->out + ((block_y*4+y)*blocks_w*4 + block_x*4) * di->out_Bpp;
     257       10420 :                 for(int x = 0; x < 4; x++)
     258             :                 {
     259        8336 :                     block.WritePixel(pixel_idx, out);
     260        8336 :                     out += di->out_Bpp;
     261        8336 :                     pixel_idx++;
     262             :                 }
     263             :             }
     264             :         }
     265             :     }
     266             : 
     267          10 :     ENSURE(s3tc_data == level_data + level_data_size);
     268          10 :     di->out += blocks_w*blocks_h * 16 * di->out_Bpp;
     269          10 : }
     270             : 
     271             : 
     272             : // decompress the given image (which is known to be stored as DXTn)
     273             : // effectively in-place. updates Tex fields.
     274          10 : static Status s3tc_decompress(Tex* t)
     275             : {
     276             :     // alloc new image memory
     277             :     // notes:
     278             :     // - dxt == 1 is the only non-alpha case.
     279             :     // - adding or stripping alpha channels during transform is not
     280             :     //   our job; we merely output the same pixel format as given
     281             :     //   (tex.cpp's plain transform could cover it, if ever needed).
     282          10 :     const size_t dxt = t->m_Flags & TEX_DXT;
     283          10 :     const size_t out_bpp = (dxt != 1)? 32 : 24;
     284          10 :     const size_t out_size = t->img_size() * out_bpp / t->m_Bpp;
     285          20 :     std::shared_ptr<u8> decompressedData;
     286          10 :     AllocateAligned(decompressedData, out_size, g_PageSize);
     287             : 
     288          10 :     const size_t s3tc_block_size = (dxt == 3 || dxt == 5)? 16 : 8;
     289          10 :     S3tcDecompressInfo di = { dxt, s3tc_block_size, out_bpp/8, decompressedData.get() };
     290          10 :     const u8* s3tc_data = t->get_data();
     291          10 :     const int levels_to_skip = (t->m_Flags & TEX_MIPMAPS)? 0 : TEX_BASE_LEVEL_ONLY;
     292          10 :     tex_util_foreach_mipmap(t->m_Width, t->m_Height, t->m_Bpp, s3tc_data, levels_to_skip, 4, s3tc_decompress_level, &di);
     293          10 :     t->m_Data = decompressedData;
     294          10 :     t->m_DataSize = out_size;
     295          10 :     t->m_Ofs = 0;
     296          10 :     t->m_Bpp = out_bpp;
     297          10 :     t->m_Flags &= ~TEX_DXT;
     298          20 :     return INFO::OK;
     299             : }
     300             : 
     301             : 
     302             : //-----------------------------------------------------------------------------
     303             : // DDS file format
     304             : //-----------------------------------------------------------------------------
     305             : 
     306             : // bit values and structure definitions taken from
     307             : // https://docs.microsoft.com/en-us/windows/win32/direct3ddds/dx-graphics-dds-reference
     308             : 
     309             : #pragma pack(push, 1)
     310             : 
     311             : // DDS_PIXELFORMAT.dwFlags
     312             : 
     313             : // This is used to distinguish RGBA from RGB and DXT1a from DXT1.
     314             : // we've seen some DXT3 files that don't have this set (which is nonsense;
     315             : // any image lacking alpha should be stored as DXT1).
     316             : #define DDPF_ALPHAPIXELS 0x00000001
     317             : // DDPF_ALPHA is used for uncompressed 8bpp greyscale, in which the data
     318             : // is stored in the alpha mask.
     319             : #define DDPF_ALPHA       0x00000002
     320             : #define DDPF_FOURCC      0x00000004
     321             : #define DDPF_RGB         0x00000040
     322             : 
     323             : struct DDS_PIXELFORMAT
     324             : {
     325             :     u32 dwSize;                       // size of structure (32)
     326             :     u32 dwFlags;                      // indicates which fields are valid
     327             :     u32 dwFourCC;                     // (DDPF_FOURCC) FOURCC code, "DXTn"
     328             :     u32 dwRGBBitCount;                // (DDPF_RGB) bits per pixel
     329             :     u32 dwRBitMask;
     330             :     u32 dwGBitMask;
     331             :     u32 dwBBitMask;
     332             :     u32 dwABitMask;                   // (DDPF_ALPHA or DDPF_ALPHAPIXELS)
     333             : };
     334             : 
     335             : 
     336             : // DDS_HEADER.dwFlags (none are optional)
     337             : #define DDSD_CAPS        0x00000001
     338             : #define DDSD_HEIGHT      0x00000002
     339             : #define DDSD_WIDTH       0x00000004
     340             : #define DDSD_PITCH       0x00000008 // used when texture is uncompressed
     341             : #define DDSD_PIXELFORMAT 0x00001000
     342             : #define DDSD_MIPMAPCOUNT 0x00020000
     343             : #define DDSD_LINEARSIZE  0x00080000 // used when texture is compressed
     344             : #define DDSD_DEPTH       0x00800000
     345             : 
     346             : // DDS_HEADER.dwCaps
     347             : #define DDSCAPS_MIPMAP   0x00400000 // optional
     348             : #define DDSCAPS_TEXTURE  0x00001000 // required
     349             : 
     350             : struct DDS_HEADER
     351             : {
     352             :     // (preceded by the FOURCC "DDS ")
     353             :     u32 dwSize;                    // size of structure (124)
     354             :     u32 dwFlags;                   // indicates which fields are valid
     355             :     u32 dwHeight;                  // (DDSD_HEIGHT) height of main image (pixels)
     356             :     u32 dwWidth;                   // (DDSD_WIDTH ) width  of main image (pixels)
     357             :     u32 dwPitchOrLinearSize;       // (DDSD_LINEARSIZE) size [bytes] of top level
     358             :                                    // (DDSD_PITCH) bytes per row (%4 = 0)
     359             :     u32 dwDepth;                   // (DDSD_DEPTH) vol. textures: vol. depth
     360             :     u32 dwMipMapCount;             // (DDSD_MIPMAPCOUNT) total # levels
     361             :     u32 dwReserved1[11];           // reserved
     362             :     DDS_PIXELFORMAT ddpf;          // (DDSD_PIXELFORMAT) surface description
     363             :     u32 dwCaps;                    // (DDSD_CAPS) misc. surface flags
     364             :     u32 dwCaps2;
     365             :     u32 dwCaps3;
     366             :     u32 dwCaps4;
     367             :     u32 dwReserved2;               // reserved
     368             : };
     369             : 
     370             : #pragma pack(pop)
     371             : 
     372             : 
     373          30 : static bool is_valid_dxt(size_t dxt)
     374             : {
     375          30 :     switch(dxt)
     376             :     {
     377          30 :     case 0:
     378             :     case 1:
     379             :     case DXT1A:
     380             :     case 3:
     381             :     case 5:
     382          30 :         return true;
     383           0 :     default:
     384           0 :         return false;
     385             :     }
     386             : }
     387             : 
     388             : 
     389             : // extract all information from DDS pixel format and store in bpp, flags.
     390             : // pf points to the DDS file's header; all fields must be endian-converted
     391             : // before use.
     392             : // output parameters invalid on failure.
     393          10 : static Status decode_pf(const DDS_PIXELFORMAT* pf, size_t& bpp, size_t& flags)
     394             : {
     395          10 :     bpp = 0;
     396          10 :     flags = 0;
     397             : 
     398             :     // check struct size
     399          10 :     if(read_le32(&pf->dwSize) != sizeof(DDS_PIXELFORMAT))
     400           0 :         return ERR::TEX_INVALID_SIZE;
     401             : 
     402             :     // determine type
     403          10 :     const size_t pf_flags = (size_t)read_le32(&pf->dwFlags);
     404             :     // .. uncompressed RGB/RGBA
     405          10 :     if(pf_flags & DDPF_RGB)
     406             :     {
     407           1 :         const size_t pf_bpp    = (size_t)read_le32(&pf->dwRGBBitCount);
     408           1 :         const size_t pf_r_mask = (size_t)read_le32(&pf->dwRBitMask);
     409           1 :         const size_t pf_g_mask = (size_t)read_le32(&pf->dwGBitMask);
     410           1 :         const size_t pf_b_mask = (size_t)read_le32(&pf->dwBBitMask);
     411           1 :         const size_t pf_a_mask = (size_t)read_le32(&pf->dwABitMask);
     412             : 
     413             :         // (checked below; must be set in case below warning is to be
     414             :         // skipped)
     415           1 :         bpp = pf_bpp;
     416             : 
     417           1 :         if(pf_flags & DDPF_ALPHAPIXELS)
     418             :         {
     419             :             // something weird other than RGBA or BGRA
     420           1 :             if(pf_a_mask != 0xFF000000)
     421           0 :                 WARN_RETURN(ERR::TEX_FMT_INVALID);
     422           1 :             flags |= TEX_ALPHA;
     423             :         }
     424             : 
     425             :         // make sure component ordering is 0xBBGGRR = RGB (see below)
     426           1 :         if(pf_r_mask != 0xFF || pf_g_mask != 0xFF00 || pf_b_mask != 0xFF0000)
     427             :         {
     428             :             // DDS_PIXELFORMAT in theory supports any ordering of R,G,B,A.
     429             :             // we need to upload to OpenGL, which can only receive BGR(A) or
     430             :             // RGB(A). the former still requires conversion (done by driver),
     431             :             // so it's slower. since the very purpose of supporting uncompressed
     432             :             // DDS is storing images in a format that requires no processing,
     433             :             // we do not allow any weird orderings that require runtime work.
     434             :             // instead, the artists must export with the correct settings.
     435           0 :             return ERR::TEX_FMT_INVALID;
     436             :         }
     437             : 
     438           1 :         RETURN_STATUS_IF_ERR(tex_validate_plain_format(bpp, (int)flags));
     439             :     }
     440             :     // .. uncompressed 8bpp greyscale
     441           9 :     else if(pf_flags & DDPF_ALPHA)
     442             :     {
     443           0 :         const size_t pf_bpp    = (size_t)read_le32(&pf->dwRGBBitCount);
     444           0 :         const size_t pf_a_mask = (size_t)read_le32(&pf->dwABitMask);
     445             : 
     446           0 :         bpp = pf_bpp;
     447             : 
     448           0 :         if(pf_bpp != 8)
     449           0 :             return ERR::TEX_FMT_INVALID;
     450             : 
     451           0 :         if(pf_a_mask != 0xFF)
     452           0 :             return ERR::TEX_FMT_INVALID;
     453           0 :         flags |= TEX_GREY;
     454             : 
     455           0 :         RETURN_STATUS_IF_ERR(tex_validate_plain_format(bpp, (int)flags));
     456             :     }
     457             :     // .. compressed
     458           9 :     else if(pf_flags & DDPF_FOURCC)
     459             :     {
     460             :         // set effective bpp and store DXT format in flags & TEX_DXT.
     461             :         // no endian conversion necessary - FOURCC() takes care of that.
     462           9 :         switch(pf->dwFourCC)
     463             :         {
     464           8 :         case FOURCC('D','X','T','1'):
     465           8 :             bpp = 4;
     466           8 :             if(pf_flags & DDPF_ALPHAPIXELS)
     467           0 :                 flags |= DXT1A | TEX_ALPHA;
     468             :             else
     469           8 :                 flags |= 1;
     470           8 :             break;
     471           0 :         case FOURCC('D','X','T','3'):
     472           0 :             bpp = 8;
     473           0 :             flags |= 3;
     474           0 :             flags |= TEX_ALPHA; // see DDPF_ALPHAPIXELS decl
     475           0 :             break;
     476           1 :         case FOURCC('D','X','T','5'):
     477           1 :             bpp = 8;
     478           1 :             flags |= 5;
     479           1 :             flags |= TEX_ALPHA; // see DDPF_ALPHAPIXELS decl
     480           1 :             break;
     481             : 
     482           0 :         default:
     483           0 :             return ERR::TEX_FMT_INVALID;
     484             :         }
     485             :     }
     486             :     // .. neither uncompressed nor compressed - invalid
     487             :     else
     488           0 :         return ERR::TEX_FMT_INVALID;
     489             : 
     490          10 :     return INFO::OK;
     491             : }
     492             : 
     493             : 
     494             : // extract all information from DDS header and store in w, h, bpp, flags.
     495             : // sd points to the DDS file's header; all fields must be endian-converted
     496             : // before use.
     497             : // output parameters invalid on failure.
     498          10 : static Status decode_sd(const DDS_HEADER* sd, size_t& w, size_t& h, size_t& bpp, size_t& flags)
     499             : {
     500             :     // check header size
     501          10 :     if(read_le32(&sd->dwSize) != sizeof(*sd))
     502           0 :         return ERR::CORRUPTED;
     503             : 
     504             :     // flags (indicate which fields are valid)
     505          10 :     const size_t sd_flags = (size_t)read_le32(&sd->dwFlags);
     506             :     // .. not all required fields are present
     507             :     // note: we can't guess dimensions - the image may not be square.
     508          10 :     const size_t sd_req_flags = DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT;
     509          10 :     if((sd_flags & sd_req_flags) != sd_req_flags)
     510           0 :         return ERR::TEX_INCOMPLETE_HEADER;
     511             : 
     512             :     // image dimensions
     513          10 :     h = (size_t)read_le32(&sd->dwHeight);
     514          10 :     w = (size_t)read_le32(&sd->dwWidth);
     515             : 
     516             :     // pixel format
     517          10 :     RETURN_STATUS_IF_ERR(decode_pf(&sd->ddpf, bpp, flags));
     518             : 
     519             :     // if the image is not aligned with the S3TC block size, it is stored
     520             :     // with extra pixels on the bottom left to fill up the space, so we need
     521             :     // to account for those when calculating how big it should be
     522             :     size_t stored_h, stored_w;
     523          10 :     if(flags & TEX_DXT)
     524             :     {
     525           9 :         stored_h = Align<4>(h);
     526           9 :         stored_w = Align<4>(w);
     527             :     }
     528             :     else
     529             :     {
     530           1 :         stored_h = h;
     531           1 :         stored_w = w;
     532             :     }
     533             : 
     534             :     // verify pitch or linear size, if given
     535          10 :     const size_t pitch = stored_w*bpp/8;
     536          10 :     const size_t sd_pitch_or_size = (size_t)read_le32(&sd->dwPitchOrLinearSize);
     537          10 :     if(sd_flags & DDSD_PITCH)
     538             :     {
     539           1 :         if(sd_pitch_or_size != Align<4>(pitch))
     540           0 :             return ERR::CORRUPTED;
     541             :     }
     542          10 :     if(sd_flags & DDSD_LINEARSIZE)
     543             :     {
     544             :         // some DDS tools mistakenly store the total size of all levels,
     545             :         // so allow values close to that as well
     546           9 :         const ssize_t totalSize = ssize_t(pitch*stored_h*1.333333f);
     547           9 :         if(sd_pitch_or_size != pitch*stored_h && std::abs(ssize_t(sd_pitch_or_size)-totalSize) > 64)
     548           0 :             return ERR::CORRUPTED;
     549             :     }
     550             :     // note: both flags set would be invalid; no need to check for that,
     551             :     // though, since one of the above tests would fail.
     552             : 
     553             :     // mipmaps
     554          10 :     if(sd_flags & DDSD_MIPMAPCOUNT)
     555             :     {
     556           7 :         const size_t mipmap_count = (size_t)read_le32(&sd->dwMipMapCount);
     557           7 :         if(mipmap_count)
     558             :         {
     559             :             // mipmap chain is incomplete
     560             :             // note: DDS includes the base level in its count, hence +1.
     561           7 :             if(mipmap_count != ceil_log2(std::max(w,h))+1)
     562           0 :                 return ERR::TEX_FMT_INVALID;
     563           7 :             flags |= TEX_MIPMAPS;
     564             :         }
     565             :     }
     566             : 
     567             :     // check for volume textures
     568          10 :     if(sd_flags & DDSD_DEPTH)
     569             :     {
     570           0 :         const size_t depth = (size_t)read_le32(&sd->dwDepth);
     571           0 :         if(depth)
     572           0 :             return ERR::NOT_SUPPORTED;
     573             :     }
     574             : 
     575             :     // check caps
     576             :     // .. this is supposed to be set, but don't bail if not (pointless)
     577          10 :     if (!(sd->dwCaps & DDSCAPS_TEXTURE))
     578           0 :         return ERR::CORRUPTED;
     579             :     // .. sanity check: warn if mipmap flag not set (don't bail if not
     580             :     // because we've already made the decision).
     581          10 :     const bool mipmap_cap = (sd->dwCaps & DDSCAPS_MIPMAP) != 0;
     582          10 :     const bool mipmap_flag = (flags & TEX_MIPMAPS) != 0;
     583          10 :     if (mipmap_cap != mipmap_flag)
     584           0 :         return ERR::CORRUPTED;
     585             :     // note: we do not check for cubemaps and volume textures (not supported)
     586             :     // because the file may still have useful data we can read.
     587             : 
     588          10 :     return INFO::OK;
     589             : }
     590             : 
     591             : 
     592             : //-----------------------------------------------------------------------------
     593             : 
     594          14 : bool TexCodecDds::is_hdr(const u8* file) const
     595             : {
     596          14 :     return *(u32*)file == FOURCC('D','D','S',' ');
     597             : }
     598             : 
     599             : 
     600           0 : bool TexCodecDds::is_ext(const OsPath& extension) const
     601             : {
     602           0 :     return extension == L".dds";
     603             : }
     604             : 
     605             : 
     606          20 : size_t TexCodecDds::hdr_size(const u8* UNUSED(file)) const
     607             : {
     608          20 :     return 4+sizeof(DDS_HEADER);
     609             : }
     610             : 
     611             : 
     612          10 : Status TexCodecDds::decode(u8* RESTRICT data, size_t UNUSED(size), Tex* RESTRICT t) const
     613             : {
     614          10 :     const DDS_HEADER* sd = (const DDS_HEADER*)(data+4);
     615          10 :     RETURN_STATUS_IF_ERR(decode_sd(sd, t->m_Width, t->m_Height, t->m_Bpp, t->m_Flags));
     616          10 :     return INFO::OK;
     617             : }
     618             : 
     619             : 
     620           0 : Status TexCodecDds::encode(Tex* RESTRICT UNUSED(t), DynArray* RESTRICT UNUSED(da)) const
     621             : {
     622             :     // note: do not return ERR::NOT_SUPPORTED et al. because that would
     623             :     // break tex_write (which assumes either this, 0 or errors are returned).
     624           0 :     return INFO::TEX_CODEC_CANNOT_HANDLE;
     625             : }
     626             : 
     627             : 
     628           1 : TIMER_ADD_CLIENT(tc_dds_transform);
     629             : 
     630          30 : Status TexCodecDds::transform(Tex* t, size_t transforms) const
     631             : {
     632          60 :     TIMER_ACCRUE(tc_dds_transform);
     633             : 
     634          30 :     size_t mipmaps = t->m_Flags & TEX_MIPMAPS;
     635          30 :     size_t dxt = t->m_Flags & TEX_DXT;
     636          30 :     ENSURE(is_valid_dxt(dxt));
     637             : 
     638          30 :     const size_t transform_mipmaps = transforms & TEX_MIPMAPS;
     639          30 :     const size_t transform_dxt = transforms & TEX_DXT;
     640             :     // requesting removal of mipmaps
     641          30 :     if(mipmaps && transform_mipmaps)
     642             :     {
     643             :         // we don't need to actually change anything except the flag - the
     644             :         // mipmap levels will just be treated as trailing junk
     645           7 :         t->m_Flags &= ~TEX_MIPMAPS;
     646           7 :         return INFO::OK;
     647             :     }
     648             :     // requesting decompression
     649          23 :     if(dxt && transform_dxt)
     650             :     {
     651          10 :         RETURN_STATUS_IF_ERR(s3tc_decompress(t));
     652          10 :         return INFO::OK;
     653             :     }
     654             :     // both are DXT (unsupported; there are no flags we can change while
     655             :     // compressed) or requesting compression (not implemented) or
     656             :     // both not DXT (nothing we can do) - bail.
     657          13 :     return INFO::TEX_CODEC_CANNOT_HANDLE;
     658           3 : }

Generated by: LCOV version 1.13