Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/torch/image.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/png.c
diff options
context:
space:
mode:
authorCédric Deltheil <cedric@moodstocks.com>2015-08-30 21:13:49 +0300
committerCédric Deltheil <cedric@moodstocks.com>2015-08-30 21:13:49 +0300
commit0501a75004bec04f4f7dd1ea2e274bb65a5b733e (patch)
tree28f482c6ee2db707ebda9b3a4512d100117f56ec /png.c
parent9871c112954be3b41c1b016b6c85fa272a6c3b6b (diff)
png.c: move apart non generic functions
Diffstat (limited to 'png.c')
-rw-r--r--png.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/png.c b/png.c
index e139bfd..2034639 100644
--- a/png.c
+++ b/png.c
@@ -4,6 +4,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <assert.h>
#define PNG_DEBUG 3
#include <png.h>
@@ -12,6 +13,47 @@
#define torch_Tensor TH_CONCAT_STRING_3(torch., Real, Tensor)
#define libpng_(NAME) TH_CONCAT_3(libpng_, Real, NAME)
+/*
+ * Bookkeeping struct for reading png data from memory
+ */
+typedef struct {
+ unsigned char* buffer;
+ png_size_t offset;
+ png_size_t length;
+} libpng_inmem_buffer;
+
+/*
+ * Call back for reading png data from memory
+ */
+static void
+libpng_userReadData(png_structp pngPtrSrc, png_bytep dest, png_size_t length)
+{
+ libpng_inmem_buffer* src = png_get_io_ptr(pngPtrSrc);
+ assert(src->offset+length <= src->length);
+ memcpy(dest, src->buffer + src->offset, length);
+ src->offset += length;
+}
+
+/*
+ * Error message wrapper (single member struct to preserve `str` size info)
+ */
+typedef struct {
+ char str[256];
+} libpng_errmsg;
+
+/*
+ * Custom error handling function (see `png_set_error_fn`)
+ */
+static void
+libpng_error_fn(png_structp png_ptr, png_const_charp error_msg)
+{
+ libpng_errmsg *errmsg = png_get_error_ptr(png_ptr);
+ int max = sizeof(errmsg->str) - 1;
+ strncpy(errmsg->str, error_msg, max);
+ errmsg->str[max] = '\0';
+ longjmp(png_jmpbuf(png_ptr), 1);
+}
+
#include "generic/png.c"
#include "THGenerateAllTypes.h"