From 0ea6dc0445be243a9a2903dbf405cb16e32cdda1 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Thu, 19 Feb 2015 04:13:58 +0100 Subject: [PATCH] (MPNG) Updates --- gfx/image/image_mpng.c | 62 ++++++++++++++-------- libretro-common/formats/mpng/mpng_decode.c | 6 ++- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/gfx/image/image_mpng.c b/gfx/image/image_mpng.c index 5b4d493e57..240eab2891 100644 --- a/gfx/image/image_mpng.c +++ b/gfx/image/image_mpng.c @@ -38,13 +38,28 @@ void texture_image_free(struct texture_image *img) } -bool texture_image_load_file(struct nbio_t* read, - void *ptr, const char *path, size_t *len) +bool texture_image_load(struct texture_image *out_img, const char *path) { + size_t len; bool looped = false; - ptr = nbio_get_ptr(read, len); - if (*len != 1024*1024) + bool ret = false; + bool use_rgba = driver.gfx_use_rgba; + unsigned a_shift = 24; + unsigned r_shift = use_rgba ? 0 : 16; + unsigned g_shift = 8; + unsigned b_shift = use_rgba ? 16 : 0; + + void *ptr = NULL; + struct mpng_image img = {0}; + struct nbio_t* read = nbio_open(path, NBIO_READ); + + if (!read) + return false; + + ptr = nbio_get_ptr(read, &len); + + if (len != 1024*1024) RARCH_ERR("ERROR: wrong size (2).\n"); if (ptr) RARCH_WARN("Read pointer is available before iterating?\n"); @@ -57,29 +72,13 @@ bool texture_image_load_file(struct nbio_t* read, if (!looped) RARCH_LOG("Read finished immediately?\n"); - ptr = nbio_get_ptr(read, len); + ptr = nbio_get_ptr(read, &len); - if (*len != 1024*1024) + if (len != 1024*1024) puts("ERROR: wrong size (3)"); if (*(char*)ptr != 0x42 || memcmp(ptr, (char*)ptr+1, 1024*1024-1)) puts("ERROR: wrong data"); - - return true; -} - -bool texture_image_load(struct texture_image *out_img, const char *path) -{ - size_t len; - void *ptr = NULL; - struct mpng_image img = {0}; - struct nbio_t* read = nbio_open(path, NBIO_READ); - - if (!read) - return false; - - texture_image_load_file(read, ptr, path, &len); - if (!png_decode(ptr, len, &img, FMT_ARGB8888)) goto error; @@ -87,6 +86,25 @@ bool texture_image_load(struct texture_image *out_img, const char *path) out_img->height = img.height; out_img->pixels = img.pixels; + /* This is quite uncommon. */ + if (a_shift != 24 || r_shift != 16 || g_shift != 8 || b_shift != 0) + { + uint32_t i; + uint32_t num_pixels = out_img->width * out_img->height; + uint32_t *pixels = (uint32_t*)out_img->pixels; + + for (i = 0; i < num_pixels; i++) + { + uint32_t col = pixels[i]; + uint8_t a = (uint8_t)(col >> 24); + uint8_t r = (uint8_t)(col >> 16); + uint8_t g = (uint8_t)(col >> 8); + uint8_t b = (uint8_t)(col >> 0); + pixels[i] = (a << a_shift) | + (r << r_shift) | (g << g_shift) | (b << b_shift); + } + } + nbio_free(read); return true; diff --git a/libretro-common/formats/mpng/mpng_decode.c b/libretro-common/formats/mpng/mpng_decode.c index 6e54676b79..50e05fc99a 100644 --- a/libretro-common/formats/mpng/mpng_decode.c +++ b/libretro-common/formats/mpng/mpng_decode.c @@ -51,7 +51,8 @@ static tinfl_status tinfl_decompress(tinfl_decompressor* r, static void tinfl_deinit(tinfl_decompressor* r) { - inflateEnd(r); + if (r) + inflateEnd(r); } #endif @@ -75,11 +76,12 @@ uint32_t read32r(const uint8_t* source) bool png_decode(const void * pngdata, size_t pnglen, struct mpng_image * img, enum video_format format) { - tinfl_decompressor inflator; unsigned i; unsigned b, x, y; + tinfl_decompressor inflator = {0}; + unsigned int bitsperchannel = 0; unsigned int colortype = 0; unsigned int compressiontype = 0;