From b211f6ea7fb36071bf1c88467a9488a431c0dee8 Mon Sep 17 00:00:00 2001 From: gc Date: Sun, 17 Jun 2007 17:34:11 +0000 Subject: [PATCH] also use libexif for sorting a directory according to EXIF date, makes operation 40 times faster --- bin/booh | 4 ++-- ext/rbbooh.c | 67 +++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/bin/booh b/bin/booh index 2ce0159..c9ac026 100755 --- a/bin/booh +++ b/bin/booh @@ -2155,8 +2155,8 @@ def sort_by_exif_date pb.text = f pb.fraction = i.to_f / current_order.size Gtk.main_iteration while Gtk.events_pending? - date_time = `identify -format "%[EXIF:DateTimeOriginal]" '#{from_utf8($current_path + "/" + f)}'`.chomp - if $? == 0 && date_time != '' + date_time = Exif.datetimeoriginal(from_utf8($current_path + "/" + f)) + if ! date_time.nil? dates[f] = date_time end end diff --git a/ext/rbbooh.c b/ext/rbbooh.c index 9f76c1c..d9bc20b 100644 --- a/ext/rbbooh.c +++ b/ext/rbbooh.c @@ -91,36 +91,68 @@ gammacorrect(self, level) return self; } -static VALUE exif_orientation(VALUE module, VALUE filename) { - VALUE ret = Qnil; - ExifData* data; +static ExifEntry* exif_entry(VALUE filename, ExifTag tag, ExifData** data) { unsigned int i; - data = exif_data_new_from_file(StringValuePtr(filename)); - if (data == NULL) { + *data = exif_data_new_from_file(StringValuePtr(filename)); + if (*data == NULL) { fprintf(stderr, "libexif failed loading file '%s'\n", StringValuePtr(filename)); - return Qnil; + return NULL; } for (i = 0; i < EXIF_IFD_COUNT; i++) { - ExifEntry* entry = exif_content_get_entry(data->ifd[i], EXIF_TAG_ORIENTATION); + ExifEntry* entry = exif_content_get_entry((*data)->ifd[i], tag); if (entry) { - ExifByteOrder o; - ExifShort v_short; - if (!entry || !entry->parent || !entry->parent->parent) - break; - o = exif_data_get_byte_order(entry->parent->parent); - v_short = exif_get_short(entry->data, o); - ret = INT2NUM(v_short); - break; + return entry; } + } + + return NULL; +} + +static VALUE exif_orientation(VALUE module, VALUE filename) { + ExifData* data = NULL; + ExifEntry* entry; + + entry = exif_entry(filename, EXIF_TAG_ORIENTATION, &data); + + if (entry != NULL) { + VALUE ret; + ExifByteOrder o; + ExifShort v_short; + if (!entry || !entry->parent || !entry->parent->parent) + return Qnil; + o = exif_data_get_byte_order(entry->parent->parent); + v_short = exif_get_short(entry->data, o); + ret = INT2NUM(v_short); + exif_data_free(data); + return ret; } + + if (data) + exif_data_free(data); + return Qnil; +} + +static VALUE exif_datetimeoriginal(VALUE module, VALUE filename) { + ExifData* data = NULL; + ExifEntry* entry; + + entry = exif_entry(filename, EXIF_TAG_DATE_TIME_ORIGINAL, &data); - exif_data_free(data); - return ret; + if (entry != NULL) { + VALUE ret = rb_str_new((const char*) entry->data, entry->size); + exif_data_free(data); + return ret; + } + + if (data) + exif_data_free(data); + return Qnil; } + void Init_libadds() { @@ -130,4 +162,5 @@ Init_libadds() VALUE exif = rb_define_module("Exif"); rb_define_module_function(exif, "orientation", exif_orientation, 1); + rb_define_module_function(exif, "datetimeoriginal", exif_datetimeoriginal, 1); } -- 2.21.3