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: 3 48 6.2 %
Date: 2023-01-19 00:18:29 Functions: 3 8 37.5 %

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

Generated by: LCOV version 1.13