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, master has been updated via 986b9dbf07932b754d452f84271e0a28c5bfd684 (commit) via 910f02b31ebb40a594febaa6069b77c569568123 (commit) from bcb2459351a67a9e7e5eabaf1fe081cc1aa724bd (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/986b9dbf07932b754d452f84271e0a28c5bfd...
commit 986b9dbf07932b754d452f84271e0a28c5bfd684 Author: Cyril Hrubis metan@ucw.cz Date: Tue Jun 12 22:37:31 2012 +0200
loaders: Add TmpFile interface.
diff --git a/demos/c_simple/Makefile b/demos/c_simple/Makefile index 22efd37..44926f8 100644 --- a/demos/c_simple/Makefile +++ b/demos/c_simple/Makefile @@ -6,7 +6,7 @@ INCLUDE= LDLIBS+=-lGP -lGP_backends -lSDL -L$(TOPDIR)/build/
APPS=backend_example loaders_example loaders filters_symmetry gfx_koch- virtual_backend_example meta_data meta_data_dump + virtual_backend_example meta_data meta_data_dump tmp_file
include $(TOPDIR)/pre.mk include $(TOPDIR)/app.mk diff --git a/include/loaders/GP_Loaders.h b/demos/c_simple/tmp_file.c similarity index 51% copy from include/loaders/GP_Loaders.h copy to demos/c_simple/tmp_file.c index c7aab95..99becbc 100644 --- a/include/loaders/GP_Loaders.h +++ b/demos/c_simple/tmp_file.c @@ -16,63 +16,95 @@ * 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-2012 Cyril Hrubis metan@ucw.cz * * * *****************************************************************************/
/*
- Core include file for loaders API. + Tmp file is interface for storing GP_Context data on disk in uncompressed + fast but non-portable format (more or less GP_Context dump).
*/
-#ifndef LOADERS_GP_LOADERS_H -#define LOADERS_GP_LOADERS_H - -#include "core/GP_Context.h" -#include "core/GP_ProgressCallback.h" - -#include "GP_PBM.h" -#include "GP_PGM.h" -#include "GP_PPM.h" - -#include "GP_BMP.h" -#include "GP_PNG.h" -#include "GP_JPG.h" -#include "GP_GIF.h" - -#include "GP_MetaData.h" - -/* - * Tries to load image accordingly to the file extension. - * - * If operation fails NULL is returned and errno is filled. - */ -GP_Context *GP_LoadImage(const char *src_path, GP_ProgressCallback *callback); - -/* - * Loads image Meta Data (if possible). - */ -int GP_LoadMetaData(const char *src_path, GP_MetaData *data); - -/* - * Simple saving function, the image format is matched by file extension. - * - * Retruns zero on succes. - * - * On failure non-zero is returned. - * - * When file type wasn't recognized by extension or if support for requested - * image format wasn't compiled in non-zero is returned and errno is set to - * ENOSYS. - * - * The resulting errno may also be set to any possible error from fopen(3), open(3), - * write(3), fwrite(3), seek(3), etc.. - */ -int GP_SaveImage(const GP_Context *src, const char *dst_path, - GP_ProgressCallback *callback); - -#endif /* LOADERS_GP_LOADERS_H */ +#include <stdio.h> +#include <string.h> +#include <errno.h> + +#include <GP.h> + +struct callback_priv { + char *op; + char *name; +}; + +static int progress_callback(GP_ProgressCallback *self) +{ + struct callback_priv *priv = self->priv; + + printf("r%s '%s' %3.1f%%", priv->op, priv->name, self->percentage); + fflush(stdout); + + return 0; +} + +int main(int argc, char *argv[]) +{ + GP_Context *img; + struct callback_priv priv; + GP_ProgressCallback callback = {.callback = progress_callback, + .priv = &priv}; + + if (argc != 2) { + fprintf(stderr, "Takes an image as an parametern"); + return 1; + } + + priv.op = "Loading"; + priv.name = argv[1]; + + img = GP_LoadImage(argv[1], &callback); + + if (img == NULL) { + fprintf(stderr, "Failed to load image '%s':%sn", argv[1], + strerror(errno)); + return 1; + } + + printf("n"); + + priv.op = "Saving"; + priv.name = "tmp.gfx"; + + if (GP_SaveTmpFile(img, priv.name, &callback)) { + fprintf(stderr, "Failed to save temp file %sn", strerror(errno)); + return 1; + } + + printf("n"); + + GP_ContextFree(img); + + priv.op = "Loading"; + + img = GP_LoadTmpFile(priv.name, &callback); + + if (img == NULL) { + fprintf(stderr, "Failed to load temp file %sn", strerror(errno)); + return 1; + } + + priv.op = "Saving"; + priv.name = "out.png"; + + printf("n"); + + if (GP_SavePNG(img, "out.png", &callback)) { + fprintf(stderr, "Failed to save image %sn", strerror(errno)); + return 1; + } + + printf("n"); + + return 0; +} diff --git a/include/loaders/GP_Loaders.h b/include/loaders/GP_Loaders.h index c7aab95..ab0f4ef 100644 --- a/include/loaders/GP_Loaders.h +++ b/include/loaders/GP_Loaders.h @@ -44,6 +44,8 @@ #include "GP_JPG.h" #include "GP_GIF.h"
+#include "GP_TmpFile.h" + #include "GP_MetaData.h"
/* diff --git a/include/loaders/GP_Loaders.h b/include/loaders/GP_TmpFile.h similarity index 56% copy from include/loaders/GP_Loaders.h copy to include/loaders/GP_TmpFile.h index c7aab95..fe1f8d1 100644 --- a/include/loaders/GP_Loaders.h +++ b/include/loaders/GP_TmpFile.h @@ -16,63 +16,43 @@ * 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-2012 Cyril Hrubis metan@ucw.cz * * * *****************************************************************************/
/* - - Core include file for loaders API. + + This is interface for saving GP_Context into non-portable uncompressed file, + which is usefull for caching GP_Context to disk.
*/
-#ifndef LOADERS_GP_LOADERS_H -#define LOADERS_GP_LOADERS_H +#ifndef LOADERS_GP_TMP_FILE_H +#define LOADERS_GP_TMP_FILE_H
#include "core/GP_Context.h" #include "core/GP_ProgressCallback.h"
-#include "GP_PBM.h" -#include "GP_PGM.h" -#include "GP_PPM.h" - -#include "GP_BMP.h" -#include "GP_PNG.h" -#include "GP_JPG.h" -#include "GP_GIF.h" - -#include "GP_MetaData.h" - /* - * Tries to load image accordingly to the file extension. + * The possible errno values: * - * If operation fails NULL is returned and errno is filled. + * - Anything FILE operation may return (fopen(), fclose(), fseek(), ...). + * - EIO for fread()/fwrite() failure + * - ENOMEM from malloc() + * - ECANCELED when call was aborted from callback */ -GP_Context *GP_LoadImage(const char *src_path, GP_ProgressCallback *callback);
/* - * Loads image Meta Data (if possible). + * Opens up and loads file. + * + * On failure NULL is returned and errno is set. */ -int GP_LoadMetaData(const char *src_path, GP_MetaData *data); +GP_Context *GP_LoadTmpFile(const char *src_path, GP_ProgressCallback *callback);
/* - * Simple saving function, the image format is matched by file extension. - * - * Retruns zero on succes. - * - * On failure non-zero is returned. - * - * When file type wasn't recognized by extension or if support for requested - * image format wasn't compiled in non-zero is returned and errno is set to - * ENOSYS. - * - * The resulting errno may also be set to any possible error from fopen(3), open(3), - * write(3), fwrite(3), seek(3), etc.. + * Saves context into a file. On failure non-zero is returned and errno is set. */ -int GP_SaveImage(const GP_Context *src, const char *dst_path, - GP_ProgressCallback *callback); +int GP_SaveTmpFile(const GP_Context *src, const char *dst_path, + GP_ProgressCallback *callback);
-#endif /* LOADERS_GP_LOADERS_H */ +#endif /* LOADERS_GP_TMP_FILE_H */ diff --git a/libs/loaders/GP_TmpFile.c b/libs/loaders/GP_TmpFile.c new file mode 100644 index 0000000..9d4a412 --- /dev/null +++ b/libs/loaders/GP_TmpFile.c @@ -0,0 +1,176 @@ +/***************************************************************************** + * 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-2012 Cyril Hrubis metan@ucw.cz * + * * + *****************************************************************************/ + +#include <stdio.h> +#include <errno.h> +#include <string.h> + +#include "core/GP_Debug.h" + +#include "GP_TmpFile.h" + +const char file_sig[] = {'G', 'F', 'X', 'p', 'r', 'i', 'm'}; + +GP_Context *GP_LoadTmpFile(const char *src_path, GP_ProgressCallback *callback) +{ + FILE *f; + uint32_t w, h, y; + uint8_t offset, flags; + uint32_t bpr; + enum GP_PixelType pixel_type; + char sig[sizeof(file_sig)]; + GP_Context *ret; + int err; + + f = fopen(src_path, "r"); + + if (f == NULL) + return NULL; + + /* Read an signature */ + if (fread(sig, sizeof(sig), 1, f) != 1) { + err = EIO; + goto err0; + } + + if (strncmp(sig, file_sig, sizeof(sig))) { + GP_WARN("Invalid file '%s' signature", src_path); + err = EINVAL; + goto err0; + } + + /* Read context metadata */ + fread(&w, sizeof(w), 1, f); + fread(&h, sizeof(h), 1, f); + fread(&offset, sizeof(offset), 1, f); + fread(&bpr, sizeof(bpr), 1, f); + fread(&pixel_type, sizeof(pixel_type), 1, f); + fread(&flags, 1, 1, f); + + if (ferror(f)) { + err = EIO; + goto err0; + } + + ret = GP_ContextAlloc(w, h, pixel_type); + + if (ret == NULL) { + err = errno; + goto err0; + } + + //TODO: We may disagree here on ill aligned subcontexts + GP_ASSERT(ret->bytes_per_row == bpr, "Invalid bytes per row"); + + ret->offset = offset; + + /* And pixels */ + for (y = 0; y < h; y++) { + if (fread(ret->pixels + bpr * y, bpr, 1, f) != 1) { + err = EIO; + goto err1; + } + + GP_ProgressCallbackReport(callback, y, h, w); + } + + /* Set the rotation flags */ + if (flags & 0x01) + ret->axes_swap = 1; + + if (flags & 0x02) + ret->x_swap = 1; + + if (flags & 0x04) + ret->y_swap = 1; + + fclose(f); + + GP_ProgressCallbackDone(callback); + + return ret; +err1: + GP_ContextFree(ret); +err0: + fclose(f); + errno = err; + return NULL; +} + +int GP_SaveTmpFile(const GP_Context *src, const char *dst_path, + GP_ProgressCallback *callback) +{ + FILE *f; + int err; + uint32_t y; + + f = fopen(dst_path, "w"); + + if (f == NULL) + return 1; + + /* Write a signature */ + fwrite(file_sig, sizeof(file_sig), 1, f); + + /* Write block of metadata */ + fwrite(&src->w, sizeof(src->w), 1, f); + fwrite(&src->h, sizeof(src->h), 1, f); + fwrite(&src->offset, sizeof(src->offset), 1, f); + fwrite(&src->bytes_per_row, sizeof(src->bytes_per_row), 1, f); + fwrite(&src->pixel_type, sizeof(src->pixel_type), 1, f); + + uint8_t flags = 0; + + if (src->axes_swap) + flags |= 0x01; + + if (src->x_swap) + flags |= 0x02; + + if (src->y_swap) + flags |= 0x04; + + fwrite(&flags, 1, 1, f); + + /* And pixels */ + for (y = 0; y < src->h; y++) { + if (fwrite(src->pixels + src->bytes_per_row * y, src->bytes_per_row, 1, f) != 1) { + err = EIO; + goto err1; + } + + GP_ProgressCallbackReport(callback, y, src->h, src->w); + } + + if (fclose(f)) + goto err0; + + GP_ProgressCallbackDone(callback); + + return 0; +err1: + fclose(f); +err0: + unlink(dst_path); + errno = err; + return 1; +}
http://repo.or.cz/w/gfxprim.git/commit/910f02b31ebb40a594febaa6069b77c569568...
commit 910f02b31ebb40a594febaa6069b77c569568123 Author: Cyril Hrubis metan@ucw.cz Date: Tue Jun 12 21:50:42 2012 +0200
core: Make sure Context functions set errno on failure.
diff --git a/libs/core/GP_Context.c b/libs/core/GP_Context.c index 3cf3ecf..ee7cc2d 100644 --- a/libs/core/GP_Context.c +++ b/libs/core/GP_Context.c @@ -23,6 +23,9 @@ * * *****************************************************************************/
+#include <errno.h> +#include <string.h> + #include "GP_Debug.h" #include "GP_Transform.h" #include "GP_Pixel.h" @@ -30,8 +33,6 @@ #include "GP_Context.h" #include "GP_Blit.h"
-#include <string.h> - static uint32_t get_bpr(uint32_t bpp, uint32_t w) { return (bpp * w) / 8 + !!((bpp * w) % 8); @@ -52,6 +53,7 @@ GP_Context *GP_ContextAlloc(GP_Size w, GP_Size h, GP_PixelType type) free(pixels); free(context); GP_WARN("Malloc failed :("); + errno = ENOMEM; return NULL; }
@@ -146,6 +148,7 @@ GP_Context *GP_ContextCopy(const GP_Context *src, int flags) free(pixels); free(new); GP_WARN("Malloc failed :("); + errno = ENOMEM; return NULL; }
@@ -209,6 +212,7 @@ GP_Context *GP_SubContextAlloc(const GP_Context *context,
if (res == NULL) { GP_WARN("Malloc failed :("); + errno = ENOMEM; return NULL; }
-----------------------------------------------------------------------
Summary of changes: demos/c_simple/Makefile | 2 +- demos/c_simple/{loaders.c => tmp_file.c} | 31 ++++- include/loaders/GP_Loaders.h | 2 + include/loaders/{GP_GIF.h => GP_TmpFile.h} | 33 +++--- libs/core/GP_Context.c | 8 +- libs/loaders/GP_TmpFile.c | 176 ++++++++++++++++++++++++++++ 6 files changed, 225 insertions(+), 27 deletions(-) copy demos/c_simple/{loaders.c => tmp_file.c} (81%) copy include/loaders/{GP_GIF.h => GP_TmpFile.h} (72%) create mode 100644 libs/loaders/GP_TmpFile.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.