Browse Source

* image_t instead direct work with gd

master
Alex 'AdUser' Z 11 years ago
parent
commit
1106b40385
  1. 14
      src/image.c
  2. 8
      src/image.h
  3. 2
      src/test-image.c

14
src/image.c

@ -20,7 +20,7 @@
#include "main.h" #include "main.h"
#include "image.h" #include "image.h"
gdImagePtr image_t *
image_from_file(const char *path, char **errstr) image_from_file(const char *path, char **errstr)
{ {
const char *mimetype; const char *mimetype;
@ -29,17 +29,21 @@ image_from_file(const char *path, char **errstr)
FILE *in; FILE *in;
gdImagePtr (*cb)(FILE *fd); gdImagePtr (*cb)(FILE *fd);
gdImagePtr img; image_t *img = NULL;
CALLOC(img, 1, sizeof(image_t));
if ((magic = magic_open(MAGIC_MIME_TYPE)) == NULL) { if ((magic = magic_open(MAGIC_MIME_TYPE)) == NULL) {
snprintf(err, 256, "%s", magic_error(magic)); snprintf(err, 256, "%s", magic_error(magic));
*errstr = err; *errstr = err;
FREE(img);
return NULL; return NULL;
} }
if (magic_load(magic, NULL) < 0) { if (magic_load(magic, NULL) < 0) {
*errstr = magic_error(magic); *errstr = magic_error(magic);
magic_close(magic); magic_close(magic);
FREE(img);
return NULL; return NULL;
} }
@ -48,6 +52,7 @@ image_from_file(const char *path, char **errstr)
snprintf(err, 256, "%s", magic_error(magic)); snprintf(err, 256, "%s", magic_error(magic));
*errstr = err; *errstr = err;
magic_close(magic); magic_close(magic);
FREE(img);
return NULL; return NULL;
} }
@ -62,18 +67,21 @@ image_from_file(const char *path, char **errstr)
} else { } else {
snprintf(err, 256, "Can't handle image type '%s'", mimetype); snprintf(err, 256, "Can't handle image type '%s'", mimetype);
*errstr = err; *errstr = err;
FREE(img);
return NULL; return NULL;
} }
STRNDUP(img->mime, mimetype, 16);
magic_close(magic); magic_close(magic);
in = fopen(path, "rb"); in = fopen(path, "rb");
if (in == NULL) { if (in == NULL) {
snprintf(err, 256, "Can't open file: %s", strerror(errno)); snprintf(err, 256, "Can't open file: %s", strerror(errno));
*errstr = err; *errstr = err;
FREE(img);
return NULL; return NULL;
} }
img = cb(in); img->data = cb(in);
fclose(in); fclose(in);
return img; return img;

8
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); image_from_file(const char *path, char **errstr);

2
src/test-image.c

@ -4,7 +4,7 @@
#include "image.h" #include "image.h"
int main(int argc, char **argv) { int main(int argc, char **argv) {
gdImagePtr img = NULL; image_t *img = NULL;
char *err; char *err;
img = image_from_file("test.png", &err); img = image_from_file("test.png", &err);

Loading…
Cancel
Save