2010-02-08 08:23:39 -08:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* 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.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2009-04-18 10:51:05 -07:00
|
|
|
/*
|
|
|
|
|
* TGA codec.
|
2006-04-23 16:14:18 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "lib/byte_order.h"
|
|
|
|
|
#include "tex_codec.h"
|
2007-05-09 14:01:11 -07:00
|
|
|
#include "lib/bits.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
|
|
|
|
|
|
enum TgaImgType
|
|
|
|
|
{
|
2015-03-15 16:59:48 -07:00
|
|
|
TGA_TRUE_COLOR = 2, // uncompressed 24 or 32 bit direct RGB
|
2006-04-23 16:14:18 -07:00
|
|
|
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
|
2015-03-15 16:59:48 -07:00
|
|
|
u8 color_map_type; // 0 - no color map present
|
2006-04-23 16:14:18 -07:00
|
|
|
u8 img_type; // see TgaImgType
|
2015-03-15 16:59:48 -07:00
|
|
|
u8 color_map[5]; // unused
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
u16 x_origin; // unused
|
|
|
|
|
u16 y_origin; // unused
|
|
|
|
|
|
|
|
|
|
u16 w;
|
|
|
|
|
u16 h;
|
|
|
|
|
u8 bpp; // bits per pixel
|
|
|
|
|
|
|
|
|
|
u8 img_desc;
|
|
|
|
|
}
|
|
|
|
|
TgaHeader;
|
|
|
|
|
|
2015-03-15 16:59:48 -07:00
|
|
|
// TGA file: header [img id] [color map] image data
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
|
|
|
2014-03-12 21:16:20 -07:00
|
|
|
Status TexCodecTga::transform(Tex* UNUSED(t), size_t UNUSED(transforms)) const
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2006-09-22 06:19:40 -07:00
|
|
|
return INFO::TEX_CODEC_CANNOT_HANDLE;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-03-12 21:16:20 -07:00
|
|
|
bool TexCodecTga::is_hdr(const u8* file) const
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
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
|
2015-03-15 16:59:48 -07:00
|
|
|
// .. not direct color
|
|
|
|
|
if(hdr->color_map_type != 0)
|
2006-04-23 16:14:18 -07:00
|
|
|
return false;
|
2015-03-15 16:59:48 -07:00
|
|
|
// .. wrong color type (not uncompressed greyscale or RGB)
|
|
|
|
|
if(hdr->img_type != TGA_TRUE_COLOR && hdr->img_type != TGA_GREY)
|
2006-04-23 16:14:18 -07:00
|
|
|
return false;
|
|
|
|
|
|
2015-03-15 16:59:48 -07:00
|
|
|
// note: we can't check img_id_len or color_map[0] - they are
|
2006-04-23 16:14:18 -07:00
|
|
|
// undefined and may assume any value.
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-03-12 21:16:20 -07:00
|
|
|
bool TexCodecTga::is_ext(const OsPath& extension) const
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2011-03-23 06:36:20 -07:00
|
|
|
return extension == L".tga";
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-03-12 21:16:20 -07:00
|
|
|
size_t TexCodecTga::hdr_size(const u8* file) const
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
size_t hdr_size = sizeof(TgaHeader);
|
|
|
|
|
if(file)
|
|
|
|
|
{
|
|
|
|
|
TgaHeader* hdr = (TgaHeader*)file;
|
|
|
|
|
hdr_size += hdr->img_id_len;
|
|
|
|
|
}
|
|
|
|
|
return hdr_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-08-17 01:38:53 -07:00
|
|
|
// requirements: uncompressed, direct color, bottom up
|
2014-03-12 21:16:20 -07:00
|
|
|
Status TexCodecTga::decode(rpU8 data, size_t UNUSED(size), Tex* RESTRICT t) const
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2011-08-17 01:38:53 -07:00
|
|
|
const TgaHeader* hdr = (const TgaHeader*)data;
|
2006-04-23 16:14:18 -07:00
|
|
|
const u8 type = hdr->img_type;
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 11:48:32 -07:00
|
|
|
const size_t w = read_le16(&hdr->w);
|
|
|
|
|
const size_t h = read_le16(&hdr->h);
|
|
|
|
|
const size_t bpp = hdr->bpp;
|
2006-04-23 16:14:18 -07:00
|
|
|
const u8 desc = hdr->img_desc;
|
|
|
|
|
|
2008-09-18 04:27:55 -07:00
|
|
|
size_t flags = 0;
|
2006-04-23 16:14:18 -07:00
|
|
|
flags |= (desc & TGA_TOP_DOWN)? TEX_TOP_DOWN : TEX_BOTTOM_UP;
|
2008-01-13 03:25:19 -08:00
|
|
|
if(desc & 0x0F) // alpha bits
|
2006-04-23 16:14:18 -07:00
|
|
|
flags |= TEX_ALPHA;
|
|
|
|
|
if(bpp == 8)
|
|
|
|
|
flags |= TEX_GREY;
|
2015-03-15 16:59:48 -07:00
|
|
|
if(type == TGA_TRUE_COLOR)
|
2006-04-23 16:14:18 -07:00
|
|
|
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)
|
2006-09-22 06:19:40 -07:00
|
|
|
WARN_RETURN(ERR::TEX_INVALID_LAYOUT);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2014-03-12 19:37:05 -07:00
|
|
|
t->m_Width = w;
|
|
|
|
|
t->m_Height = h;
|
|
|
|
|
t->m_Bpp = bpp;
|
|
|
|
|
t->m_Flags = flags;
|
2006-09-22 06:19:40 -07:00
|
|
|
return INFO::OK;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-03-12 21:16:20 -07:00
|
|
|
Status TexCodecTga::encode(Tex* RESTRICT t, DynArray* RESTRICT da) const
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
u8 img_desc = 0;
|
2014-03-12 19:37:05 -07:00
|
|
|
if(t->m_Flags & TEX_TOP_DOWN)
|
2006-04-23 16:14:18 -07:00
|
|
|
img_desc |= TGA_TOP_DOWN;
|
2014-03-12 19:37:05 -07:00
|
|
|
if(t->m_Bpp == 32)
|
2006-04-23 16:14:18 -07:00
|
|
|
img_desc |= 8; // size of alpha channel
|
2015-03-15 16:59:48 -07:00
|
|
|
TgaImgType img_type = (t->m_Flags & TEX_GREY)? TGA_GREY : TGA_TRUE_COLOR;
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2014-03-12 19:37:05 -07:00
|
|
|
size_t transforms = t->m_Flags;
|
2006-04-23 16:14:18 -07:00
|
|
|
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
|
2015-03-15 16:59:48 -07:00
|
|
|
0, // no color map present
|
2006-04-23 16:14:18 -07:00
|
|
|
(u8)img_type,
|
2015-03-15 16:59:48 -07:00
|
|
|
{0,0,0,0,0}, // unused (color map)
|
2006-04-23 16:14:18 -07:00
|
|
|
0, 0, // unused (origin)
|
2014-03-12 19:37:05 -07:00
|
|
|
(u16)t->m_Width,
|
|
|
|
|
(u16)t->m_Height,
|
|
|
|
|
(u8)t->m_Bpp,
|
2006-04-23 16:14:18 -07:00
|
|
|
img_desc
|
|
|
|
|
};
|
|
|
|
|
const size_t hdr_size = sizeof(hdr);
|
|
|
|
|
return tex_codec_write(t, transforms, &hdr, hdr_size, da);
|
|
|
|
|
}
|
|
|
|
|
|