From 6df772741b423afcc642a474910ad35cdd788f25 Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Sat, 20 Jul 2019 18:39:44 +0200 Subject: [PATCH] Finish grey picture re-encoding for PNG output --- src/HMT_Parser.c | 3 --- src/Image_Exporter.c | 23 +++++++++++++++++------ src/RS_images.c | 22 +++++++++++++++------- src/RS_images.h | 9 +++++---- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/HMT_Parser.c b/src/HMT_Parser.c index 5d4f80c..4848294 100644 --- a/src/HMT_Parser.c +++ b/src/HMT_Parser.c @@ -109,9 +109,6 @@ void purgeHMTFromMemory(HMT_FILE *_f) { 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); } - if (_f->textures_list != NULL) { - if (_f->textures_list->image.pixels != NULL) free(_f->textures_list->image.pixels); - } free(_f->textures_list); if (_f->materials_list != NULL) free(_f->materials_list); diff --git a/src/Image_Exporter.c b/src/Image_Exporter.c index fcf74fc..f430f3c 100644 --- a/src/Image_Exporter.c +++ b/src/Image_Exporter.c @@ -6,10 +6,11 @@ int saveToPNG(RS_IMAGE *img, char *tex_name) { FILE *_png_f = NULL; png_structp png_ptr = NULL; png_infop info_ptr = NULL; - //size_t x,y; + size_t x,y; png_byte **row_ptrs = NULL; + PIXEL *pixel = NULL; //int pixel_size = 3; - //int depth = 8; + //int depth = 8; //bit par color channel (RGB) strcpy(tex_path,tex_name); strcat(tex_path, ".png"); @@ -31,23 +32,33 @@ int saveToPNG(RS_IMAGE *img, char *tex_name) { // Init PNG datas row_ptrs = png_malloc(png_ptr, img->height * sizeof(png_byte *)); - /*for (y=0; yheight; y++) { - png_byte *row = png_malloc(png_ptr, img->width*sizeof(unsigned char)*3); + for (y=0; yheight; y++) { + png_byte *row = png_malloc(png_ptr, img->width*sizeof(PIXEL)); row_ptrs[y] = row; for (x=0; xwidth; x++) { - unsigned char pixel[3] = pixelAt(img->pixels); + pixel = pixelAt(img, x , y); + if(pixel == NULL) return EXIT_FAILURE; + + *row++ = pixel->_red; + *row++ = pixel->_green; + *row++ = pixel->_blue; } - }*/ + } + /* if (img->paletteEntries == 0 || img->sampleBits == 32 || img->sampleBits == 16) { memcpy(row_ptrs, img->pixels, sizeof(*img->pixels)*img->height*img->width); } else { memcpy(row_ptrs, img->pixels, sizeof(*img->pixels)*3*img->height*img->width); } + */ png_init_io(png_ptr, _png_f); png_set_rows(png_ptr, info_ptr, row_ptrs); png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); + for (y=0; yheight; y++) { + png_free(png_ptr, row_ptrs[y]); + } png_free(png_ptr, row_ptrs); png_destroy_write_struct(&png_ptr, &info_ptr); fclose(_png_f); diff --git a/src/RS_images.c b/src/RS_images.c index 6a6e175..f11946c 100644 --- a/src/RS_images.c +++ b/src/RS_images.c @@ -35,7 +35,7 @@ int getSamplesFromFile(RS_IMAGE *img, FILE *f) { void decodePixels(RS_IMAGE *img) { int size; - img->pixels = NULL; + //img->pixels = NULL; if (!(img->type_ == 0 || img->type_ == 1 || img->type_ == 2 || @@ -51,9 +51,9 @@ void decodePixels(RS_IMAGE *img) { break; case 4: if (img->paletteEntries == 0) { - size = img->width * img->height; + size = sizeof(PIXEL) * img->height * img->width; img->pixels = calloc(1, size); - unpack4To8bits(img->samples, img->pixels, size); + convert4bitsGreyto8bitsRGB(img->samples, img->pixels, div(size,3).quot); } else if (img->paletteEntries == 16) { size = img->width * img->height; img->pixels = calloc(1, size*3); @@ -81,14 +81,18 @@ void decodePixels(RS_IMAGE *img) { } } -void unpack4To8bits(unsigned char *samples_tab, unsigned char *pixels_tab, int sampling) { +void convert4bitsGreyto8bitsRGB(unsigned char *samples_tab, PIXEL *pixels_tab, int sampling) { int i; - char v; + unsigned char v; for(i=0; i> 4) & 0xF) << 4; - pixels_tab[i*2+1] = (v & 0xF) << 4; + pixels_tab[i*2]._red = div((v >> 4 & 0xF) * 256, 16).quot; + pixels_tab[i*2]._green = div((v >> 4 & 0xF) * 256, 16).quot; + pixels_tab[i*2]._blue = div((v >> 4 & 0xF) * 256, 16).quot; + pixels_tab[i*2+1]._red = div((v & 0xF) * 256, 16).quot; + pixels_tab[i*2+1]._green = div((v & 0xF) * 256, 16).quot; + pixels_tab[i*2+1]._blue = div((v & 0xF) * 256, 16).quot; } } @@ -119,6 +123,10 @@ void useOddBytes(unsigned char *src, unsigned char *dst, int size) { } } +PIXEL *pixelAt(RS_IMAGE *img, int posX , int posY) { + return img->pixels + img->width * posY + posX; +} + RS_IMAGE_DESC getImageDescFromType(unsigned char type) { RS_IMAGE_DESC desc; diff --git a/src/RS_images.h b/src/RS_images.h index f3ba2fc..d3be214 100644 --- a/src/RS_images.h +++ b/src/RS_images.h @@ -21,9 +21,9 @@ * @brief RGB pixel structure, used to store datas for implementation inside PNG files */ typedef struct PixelRGB { - char _red; - char _green; - char _blue; + unsigned char _red; + unsigned char _green; + unsigned char _blue; }PIXEL; /** @@ -51,11 +51,12 @@ typedef struct RSImage_desc { ///// Declare functions ///// ///////////////////////////// RS_IMAGE_DESC getImageDescFromType(unsigned char type); //TODO: Optimise function -void unpack4To8bits(unsigned char *samples_tab, unsigned char *pixels_tab, int sampling); +void convert4bitsGreyto8bitsRGB(unsigned char *samples_tab, PIXEL *pixels_tab, int sampling); void unpack4To24bitsRGB(unsigned char *samples_tab, unsigned char *pixels_tab, int size, unsigned char pal[256][3]); void unpack8To24bitsRGB(unsigned char *samples_tab, unsigned char *pixels_tab, int size, unsigned char pal[256][3]); void useOddBytes(unsigned char *src, unsigned char *dst, int size); void decodePixels(RS_IMAGE *img); +PIXEL *pixelAt(RS_IMAGE *img, int posX , int posY); int getPaletteFromFile(RS_IMAGE *img, FILE *f); int getSamplesFromFile(RS_IMAGE *img, FILE *f);