LCOV - code coverage report
Current view: top level - source/lib/tex - tex_bmp.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 2 47 4.3 %
Date: 2026-12-03 01:19:39 Functions: 1 6 16.7 %

          Line data    Source code
       1             : /* Copyright (C) 2026 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             :  * Windows BMP codec
      25             :  */
      26             : 
      27             : #include "precompiled.h"
      28             : 
      29             : #include "tex_codec.h"
      30             : 
      31             : #include "lib/byte_order.h"
      32             : #include "lib/code_annotation.h"
      33             : #include "lib/debug.h"
      34             : #include "lib/os_path.h"
      35             : #include "lib/status.h"
      36             : #include "lib/tex/tex.h"
      37             : 
      38             : #include <cstdlib>
      39             : 
      40             : #pragma pack(push, 1)
      41             : 
      42             : struct BmpHeader
      43             : {
      44             :     // BITMAPFILEHEADER
      45             :     std::uint16_t bfType;           // "BM"
      46             :     std::uint32_t bfSize;           // of file
      47             :     std::uint16_t bfReserved1;
      48             :     std::uint16_t bfReserved2;
      49             :     std::uint32_t bfOffBits;        // offset to image data
      50             : 
      51             :     // BITMAPINFOHEADER
      52             :     std::uint32_t biSize;
      53             :     std::int32_t biWidth;
      54             :     std::int32_t biHeight;
      55             :     std::uint16_t biPlanes;
      56             :     std::uint16_t biBitCount;
      57             :     std::uint32_t biCompression;
      58             :     std::uint32_t biSizeImage;
      59             :     // the following are unused and zeroed when writing:
      60             :     std::int32_t biXPelsPerMeter;
      61             :     std::int32_t biYPelsPerMeter;
      62             :     std::uint32_t biClrUsed;
      63             :     std::uint32_t biClrImportant;
      64             : };
      65             : 
      66             : #pragma pack(pop)
      67             : 
      68             : #define BI_RGB 0        // biCompression
      69             : 
      70             : 
      71          13 : Status TexCodecBmp::transform(Tex*, size_t /*transforms*/) const
      72             : {
      73          13 :     return INFO::TEX_CODEC_CANNOT_HANDLE;
      74             : }
      75             : 
      76             : 
      77           0 : bool TexCodecBmp::is_hdr(const std::uint8_t* file) const
      78             : {
      79             :     // check header signature (bfType == "BM"?).
      80             :     // we compare single bytes to be endian-safe.
      81           0 :     return (file[0] == 'B' && file[1] == 'M');
      82             : }
      83             : 
      84             : 
      85           0 : bool TexCodecBmp::is_ext(const OsPath& extension) const
      86             : {
      87           0 :     return extension == L".bmp";
      88             : }
      89             : 
      90             : 
      91           0 : size_t TexCodecBmp::hdr_size(const std::uint8_t* file) const
      92             : {
      93           0 :     const size_t hdr_size = sizeof(BmpHeader);
      94           0 :     if(file)
      95             :     {
      96           0 :         BmpHeader* hdr = (BmpHeader*)file;
      97           0 :         const std::uint32_t ofs = read_le32(&hdr->bfOffBits);
      98           0 :         ENSURE(ofs >= hdr_size && "bmp_hdr_size invalid");
      99           0 :         return ofs;
     100             :     }
     101             :     return hdr_size;
     102             : }
     103             : 
     104             : 
     105             : // requirements: uncompressed, direct color, bottom up
     106           0 : Status TexCodecBmp::decode(std::uint8_t* RESTRICT data, size_t /*size*/, Tex* RESTRICT t) const
     107             : {
     108           0 :     const BmpHeader* hdr = (const BmpHeader*)data;
     109           0 :     const long w       = (long)read_le32(&hdr->biWidth);
     110           0 :     const long h_      = (long)read_le32(&hdr->biHeight);
     111           0 :     const std::uint16_t bpp = read_le16(&hdr->biBitCount);
     112           0 :     const std::uint32_t compress = read_le32(&hdr->biCompression);
     113             : 
     114           0 :     const long h = std::labs(h_);
     115             : 
     116           0 :     size_t flags = 0;
     117           0 :     flags |= (h_ < 0)? TEX_TOP_DOWN : TEX_BOTTOM_UP;
     118           0 :     if(bpp > 16)
     119           0 :         flags |= TEX_BGR;
     120           0 :     if(bpp == 32)
     121           0 :         flags |= TEX_ALPHA;
     122             : 
     123             :     // sanity checks
     124           0 :     if(compress != BI_RGB)
     125           0 :         WARN_RETURN(ERR::TEX_COMPRESSED);
     126             : 
     127           0 :     t->m_Width  = w;
     128           0 :     t->m_Height = h;
     129           0 :     t->m_Bpp    = bpp;
     130           0 :     t->m_Flags  = flags;
     131           0 :     return INFO::OK;
     132             : }
     133             : 
     134             : 
     135           0 : Status TexCodecBmp::encode(Tex* RESTRICT t, DynArray* RESTRICT da) const
     136             : {
     137           0 :     const size_t hdr_size = sizeof(BmpHeader);  // needed for BITMAPFILEHEADER
     138           0 :     const size_t img_size = t->img_size();
     139           0 :     const size_t file_size = hdr_size + img_size;
     140           0 :     const std::int32_t h = (t->m_Flags & TEX_TOP_DOWN)? -static_cast<std::int32_t>(t->m_Height) :
     141           0 :         static_cast<std::int32_t>(t->m_Height);
     142             : 
     143           0 :     size_t transforms = t->m_Flags;
     144           0 :     transforms &= ~TEX_ORIENTATION; // no flip needed - we can set top-down bit.
     145           0 :     transforms ^= TEX_BGR;          // BMP is native BGR.
     146             : 
     147           0 :     const BmpHeader hdr =
     148             :     {
     149             :         // BITMAPFILEHEADER
     150             :         0x4D42,             // bfType = 'B','M'
     151             :         static_cast<std::uint32_t>(file_size),        // bfSize
     152             :         0, 0,               // bfReserved1,2
     153             :         hdr_size,           // bfOffBits
     154             : 
     155             :         // BITMAPINFOHEADER
     156             :         40,                 // biSize = sizeof(BITMAPINFOHEADER)
     157           0 :         static_cast<std::int32_t>(t->m_Width),
     158             :         h,
     159             :         1,                  // biPlanes
     160           0 :         static_cast<std::uint16_t>(t->m_Bpp),
     161             :         BI_RGB,             // biCompression
     162             :         static_cast<std::uint32_t>(img_size),     // biSizeImage
     163             :         0, 0, 0, 0          // unused (bi?PelsPerMeter, biClr*)
     164           0 :     };
     165           0 :     return tex_codec_write(t, transforms, &hdr, hdr_size, da);
     166             : }

Generated by: LCOV version 1.16