From 2d7d2156ff289618df274fc671023c6fec48be0c Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Wed, 3 Jul 2019 01:02:50 +0200 Subject: [PATCH] Finished HMT_parser --- src/HMT_Parser.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- src/RS_images.c | 31 +++++++++++++++++++++++++++++++ src/RS_images.h | 7 +++++++ 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/src/HMT_Parser.c b/src/HMT_Parser.c index 0e0466c..953f8aa 100644 --- a/src/HMT_Parser.c +++ b/src/HMT_Parser.c @@ -6,7 +6,7 @@ int parseHMTFile(FILE *hmt_src, HMT_FILE *dst) { if (hmt_src == NULL) return -1; - fseek(hmt_src, 0, SEEK_SET); //Rewind file at the start + rewind(hmt_src); //Rewind file at the start fread(&(dst->material_count), 4, 1, hmt_src); // Extract first usefull datas fread(&(dst->texture_offset), 4, 1, hmt_src); @@ -48,6 +48,52 @@ int readMaterial(HMT_MATERIAL *mat, FILE *hmt_src) { int readTexture(HMT_TEXTURE *tex, FILE *hmt_src) { int result = 0; + char u0,u1,bpp; + int color_rgba; + long pos; + RS_IMAGE_DESC desc; + + fread(&(tex->data_offset), 4, 1, hmt_src); + fseek(hmt_src, 28, SEEK_CUR); // Skip zeros area + fread(&(tex->palette_offset), 4, 1, hmt_src); + fread(&(tex->textureName_offset), 4, 1, hmt_src); + fread(&(tex->width), 2, 1, hmt_src); + fread(&(tex->height), 2, 1, hmt_src); + + fread(&u0, 1, 1, hmt_src); + fread(&bpp, 1, 1, hmt_src); + fread(&(tex->image.type_), 1, 1, hmt_src); + fread(&u1, 1, 1, hmt_src); + fread(&color_rgba, 4, 1, hmt_src); + + pos = ftell(hmt_src); + fseek(hmt_src, tex->textureName_offset, SEEK_SET); + fread(&(tex->name), 16, 1, hmt_src); + fseek(hmt_src, pos, SEEK_SET); + + desc = getImageDescFromType(tex->image.type_); + tex->image.paletteEntries = desc.palette_enties; + tex->image.sampleBits = desc.sample_bits; + tex->image.width = tex->width; + tex->image.height = tex->height; + + printf("Texture name: %s\n", tex->name); + printf("Size w: %ld h: %ld\n", tex->width, tex->height); + printf("Texture subtype: %d\n", tex->image.type_); + printf("Palette offset: %d\n", tex->palette_offset); + printf("Data offset: %d\n", tex->data_offset); + printf("u0: %d u1: %d\n", u0, u1); + + if (tex->palette_offset > 0) { + printf("\nPalette entries: %d\n", tex->image.paletteEntries); + fseek(hmt_src, tex->palette_offset, SEEK_SET); + //loadPalette(&(tex->image), stream); + } + fseek(hmt_src, tex->data_offset, SEEK_SET); + //loadSamples(&(tex->image), stream); + //decodePixels(&(tex->image)); + + fseek(hmt_src, pos, SEEK_SET); return result; } diff --git a/src/RS_images.c b/src/RS_images.c index e8fdbae..4812109 100644 --- a/src/RS_images.c +++ b/src/RS_images.c @@ -1,3 +1,34 @@ #include "RS_images.h" +RS_IMAGE_DESC getImageDescFromType(unsigned char type) { + RS_IMAGE_DESC desc; + switch(type) { + case 0: + desc.palette_enties = 16; + desc.sample_bits = 4; + break; + case 1: + desc.palette_enties = 256; + desc.sample_bits = 8; + break; + case 2: + desc.palette_enties = 0; + desc.sample_bits = 16; + break; + case 3: + desc.palette_enties = 0; + desc.sample_bits = 32; + break; + case 4: + desc.palette_enties = 0; + desc.sample_bits = 4; + break; + case 5: + desc.palette_enties = 0; + desc.sample_bits = 8; + break; + } + + return desc; +} diff --git a/src/RS_images.h b/src/RS_images.h index d28a8a6..ca12424 100644 --- a/src/RS_images.h +++ b/src/RS_images.h @@ -22,10 +22,17 @@ typedef struct RSImage { unsigned char palette[256][3]; /**< Image palette definition */ }RS_IMAGE; +typedef struct RSImage_desc { + int palette_enties; + int sample_bits; + //char alpha; +}RS_IMAGE_DESC; + ///////////////////////////// ///// Declare functions ///// ///////////////////////////// +RS_IMAGE_DESC getImageDescFromType(unsigned char type); void Unpack4To8bit(unsigned char *src, unsigned char *dst, int sampling); void Unpack4bitTo24bitRGB(unsigned char *src, unsigned char *dst, int size, unsigned char pal[256][3]); void UseOddBytes(unsigned char *src, unsigned char *dst, int size);