LCOV - code coverage report
Current view: top level - source/lib/tex - tex_tga.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 32 53 60.4 %
Date: 2026-11-28 01:18:48 Functions: 4 6 66.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             :  * TGA codec.
      25             :  */
      26             : 
      27             : #include "precompiled.h"
      28             : 
      29             : #include "tex_codec.h"
      30             : 
      31             : #include "lib/bits.h"
      32             : #include "lib/byte_order.h"
      33             : #include "lib/code_annotation.h"
      34             : #include "lib/os_path.h"
      35             : #include "lib/status.h"
      36             : #include "lib/tex/tex.h"
      37             : 
      38             : #include <cstddef>
      39             : 
      40             : #pragma pack(push, 1)
      41             : 
      42             : enum TgaImgType
      43             : {
      44             :     TGA_TRUE_COLOR = 2, // uncompressed 24 or 32 bit direct RGB
      45             :     TGA_GREY        = 3     // uncompressed 8 bit direct greyscale
      46             : };
      47             : 
      48             : enum TgaImgDesc
      49             : {
      50             :     TGA_RIGHT_TO_LEFT = BIT(4),
      51             :     TGA_TOP_DOWN      = BIT(5),
      52             : };
      53             : 
      54             : typedef struct
      55             : {
      56             :     std::uint8_t img_id_len;            // 0 - no image identifier present
      57             :     std::uint8_t color_map_type;        // 0 - no color map present
      58             :     std::uint8_t img_type;          // see TgaImgType
      59             :     std::uint8_t color_map[5];      // unused
      60             : 
      61             :     std::uint16_t x_origin;         // unused
      62             :     std::uint16_t y_origin;         // unused
      63             : 
      64             :     std::uint16_t w;
      65             :     std::uint16_t h;
      66             :     std::uint8_t bpp;                   // bits per pixel
      67             : 
      68             :     std::uint8_t img_desc;
      69             : }
      70             : TgaHeader;
      71             : 
      72             : // TGA file: header [img id] [color map] image data
      73             : 
      74             : #pragma pack(pop)
      75             : 
      76             : 
      77          13 : Status TexCodecTga::transform(Tex*, size_t /*transforms*/) const
      78             : {
      79          13 :     return INFO::TEX_CODEC_CANNOT_HANDLE;
      80             : }
      81             : 
      82             : 
      83           1 : bool TexCodecTga::is_hdr(const std::uint8_t* file) const
      84             : {
      85           1 :     TgaHeader* hdr = (TgaHeader*)file;
      86             : 
      87             :     // the first TGA header doesn't have a magic field;
      88             :     // we can only check if the first 4 bytes are valid
      89             :     // .. not direct color
      90           1 :     if(hdr->color_map_type != 0)
      91             :         return false;
      92             :     // .. wrong color type (not uncompressed greyscale or RGB)
      93           1 :     if(hdr->img_type != TGA_TRUE_COLOR && hdr->img_type != TGA_GREY)
      94           0 :         return false;
      95             : 
      96             :     // note: we can't check img_id_len or color_map[0] - they are
      97             :     // undefined and may assume any value.
      98             : 
      99             :     return true;
     100             : }
     101             : 
     102             : 
     103           0 : bool TexCodecTga::is_ext(const OsPath& extension) const
     104             : {
     105           0 :     return extension == L".tga";
     106             : }
     107             : 
     108             : 
     109           2 : size_t TexCodecTga::hdr_size(const std::uint8_t* file) const
     110             : {
     111           2 :     size_t hdr_size = sizeof(TgaHeader);
     112           2 :     if(file)
     113             :     {
     114           1 :         TgaHeader* hdr = (TgaHeader*)file;
     115           1 :         hdr_size += hdr->img_id_len;
     116             :     }
     117           2 :     return hdr_size;
     118             : }
     119             : 
     120             : 
     121             : // requirements: uncompressed, direct color, bottom up
     122           1 : Status TexCodecTga::decode(std::uint8_t* RESTRICT data, size_t /*size*/, Tex* RESTRICT t) const
     123             : {
     124           1 :     const TgaHeader* hdr = (const TgaHeader*)data;
     125           1 :     const std::uint8_t type  = hdr->img_type;
     126           1 :     const size_t w   = read_le16(&hdr->w);
     127           1 :     const size_t h   = read_le16(&hdr->h);
     128           1 :     const size_t bpp = hdr->bpp;
     129           1 :     const std::uint8_t desc  = hdr->img_desc;
     130             : 
     131           1 :     size_t flags = 0;
     132           1 :     flags |= (desc & TGA_TOP_DOWN)? TEX_TOP_DOWN : TEX_BOTTOM_UP;
     133           1 :     if(desc & 0x0F) // alpha bits
     134           1 :         flags |= TEX_ALPHA;
     135           1 :     if(bpp == 8)
     136           0 :         flags |= TEX_GREY;
     137           1 :     if(type == TGA_TRUE_COLOR)
     138           1 :         flags |= TEX_BGR;
     139             : 
     140             :     // sanity checks
     141             :     // .. storing right-to-left is just stupid;
     142             :     //    we're not going to bother converting it.
     143           1 :     if(desc & TGA_RIGHT_TO_LEFT)
     144           0 :         WARN_RETURN(ERR::TEX_INVALID_LAYOUT);
     145             : 
     146           1 :     t->m_Width  = w;
     147           1 :     t->m_Height = h;
     148           1 :     t->m_Bpp    = bpp;
     149           1 :     t->m_Flags  = flags;
     150           1 :     return INFO::OK;
     151             : }
     152             : 
     153             : 
     154           0 : Status TexCodecTga::encode(Tex* RESTRICT t, DynArray* RESTRICT da) const
     155             : {
     156           0 :     std::uint8_t img_desc = 0;
     157           0 :     if(t->m_Flags & TEX_TOP_DOWN)
     158           0 :         img_desc |= TGA_TOP_DOWN;
     159           0 :     if(t->m_Bpp == 32)
     160           0 :         img_desc |= 8;  // size of alpha channel
     161           0 :     TgaImgType img_type = (t->m_Flags & TEX_GREY)? TGA_GREY : TGA_TRUE_COLOR;
     162             : 
     163           0 :     size_t transforms = t->m_Flags;
     164           0 :     transforms &= ~TEX_ORIENTATION; // no flip needed - we can set top-down bit.
     165           0 :     transforms ^= TEX_BGR;          // TGA is native BGR.
     166             : 
     167           0 :     const TgaHeader hdr =
     168             :     {
     169             :         0,              // no image identifier present
     170             :         0,              // no color map present
     171             :         static_cast<std::uint8_t>(img_type),
     172             :         {0,0,0,0,0},    // unused (color map)
     173             :         0, 0,           // unused (origin)
     174           0 :         static_cast<std::uint16_t>(t->m_Width),
     175           0 :         static_cast<std::uint16_t>(t->m_Height),
     176             :         static_cast<std::uint8_t>(t->m_Bpp),
     177             :         img_desc
     178           0 :     };
     179           0 :     const size_t hdr_size = sizeof(hdr);
     180           0 :     return tex_codec_write(t, transforms, &hdr, hdr_size, da);
     181             : }
     182             : 

Generated by: LCOV version 1.16