RSE-Texture/src/HMT_Parser.c
JackCarterSmith a815565fbd Added last decoding pixel function
Need to change palettes management
2019-07-03 20:02:26 +02:00

96 lines
3.0 KiB
C

#include "HMT_Parser.h"
int parseHMTFile(FILE *hmt_src, HMT_FILE *dst) {
int i;
if (hmt_src == NULL) return EXIT_FAILURE;
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);
// Read materials
printf("\n\nMaterials detected: %d\n", dst->material_count);
dst->materials_list = calloc(dst->material_count, sizeof(HMT_MATERIAL));
for (i=0; i<dst->material_count; i++) {
// Extract materials datas
if (readMaterial(&(dst->materials_list[i]), hmt_src) != 0) return EXIT_FAILURE;
}
// Read textures
fseek(hmt_src, dst->texture_offset, SEEK_SET);
fread(&(dst->texture_count), 4, 1, hmt_src);
printf("\n\nTextures detected: %d\n", dst->texture_count);
if (dst->texture_count > 0) {
dst->textures_list = calloc(dst->texture_count, sizeof(HMT_TEXTURE));
for (i=0; i<dst->texture_count; i++) {
// Extract textures datas
if (readTexture(&(dst->textures_list[i]), hmt_src) != 0) return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
int readMaterial(HMT_MATERIAL *mat, FILE *hmt_src) {
if (mat == NULL || hmt_src == NULL) return EXIT_FAILURE;
fread(mat, sizeof(HMT_MATERIAL), 1, hmt_src);
if (mat->zero != 0 || mat->hex_a != 0x0A) printf("\n Uncommon file detected!\n");
printf("Material type: %d\nTexture index: %d\n", mat->type_, mat->texture_index);
return EXIT_SUCCESS;
}
int readTexture(HMT_TEXTURE *tex, FILE *hmt_src) {
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);
getPaletteFromFile(&(tex->image), hmt_src);
}
fseek(hmt_src, tex->data_offset, SEEK_SET);
getSamplesFromFile(&(tex->image), hmt_src);
decodePixels(&(tex->image));
fseek(hmt_src, pos, SEEK_SET);
return EXIT_SUCCESS;
}