From 577deb028b3bb365c1bc764cd599ccd28c712a57 Mon Sep 17 00:00:00 2001 From: Justin Maggard Date: Thu, 22 Jan 2009 23:48:49 +0000 Subject: [PATCH] If EXIF parsing fails, fall through to reading the JPEG data with libjpeg to get the resolution. --- Makefile | 2 +- metadata.c | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index d2c4a00..ec327d8 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ BASEOBJS = minidlna.o upnphttp.o upnpdescgen.o upnpsoap.o \ ALLOBJS = $(BASEOBJS) $(LNXOBJS) #LIBS = -liptc -LIBS = -lexif -ltag_c -lsqlite3 -lavformat -luuid #-lgd +LIBS = -lexif -ljpeg -ltag_c -lsqlite3 -lavformat -luuid #-lgd TESTUPNPDESCGENOBJS = testupnpdescgen.o upnpdescgen.o diff --git a/metadata.c b/metadata.c index 53bd13b..2af68c9 100644 --- a/metadata.c +++ b/metadata.c @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include #include #include @@ -342,12 +344,26 @@ GetAudioMetadata(const char * path, char * name) return ret; } +/* For libjpeg error handling */ +jmp_buf setjmp_buffer; +static void +libjpeg_error_handler(j_common_ptr cinfo) +{ + cinfo->err->output_message (cinfo); + longjmp(setjmp_buffer, 1); + return; +} + sqlite_int64 GetImageMetadata(const char * path, char * name) { ExifData *ed; ExifEntry *e = NULL; + ExifLoader *l; ExifTag tag; + struct jpeg_decompress_struct cinfo; + struct jpeg_error_mgr jerr; + FILE *infile; int width=0, height=0, thumb=0; off_t size; char date[64], make[32], model[64]; @@ -373,7 +389,7 @@ GetImageMetadata(const char * path, char * name) /* MIME hard-coded to JPEG for now, until we add PNG support */ asprintf(&m.mime, "image/jpeg"); - ExifLoader * l = exif_loader_new(); + l = exif_loader_new(); exif_loader_write_file(l, path); ed = exif_loader_get_data(l); exif_loader_unref(l); @@ -435,6 +451,25 @@ GetImageMetadata(const char * path, char * name) exif_data_unref(ed); + /* If EXIF parsing fails, then fall through to reading the JPEG data with libjpeg to get the resolution */ + if( !width || !height ) + { + infile = fopen(path, "r"); + cinfo.err = jpeg_std_error(&jerr); + jerr.error_exit = libjpeg_error_handler; + jpeg_create_decompress(&cinfo); + if( setjmp(setjmp_buffer) ) + goto error; + jpeg_stdio_src(&cinfo, infile); + jpeg_read_header(&cinfo, TRUE); + jpeg_start_decompress(&cinfo); + width = cinfo.output_width; + height = cinfo.output_height; + error: + jpeg_destroy_decompress(&cinfo); + fclose(infile); + } + if( width <= 640 && height <= 480 ) asprintf(&m.dlna_pn, "JPEG_SM;DLNA.ORG_OP=01;DLNA.ORG_CI=0"); else if( width <= 1024 && height <= 768 )