From 1106b40385c5fc23b43cd7b72cc893810ce833da Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Thu, 13 Mar 2014 13:01:39 +1100 Subject: [PATCH] * image_t instead direct work with gd --- src/image.c | 14 +++++++++++--- src/image.h | 8 +++++++- src/test-image.c | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/image.c b/src/image.c index 11cdef5..1c86168 100644 --- a/src/image.c +++ b/src/image.c @@ -20,7 +20,7 @@ #include "main.h" #include "image.h" -gdImagePtr +image_t * image_from_file(const char *path, char **errstr) { const char *mimetype; @@ -29,17 +29,21 @@ image_from_file(const char *path, char **errstr) FILE *in; gdImagePtr (*cb)(FILE *fd); - gdImagePtr img; + image_t *img = NULL; + + CALLOC(img, 1, sizeof(image_t)); if ((magic = magic_open(MAGIC_MIME_TYPE)) == NULL) { snprintf(err, 256, "%s", magic_error(magic)); *errstr = err; + FREE(img); return NULL; } if (magic_load(magic, NULL) < 0) { *errstr = magic_error(magic); magic_close(magic); + FREE(img); return NULL; } @@ -48,6 +52,7 @@ image_from_file(const char *path, char **errstr) snprintf(err, 256, "%s", magic_error(magic)); *errstr = err; magic_close(magic); + FREE(img); return NULL; } @@ -62,18 +67,21 @@ image_from_file(const char *path, char **errstr) } else { snprintf(err, 256, "Can't handle image type '%s'", mimetype); *errstr = err; + FREE(img); return NULL; } + STRNDUP(img->mime, mimetype, 16); magic_close(magic); in = fopen(path, "rb"); if (in == NULL) { snprintf(err, 256, "Can't open file: %s", strerror(errno)); *errstr = err; + FREE(img); return NULL; } - img = cb(in); + img->data = cb(in); fclose(in); return img; diff --git a/src/image.h b/src/image.h index 0e9d491..6f916f0 100644 --- a/src/image.h +++ b/src/image.h @@ -1,2 +1,8 @@ -gdImagePtr +typedef struct +{ + gdImagePtr data; + char *mime; +} image_t; + +image_t * image_from_file(const char *path, char **errstr); diff --git a/src/test-image.c b/src/test-image.c index 3da91ad..0225944 100644 --- a/src/test-image.c +++ b/src/test-image.c @@ -4,7 +4,7 @@ #include "image.h" int main(int argc, char **argv) { - gdImagePtr img = NULL; + image_t *img = NULL; char *err; img = image_from_file("test.png", &err);