Line data Source code
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 :
70 13 : Status TexCodecTga::transform(Tex* UNUSED(t), size_t UNUSED(transforms)) const
71 : {
72 13 : return INFO::TEX_CODEC_CANNOT_HANDLE;
73 : }
74 :
75 :
76 1 : bool TexCodecTga::is_hdr(const u8* file) const
77 : {
78 1 : 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 1 : if(hdr->color_map_type != 0)
84 0 : return false;
85 : // .. wrong color type (not uncompressed greyscale or RGB)
86 1 : if(hdr->img_type != TGA_TRUE_COLOR && hdr->img_type != TGA_GREY)
87 0 : 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 1 : return true;
93 : }
94 :
95 :
96 0 : bool TexCodecTga::is_ext(const OsPath& extension) const
97 : {
98 0 : return extension == L".tga";
99 : }
100 :
101 :
102 2 : size_t TexCodecTga::hdr_size(const u8* file) const
103 : {
104 2 : size_t hdr_size = sizeof(TgaHeader);
105 2 : if(file)
106 : {
107 1 : TgaHeader* hdr = (TgaHeader*)file;
108 1 : hdr_size += hdr->img_id_len;
109 : }
110 2 : return hdr_size;
111 : }
112 :
113 :
114 : // requirements: uncompressed, direct color, bottom up
115 1 : Status TexCodecTga::decode(u8* RESTRICT data, size_t UNUSED(size), Tex* RESTRICT t) const
116 : {
117 1 : const TgaHeader* hdr = (const TgaHeader*)data;
118 1 : const u8 type = hdr->img_type;
119 1 : const size_t w = read_le16(&hdr->w);
120 1 : const size_t h = read_le16(&hdr->h);
121 1 : const size_t bpp = hdr->bpp;
122 1 : const u8 desc = hdr->img_desc;
123 :
124 1 : size_t flags = 0;
125 1 : flags |= (desc & TGA_TOP_DOWN)? TEX_TOP_DOWN : TEX_BOTTOM_UP;
126 1 : if(desc & 0x0F) // alpha bits
127 1 : flags |= TEX_ALPHA;
128 1 : if(bpp == 8)
129 0 : flags |= TEX_GREY;
130 1 : if(type == TGA_TRUE_COLOR)
131 1 : 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 1 : if(desc & TGA_RIGHT_TO_LEFT)
137 0 : WARN_RETURN(ERR::TEX_INVALID_LAYOUT);
138 :
139 1 : t->m_Width = w;
140 1 : t->m_Height = h;
141 1 : t->m_Bpp = bpp;
142 1 : t->m_Flags = flags;
143 1 : return INFO::OK;
144 : }
145 :
146 :
147 0 : Status TexCodecTga::encode(Tex* RESTRICT t, DynArray* RESTRICT da) const
148 : {
149 0 : u8 img_desc = 0;
150 0 : if(t->m_Flags & TEX_TOP_DOWN)
151 0 : img_desc |= TGA_TOP_DOWN;
152 0 : if(t->m_Bpp == 32)
153 0 : img_desc |= 8; // size of alpha channel
154 0 : TgaImgType img_type = (t->m_Flags & TEX_GREY)? TGA_GREY : TGA_TRUE_COLOR;
155 :
156 0 : size_t transforms = t->m_Flags;
157 0 : transforms &= ~TEX_ORIENTATION; // no flip needed - we can set top-down bit.
158 0 : transforms ^= TEX_BGR; // TGA is native BGR.
159 :
160 0 : 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 0 : (u16)t->m_Width,
168 0 : (u16)t->m_Height,
169 0 : (u8)t->m_Bpp,
170 : img_desc
171 0 : };
172 0 : const size_t hdr_size = sizeof(hdr);
173 0 : return tex_codec_write(t, transforms, &hdr, hdr_size, da);
174 3 : }
175 :
|