From 293424637531569fb5536049a0c73b30837d86f5 Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Mon, 8 Jul 2019 23:00:08 +0200 Subject: [PATCH] Fixed pixels not decoding correctly TODO: Try to make first PNG file --- src/HMT_Parser.c | 4 ++-- src/HMT_Parser.h | 19 +++++++++++-------- src/RS_images.c | 21 ++++++++++++++------- src/RS_images.h | 4 ++-- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/HMT_Parser.c b/src/HMT_Parser.c index c6713cf..5d4f80c 100644 --- a/src/HMT_Parser.c +++ b/src/HMT_Parser.c @@ -13,7 +13,7 @@ HMT_FILE *parseHMTFile(FILE *hmt_src) { // Read materials printf("\n\nMaterials detected: %d\n", _buff->material_count); - _buff->materials_list = calloc(_buff->material_count, sizeof(HMT_MATERIAL)); + _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) { @@ -27,7 +27,7 @@ HMT_FILE *parseHMTFile(FILE *hmt_src) { 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)); + _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) { diff --git a/src/HMT_Parser.h b/src/HMT_Parser.h index c26f7ed..ceae4aa 100644 --- a/src/HMT_Parser.h +++ b/src/HMT_Parser.h @@ -37,7 +37,6 @@ typedef struct HMTMaterial { int zero; int hex_a; char name[16]; - struct HMTMaterial *_next; }HMT_MATERIAL; /** @@ -76,18 +75,18 @@ typedef struct HMTTexture { unsigned long width, height; char name[16]; RS_IMAGE image; - struct HMTTexture *_next; }HMT_TEXTURE; /** - * @brief HMT file struct + * @brief Instance of HMTFile in memory + * This structure contain all parsed data for a single HMT file. */ typedef struct HMTFile { - int material_count; - int texture_offset; - int texture_count; - HMT_MATERIAL *materials_list; - HMT_TEXTURE *textures_list; + int material_count; /**< Number of materials registered inside HMTFile instance. */ + int texture_offset; /**< Address from which texture data begins. */ + int texture_count; /**< Number of textures registered inside HMTFile instance. */ + HMT_MATERIAL *materials_list; /**< Starting pointer of the materials list. Managed like an array and declared as a pointer */ + HMT_TEXTURE *textures_list; /**< Starting pointer of the textures list. Managed like an array and declared as a pointer*/ }HMT_FILE; @@ -97,6 +96,10 @@ typedef struct HMTFile { HMT_FILE *parseHMTFile(FILE *hmt_src); int readMaterial(HMT_MATERIAL *mat, FILE *hmt_src); int readTexture(HMT_TEXTURE *tex, FILE *hmt_src); +HMT_MATERIAL *getMaterialFromIndex(int i); +HMT_MATERIAL *getMaterialFromName(char *matName); +HMT_TEXTURE *getTextureFromIndex(int i); +HMT_TEXTURE *getTextureFromMaterial(HMT_MATERIAL *mat); void purgeHMTFromMemory(HMT_FILE *_f); #endif diff --git a/src/RS_images.c b/src/RS_images.c index fbb81da..fa6b447 100644 --- a/src/RS_images.c +++ b/src/RS_images.c @@ -16,7 +16,7 @@ int getPaletteFromFile(RS_IMAGE *img, FILE *f) { int getSamplesFromFile(RS_IMAGE *img, FILE *f) { int sample_bits = img->sampleBits; - int size = img->width*img->height*div(sample_bits, 8).quot; + int size = div(img->width*img->height*sample_bits, 8).quot; if (f->_bufsiz >= ftell(f)+size) { printf("WARNING! Please fix size/sample."); @@ -27,6 +27,13 @@ int getSamplesFromFile(RS_IMAGE *img, FILE *f) { fread(img->samples, size, 1, f); if (img->type_ == 2) fread(img->samples, div(size, 4).quot, 1, f); + /* + unsigned char temp[size]; + for (int i=0; isamples[i]; + } + */ + return EXIT_SUCCESS; } @@ -34,12 +41,12 @@ void decodePixels(RS_IMAGE *img) { int size; img->pixels = NULL; - if (img->type_ != 0 || - img->type_ != 1 || - img->type_ != 2 || - img->type_ != 3 || - img->type_ != 4 || - img->type_ != 5) return; + if (!(img->type_ == 0 || + img->type_ == 1 || + img->type_ == 2 || + img->type_ == 3 || + img->type_ == 4 || + img->type_ == 5)) return; switch (img->sampleBits) { case 32: diff --git a/src/RS_images.h b/src/RS_images.h index d74b387..2522fb0 100644 --- a/src/RS_images.h +++ b/src/RS_images.h @@ -26,8 +26,8 @@ typedef struct RSImage { unsigned char type_; /**< Image type (0 = RBG/4bits per pixel, 1 = RBG/8bpp, 3 = RGBA/32bpp , 4 = grayscale/4bpp, 5 = grayscale/8bpp */ unsigned char sampleBits; /**< Bits per samble */ int paletteEntries; - unsigned char *pixels; /**< Image pixels list */ - unsigned char *samples; /**< Image samples list */ + unsigned char *pixels; /**< Image pixels list, managed like an array and declared as a pointer */ + unsigned char *samples; /**< Image samples list managed like an array and declared as a pointer */ unsigned char palette[256][3]; /**< Image palette definition */ //TODO: Create union struct type instead }RS_IMAGE;