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