1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
/* Copyright (C) 2017 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* TGA codec.
*/
#include "precompiled.h"
#include "lib/byte_order.h"
#include "tex_codec.h"
#include "lib/bits.h"
#pragma pack(push, 1)
enum TgaImgType
{
TGA_TRUE_COLOR = 2, // uncompressed 24 or 32 bit direct RGB
TGA_GREY = 3 // uncompressed 8 bit direct greyscale
};
enum TgaImgDesc
{
TGA_RIGHT_TO_LEFT = BIT(4),
TGA_TOP_DOWN = BIT(5),
};
typedef struct
{
u8 img_id_len; // 0 - no image identifier present
u8 color_map_type; // 0 - no color map present
u8 img_type; // see TgaImgType
u8 color_map[5]; // unused
u16 x_origin; // unused
u16 y_origin; // unused
u16 w;
u16 h;
u8 bpp; // bits per pixel
u8 img_desc;
}
TgaHeader;
// TGA file: header [img id] [color map] image data
#pragma pack(pop)
Status TexCodecTga::transform(Tex* UNUSED(t), size_t UNUSED(transforms)) const
{
return INFO::TEX_CODEC_CANNOT_HANDLE;
}
bool TexCodecTga::is_hdr(const u8* file) const
{
TgaHeader* hdr = (TgaHeader*)file;
// the first TGA header doesn't have a magic field;
// we can only check if the first 4 bytes are valid
// .. not direct color
if(hdr->color_map_type != 0)
return false;
// .. wrong color type (not uncompressed greyscale or RGB)
if(hdr->img_type != TGA_TRUE_COLOR && hdr->img_type != TGA_GREY)
return false;
// note: we can't check img_id_len or color_map[0] - they are
// undefined and may assume any value.
return true;
}
bool TexCodecTga::is_ext(const OsPath& extension) const
{
return extension == L".tga";
}
size_t TexCodecTga::hdr_size(const u8* file) const
{
size_t hdr_size = sizeof(TgaHeader);
if(file)
{
TgaHeader* hdr = (TgaHeader*)file;
hdr_size += hdr->img_id_len;
}
return hdr_size;
}
// requirements: uncompressed, direct color, bottom up
Status TexCodecTga::decode(u8* RESTRICT data, size_t UNUSED(size), Tex* RESTRICT t) const
{
const TgaHeader* hdr = (const TgaHeader*)data;
const u8 type = hdr->img_type;
const size_t w = read_le16(&hdr->w);
const size_t h = read_le16(&hdr->h);
const size_t bpp = hdr->bpp;
const u8 desc = hdr->img_desc;
size_t flags = 0;
flags |= (desc & TGA_TOP_DOWN)? TEX_TOP_DOWN : TEX_BOTTOM_UP;
if(desc & 0x0F) // alpha bits
flags |= TEX_ALPHA;
if(bpp == 8)
flags |= TEX_GREY;
if(type == TGA_TRUE_COLOR)
flags |= TEX_BGR;
// sanity checks
// .. storing right-to-left is just stupid;
// we're not going to bother converting it.
if(desc & TGA_RIGHT_TO_LEFT)
WARN_RETURN(ERR::TEX_INVALID_LAYOUT);
t->m_Width = w;
t->m_Height = h;
t->m_Bpp = bpp;
t->m_Flags = flags;
return INFO::OK;
}
Status TexCodecTga::encode(Tex* RESTRICT t, DynArray* RESTRICT da) const
{
u8 img_desc = 0;
if(t->m_Flags & TEX_TOP_DOWN)
img_desc |= TGA_TOP_DOWN;
if(t->m_Bpp == 32)
img_desc |= 8; // size of alpha channel
TgaImgType img_type = (t->m_Flags & TEX_GREY)? TGA_GREY : TGA_TRUE_COLOR;
size_t transforms = t->m_Flags;
transforms &= ~TEX_ORIENTATION; // no flip needed - we can set top-down bit.
transforms ^= TEX_BGR; // TGA is native BGR.
const TgaHeader hdr =
{
0, // no image identifier present
0, // no color map present
(u8)img_type,
{0,0,0,0,0}, // unused (color map)
0, 0, // unused (origin)
(u16)t->m_Width,
(u16)t->m_Height,
(u8)t->m_Bpp,
img_desc
};
const size_t hdr_size = sizeof(hdr);
return tex_codec_write(t, transforms, &hdr, hdr_size, da);
}
|