2005-09-02 04:56:54 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "lib/byte_order.h"
|
|
|
|
#include "tex_codec.h"
|
|
|
|
|
|
|
|
// NOTE: the convention is bottom-up for DDS, but there's no way to tell.
|
|
|
|
|
|
|
|
// defs modified from ddraw header
|
|
|
|
|
|
|
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
|
|
|
|
// DDPIXELFORMAT.dwFlags
|
|
|
|
#define DDPF_ALPHAPIXELS 0x00000001
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
u32 dwSize; // size of structure (32)
|
|
|
|
u32 dwFlags; // indicates which fields are valid
|
|
|
|
u32 dwFourCC; // (DDPF_FOURCC) FOURCC code, "DXTn"
|
|
|
|
u32 dwReserved1[5]; // reserved
|
|
|
|
}
|
|
|
|
DDPIXELFORMAT;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
u32 dwCaps[4];
|
|
|
|
}
|
|
|
|
DDSCAPS2;
|
|
|
|
|
|
|
|
// DDSURFACEDESC2.dwFlags
|
|
|
|
#define DDSD_HEIGHT 0x00000002
|
|
|
|
#define DDSD_WIDTH 0x00000004
|
|
|
|
#define DDSD_PIXELFORMAT 0x00001000
|
|
|
|
#define DDSD_MIPMAPCOUNT 0x00020000
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
u32 dwSize; // size of structure (124)
|
|
|
|
u32 dwFlags; // indicates which fields are valid
|
|
|
|
u32 dwHeight; // height of main image (pixels)
|
|
|
|
u32 dwWidth; // width of main image (pixels)
|
|
|
|
u32 dwLinearSize; // (DDSD_LINEARSIZE): total image size
|
|
|
|
u32 dwDepth; // (DDSD_DEPTH) vol. textures: vol. depth
|
|
|
|
u32 dwMipMapCount; // (DDSD_MIPMAPCOUNT) total # levels
|
|
|
|
u32 dwReserved1[11]; // reserved
|
|
|
|
DDPIXELFORMAT ddpfPixelFormat; // pixel format description of the surface
|
|
|
|
DDSCAPS2 ddsCaps; // direct draw surface capabilities
|
|
|
|
u32 dwReserved2; // reserved
|
|
|
|
}
|
|
|
|
DDSURFACEDESC2;
|
|
|
|
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
|
2005-09-17 17:47:00 +02:00
|
|
|
enum RGBA { R, G, B };
|
|
|
|
|
|
|
|
|
|
|
|
static inline void mix_2_3(uint dst[4], uint c0[4], uint c1[4])
|
|
|
|
{
|
|
|
|
for(int i = 0; i < 3; i++) dst[i] = (c0[i]*2 + c1[i] + 1)/3;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void mix_avg(uint dst[4], uint c0[4], uint c1[4])
|
|
|
|
{
|
|
|
|
for(int i = 0; i < 3; i++) dst[i] = (c0[i]+c1[i])/2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint access_bit_tbl(const u8* ptbl, uint idx, uint bit_width)
|
|
|
|
{
|
|
|
|
const u32 tbl = *(const u32*)ptbl;
|
|
|
|
uint val = tbl >> (idx*bit_width);
|
|
|
|
val &= (1u << bit_width)-1;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint access_bit_tbl64(const u8* ptbl, uint idx, uint bit_width)
|
|
|
|
{
|
|
|
|
const u64 tbl = *(const u64*)ptbl;
|
|
|
|
uint val = (uint)(tbl >> (idx*bit_width));
|
|
|
|
val &= (1u << bit_width)-1;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// c = color_tbl (shorthand)
|
|
|
|
static void precalc_color(int dxt, const u8* color_block, uint c[4][4])
|
|
|
|
{
|
|
|
|
const u16 c0 = *(u16*)(color_block+0);
|
|
|
|
const u16 c1 = *(u16*)(color_block+2);
|
|
|
|
|
|
|
|
// Unpack 565, and copy high bits to low bits
|
|
|
|
c[0][R] = ((c0>>8)&0xF8) | ((c0>>13)&7);
|
|
|
|
c[0][G] = ((c0>>3)&0xFC) | ((c0>>9 )&3);
|
|
|
|
c[0][B] = ((c0<<3)&0xF8) | ((c0>>2 )&7);
|
|
|
|
c[1][R] = ((c1>>8)&0xF8) | ((c1>>13)&7);
|
|
|
|
c[1][G] = ((c1>>3)&0xFC) | ((c1>>9 )&3);
|
|
|
|
c[1][B] = ((c1<<3)&0xF8) | ((c1>>2 )&7);
|
|
|
|
|
|
|
|
if((dxt != 1 || c0 > c1))
|
|
|
|
{
|
|
|
|
mix_2_3(c[2], c[0], c[1]); // c2 = 2/3*c0 + 1/3*c1
|
|
|
|
mix_2_3(c[3], c[1], c[0]); // c3 = 1/3*c0 + 2/3*c1
|
|
|
|
}
|
|
|
|
// DXT1 special case:
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mix_avg(c[2], c[0], c[1]); // c2 = (c0+c1)/2
|
|
|
|
for(int i = 0; i < 4; i++) c[3][i] = 0; // c3 = black
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void precalc_dxt5_alpha(const u8* alpha_block, u8 dxt5_alpha_tbl[8])
|
|
|
|
{
|
|
|
|
const uint a0 = alpha_block[0], a1 = alpha_block[1];
|
|
|
|
|
|
|
|
dxt5_alpha_tbl[0] = a0;
|
|
|
|
dxt5_alpha_tbl[1] = a1;
|
|
|
|
if(a0 > a1)
|
|
|
|
{
|
|
|
|
dxt5_alpha_tbl[2] = (6*a0 + 1*a1 + 3)/7;
|
|
|
|
dxt5_alpha_tbl[3] = (5*a0 + 2*a1 + 3)/7;
|
|
|
|
dxt5_alpha_tbl[4] = (4*a0 + 3*a1 + 3)/7;
|
|
|
|
dxt5_alpha_tbl[5] = (3*a0 + 4*a1 + 3)/7;
|
|
|
|
dxt5_alpha_tbl[6] = (2*a0 + 5*a1 + 3)/7;
|
|
|
|
dxt5_alpha_tbl[7] = (1*a0 + 6*a1 + 3)/7;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dxt5_alpha_tbl[2] = (4*a0 + 1*a1 + 2)/5;
|
|
|
|
dxt5_alpha_tbl[3] = (3*a0 + 2*a1 + 2)/5;
|
|
|
|
dxt5_alpha_tbl[4] = (2*a0 + 3*a1 + 2)/5;
|
|
|
|
dxt5_alpha_tbl[5] = (1*a0 + 4*a1 + 2)/5;
|
|
|
|
dxt5_alpha_tbl[6] = 0;
|
|
|
|
dxt5_alpha_tbl[7] = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const uint* choose_color(uint pixel_idx, const u8* color_block, const uint c_tbl[4][4])
|
|
|
|
{
|
|
|
|
debug_assert(pixel_idx < 16);
|
|
|
|
|
|
|
|
// pixel index -> color selector (2 bit) -> color
|
|
|
|
const uint c_idx = access_bit_tbl(color_block+4, pixel_idx, 2);
|
|
|
|
return c_tbl[c_idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static uint choose_alpha(int dxt, uint pixel_idx, const u8* alpha_block, const u8 dxt5_alpha_tbl[8])
|
|
|
|
{
|
|
|
|
debug_assert(pixel_idx < 16);
|
|
|
|
|
|
|
|
uint a = 255;
|
|
|
|
if(dxt == 3)
|
|
|
|
{
|
|
|
|
// table of 4-bit alpha entries
|
|
|
|
a = access_bit_tbl64(alpha_block, pixel_idx, 4);
|
|
|
|
a |= a << 4; // copy low bits to high bits
|
|
|
|
}
|
|
|
|
else if(dxt == 5)
|
|
|
|
{
|
|
|
|
// pixel index -> alpha selector (3 bit) -> alpha
|
|
|
|
const uint a_idx = access_bit_tbl(alpha_block+2, pixel_idx, 3);
|
|
|
|
a = dxt5_alpha_tbl[a_idx];
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// in ogl_emulate_dds: debug_assert(compressedimageSize == blocks * (dxt1? 8 : 16));
|
|
|
|
|
|
|
|
|
2005-09-02 04:56:54 +02:00
|
|
|
static int dds_decompress(Tex* t)
|
|
|
|
{
|
2005-09-17 17:47:00 +02:00
|
|
|
return ERR_NOT_IMPLEMENTED;
|
|
|
|
|
2005-09-02 04:56:54 +02:00
|
|
|
uint w = t->w, h = t->h;
|
|
|
|
if(w==0 || h==0 || w%4 || h%4)
|
|
|
|
return ERR_TEX_FMT_INVALID;
|
|
|
|
|
|
|
|
int dxt = t->flags & TEX_DXT;
|
|
|
|
debug_assert(dxt == 1 || dxt == 3 || dxt == 5);
|
|
|
|
|
2005-09-17 17:47:00 +02:00
|
|
|
bool output_rgb = true;
|
2005-09-02 04:56:54 +02:00
|
|
|
|
2005-09-17 17:47:00 +02:00
|
|
|
const u8* data = (const u8*)tex_get_data(t);
|
2005-09-02 04:56:54 +02:00
|
|
|
|
2005-09-17 17:47:00 +02:00
|
|
|
uint blocks_w = (uint)(round_up(w, 4) / 4);
|
|
|
|
uint blocks_h = (uint)(round_up(h, 4) / 4);
|
|
|
|
uint blocks = blocks_w * blocks_h;
|
|
|
|
size_t rgb_size = blocks * 16 * (output_rgb? 3 : 4);
|
|
|
|
void* rgb_data = malloc(rgb_size);
|
2005-09-02 04:56:54 +02:00
|
|
|
|
2005-09-17 17:47:00 +02:00
|
|
|
// This code is inefficient, but I don't care:
|
|
|
|
for(uint block_y = 0; block_y < blocks_h; block_y++)
|
|
|
|
for(uint block_x = 0; block_x < blocks_w; block_x++)
|
2005-09-02 04:56:54 +02:00
|
|
|
{
|
2005-09-17 17:47:00 +02:00
|
|
|
const u8* alpha_block = data;
|
|
|
|
u8 dxt5_alpha_tbl[8];
|
|
|
|
if(dxt == 5)
|
|
|
|
precalc_dxt5_alpha(alpha_block, dxt5_alpha_tbl);
|
|
|
|
if(dxt != 1)
|
|
|
|
data += 8;
|
2005-09-02 04:56:54 +02:00
|
|
|
|
2005-09-17 17:47:00 +02:00
|
|
|
const u8* color_block = data;
|
|
|
|
uint color_tbl[4][4]; // c[i][RGBA_component]
|
|
|
|
precalc_color(dxt, color_block, color_tbl);
|
|
|
|
data += 8;
|
2005-09-02 04:56:54 +02:00
|
|
|
|
2005-09-17 17:47:00 +02:00
|
|
|
uint pixel_idx = 0;
|
|
|
|
if(output_rgb)
|
|
|
|
for(int y = 0; y < 4; y++)
|
2005-09-02 04:56:54 +02:00
|
|
|
{
|
|
|
|
u8* out = (u8*)rgb_data + ((block_y*4+y)*blocks_w*4 + block_x*4) * 3;
|
2005-09-17 17:47:00 +02:00
|
|
|
for(int x = 0; x < 4; x++)
|
2005-09-02 04:56:54 +02:00
|
|
|
{
|
2005-09-17 17:47:00 +02:00
|
|
|
const uint* c = choose_color(pixel_idx, color_block, color_tbl);
|
|
|
|
*out++ = c[R]; *out++ = c[G]; *out++ = c[B];
|
|
|
|
pixel_idx++;
|
2005-09-02 04:56:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for(int y = 0; y < 4; ++y)
|
|
|
|
{
|
|
|
|
u8* out = (u8*)rgb_data + ((block_y*4+y)*blocks_w*4 + block_x*4) * 4;
|
2005-09-17 17:47:00 +02:00
|
|
|
for(int x = 0; x < 4; ++x)
|
2005-09-02 04:56:54 +02:00
|
|
|
{
|
2005-09-17 17:47:00 +02:00
|
|
|
const uint* c = choose_color(pixel_idx, color_block, color_tbl);
|
|
|
|
*out++ = c[R]; *out++ = c[G]; *out++ = c[B];
|
|
|
|
|
|
|
|
const uint a = choose_alpha(dxt, pixel_idx, alpha_block, dxt5_alpha_tbl);
|
2005-09-02 04:56:54 +02:00
|
|
|
*out++ = a;
|
2005-09-17 17:47:00 +02:00
|
|
|
|
|
|
|
pixel_idx++;
|
2005-09-02 04:56:54 +02:00
|
|
|
}
|
|
|
|
}
|
2005-09-17 17:47:00 +02:00
|
|
|
} // for block_x
|
2005-09-02 04:56:54 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-17 17:47:00 +02:00
|
|
|
|
2005-09-07 00:44:48 +02:00
|
|
|
static int dds_transform(Tex* t, uint transforms)
|
2005-09-02 04:56:54 +02:00
|
|
|
{
|
2005-09-07 00:44:48 +02:00
|
|
|
const int is_dxt = t->flags & TEX_DXT, transform_dxt = transforms & TEX_DXT;
|
2005-09-02 04:56:54 +02:00
|
|
|
// requesting decompression
|
2005-09-07 00:44:48 +02:00
|
|
|
if(is_dxt && transform_dxt)
|
2005-09-02 04:56:54 +02:00
|
|
|
return dds_decompress(t);
|
|
|
|
// both are DXT (unsupported; there are no flags we can change while
|
|
|
|
// compressed) or requesting compression (not implemented) or
|
|
|
|
// both not DXT (nothing we can do) - bail.
|
|
|
|
else
|
|
|
|
return TEX_CODEC_CANNOT_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-08 03:47:45 +02:00
|
|
|
static bool dds_is_hdr(const u8* file)
|
2005-09-02 04:56:54 +02:00
|
|
|
{
|
2005-09-08 03:47:45 +02:00
|
|
|
return *(u32*)file == FOURCC('D','D','S',' ');
|
|
|
|
}
|
2005-09-02 04:56:54 +02:00
|
|
|
|
|
|
|
|
2005-09-10 16:28:55 +02:00
|
|
|
static bool dds_is_ext(const char* ext)
|
|
|
|
{
|
|
|
|
return !stricmp(ext, "dds");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-08 03:47:45 +02:00
|
|
|
static size_t dds_hdr_size(const u8* UNUSED(file))
|
|
|
|
{
|
|
|
|
return 4+sizeof(DDSURFACEDESC2);
|
|
|
|
}
|
|
|
|
|
2005-09-02 04:56:54 +02:00
|
|
|
|
2005-09-10 16:28:55 +02:00
|
|
|
static int dds_decode(DynArray* da, Tex* t)
|
2005-09-08 03:47:45 +02:00
|
|
|
{
|
|
|
|
u8* file = da->base;
|
|
|
|
|
|
|
|
const DDSURFACEDESC2* hdr = (const DDSURFACEDESC2*)(file+4);
|
|
|
|
const u32 sd_size = read_le32(&hdr->dwSize);
|
|
|
|
const u32 sd_flags = read_le32(&hdr->dwFlags);
|
|
|
|
const u32 h = read_le32(&hdr->dwHeight);
|
|
|
|
const u32 w = read_le32(&hdr->dwWidth);
|
|
|
|
u32 mipmaps = read_le32(&hdr->dwMipMapCount);
|
|
|
|
const u32 pf_size = read_le32(&hdr->ddpfPixelFormat.dwSize);
|
|
|
|
const u32 pf_flags = read_le32(&hdr->ddpfPixelFormat.dwFlags);
|
|
|
|
const u32 fourcc = hdr->ddpfPixelFormat.dwFourCC;
|
2005-09-02 04:56:54 +02:00
|
|
|
// compared against FOURCC, which takes care of endian conversion.
|
|
|
|
|
|
|
|
// we'll use these fields; make sure they're present below.
|
|
|
|
// note: we can't guess image dimensions if not specified -
|
|
|
|
// the image isn't necessarily square.
|
|
|
|
const u32 sd_req_flags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
|
|
|
|
|
|
|
|
// make sure fields that aren't indicated as valid are zeroed.
|
|
|
|
if(!(sd_flags & DDSD_MIPMAPCOUNT))
|
|
|
|
mipmaps = 0;
|
|
|
|
|
2005-09-04 22:05:21 +02:00
|
|
|
|
|
|
|
// determine flags and bpp.
|
|
|
|
// we store DXT format (one of {1,3,5}) in flags & TEX_DXT.
|
|
|
|
//
|
|
|
|
// unfortunately there are problems with some DDS headers:
|
|
|
|
// - DXTex doesn't set the required dwPitchOrLinearSize field -
|
|
|
|
// MS can't even write out their own file format correctly. *sigh*
|
|
|
|
// it's needed by OpenGL, so we calculate it from w, h, and bpp.
|
|
|
|
// - pf_flags & DDPF_ALPHAPIXELS can only be used to check for
|
|
|
|
// DXT1a (the only way to detect it); we have observed some DXT3 files
|
|
|
|
// that don't have it set. grr
|
2005-09-02 04:56:54 +02:00
|
|
|
int bpp = 0;
|
|
|
|
int flags = 0;
|
|
|
|
switch(fourcc)
|
|
|
|
{
|
|
|
|
case FOURCC('D','X','T','1'):
|
|
|
|
bpp = 4;
|
2005-09-04 22:05:21 +02:00
|
|
|
flags |= 1;
|
|
|
|
if(pf_flags & DDPF_ALPHAPIXELS)
|
|
|
|
flags |= TEX_ALPHA;
|
2005-09-02 04:56:54 +02:00
|
|
|
break;
|
|
|
|
case FOURCC('D','X','T','3'):
|
|
|
|
bpp = 8;
|
2005-09-04 22:05:21 +02:00
|
|
|
flags |= 3;
|
|
|
|
flags |= TEX_ALPHA;
|
2005-09-02 04:56:54 +02:00
|
|
|
break;
|
|
|
|
case FOURCC('D','X','T','5'):
|
|
|
|
bpp = 8;
|
2005-09-04 22:05:21 +02:00
|
|
|
flags |= 5;
|
|
|
|
flags |= TEX_ALPHA;
|
2005-09-02 04:56:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(mipmaps)
|
|
|
|
flags |= TEX_MIPMAPS;
|
|
|
|
|
2005-09-04 22:05:21 +02:00
|
|
|
|
2005-09-02 04:56:54 +02:00
|
|
|
// sanity checks
|
2005-09-10 16:28:55 +02:00
|
|
|
// .. dimensions not padded to S3TC block size
|
2005-09-02 04:56:54 +02:00
|
|
|
if(w % 4 || h % 4)
|
2005-09-10 16:28:55 +02:00
|
|
|
return ERR_TEX_INVALID_SIZE;
|
|
|
|
// .. unknown FOURCC
|
|
|
|
if((flags & TEX_DXT) == 0)
|
|
|
|
return ERR_UNKNOWN_FORMAT;
|
|
|
|
// .. missing required field(s)
|
2005-09-02 04:56:54 +02:00
|
|
|
if((sd_flags & sd_req_flags) != sd_req_flags)
|
2005-09-10 16:28:55 +02:00
|
|
|
return ERR_INCOMPLETE_HEADER;
|
2005-09-02 04:56:54 +02:00
|
|
|
if(sizeof(DDPIXELFORMAT) != pf_size)
|
2005-09-10 16:28:55 +02:00
|
|
|
return ERR_CORRUPTED;
|
2005-09-02 04:56:54 +02:00
|
|
|
if(sizeof(DDSURFACEDESC2) != sd_size)
|
2005-09-08 03:47:45 +02:00
|
|
|
return ERR_CORRUPTED;
|
2005-09-02 04:56:54 +02:00
|
|
|
|
|
|
|
t->w = w;
|
|
|
|
t->h = h;
|
|
|
|
t->bpp = bpp;
|
|
|
|
t->flags = flags;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-10 16:28:55 +02:00
|
|
|
static int dds_encode(Tex* UNUSED(t), DynArray* UNUSED(da))
|
2005-09-02 04:56:54 +02:00
|
|
|
{
|
2005-09-07 00:44:48 +02:00
|
|
|
// note: do not return ERR_NOT_IMPLEMENTED et al. because that would
|
|
|
|
// break tex_write (which assumes either this, 0 or errors are returned).
|
|
|
|
return TEX_CODEC_CANNOT_HANDLE;
|
|
|
|
|
2005-09-02 04:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEX_CODEC_REGISTER(dds);
|