X-Git-Url: http://git.datanom.net/lensdb.git/blobdiff_plain/2c9438951b2f16a0470e7240bcb6f2da230ee547..refs/heads/release/0.9.2:/lib/main.cpp diff --git a/lib/main.cpp b/lib/main.cpp index 27efeaa..5bf9c43 100644 --- a/lib/main.cpp +++ b/lib/main.cpp @@ -1,39 +1,133 @@ #include "main.hpp" +#include +#include using namespace std; +static void print_all(gpointer key, gpointer value, gpointer user_data) { + cout << (gchar*)key << " = " << (gchar*)value << endl; +} ExifScanner::ExifScanner(const char* file): file(file) { - + exif_tags = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + get_data_from_image(); } ExifScanner::~ExifScanner() { + g_hash_table_destroy(exif_tags); +} + +std::string ExifScanner::get_error() { + return error; +} + +void ExifScanner::get_data_from_image() { + if (file != NULL) { + // create our ExifTool object + ExifTool *et = new ExifTool(); + // read metadata from the image + TagInfo *info = et->ImageInfo(file); + if (info) { + // print returned information + for (TagInfo *i=info; i; i=i->next) { + g_hash_table_insert(exif_tags, g_strdup(i->name), g_strdup(i->value)); + } + // we are responsible for deleting the information when done + delete info; + } else if (et->LastComplete() <= 0) { + error = "Error executing exiftool!"; + } + // print exiftool stderr messages + char *err = et->GetError(); + if (err) { + error.append("\n"); + error.append(err); + } + delete et; // delete our ExifTool object + } +} + +string ExifScanner::get_maker() { + string maker; + + //g_hash_table_foreach(exif_tags, print_all, NULL); + const gchar* lensId = (gchar *)g_hash_table_lookup(exif_tags, "LensMake"); + gulong len = g_utf8_strlen(lensId, -1); + if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1) { + const gchar* s = (gchar *)g_hash_table_lookup(exif_tags, "LensID"); + if (s && g_strcmp0("Unknown (2822)", s) != 0) { + gchar* pos = g_strstr_len(s, -1, " "); + //cout << pos << endl; + if (pos) { + //cout << "S length: " << strlen(s) << "POS length: " << strlen(pos) << endl; + lensId = g_strndup(s, strlen(s) - strlen(pos)); + } + len = g_utf8_strlen(lensId, -1); + } + } + + if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1) { + const gchar* s = (gchar *)g_hash_table_lookup(exif_tags, "LensModel"); + if (s) { + gchar* pos = g_strstr_len(s, -1, " "); + //cout << pos << endl; + if (pos) { + //cout << "S length: " << strlen(s) << "POS length: " << strlen(pos) << endl; + lensId = g_strndup(s, strlen(s) - strlen(pos)); + len = g_utf8_strlen(lensId, -1); + } + } + } + if (!lensId || len < 1) + maker = get_cscring("Unknown"); + else + maker = get_cscring(lensId); + + return maker; +} + +string ExifScanner::get_serial_number() { + string sn; + + const gchar* id = (gchar *)g_hash_table_lookup(exif_tags, "LensSerialNumber"); + gulong len = g_utf8_strlen(id, -1); + if (!id || len < 1) { + id = (gchar *)g_hash_table_lookup(exif_tags, "SerialNumber"); + len = g_utf8_strlen(id, -1); + } + + if (!id || len < 1) + sn = get_cscring("Unknown"); + else + sn = get_cscring(id); + + return sn; +} + +string ExifScanner::get_lens() { + string lens; + const gchar* lensId = (gchar *)g_hash_table_lookup(exif_tags, "LensID"); + gulong len = g_utf8_strlen(lensId, -1); + if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1) { + lensId = (gchar *)g_hash_table_lookup(exif_tags, "LensModel"); + len = g_utf8_strlen(lensId, -1); + if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1) { + lensId = (gchar *)g_hash_table_lookup(exif_tags, "LensSpec"); + len = g_utf8_strlen(lensId, -1); + } + } + + if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1) + lens = get_cscring("Unknown"); + else + lens = get_cscring(lensId); + + return lens; } -void ExifScanner::scanner_do_work() { - if (file == NULL){ - cout << "Example1: Read metadata from an image." << endl; - cout << "Please specify input file name" << endl; - return; - } - - // create our ExifTool object - ExifTool *et = new ExifTool(); - // read metadata from the image - TagInfo *info = et->ImageInfo(file); - if (info) { - // print returned information - for (TagInfo *i=info; i; i=i->next) { - cout << i->name << " = " << i->value << endl; - } - // we are responsible for deleting the information when done - delete info; - } else if (et->LastComplete() <= 0) { - cerr << "Error executing exiftool!" << endl; - } - // print exiftool stderr messages - char *err = et->GetError(); - if (err) cout << err; - delete et; // delete our ExifTool object +gchar* ExifScanner::get_cscring(string s) { + gchar* cstring = g_strdup(s.c_str()); + + return cstring; }