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 af5fd7582e11df218ea97fa3d5299a86e0a07194 (commit) from c019f09ad68b300c79e25e59465f0a80ef999c7c (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/af5fd7582e11df218ea97fa3d5299a86e0a07...
commit af5fd7582e11df218ea97fa3d5299a86e0a07194 Author: Cyril Hrubis metan@ucw.cz Date: Wed Sep 21 12:44:06 2011 +0200
Working on PPM loader (binary works).
diff --git a/include/loaders/GP_Loaders.h b/include/loaders/GP_Loaders.h index e7ac305..e03f15d 100644 --- a/include/loaders/GP_Loaders.h +++ b/include/loaders/GP_Loaders.h @@ -34,7 +34,6 @@
#include "GP_PBM.h" #include "GP_PGM.h" - -struct GP_Context *GP_Load(const char *src_path); +#include "GP_PPM.h"
#endif /* GP_LOADERS_H */ diff --git a/include/loaders/GP_Loaders.h b/include/loaders/GP_PPM.h similarity index 78% copy from include/loaders/GP_Loaders.h copy to include/loaders/GP_PPM.h index e7ac305..78ba9f7 100644 --- a/include/loaders/GP_Loaders.h +++ b/include/loaders/GP_PPM.h @@ -16,25 +16,17 @@ * 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. - - */ +#ifndef GP_PPM_H +#define GP_PPM_H
-#ifndef GP_LOADERS_H -#define GP_LOADERS_H +#include "core/GP_Context.h"
-#include "GP_PBM.h" -#include "GP_PGM.h" +GP_RetCode GP_LoadPPM(const char *src_path, GP_Context **res);
-struct GP_Context *GP_Load(const char *src_path); +GP_RetCode GP_SavePPM(const char *res_path, GP_Context *src);
-#endif /* GP_LOADERS_H */ +#endif /* GP_PPM_H */ diff --git a/include/loaders/GP_Loaders.h b/libs/loaders/GP_PPM.c similarity index 54% copy from include/loaders/GP_Loaders.h copy to libs/loaders/GP_PPM.c index e7ac305..1eec6e9 100644 --- a/include/loaders/GP_Loaders.h +++ b/libs/loaders/GP_PPM.c @@ -16,25 +16,93 @@ * 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 * * * *****************************************************************************/
- /* +/* + + PPM portable bitmap loader/saver. + + */ + +#include <stdint.h> + +#include <GP_Debug.h> +#include <GP_Context.h> +#include <GP_Pixel.h> +#include <GP_GetPutPixel.h> + +#include "GP_PXMCommon.h" + +int load_binary_ppm(FILE *f, uint32_t w, uint32_t h, uint32_t depth, + GP_Context *res) +{ + uint32_t x, y; + int r, g, b; + + for (x = 0; x < w; x++) + for (y = 0; y < h; y++) { + r = fgetc(f); + g = fgetc(f); + b = fgetc(f); + + if (r == EOF || g == EOF || b == EOF) { + GP_DEBUG(1, "Unexpected end of PBM file"); + return 1; + } + + //TODO depth + GP_Pixel pix = GP_Pixel_CREATE_RGB888(r, g, b); + GP_PutPixel_Raw_24BPP(res, x, y, pix); + } + + return 0; +} + +GP_RetCode GP_LoadPPM(const char *src_path, GP_Context **res) +{ + uint32_t w, h, depth; + char fmt; + FILE *f; + + f = GP_OpenPNM(src_path, &fmt, &w, &h, &depth); + + if (f == NULL) + return GP_EBADFILE;
- Core include file for loaders API. + if (fmt != '3' && fmt != '6') { + GP_DEBUG(1, "Asked to load PPM but header is 'P%c'", fmt); + goto err1; + }
- */ + *res = GP_ContextAlloc(w, h, GP_PIXEL_RGB888);
-#ifndef GP_LOADERS_H -#define GP_LOADERS_H + if (res == NULL) + goto err1;
-#include "GP_PBM.h" -#include "GP_PGM.h" + switch (fmt) { + case '3': + //TODO + fclose(f); + free(res); + return GP_ENOIMPL; + case '6': + if (load_binary_ppm(f, w, h, depth, *res)) + goto err2; + break; + }
-struct GP_Context *GP_Load(const char *src_path); + fclose(f); + return GP_ESUCCESS; +err2: + free(*res); +err1: + fclose(f); + return GP_EBADFILE; +}
-#endif /* GP_LOADERS_H */ +GP_RetCode GP_SavePPM(const char *res_path, GP_Context *src) +{ + return GP_ENOIMPL; +} diff --git a/libs/loaders/GP_PXMCommon.c b/libs/loaders/GP_PXMCommon.c index 62ee36b..9b250bd 100644 --- a/libs/loaders/GP_PXMCommon.c +++ b/libs/loaders/GP_PXMCommon.c @@ -23,10 +23,15 @@ * * *****************************************************************************/
-#include <stdio.h> #include <stdint.h> #include <inttypes.h>
+#include <ctype.h> +#include <errno.h> +#include <string.h> + +#include <GP_Debug.h> + #include "GP_PXMCommon.h"
static int read_comment(FILE *f) @@ -381,3 +386,127 @@ GP_RetCode GP_PXMSave8bpp(FILE *f, GP_Context *context)
return GP_ESUCCESS; } + +static void try_read_comments(FILE *f) +{ + char c1, c2; + + while (isspace(c1 = fgetc(f))); + + ungetc(c1, f); + + while ((c1 = fgetc(f)) == '#') { + do { + c2 = fgetc(f); + } while (c2 != 'n' && c2 != EOF); + } + + ungetc(c1, f); +} + +/* + + PNM portable bitmap header loader. + + Format: + + a magick number value of 'P' and one of + '1' - PBM 2bpp gray ASCII + '2' - PGM gray ASCII + '3' - PPM rgb888 ASCII + '4' - PBM 2bpp gray BINARY + '5' - PGM gray BINARY + '6' - PPM rgb888 BINARY + whitespace (blanks, TABs, CRs, LFs). + ascii width + whitespace + ascii height + whitespace + maximal value (interval is 0 ... max) (not applicable for PBM) + width * height ascii or binary values + + lines starting with '#' are comments to the end of line + + */ + +static char *pnm_names[] = { + "ASCII encoded PBM", + "ASCII encoded PGM", + "ASCII encoded PPM", + "BINARY encoded PBM", + "BINARY encoded PGM", + "BINARY encoded PPM", +}; + +FILE *GP_OpenPNM(const char *src_path, char *fmt, uint32_t *w, uint32_t *h, + uint32_t *depth) +{ + FILE *f = fopen(src_path, "r"); + int ch; + + if (f == NULL) { + GP_DEBUG(1, "Failed to open file '%s': %s", + src_path, strerror(errno)); + return NULL; + } + + ch = fgetc(f); + + if (ch != 'P') { + GP_DEBUG(1, "Invalid PNM header start '%c' (0x%2x) expecting 'P'", + isprint(ch) ? ch : ' ', ch); + goto err1; + } + + ch = fgetc(f); + + switch (ch) { + case '4': + case '1': + *depth = 1; + break; + case '2': + case '3': + case '5': + case '6': + break; + default: + GP_DEBUG(1, "Invalid PNM format 'P%c' (0x%2x)", + isprint(ch) ? ch : ' ', ch); + goto err1; + } + + *fmt = ch; + + try_read_comments(f); + + if (fscanf(f, "%"PRIu32"n", w) < 1) { + GP_DEBUG(1, "Failed to read PNM header width"); + goto err1; + } + + try_read_comments(f); + + if (fscanf(f, "%"PRIu32"n", h) < 1) { + GP_DEBUG(1, "Failed to read PNM header height"); + goto err1; + } + + GP_DEBUG(2, "Have %s size %"PRIu32"x%"PRIu32, + pnm_names[*fmt - '1'], *w, *h); + + if (*fmt == '1' || *fmt == '3') + return f; + + try_read_comments(f); + + if (fscanf(f, "%"PRIu32"n", depth) < 1) { + GP_DEBUG(1, "Failed to read PNM header depth"); + goto err1; + } + + return f; +err1: + fclose(f); + return NULL; +} diff --git a/libs/loaders/GP_PXMCommon.h b/libs/loaders/GP_PXMCommon.h index 0c52e71..0c231e2 100644 --- a/libs/loaders/GP_PXMCommon.h +++ b/libs/loaders/GP_PXMCommon.h @@ -53,4 +53,11 @@ GP_RetCode GP_PXMLoad2bpp(FILE *f, GP_Context *context); GP_RetCode GP_PXMLoad4bpp(FILE *f, GP_Context *context); GP_RetCode GP_PXMLoad8bpp(FILE *f, GP_Context *context);
+/* + * Loads image header, returns pointer to FILE* on success, fills image + * metadata into arguments. + */ +FILE *GP_OpenPNM(const char *src_path, char *fmt, uint32_t *w, uint32_t *h, + uint32_t *depth); + #endif /* GP_PXM_COMMON_H */ diff --git a/tests/SDL/ball.ppm b/tests/SDL/ball.ppm new file mode 100644 index 0000000..1ced238 Binary files /dev/null and b/tests/SDL/ball.ppm differ diff --git a/tests/SDL/blittest.c b/tests/SDL/blittest.c index 2e34564..1f77533 100644 --- a/tests/SDL/blittest.c +++ b/tests/SDL/blittest.c @@ -164,7 +164,7 @@ int main(int argc, char *argv[])
GP_RetCode ret;
- if ((ret = GP_LoadPGM("ball.pgm", &bitmap_raw))) { + if ((ret = GP_LoadPPM("ball.ppm", &bitmap_raw))) { fprintf(stderr, "Failed to load bitmap: %sn", GP_RetCodeName(ret)); return 1; }
-----------------------------------------------------------------------
Summary of changes: include/loaders/GP_Loaders.h | 3 +- libs/core/GP_Debug.c => include/loaders/GP_PPM.h | 17 +-- libs/loaders/{GP_PBM.c => GP_PPM.c} | 112 +++++++++---------- libs/loaders/GP_PXMCommon.c | 131 +++++++++++++++++++++- libs/loaders/GP_PXMCommon.h | 7 + tests/SDL/ball.ppm | Bin 0 -> 30054 bytes tests/SDL/blittest.c | 2 +- 7 files changed, 201 insertions(+), 71 deletions(-) copy libs/core/GP_Debug.c => include/loaders/GP_PPM.h (88%) copy libs/loaders/{GP_PBM.c => GP_PPM.c} (56%) create mode 100644 tests/SDL/ball.ppm
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.