From 09ad65d3dd7a3c64523be57582e7c2dfad54511e Mon Sep 17 00:00:00 2001 From: Xele02 Date: Fri, 14 Dec 2012 23:35:09 +0100 Subject: [PATCH] Add decoding png function for Save Dialog --- Android.mk | 2 ++ image/png_load.cpp | 25 +++++++++++++++++++++++++ image/png_load.h | 3 +++ 3 files changed, 30 insertions(+) diff --git a/Android.mk b/Android.mk index 56edd11034..a8588cbc6e 100644 --- a/Android.mk +++ b/Android.mk @@ -17,6 +17,7 @@ LOCAL_SRC_FILES :=\ base/error_context.cpp \ base/stringutil.cpp \ ext/sha1/sha1.cpp \ + ext/stb_image/stb_image.c \ ext/stb_vorbis/stb_vorbis.c.arm \ ext/vjson/json.cpp \ ext/vjson/block_allocator.cpp \ @@ -48,6 +49,7 @@ LOCAL_SRC_FILES :=\ gfx/texture_atlas.cpp \ gfx/texture_gen.cpp \ image/zim_load.cpp \ + image/png_load.cpp \ ui/ui.cpp \ ui/ui_context.cpp \ ui/screen.cpp \ diff --git a/image/png_load.cpp b/image/png_load.cpp index 803b2e9a0c..3d264879bc 100644 --- a/image/png_load.cpp +++ b/image/png_load.cpp @@ -36,6 +36,31 @@ int pngLoad(const char *file, int *pwidth, int *pheight, unsigned char **image_d return 1; } +int pngLoadPtr(const unsigned char *input_ptr, const int input_len, int *pwidth, int *pheight, unsigned char **image_data_ptr, + bool flip) { + if (flip) + { + ELOG("pngLoad: flip flag not supported, image will be loaded upside down"); + } + + int x,y,n; + unsigned char *data = stbi_load_from_memory(input_ptr,input_len, &x, &y, &n, 4); // 4 = force RGBA + if (!data) + return 0; + + *pwidth = x; + *pheight = y; + // ... process data if not NULL ... + // ... x = width, y = height, n = # 8-bit components per pixel ... + // ... replace '0' with '1'..'4' to force that many components per pixel + // ... but 'n' will always be the number that it would have been if you said 0 + + // TODO: Get rid of this silly copy which is only to make the buffer free-able with free() + *image_data_ptr = (unsigned char *)malloc(x * y * 4); + memcpy(*image_data_ptr, data, x * y * 4); + stbi_image_free(data); + return 1; +} #else diff --git a/image/png_load.h b/image/png_load.h index 5a4ee08c54..1b1babc1af 100644 --- a/image/png_load.h +++ b/image/png_load.h @@ -6,6 +6,9 @@ int pngLoad(const char *file, int *pwidth, int *pheight, unsigned char **image_data_ptr, bool flip); +int pngLoadPtr(const unsigned char *input_ptr, const int input_len, int *pwidth, + int *pheight, unsigned char **image_data_ptr, bool flip); + #endif // _PNG_LOAD_H