diff --git a/src/HMT_Parser.c b/src/HMT_Parser.c index 953f8aa..b59e4f5 100644 --- a/src/HMT_Parser.c +++ b/src/HMT_Parser.c @@ -1,10 +1,9 @@ #include "HMT_Parser.h" int parseHMTFile(FILE *hmt_src, HMT_FILE *dst) { - int result = 0; int i; - if (hmt_src == NULL) return -1; + 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 @@ -15,7 +14,7 @@ int parseHMTFile(FILE *hmt_src, HMT_FILE *dst) { dst->materials_list = calloc(dst->material_count, sizeof(HMT_MATERIAL)); for (i=0; imaterial_count; i++) { // Extract materials datas - if (readMaterial(&(dst->materials_list[i]), hmt_src) != 0) return -1; + if (readMaterial(&(dst->materials_list[i]), hmt_src) != 0) return EXIT_FAILURE; } // Read textures @@ -26,28 +25,25 @@ int parseHMTFile(FILE *hmt_src, HMT_FILE *dst) { dst->textures_list = calloc(dst->texture_count, sizeof(HMT_TEXTURE)); for (i=0; itexture_count; i++) { // Extract textures datas - if (readTexture(&(dst->textures_list[i]), hmt_src) != 0) return -1; + if (readTexture(&(dst->textures_list[i]), hmt_src) != 0) return EXIT_FAILURE; } } - return result; + return EXIT_SUCCESS; } int readMaterial(HMT_MATERIAL *mat, FILE *hmt_src) { - int result = 0; - - if (mat == NULL || hmt_src == NULL) return -1; + 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 result; + return EXIT_SUCCESS; } int readTexture(HMT_TEXTURE *tex, FILE *hmt_src) { - int result = 0; char u0,u1,bpp; int color_rgba; long pos; @@ -87,13 +83,13 @@ int readTexture(HMT_TEXTURE *tex, FILE *hmt_src) { 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); + getPaletteFromFile(&(tex->image), hmt_src); } fseek(hmt_src, tex->data_offset, SEEK_SET); - //loadSamples(&(tex->image), stream); + getSamplesFromFile(&(tex->image), hmt_src); //decodePixels(&(tex->image)); fseek(hmt_src, pos, SEEK_SET); - return result; + return EXIT_SUCCESS; } diff --git a/src/RS_images.c b/src/RS_images.c index 4812109..d0ab6b6 100644 --- a/src/RS_images.c +++ b/src/RS_images.c @@ -1,5 +1,30 @@ #include "RS_images.h" + +void getPaletteFromFile(RS_IMAGE *img, FILE *f) { + int entries = img->paletteEntries; + + switch (entries) { + case 16: + case 256: + fread(img->palette, sizeof(unsigned char), entries*3, f); + } +} + +void getSamplesFromFile(RS_IMAGE *img, FILE *f) { + int sample_bits = img->sampleBits; + int size = img->width*img->height*div(sample_bits, 8).quot; + + if (f->_bufsiz >= ftell(f)+size) { + printf("WARNING! Please fix size/sample."); + abort(); + } + + img->samples = calloc(1, size); + fread(img->samples, size, 1, f); + if (img->type_ == 2) fread(img->samples, div(size, 4).quot, 1, f); +} + RS_IMAGE_DESC getImageDescFromType(unsigned char type) { RS_IMAGE_DESC desc; diff --git a/src/RS_images.h b/src/RS_images.h index ca12424..0095287 100644 --- a/src/RS_images.h +++ b/src/RS_images.h @@ -3,6 +3,13 @@ #include #include +#if defined(_WIN32) + #include +#else + #include + #include +#endif + ///////////////////////////// ///// Define new types ////// @@ -32,12 +39,12 @@ typedef struct RSImage_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); -void DecodePixel(RS_IMAGE img); -int WritePaletteToFile(RS_IMAGE img, FILE *f); -int WriteSamplesToFile(RS_IMAGE img, FILE *f); +RS_IMAGE_DESC getImageDescFromType(unsigned char type); //TODO: Optimise function +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); +void decodePixel(RS_IMAGE img); +void getPaletteFromFile(RS_IMAGE *img, FILE *f); +void getSamplesFromFile(RS_IMAGE *img, FILE *f); #endif