118 lines
3.6 KiB
C
118 lines
3.6 KiB
C
#include "HMT_Parser.h"
|
|
|
|
HMT_FILE *parseHMTFile(FILE *hmt_src) {
|
|
int i;
|
|
HMT_FILE *_buff = NULL;
|
|
|
|
if (hmt_src == NULL) return NULL;
|
|
_buff = calloc(1, sizeof(HMT_FILE));
|
|
|
|
rewind(hmt_src); //Rewind file at the start
|
|
fread(&(_buff->material_count), 4, 1, hmt_src); // Extract first usefull datas
|
|
fread(&(_buff->texture_offset), 4, 1, hmt_src);
|
|
|
|
// Read materials
|
|
printf("\n\nMaterials detected: %d\n", _buff->material_count);
|
|
_buff->materials_list = calloc(_buff->material_count, sizeof(HMT_MATERIAL)); // Create a big list of materials entries
|
|
for (i=0; i<_buff->material_count; i++) {
|
|
// Extract materials datas
|
|
if (readMaterial(&(_buff->materials_list[i]), hmt_src) != 0) {
|
|
purgeHMTFromMemory(_buff);
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
// Read textures
|
|
fseek(hmt_src, _buff->texture_offset, SEEK_SET);
|
|
fread(&(_buff->texture_count), 4, 1, hmt_src);
|
|
printf("\n\nTextures detected: %d\n", _buff->texture_count);
|
|
if (_buff->texture_count > 0) {
|
|
_buff->textures_list = calloc(_buff->texture_count, sizeof(HMT_TEXTURE)); // Create a big list of textures entries
|
|
for (i=0; i<_buff->texture_count; i++) {
|
|
// Extract textures datas
|
|
if (readTexture(&(_buff->textures_list[i]), hmt_src) != 0) {
|
|
purgeHMTFromMemory(_buff);
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
return _buff;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
void purgeHMTFromMemory(HMT_FILE *_f) {
|
|
if (_f == NULL) return;
|
|
|
|
if (_f->textures_list != NULL) {
|
|
if (_f->textures_list->image.pixels != NULL) free(_f->textures_list->image.pixels);
|
|
if (_f->textures_list->image.samples != NULL) free(_f->textures_list->image.samples);
|
|
}
|
|
free(_f->textures_list);
|
|
|
|
if (_f->materials_list != NULL) free(_f->materials_list);
|
|
|
|
free(_f);
|
|
}
|