LCOV - code coverage report
Current view: top level - source/lib/tex - tex_codec.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 12 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 4 0.0 %

          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             :  * support routines and interface for texture codecs.
      25             :  */
      26             : 
      27             : #ifndef INCLUDED_TEX_CODEC
      28             : #define INCLUDED_TEX_CODEC
      29             : 
      30             : #include "lib/code_annotation.h"
      31             : #include "lib/os_path.h"
      32             : #include "lib/status.h"
      33             : 
      34             : #include <cstddef>
      35             : #include <vector>
      36             : 
      37             : class Tex;
      38             : struct DynArray;
      39             : 
      40             : /**
      41             :  * virtual method table for TexCodecs.
      42             :  * rationale: this works in C and also allows storing name and next in vtbl.
      43             :  * 'template method'-style interface to increase code reuse and
      44             :  * simplify writing new codecs.
      45             :  **/
      46             : class ITexCodec
      47             : {
      48             : public:
      49             :     /**
      50             :      * decode the file into a Tex structure.
      51             :      *
      52             :      * @param data input data array (non-const, because the texture
      53             :      * may have to be flipped in-place - see "texture orientation").
      54             :      * @param size [bytes] of data, always >= 4
      55             :      *   (this is usually enough to compare the header's "magic" field,
      56             :      *    and no legitimate file will be smaller)
      57             :      * @param t output texture object
      58             :      * @return Status
      59             :      **/
      60             :     virtual Status decode(std::uint8_t* data, size_t size, Tex* RESTRICT t) const = 0;
      61             : 
      62             :     /**
      63             :      * encode the texture data into the codec's file format (in memory).
      64             :      *
      65             :      * @param t input texture object. note: non-const because encoding may
      66             :      * require a Tex::transform.
      67             :      * @param da output data array, allocated by codec.
      68             :      * rationale: some codecs cannot calculate the output size beforehand
      69             :      * (e.g. PNG output via libpng), so the output memory cannot be allocated
      70             :      * by the caller.
      71             :      * @return Status
      72             :      **/
      73             :     virtual Status encode(Tex* RESTRICT t, DynArray* RESTRICT da) const = 0;
      74             : 
      75             :     /**
      76             :      * transform the texture's pixel format.
      77             :      *
      78             :      * @param t texture object
      79             :      * @param transforms: OR-ed combination of TEX_* flags that are to
      80             :      * be changed. note: the codec needs only handle situations specific
      81             :      * to its format; generic pixel format transforms are handled by
      82             :      * the caller.
      83             :      **/
      84             :     virtual Status transform(Tex* t, size_t transforms) const = 0;
      85             : 
      86             :     /**
      87             :      * indicate if the data appears to be an instance of this codec's header,
      88             :      * i.e. can this codec decode it?
      89             :      *
      90             :      * @param file input data; only guaranteed to be 4 bytes!
      91             :      * (this should be enough to examine the header's 'magic' field)
      92             :      * @return bool
      93             :      **/
      94             :     virtual bool is_hdr(const std::uint8_t* file) const = 0;
      95             : 
      96             :     /**
      97             :      * is the extension that of a file format supported by this codec?
      98             :      *
      99             :      * rationale: cannot just return the extension string and have
     100             :      * caller compare it (-> smaller code) because a codec's file format
     101             :      * may have several valid extensions (e.g. jpg and jpeg).
     102             :      *
     103             :      * @param extension (including '.')
     104             :      * @return bool
     105             :      **/
     106             :     virtual bool is_ext(const OsPath& extension) const = 0;
     107             : 
     108             :     /**
     109             :      * return size of the file header supported by this codec.
     110             :      *
     111             :      * @param file the specific header to return length of (taking its
     112             :      * variable-length fields into account). if NULL, return minimum
     113             :      * guaranteed header size, i.e. the header without any
     114             :      * variable-length fields.
     115             :      * @return size [bytes]
     116             :      **/
     117             :     virtual size_t hdr_size(const std::uint8_t* file) const = 0;
     118             : 
     119             :     /**
     120             :      * name of codec for debug purposes. typically set via TEX_CODEC_REGISTER.
     121             :      **/
     122             :     virtual const wchar_t* get_name() const = 0;
     123             : 
     124             :     virtual ~ITexCodec() {}
     125             : };
     126             : 
     127             : class TexCodecPng:ITexCodec {
     128             : public:
     129             :     virtual Status decode(std::uint8_t* data, size_t size, Tex* RESTRICT t) const;
     130             :     virtual Status encode(Tex* RESTRICT t, DynArray* RESTRICT da) const;
     131             :     virtual Status transform(Tex* t, size_t transforms) const;
     132             :     virtual bool is_hdr(const std::uint8_t* file) const;
     133             :     virtual bool is_ext(const OsPath& extension) const;
     134             :     virtual size_t hdr_size(const std::uint8_t* file) const;
     135           0 :     virtual const wchar_t* get_name() const {
     136           0 :         static const wchar_t *name = L"png";
     137           0 :         return name;
     138             :     };
     139             : };
     140             : 
     141             : class TexCodecDds:ITexCodec {
     142             : public:
     143             :     virtual Status decode(std::uint8_t* data, size_t size, Tex* RESTRICT t) const;
     144             :     virtual Status encode(Tex* RESTRICT t, DynArray* RESTRICT da) const;
     145             :     virtual Status transform(Tex* t, size_t transforms) const;
     146             :     virtual bool is_hdr(const std::uint8_t* file) const;
     147             :     virtual bool is_ext(const OsPath& extension) const;
     148             :     virtual size_t hdr_size(const std::uint8_t* file) const;
     149           0 :     virtual const wchar_t* get_name() const {
     150           0 :         static const wchar_t *name = L"dds";
     151           0 :         return name;
     152             :     };
     153             : };
     154             : 
     155             : class TexCodecTga:ITexCodec {
     156             : public:
     157             :     virtual Status decode(std::uint8_t* data, size_t size, Tex* RESTRICT t) const;
     158             :     virtual Status encode(Tex* RESTRICT t, DynArray* RESTRICT da) const;
     159             :     virtual Status transform(Tex* t, size_t transforms) const;
     160             :     virtual bool is_hdr(const std::uint8_t* file) const;
     161             :     virtual bool is_ext(const OsPath& extension) const;
     162             :     virtual size_t hdr_size(const std::uint8_t* file) const;
     163           0 :     virtual const wchar_t* get_name() const {
     164           0 :         static const wchar_t *name = L"tga";
     165           0 :         return name;
     166             :     };
     167             : };
     168             : 
     169             : class TexCodecBmp:ITexCodec {
     170             : public:
     171             :     virtual Status decode(std::uint8_t* data, size_t size, Tex* RESTRICT t) const;
     172             :     virtual Status encode(Tex* RESTRICT t, DynArray* RESTRICT da) const;
     173             :     virtual Status transform(Tex* t, size_t transforms) const;
     174             :     virtual bool is_hdr(const std::uint8_t* file) const;
     175             :     virtual bool is_ext(const OsPath& extension) const;
     176             :     virtual size_t hdr_size(const std::uint8_t* file) const;
     177           0 :     virtual const wchar_t* get_name() const {
     178           0 :         static const wchar_t *name = L"bmp";
     179           0 :         return name;
     180             :     };
     181             : };
     182             : 
     183             : /**
     184             :  * Find codec that recognizes the desired output file extension.
     185             :  *
     186             :  * @param extension
     187             :  * @param c (out) vtbl of responsible codec
     188             :  * @return Status; ERR::RES_UNKNOWN_FORMAT (without warning, because this is
     189             :  * called by tex_is_known_extension) if no codec indicates they can
     190             :  * handle the given extension.
     191             :  **/
     192             : extern Status tex_codec_for_filename(const OsPath& extension, const ITexCodec** c);
     193             : 
     194             : /**
     195             :  * find codec that recognizes the header's magic field.
     196             :  *
     197             :  * @param data typically contents of file, but need only include the
     198             :  * (first 4 bytes of) header.
     199             :  * @param data_size [bytes]
     200             :  * @param c (out) vtbl of responsible codec
     201             :  * @return Status; ERR::RES_UNKNOWN_FORMAT if no codec indicates they can
     202             :  * handle the given format (header).
     203             :  **/
     204             : extern Status tex_codec_for_header(const std::uint8_t* data, size_t data_size, const ITexCodec** c);
     205             : 
     206             : /**
     207             :  * transform the texture's pixel format.
     208             :  * tries each codec's transform method once, or until one indicates success.
     209             :  *
     210             :  * @param t texture object
     211             :  * @param transforms: OR-ed combination of TEX_* flags that are to
     212             :  * be changed.
     213             :  * @return Status
     214             :  **/
     215             : extern Status tex_codec_transform(Tex* t, size_t transforms);
     216             : 
     217             : /**
     218             :  * allocate an array of row pointers that point into the given texture data.
     219             :  * for texture decoders that support output via row pointers (e.g. PNG),
     220             :  * this allows flipping the image vertically (useful when matching bottom-up
     221             :  * textures to a global orientation) directly, which is much more
     222             :  * efficient than transforming later via copying all pixels.
     223             :  *
     224             :  * @param data the texture data into which row pointers will point.
     225             :  * note: we don't allocate it here because this function is
     226             :  * needed for encoding, too (where data is already present).
     227             :  * @param h height [pixels] of texture.
     228             :  * @param pitch size [bytes] of one texture row, i.e. width*bytes_per_pixel.
     229             :  * @param src_flags TexFlags of source texture. used to extract its
     230             :  * orientation.
     231             :  * @param dst_orientation desired orientation of the output data.
     232             :  * can be one of TEX_BOTTOM_UP, TEX_TOP_DOWN, or 0 for the
     233             :  * "global orientation".
     234             :  * depending on src and dst, the row array is flipped if necessary.
     235             :  **/
     236             : typedef const std::uint8_t* RowPtr;
     237             : extern std::vector<RowPtr> tex_codec_alloc_rows(const std::uint8_t* data, size_t h, size_t pitch, size_t src_flags, size_t dst_orientation);
     238             : 
     239             : /**
     240             :  * apply transforms and then copy header and image into output buffer.
     241             :  *
     242             :  * @param t input texture object
     243             :  * @param transforms transformations to be applied to pixel format
     244             :  * @param hdr header data
     245             :  * @param hdr_size [bytes]
     246             :  * @param da output data array (will be expanded as necessary)
     247             :  * @return Status
     248             :  **/
     249             : extern Status tex_codec_write(Tex* t, size_t transforms, const void* hdr, size_t hdr_size, DynArray* da);
     250             : 
     251             : #endif   // #ifndef INCLUDED_TEX_CODEC

Generated by: LCOV version 1.16