From 1583aadc4c6e1cc7e9d5b85347d51bf766f75226 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Thu, 13 Mar 2014 11:00:05 +1100 Subject: [PATCH] + image_from_file --- requirements.txt | 3 +++ src/Makefile | 9 ++++++- src/image.c | 63 +++++++++++++++++++++++++++++++++++++++++++++--- src/image.h | 3 ++- src/test-image.c | 16 ++++++++++++ 5 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 requirements.txt create mode 100644 src/test-image.c diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..af8a16d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +libgd2-noxpm-dev +libgdbm-dev +libmagic-dev diff --git a/src/Makefile b/src/Makefile index 025acfa..972b196 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,14 @@ -all: +CFLAGS=-Wall -Wextra -O0 -g -ggdb -pedantic + +all: test-image + +%.c: %.h %.o: %.c gcc -O0 -Wall -Wextra -pedantic -ggdb $< -o $@ clean: rm -f *.o + +test-image: image.c test-image.c + gcc $(CFLAGS) -lmagic -lgd image.c test-image.c -o $@ diff --git a/src/image.c b/src/image.c index 26541cd..11cdef5 100644 --- a/src/image.c +++ b/src/image.c @@ -14,12 +14,67 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA */ +#include +#include + #include "main.h" #include "image.h" -#include - -int image_load(void) +gdImagePtr +image_from_file(const char *path, char **errstr) { - return 1; + const char *mimetype; + magic_t magic; + char err[256] = "\0"; + + FILE *in; + gdImagePtr (*cb)(FILE *fd); + gdImagePtr img; + + if ((magic = magic_open(MAGIC_MIME_TYPE)) == NULL) { + snprintf(err, 256, "%s", magic_error(magic)); + *errstr = err; + return NULL; + } + + if (magic_load(magic, NULL) < 0) { + *errstr = magic_error(magic); + magic_close(magic); + return NULL; + } + + mimetype = magic_file(magic, path); + if (mimetype == NULL) { + snprintf(err, 256, "%s", magic_error(magic)); + *errstr = err; + magic_close(magic); + return NULL; + } + + if (strncmp(mimetype, "image/png", 9) == 0) { + cb = gdImageCreateFromPng; + } else + if (strncmp(mimetype, "image/gif", 9) == 0) { + cb = gdImageCreateFromGif; + } else + if (strncmp(mimetype, "image/jpeg", 10) == 0) { + cb = gdImageCreateFromJpeg; + } else { + snprintf(err, 256, "Can't handle image type '%s'", mimetype); + *errstr = err; + return NULL; + } + magic_close(magic); + + in = fopen(path, "rb"); + if (in == NULL) { + snprintf(err, 256, "Can't open file: %s", strerror(errno)); + *errstr = err; + return NULL; + } + + img = cb(in); + fclose(in); + + return img; } diff --git a/src/image.h b/src/image.h index 0551ace..0e9d491 100644 --- a/src/image.h +++ b/src/image.h @@ -1 +1,2 @@ -int image_load(void); +gdImagePtr +image_from_file(const char *path, char **errstr); diff --git a/src/test-image.c b/src/test-image.c new file mode 100644 index 0000000..3da91ad --- /dev/null +++ b/src/test-image.c @@ -0,0 +1,16 @@ +#include + +#include "main.h" +#include "image.h" + +int main(int argc, char **argv) { + gdImagePtr img = NULL; + char *err; + + img = image_from_file("test.png", &err); + if (img == NULL) { + printf("%s\n", err); + exit(EXIT_FAILURE); + } + exit(EXIT_SUCCESS); +}