From 93d9689cfe1aa32147727c23170f4a039d1a7e33 Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Thu, 4 Jul 2019 16:26:38 +0200 Subject: [PATCH] Done textures decoder Next step, convert to PNG format --- src/RS_images.c | 32 +++++++++++++++++--------------- src/RS_images.h | 6 +++--- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/RS_images.c b/src/RS_images.c index 0283173..a29a080 100644 --- a/src/RS_images.c +++ b/src/RS_images.c @@ -8,6 +8,7 @@ void getPaletteFromFile(RS_IMAGE *img, FILE *f) { case 16: case 256: fread(img->palette, sizeof(unsigned char), entries*3, f); + break; } } @@ -44,60 +45,61 @@ void decodePixels(RS_IMAGE *img) { break; case 4: if (img->paletteEntries == 0) { + size = img->width * img->height; img->pixels = calloc(1, size); unpack4To8bits(img->samples, img->pixels, size); } else if (img->paletteEntries == 16) { + size = img->width * img->height; img->pixels = calloc(1, size*3); unpack4To24bitsRGB(img->samples, img->pixels, size, img->palette); } break; case 8: if (img->paletteEntries == 0) { + size = img->width * img->height; img->pixels = calloc(1, size); memcpy(img->pixels, img->samples, size); } else if (img->paletteEntries == 256) { + size = img->width * img->height; img->pixels = calloc(1, size*3); unpack8To24bitsRGB(img->samples, img->pixels, size, img->palette); } break; case 16: + size = img->width * img->height; img->pixels = calloc(1, size); useOddBytes(img->samples, img->pixels, size); break; } } -void unpack4To8bits(unsigned char *src, unsigned char *dst, int sampling) { +void unpack4To8bits(unsigned char *samples_tab, unsigned char *pixels_tab, int sampling) { int i; char v; for(i=0; i> 4) & 0xF) << 4; - dst[i*2+1] = (v & 0xF) << 4; + v = samples_tab[i]; + pixels_tab[i*2] = ((v >> 4) & 0xF) << 4; + pixels_tab[i*2+1] = (v & 0xF) << 4; } } -void unpack4To24bitsRGB(unsigned char *src, unsigned char *dst, int size, unsigned char pal[256][3]) { +void unpack4To24bitsRGB(unsigned char *samples_tab, unsigned char *pixels_tab, int size, unsigned char pal[256][3]) { int i,index; - //unsigned char _pRGB[3]; //TODO: Can be remove and use 'dst' directly - //_pRGB = dst; for(i=0; i> 4) & 0xF]; - dst[i*2+1] = pal[index & 0xF]; + index = samples_tab[i]; + memcpy(&(pixels_tab[i*2]), pal[(index >> 4) & 0xF], sizeof(unsigned char)*3); + memcpy(&(pixels_tab[i*2+1]), pal[index & 0xF], sizeof(unsigned char)*3); } } -void unpack8To24bitsRGB(unsigned char *src, unsigned char *dst, int size, unsigned char pal[256][3]) { +void unpack8To24bitsRGB(unsigned char *samples_tab, unsigned char *pixels_tab, int size, unsigned char pal[256][3]) { int i,index; - unsigned char _pRGB[3]; //TODO: Can be remove and use 'dst' directly - _pRGB = dst; for(i=0; i