]> git.datanom.net - lensdb.git/blobdiff - lib/main.cpp
Readd missing feature
[lensdb.git] / lib / main.cpp
index ad1d7048115b92e36978ce819ecf16788cc01926..d8a5c1e7eaaac44a960f6f5e43e72d8e469fa84f 100644 (file)
@@ -1,3 +1,217 @@
- int main(int argc, char** argv) {
-        return 0;
- }
+#include "main.hpp"
+#include <cstring>
+#include <string.h>
+
+using namespace std;
+
+static const char* LensMakers[] = {
+               "7Artisans",
+               "Canon",
+               "Carl Zeiss",
+               "Chinon",
+               "Cosina",
+               "Essilor",
+               "EssilorLuxottica",
+               "Fujifilm",
+               "Goerz",
+               "Hoya",
+               "Kenko",
+               "Konica",
+               "Leica",
+               "Leidolf",
+               "Lumix",
+               "Meopta",
+               "Meyer Optik Görlitz",
+               "Minolta",
+               "Nikon",
+               "Nisi",
+               "Olympus",
+               "OM Digital Solutions",
+               "Pentacon",
+               "Pentax",
+               "Pergear",
+               "Rodenstock",
+               "Samyang",
+               "Schneider Kreuznach",
+               "Seagull",
+               "Seiko",
+               "Sigma",
+               "Silor",
+               "Schneider Kreuznach",
+               "Soligor",
+               "Sony",
+               "Tamron",
+               "Tokina",
+               "Topcon",
+               "TTArtisan",
+               "Yongnuo",
+               "Viltrox",
+               "Voigtländer",
+               NULL
+};
+
+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);
+       gchar* lensId = g_strdup((gchar *)g_hash_table_lookup(exif_tags, "LensMake"));
+       gulong len = char_len(lensId);
+       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;
+                               g_free(lensId);
+                               lensId = g_strndup(s, strlen(s) - strlen(pos));
+                       }
+                       len = g_utf8_strlen(lensId, -1);
+               }
+       }
+
+       if (match_maker(lensId) == false) {
+               g_free(lensId);
+               lensId = NULL;
+       }
+       
+       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;
+                               g_free(lensId);
+                               lensId = g_strndup(s, strlen(s) - strlen(pos));
+                               len = char_len(lensId);
+                       }
+               }
+       }
+
+       if (match_maker(lensId) == false) {
+               g_free(lensId);
+               lensId = NULL;
+       }
+
+       if (!lensId || len < 1)
+               maker = get_cstring("Unknown");
+       else
+               maker = get_cstring(lensId);
+       g_free(lensId);
+       
+       return maker;
+}
+
+string ExifScanner::get_serial_number() {
+       string sn;
+
+       const gchar* id = (gchar *)g_hash_table_lookup(exif_tags, "LensSerialNumber");
+       gulong len = char_len(id);
+       if (!id || len < 1) {
+               id = (gchar *)g_hash_table_lookup(exif_tags, "SerialNumber");
+               len = char_len(id);
+       }
+       
+       if (!id || len < 1)
+               sn = get_cstring("Unknown");
+       else
+               sn = get_cstring(id);
+       
+       return sn;
+}
+
+string ExifScanner::get_lens() {
+       string lens;
+
+       const gchar* lensId = (gchar *)g_hash_table_lookup(exif_tags, "LensID");
+       gulong len = char_len(lensId);
+       if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1) {
+               lensId = (gchar *)g_hash_table_lookup(exif_tags, "LensModel");
+               len = char_len(lensId);
+               if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1) {
+                       lensId = (gchar *)g_hash_table_lookup(exif_tags, "LensSpec");
+                       len = char_len(lensId);
+               }
+       }
+       
+       if (!lensId || g_strcmp0("Unknown (2822)", lensId) == 0 || len < 1)
+               lens = get_cstring("Unknown");
+       else
+               lens = get_cstring(lensId);
+       
+       return lens;
+}
+
+gchar* ExifScanner::get_cstring(string s) {
+       gchar* cstring = g_strdup(s.c_str());
+       
+       return cstring;
+}
+
+bool ExifScanner::match_maker(const gchar* maker) {
+       int i = 0;
+       const char* lensmaker = NULL;
+       
+       if (maker == NULL) return false;
+       
+       lensmaker = LensMakers[i];
+       while (lensmaker) {
+               if (g_ascii_strcasecmp(lensmaker, maker) == 0) {
+                       return true;
+               }
+               i++;
+               lensmaker = LensMakers[i];
+       }
+       
+       return false;
+}
+
+gulong ExifScanner::char_len(const gchar* elem) {
+       if (!elem) return 0;
+       
+       return g_utf8_strlen(elem, -1);
+}
This page took 0.532186 seconds and 5 git commands to generate.