This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project gfxprim.git.
The branch, generate has been updated via 570b772500147f49fedbc21b6d2aedc7be13a16b (commit) via 784faa3832daa85cf2f0a8d399ef557645b4d271 (commit) from 0543a967e4953d105ccbb8ca1bd7fd2915a4a298 (commit)
Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below.
- Log ----------------------------------------------------------------- http://repo.or.cz/w/gfxprim.git/commit/570b772500147f49fedbc21b6d2aedc7be13a...
commit 570b772500147f49fedbc21b6d2aedc7be13a16b Author: Cyril Hrubis metan@ucw.cz Date: Thu Sep 22 19:43:30 2011 +0200
Added basic PNG support using libpng.
* RGB888 * Grayscale 1BPP - 8BPP
diff --git a/include/loaders/GP_Loaders.h b/include/loaders/GP_Loaders.h index e03f15d..99253bf 100644 --- a/include/loaders/GP_Loaders.h +++ b/include/loaders/GP_Loaders.h @@ -36,4 +36,6 @@ #include "GP_PGM.h" #include "GP_PPM.h"
+#include "GP_PNG.h" + #endif /* GP_LOADERS_H */ diff --git a/include/loaders/GP_Loaders.h b/include/loaders/GP_PNG.h similarity index 72% copy from include/loaders/GP_Loaders.h copy to include/loaders/GP_PNG.h index e03f15d..2b02deb 100644 --- a/include/loaders/GP_Loaders.h +++ b/include/loaders/GP_PNG.h @@ -16,24 +16,36 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * - * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos * - * jiri.bluebear.dluhos@gmail.com * - * * * Copyright (C) 2009-2011 Cyril Hrubis metan@ucw.cz * * * *****************************************************************************/
/* - - Core include file for loaders API. + + PNG support using libpng library.
*/
-#ifndef GP_LOADERS_H -#define GP_LOADERS_H +#ifndef GP_PNG_H +#define GP_PNG_H + +#include "core/GP_Context.h" + +/* + * Opens up file and checks signature. Upon successful return the file + * possition would be set to eight bytes (exactly after the PNG signature). + */ +GP_RetCode GP_OpenPNG(const char *src_path, FILE **f); + +/* + * Reads PNG from an open FILE. Expects the file possition set after the eight + * bytes PNG signature. + */ +GP_RetCode GP_ReadPNG(FILE *f, GP_Context **res);
-#include "GP_PBM.h" -#include "GP_PGM.h" -#include "GP_PPM.h" +/* + * Loads a PNG file into GP_Context. The Context is newly allocated. + */ +GP_RetCode GP_LoadPNG(const char *src_path, GP_Context **res);
-#endif /* GP_LOADERS_H */ +#endif /* GP_PNG_H */ diff --git a/libs/loaders/GP_PNG.c b/libs/loaders/GP_PNG.c new file mode 100644 index 0000000..a533287 --- /dev/null +++ b/libs/loaders/GP_PNG.c @@ -0,0 +1,181 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * Gfxprim is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2009-2010 Cyril Hrubis metan@ucw.cz * + * * + *****************************************************************************/ + +/* + + PNG image support using libpng. + + */ + +#include <stdint.h> +#include <inttypes.h> + +#include <errno.h> +#include <string.h> + +#include <png.h> + +#include <GP_Context.h> +#include <GP_Debug.h> + +GP_RetCode GP_OpenPNG(const char *src_path, FILE **f) +{ + uint8_t sig[8]; + + *f = fopen(src_path, "r"); + + if (*f == NULL) { + GP_DEBUG(1, "Failed to open '%s' : %s", + src_path, strerror(errno)); + return GP_EBADFILE; + } + + if (fread(sig, 1, 8, *f) <= 0) { + GP_DEBUG(1, "Failed to read '%s' : %s", + src_path, strerror(errno)); + goto err; + } + + if (png_sig_cmp(sig, 0, 8)) { + GP_DEBUG(1, "Invalid file header, '%s' not a PNG image?", + src_path); + goto err; + } + + GP_DEBUG(1, "Found PNG signature in '%s'", src_path); + + return GP_ESUCCESS; +err: + fclose(*f); + *f = NULL; + return GP_EBADFILE; +} + +GP_RetCode GP_ReadPNG(FILE *f, GP_Context **res) +{ + png_structp png; + png_infop png_info = NULL; + png_uint_32 w, h; + int depth, color_type; + GP_PixelType pixel_type = GP_PIXEL_UNKNOWN; + GP_RetCode ret; + + png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + + if (png == NULL) { + GP_DEBUG(1, "Failed to allocate PNG read buffer"); + ret = GP_ENOMEM; + goto err1; + } + + png_info = png_create_info_struct(png); + + if (png_info == NULL) { + GP_DEBUG(1, "Failed to allocate PNG info buffer"); + ret = GP_ENOMEM; + goto err2; + } + + if (setjmp(png_jmpbuf(png))) { + GP_DEBUG(1, "Failed to read PNG file :("); + ret = GP_EBADFILE; + goto err2; + } + + png_init_io(png, f); + png_set_sig_bytes(png, 8); + png_read_info(png, png_info); + + png_get_IHDR(png, png_info, &w, &h, &depth, + &color_type, NULL, NULL, NULL); + + GP_DEBUG(2, "Have %s PNG%s size %ux%u depth %i", + color_type & PNG_COLOR_MASK_COLOR ? "color" : "gray", + color_type & PNG_COLOR_MASK_ALPHA ? " with alpha channel" : "", + w, h, depth); + + switch (color_type) { + case PNG_COLOR_TYPE_GRAY: + switch (depth) { + case 1: + pixel_type = GP_PIXEL_G1; + break; + case 2: + pixel_type = GP_PIXEL_G2; + break; + case 4: + pixel_type = GP_PIXEL_G4; + break; + case 8: + pixel_type = GP_PIXEL_G8; + break; + } + break; + case PNG_COLOR_TYPE_RGB: + + png_set_bgr(png); + + switch (depth) { + case 8: + pixel_type = GP_PIXEL_RGB888; + break; + } + break; + } + + if (pixel_type == GP_PIXEL_UNKNOWN) { + ret = GP_ENOIMPL; + goto err2; + } + + *res = GP_ContextAlloc(w, h, pixel_type); + + if (*res == NULL) { + ret = GP_ENOMEM; + goto err2; + } + + uint32_t y; + + for (y = 0; y < h; y++) { + png_bytep addr = GP_PIXEL_ADDR(*res, 0, y); + png_read_rows(png, &addr, NULL, 1); + } + + ret = GP_ESUCCESS; +err2: + png_destroy_read_struct(&png, png_info ? &png_info : NULL, NULL); +err1: + fclose(f); + return ret; +} + +GP_RetCode GP_LoadPNG(const char *src_path, GP_Context **res) +{ + FILE *f; + GP_RetCode ret; + + if ((ret = GP_OpenPNG(src_path, &f))) + return ret; + + return GP_ReadPNG(f, res); +} diff --git a/tests/SDL/Makefile b/tests/SDL/Makefile index 86108c0..e5d9500 100644 --- a/tests/SDL/Makefile +++ b/tests/SDL/Makefile @@ -3,7 +3,7 @@ TOPDIR=../.. CSOURCES=$(shell echo *.c)
INCLUDE=core gfx SDL backends -LDLIBS+=-lGP -L$(TOPDIR)/build/ -lGP_SDL -lSDL +LDLIBS+=-lGP -L$(TOPDIR)/build/ -lGP_SDL -lSDL -lpng
APPS=pixeltest fileview fonttest linetest randomshapetest shapetest sierpinsky symbolstest textaligntest trianglefps input blittest subcontext ppm_test http://repo.or.cz/w/gfxprim.git/commit/784faa3832daa85cf2f0a8d399ef557645b4d...
commit 784faa3832daa85cf2f0a8d399ef557645b4d271 Author: Cyril Hrubis metan@ucw.cz Date: Thu Sep 22 19:35:00 2011 +0200
Add missing braces around macro parameter.
diff --git a/include/core/GP_Context.h b/include/core/GP_Context.h index b4d5ce6..6bc244c 100644 --- a/include/core/GP_Context.h +++ b/include/core/GP_Context.h @@ -63,9 +63,9 @@ static inline GP_PixelType GP_GetContextPixelType(const GP_Context *context) * Rows and columns are specified in the image's orientation * (i.e. they might not be XY if the image is rotated). */ -#define GP_PIXEL_ADDR(context, x, y) ((context->pixels - + y * context->bytes_per_row - + (x * context->bpp) / 8)) +#define GP_PIXEL_ADDR(context, x, y) (((context)->pixels + + y * (context)->bytes_per_row + + (x * (context)->bpp) / 8))
#define GP_CALC_ROW_SIZE(pixel_type, width) ((GP_PixelSize(pixel_type) * width) / 8 + -----------------------------------------------------------------------
Summary of changes: include/core/GP_Context.h | 6 +- include/loaders/GP_Loaders.h | 2 + include/{filters/GP_Resize.h => loaders/GP_PNG.h} | 31 +++- libs/loaders/GP_PNG.c | 181 +++++++++++++++++++++ tests/SDL/Makefile | 2 +- 5 files changed, 210 insertions(+), 12 deletions(-) copy include/{filters/GP_Resize.h => loaders/GP_PNG.h} (73%) create mode 100644 libs/loaders/GP_PNG.c
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos@gmail.com if you want to unsubscribe, or site admin admin@repo.or.cz if you receive no reply.