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 c8554f3ed96486d4ef20838b260c369f61c61b3c (commit) from 298530749895f3e6eb2805c4cb422e217e85586a (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/c8554f3ed96486d4ef20838b260c369f61c61...
commit c8554f3ed96486d4ef20838b260c369f61c61b3c Author: Cyril Hrubis metan@ucw.cz Date: Tue Oct 25 19:40:48 2011 +0200
Loaders: Added experimental JPEG support.
diff --git a/app.mk b/app.mk index 4c90884..9b31c14 100644 --- a/app.mk +++ b/app.mk @@ -2,6 +2,8 @@ ifndef APPS $(error APPS not defined, fix your library Makefile) endif
+LDFLAGS+=-lpng -ljpeg + all: $(APPS)
CLEAN+=$(APPS) diff --git a/include/loaders/GP_Loaders.h b/include/loaders/GP_JPG.h similarity index 74% copy from include/loaders/GP_Loaders.h copy to include/loaders/GP_JPG.h index a245e9a..bef6e10 100644 --- a/include/loaders/GP_Loaders.h +++ b/include/loaders/GP_JPG.h @@ -16,33 +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. + + JPG support using jpeg library.
*/
-#ifndef GP_LOADERS_H -#define GP_LOADERS_H +#ifndef GP_JPG_H +#define GP_JPG_H
#include "core/GP_Context.h"
-#include "GP_PBM.h" -#include "GP_PGM.h" -#include "GP_PPM.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_OpenJPG(const char *src_path, FILE **f);
-#include "GP_PNG.h" +/* + * Reads PNG from an open FILE. Expects the file possition set after the eight + * bytes PNG signature. + */ +GP_RetCode GP_ReadJPG(FILE *f, GP_Context **res);
/* - * Tries to load image accordingly to extension. + * Loads a PNG file into GP_Context. The Context is newly allocated. */ -GP_RetCode GP_LoadImage(const char *src_path, GP_Context **res); +GP_RetCode GP_LoadJPG(const char *src_path, GP_Context **res);
-#endif /* GP_LOADERS_H */ +#endif /* GP_JPG_H */ diff --git a/include/loaders/GP_Loaders.h b/include/loaders/GP_Loaders.h index a245e9a..e3ff703 100644 --- a/include/loaders/GP_Loaders.h +++ b/include/loaders/GP_Loaders.h @@ -40,6 +40,8 @@
#include "GP_PNG.h"
+#include "GP_JPG.h" + /* * Tries to load image accordingly to extension. */ diff --git a/libs/loaders/GP_JPG.c b/libs/loaders/GP_JPG.c new file mode 100644 index 0000000..69ddd55 --- /dev/null +++ b/libs/loaders/GP_JPG.c @@ -0,0 +1,182 @@ +/***************************************************************************** + * 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-2011 Cyril Hrubis metan@ucw.cz * + * * + *****************************************************************************/ + +/* + + JPG image support using jpeg library. + + */ + +#include <stdint.h> +#include <inttypes.h> + +#include <errno.h> +#include <string.h> +#include <stdio.h> +#include <setjmp.h> + +#include <jpeglib.h> + +#include <GP_Context.h> +#include <GP_Debug.h> + +GP_RetCode GP_OpenJPG(const char *src_path, FILE **f) +{ + *f = fopen(src_path, "rb"); + + if (*f == NULL) { + GP_DEBUG(1, "Failed to open '%s' : %s", + src_path, strerror(errno)); + return GP_EBADFILE; + } + + //TODO: check signature and rewind the stream + + return GP_ESUCCESS; +} + +struct my_jpg_err { + struct jpeg_error_mgr error_mgr; + + jmp_buf setjmp_buf; +}; + +static void my_error_exit(j_common_ptr cinfo) +{ + struct my_jpg_err *my_err = (struct my_jpg_err*) cinfo->err; + + GP_DEBUG(1, "ERROR reading jpeg file"); + + longjmp(my_err->setjmp_buf, 1); +} + +static const char *get_colorspace(J_COLOR_SPACE color_space) +{ + switch (color_space) { + case JCS_GRAYSCALE: + return "Grayscale"; + case JCS_RGB: + return "RGB"; + case JCS_YCbCr: + return "YCbCr"; + case JCS_CMYK: + return "CMYK"; + case JCS_YCCK: + return "YCCK"; + default: + return "Unknown"; + }; +} + +GP_RetCode GP_ReadJPG(FILE *f, GP_Context **res) +{ + struct jpeg_decompress_struct cinfo; + struct my_jpg_err my_err; + GP_Context *ret = NULL; + + cinfo.err = jpeg_std_error(&my_err.error_mgr); + my_err.error_mgr.error_exit = my_error_exit; + + if (setjmp(my_err.setjmp_buf)) { + jpeg_destroy_decompress(&cinfo); + GP_ContextFree(ret); + fclose(f); + return GP_EBADFILE; + } + + jpeg_create_decompress(&cinfo); + jpeg_stdio_src(&cinfo, f); + + jpeg_read_header(&cinfo, TRUE); + + GP_DEBUG(1, "Have %s JPEG size %ux%u %i channels", + get_colorspace(cinfo.jpeg_color_space), + cinfo.image_width, cinfo.image_height, + cinfo.num_components); + + GP_Pixel pixel_type; + + switch (cinfo.out_color_space) { + case JCS_GRAYSCALE: + pixel_type = GP_PIXEL_G8; + break; + case JCS_RGB: + pixel_type = GP_PIXEL_RGB888; + break; + default: + pixel_type = GP_PIXEL_UNKNOWN; + } + + if (pixel_type == GP_PIXEL_UNKNOWN) { + GP_DEBUG(1, "Can't handle %s JPEG output format", + get_colorspace(cinfo.out_color_space)); + jpeg_destroy_decompress(&cinfo); + fclose(f); + return GP_EBADFILE; + } + + ret = GP_ContextAlloc(cinfo.image_width, cinfo.image_height, + pixel_type); + + if (ret == NULL) { + GP_DEBUG(1, "Malloc failed :("); + jpeg_destroy_decompress(&cinfo); + fclose(f); + return GP_ENOMEM; + } + + jpeg_start_decompress(&cinfo); + + while (cinfo.output_scanline < cinfo.output_height) { + uint32_t y = cinfo.output_scanline; + + JSAMPROW addr = (void*)GP_PIXEL_ADDR(ret, 0, y); + jpeg_read_scanlines(&cinfo, &addr, 1); + + //TODO: fixme bigendian? + /* fix the pixel, as we want in fact BGR */ + uint32_t i; + + for (i = 0; i < ret->w; i++) { + uint8_t *pix = GP_PIXEL_ADDR(ret, i, y); + GP_SWAP(pix[0], pix[2]); + } + } + + jpeg_finish_decompress(&cinfo); + jpeg_destroy_decompress(&cinfo); + fclose(f); + *res = ret; + + return GP_ESUCCESS; +} + +GP_RetCode GP_LoadJPG(const char *src_path, GP_Context **res) +{ + FILE *f; + GP_RetCode ret; + + if ((ret = GP_OpenJPG(src_path, &f))) + return ret; + + return GP_ReadJPG(f, res); +} diff --git a/libs/loaders/GP_Loaders.c b/libs/loaders/GP_Loaders.c index 63f1da5..8f69d03 100644 --- a/libs/loaders/GP_Loaders.c +++ b/libs/loaders/GP_Loaders.c @@ -56,6 +56,20 @@ GP_RetCode GP_LoadImage(const char *src_path, GP_Context **res) src_path[len - 3] == 'P') ret = GP_LoadPNG(src_path, res); break; + case 'p': + case 'P': + if (src_path[len - 3] == 'j' || + src_path[len - 3] == 'J') + ret = GP_LoadJPG(src_path, res); + break; + case 'e': + case 'E': + if ((src_path[len - 3] == 'p' || + src_path[len - 3] == 'P') && + (src_path[len - 4] == 'j' || + src_path[len - 4] == 'J')) + ret = GP_LoadJPG(src_path, res); + break; } break; /* PPM, PGM, PBM, PNM */
-----------------------------------------------------------------------
Summary of changes: app.mk | 2 + include/loaders/{GP_PNG.h => GP_JPG.h} | 14 ++-- include/loaders/GP_Loaders.h | 2 + libs/loaders/GP_JPG.c | 182 ++++++++++++++++++++++++++++++++ libs/loaders/GP_Loaders.c | 14 +++ 5 files changed, 207 insertions(+), 7 deletions(-) copy include/loaders/{GP_PNG.h => GP_JPG.h} (88%) create mode 100644 libs/loaders/GP_JPG.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.