Skip to content

Commit

Permalink
more correct tex header handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Morilli committed Jan 25, 2022
1 parent fff5cb7 commit f86ce07
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
23 changes: 11 additions & 12 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,23 @@ void dds2tex(const char* dds_path)
DDS_HEADER dds_header;
assert(fread(&dds_header, sizeof(DDS_HEADER), 1, dds_file) == 1);

const uint8_t* tex_format;
TEX_HEADER tex_header = {
.magic = tex_magic,
.image_width = dds_header.dwWidth,
.image_height = dds_header.dwHeight,
.unk1 = 1 // this is always set to 1 in original tex files; no idea what it stands for
};
if (memcmp(dds_header.ddspf.dwFourCC, "DXT1", 4) == 0) {
tex_format = tex_dxt1_format;
tex_header.tex_format = tex_format_dxt1;
} else if (memcmp(dds_header.ddspf.dwFourCC, "DXT5", 4) == 0) {
tex_format = tex_dxt5_format;
tex_header.tex_format = tex_format_dxt5;
} else {
fprintf(stderr, "Error: dds file needs to be in either DXT1 or DXT5 format!\n");
fclose(dds_file);
return;
}
if (dds_header.dwMipMapCount > 1) { // this value may be set to 1, which is equivalent to leaving it at 0 (no mipmaps)
tex_header.has_mipmaps = true;
if (dds_header.dwMipMapCount != 32u - __builtin_clz(max(dds_header.dwWidth, dds_header.dwHeight))) {
fprintf(stderr, "Error: DDS mipmap count mismatch; expected %u mipmaps, got %u\n", 32 - __builtin_clz(max(dds_header.dwWidth, dds_header.dwHeight)), dds_header.dwMipMapCount);
fclose(dds_file);
Expand All @@ -70,13 +76,6 @@ void dds2tex(const char* dds_path)
fclose(dds_file);
return;
}
TEX_HEADER tex_header = {
.magic = tex_magic,
.image_width = dds_header.dwWidth,
.image_height = dds_header.dwHeight,
.has_mipmaps = dds_header.dwMipMapCount > 1
};
memcpy(tex_header.tex_format, tex_format, 2);
fwrite(&tex_header, sizeof(TEX_HEADER), 1, tex_file);

printf("Info: Converting %ux%u DDS file \"%s\" to TEX file \"%s\".\n", tex_header.image_width, tex_header.image_height, dds_path, tex_path);
Expand Down Expand Up @@ -131,9 +130,9 @@ void tex2dds(const char* tex_path)
assert(fread((uint8_t*) &tex_header + 4, sizeof(TEX_HEADER) - 4, 1, tex_file) == 1);

const char* dds_format;
if (memcmp(tex_header.tex_format, tex_dxt1_format, 2) == 0) {
if (tex_header.tex_format == tex_format_dxt1) {
dds_format = "DXT1";
} else if (memcmp(tex_header.tex_format, tex_dxt5_format, 2) == 0) {
} else if (tex_header.tex_format == tex_format_dxt5) {
dds_format = "DXT5";
} else {
fprintf(stderr, "Error: tex file needs to be in either DXT1 or DXT5 format!\n");
Expand Down
13 changes: 9 additions & 4 deletions tex.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#include <stdint.h>
#include <stdbool.h>

const uint8_t tex_dxt1_format[2] = {1, 10};
const uint8_t tex_dxt5_format[2] = {1, 12};

enum tex_format {
tex_format_d32 = 0x1,
tex_format_d24s8 = 0x2,
tex_foramt_d24x8 = 0x3,
tex_format_dxt1 = 0xA,
tex_format_dxt5 = 0xC
};
#define tex_magic "TEX"

typedef struct {
uint8_t magic[4];
uint16_t image_width;
uint16_t image_height;
uint8_t tex_format[2];
uint8_t unk1;
uint8_t tex_format;
uint8_t unk2;
bool has_mipmaps;
} TEX_HEADER;

0 comments on commit f86ce07

Please sign in to comment.