Browse Source

+ image_from_file

master
Alex 'AdUser' Z 11 years ago
parent
commit
1583aadc4c
  1. 3
      requirements.txt
  2. 9
      src/Makefile
  3. 63
      src/image.c
  4. 3
      src/image.h
  5. 16
      src/test-image.c

3
requirements.txt

@ -0,0 +1,3 @@
libgd2-noxpm-dev
libgdbm-dev
libmagic-dev

9
src/Makefile

@ -1,7 +1,14 @@
all: CFLAGS=-Wall -Wextra -O0 -g -ggdb -pedantic
all: test-image
%.c: %.h
%.o: %.c %.o: %.c
gcc -O0 -Wall -Wextra -pedantic -ggdb $< -o $@ gcc -O0 -Wall -Wextra -pedantic -ggdb $< -o $@
clean: clean:
rm -f *.o rm -f *.o
test-image: image.c test-image.c
gcc $(CFLAGS) -lmagic -lgd image.c test-image.c -o $@

63
src/image.c

@ -14,12 +14,67 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
*/ */
#include <gd.h>
#include <magic.h>
#include "main.h" #include "main.h"
#include "image.h" #include "image.h"
#include <gd.h> gdImagePtr
image_from_file(const char *path, char **errstr)
int image_load(void)
{ {
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;
} }

3
src/image.h

@ -1 +1,2 @@
int image_load(void); gdImagePtr
image_from_file(const char *path, char **errstr);

16
src/test-image.c

@ -0,0 +1,16 @@
#include <gd.h>
#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);
}
Loading…
Cancel
Save