From 2c9438951b2f16a0470e7240bcb6f2da230ee547 Mon Sep 17 00:00:00 2001 From: Michael Rasmussen Date: Thu, 24 Oct 2024 02:25:00 +0200 Subject: [PATCH] Implement Exif scanner Signed-off-by: Michael Rasmussen --- Makefile.am | 4 +- configure.ac | 10 +++- lib/Makefile.am | 13 ++++-- lib/exiftool/ExifTool.h | 93 +++++++++++++++++++++++++++++++++++++ lib/exiftool/ExifToolPipe.h | 43 +++++++++++++++++ lib/exiftool/TagInfo.h | 35 ++++++++++++++ lib/main.cpp | 42 +++++++++++++++-- lib/main.hpp | 43 +++++++++++++++++ lib/scanner.cpp | 59 +++++++++++++++++++++++ lib/scanner.h | 45 ++++++++++++++++++ src/Makefile.am | 14 +++++- src/main.c | 8 +++- 12 files changed, 397 insertions(+), 12 deletions(-) create mode 100644 lib/exiftool/ExifTool.h create mode 100644 lib/exiftool/ExifToolPipe.h create mode 100644 lib/exiftool/TagInfo.h create mode 100644 lib/main.hpp create mode 100644 lib/scanner.cpp create mode 100644 lib/scanner.h diff --git a/Makefile.am b/Makefile.am index 420aa24..401c4a6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,8 +3,8 @@ AUTOMAKE_OPTIONS = gnu ACLOCAL_AMFLAGS = -I m4 SUBDIRS = \ - src \ - lib + lib \ + src EXTRA_DIST = \ AUTHORS \ diff --git a/configure.ac b/configure.ac index 4245496..f628fb8 100644 --- a/configure.ac +++ b/configure.ac @@ -25,11 +25,13 @@ GLIB_REQUIRED=2.50.0 GOBJECT_REQUIRED=2.50.0 GTK_REQUIRED=3.24 SQLITE3_REQUIRED=3.40 +EXIV2_REQUIRED=0.28 AC_SUBST(GLIB_REQUIRED) AC_SUBST(GOBJECT_REQUIRED) AC_SUBST(GTK_REQUIRED) AC_SUBST(SQLITE3_REQUIRED) +AC_SUBST(EXIV2_REQUIRED) AC_CONFIG_HEADERS(config.h) @@ -41,6 +43,7 @@ AC_PROG_LN_S AC_PROG_MAKE_SET AC_PROG_CPP LT_INIT + if test -n "$lt_prog_compiler_pic"; then CFLAGS="$CFLAGS $lt_prog_compiler_pic" fi @@ -58,9 +61,10 @@ enable_exif_scanner=yes AC_MSG_CHECKING([whether to build EXIF scanner]) if test x"$HAVE_CXX" = xno; then AC_MSG_RESULT(no) - AC_MSG_ERROR("Auto read EXIF cannot be built; missing $dependencies_missing") + AC_MSG_WARN("Auto read EXIF cannot be built; missing dependencies") enable_exif_scanner=no else + LIBS+="-lstdc++" AC_MSG_RESULT(yes) fi @@ -86,6 +90,10 @@ PKG_CHECK_MODULES([SQLITE3], [sqlite3 >= $SQLITE3_REQUIRED]) AC_SUBST(SQLITE3_LIBS) AC_SUBST(SQLITE3_CFLAGS) +PKG_CHECK_MODULES([EXIV2], [exiv2 >= $EXIV2_REQUIRED]) +AC_SUBST(EXIV2_LIBS) +AC_SUBST(EXIV2_CFLAGS) + # Checks for header files. AC_CHECK_HEADER([assert.h], AC_DEFINE([HAVE_ASSERT_H], [1], [Define to 1 if you have the header file.])) diff --git a/lib/Makefile.am b/lib/Makefile.am index bb28425..0082fae 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,17 +1,22 @@ AUTOMAKE_OPTIONS = gnu -IFLAGS = \ +AM_CPPFLAGS = \ -I${top_srcdir} \ - -I${top_builddir} + -I${top_builddir} \ + @EXIV2_CFLAGS@ if BUILD_EXIF_SCANNER noinst_LIBRARIES = libscanner.a libscanner_a_SOURCES = \ - main.cpp + main.cpp \ + main.hpp \ + scanner.h \ + scanner.cpp libscanner_a_LIBADD = \ $(LIBOBJS) \ - $(ALLOCA) + $(ALLOCA) \ + @EXIV2_LIBS@ endif diff --git a/lib/exiftool/ExifTool.h b/lib/exiftool/ExifTool.h new file mode 100644 index 0000000..247ef1b --- /dev/null +++ b/lib/exiftool/ExifTool.h @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// File: ExifTool.h +// +// Description: C++ library interface to Perl exiftool application script +// +// License: Copyright 2013-2019, Phil Harvey (philharvey66 at gmail.com) +// +// This is software, in whole or part, is free for use in +// non-commercial applications, provided that this copyright notice +// is retained. A licensing fee may be required for use in a +// commercial application. +// +// Created: 2013-11-23 - Phil Harvey +//------------------------------------------------------------------------------ +#ifndef __EXIFTOOL_H__ +#define __EXIFTOOL_H__ + +#include "ExifToolPipe.h" +#include "TagInfo.h" + +#define NOW 0 +#define NEVER 1e9 + +#define SUMMARY_DIRECTORIES_SCANNED "directories scanned" +#define SUMMARY_DIRECTORIES_CREATED "directories created" +#define SUMMARY_FILES_FAILED_CONDITION "files failed condition" +#define SUMMARY_IMAGE_FILES_CREATED "image files created" +#define SUMMARY_IMAGE_FILES_UPDATED "image files updated" +#define SUMMARY_IMAGE_FILES_UNCHANGED "image files unchanged" +#define SUMMARY_IMAGE_FILES_MOVED "image files moved" +#define SUMMARY_IMAGE_FILES_COPIED "image files copied" +#define SUMMARY_FILE_UPDATE_ERRORS "files weren't updated due to errors" +#define SUMMARY_FILE_CREATE_ERRORS "files weren't created due to errors" +#define SUMMARY_IMAGE_FILES_READ "image files read" +#define SUMMARY_IMAGE_FILE_ERRORS "files could not be read" +#define SUMMARY_OUTPUT_FILES_CREATED "output files created" +#define SUMMARY_OUTPUT_FILES_APPENDED "output files appended" +#define SUMMARY_HARD_LINKS_CREATED "hard links created" +#define SUMMARY_HARD_LINK_ERRORS "hard links could not be created" +#define SUMMARY_SYMBOLIC_LINKS_CREATED "symbolic links created" +#define SUMMARY_SYMBOLIC_LINK_ERRORS "symbolic links could not be created" + +class ExifTool +{ +public: + ExifTool(const char *exec=NULL, const char *arg1=NULL); + virtual ~ExifTool(); + + TagInfo *ImageInfo(const char *file, const char *opts=NULL, double timeout=NEVER); + + int ExtractInfo(const char *file, const char *opts=NULL); + TagInfo *GetInfo(int cmdNum=0, double timeout=NEVER); + + int SetNewValue(const char *tag=NULL, const char *value=NULL, int len=-1); + int WriteInfo(const char *file, const char *opts=NULL, TagInfo *info=NULL); + + int Command(const char *cmd=NULL); + int Complete(double timeout=NEVER); + + int IsRunning(); + int LastComplete() { return mLastComplete; } + int LastCommand() { return mCmdNum; } // (undocumented) + void SetLastComplete(int lastComplete) { mLastComplete = lastComplete; } + void SetWaitTime(int waitTime) { mWaitTime = waitTime; } + + char * GetOutput() { return mLastComplete > 0 ? mStdout.GetString() : NULL; } + int GetOutputLen() { return mLastComplete > 0 ? mStdout.GetStringLen() : 0; } + char * GetError() { return mLastComplete > 0 ? mStderr.GetString() : NULL; } + int GetErrorLen() { return mLastComplete > 0 ? mStderr.GetStringLen() : 0; } // (undocumented) + + int GetSummary(const char *msg); + + // flags to allow some ExifTool features to be disabled + // (must be set before creating ExifTool object) + static int sNoSigPipe; // set to disable SIGPIPE handler + static int sNoWatchdog; // set to disable watchdog process + +private: + ExifToolPipe mStdout; // buffer for exiftool stdout read pipe + ExifToolPipe mStderr; // buffer for exiftool stderr read pipe + int mTo; // write pipe for exiftool stdin + int mPid; // exiftool application process ID + int mWatchdog; // watchdog process ID + TagInfo * mWriteInfo; // tag information to write + char * mCmdQueue; // queued command arguments (NULL if nothing queued) + int mCmdQueueLen; // length of data in command queue + int mCmdQueueSize;// size of command queue + int mLastComplete;// result of last Complete() call + int mCmdNum; // last command number + int mWaitTime; // time to wait (microsec) if no commands in queue +}; + +#endif // __EXIFTOOL_H__ diff --git a/lib/exiftool/ExifToolPipe.h b/lib/exiftool/ExifToolPipe.h new file mode 100644 index 0000000..8b65cd5 --- /dev/null +++ b/lib/exiftool/ExifToolPipe.h @@ -0,0 +1,43 @@ +//------------------------------------------------------------------------------ +// File: ExifToolPipe.h +// +// Description: Piped output from exiftool application +// +// License: Copyright 2013-2019, Phil Harvey (philharvey66 at gmail.com) +// +// This is software, in whole or part, is free for use in +// non-commercial applications, provided that this copyright notice +// is retained. A licensing fee may be required for use in a +// commercial application. +// +// Created: 2013-11-23 - Phil Harvey +//------------------------------------------------------------------------------ +#ifndef __EXIFTOOLPIPE_H__ +#define __EXIFTOOLPIPE_H__ + +class ExifToolPipe +{ +public: + ExifToolPipe(); + ~ExifToolPipe(); + void Init(int fd, int pid, int initialSize); + int Read(); // read exiftool response + void Free(); // free buffer memory + void Flush(); // remove previous response from buffer + char * GetString() { return (mString && mString[0]) ? mString : (char *)0; } + int GetStringLen() { return mStringLen; } + +private: + int mFile; // read file descriptor + char * mBuff; // buffer pointer + int mSize; // buffer size + int mLen; // length of data in buffer + int mPos; // current read position in buffer + int mSearchPos; // current search position in buffer + int mBlockSize; // initial buffer size + char * mString; // returned string from last Read() + int mStringLen; // length of returned string + int mPid; // process id for other side of the pipe +}; + +#endif // __EXIFTOOLPIPE_H__ diff --git a/lib/exiftool/TagInfo.h b/lib/exiftool/TagInfo.h new file mode 100644 index 0000000..57d3f45 --- /dev/null +++ b/lib/exiftool/TagInfo.h @@ -0,0 +1,35 @@ +//------------------------------------------------------------------------------ +// File: TagInfo.h +// +// Description: Tag information object +// +// License: Copyright 2013-2019, Phil Harvey (philharvey66 at gmail.com) +// +// This is software, in whole or part, is free for use in +// non-commercial applications, provided that this copyright notice +// is retained. A licensing fee may be required for use in a +// commercial application. +// +// Created: 2013-11-23 - Phil Harvey +//------------------------------------------------------------------------------ +#ifndef __TAGINFO_H__ +#define __TAGINFO_H__ + +struct TagInfo +{ + TagInfo(); + virtual ~TagInfo(); + + char *group[3]; // family 0-2 group names + char *name; // tag name + char *desc; // tag description + char *id; // tag ID + char *value; // converted value + int valueLen; // length of value in bytes (not including null terminator) + char *num; // "numerical" value + int numLen; // length of numerical value + int copyNum; // copy number for this tag name + TagInfo *next; // next TagInfo in linked list +}; + +#endif // __TAGINFO_H__ diff --git a/lib/main.cpp b/lib/main.cpp index ad1d704..27efeaa 100644 --- a/lib/main.cpp +++ b/lib/main.cpp @@ -1,3 +1,39 @@ - int main(int argc, char** argv) { - return 0; - } +#include "main.hpp" + +using namespace std; + + +ExifScanner::ExifScanner(const char* file): file(file) { + +} + +ExifScanner::~ExifScanner() { + +} + +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 +} diff --git a/lib/main.hpp b/lib/main.hpp new file mode 100644 index 0000000..b828632 --- /dev/null +++ b/lib/main.hpp @@ -0,0 +1,43 @@ +/* + * main.hpp + * + * Copyright 2024 Michael Rasmussen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + * + */ + + +#ifndef _MAIN_HPP_ +#define _MAIN_HPP_ + +#include +#include "exiftool/ExifTool.h" + +class ExifScanner { +public: + ExifScanner(const char* file); + ~ExifScanner(); + + void scanner_do_work(); + +private: + const char* file; + +}; + +#endif /* _MAIN_HPP_ */ diff --git a/lib/scanner.cpp b/lib/scanner.cpp new file mode 100644 index 0000000..961f64d --- /dev/null +++ b/lib/scanner.cpp @@ -0,0 +1,59 @@ +/* + * scanner.cpp + * + * Copyright 2024 Michael Rasmussen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + * + */ + +#include +#include "main.hpp" +#include "scanner.h" + +struct Scanner { + void* obj; +}; + +Scanner_t* scanner_create(const char* file) { + Scanner_t* s; + ExifScanner* obj; + + s = (typeof(s))malloc(sizeof(*s)); + obj = new ExifScanner(file); + s->obj = obj; + + return s; +} + +void scanner_destroy(Scanner_t* s) { + if (s == NULL) + return; + + delete static_cast(s->obj); + free(s); +} + +void scanner_do_work(Scanner_t* s) { + ExifScanner* obj; + + if (s == NULL) + return; + + obj = static_cast(s->obj); + obj->scanner_do_work(); +} diff --git a/lib/scanner.h b/lib/scanner.h new file mode 100644 index 0000000..555ac99 --- /dev/null +++ b/lib/scanner.h @@ -0,0 +1,45 @@ +/* + * scanner.h + * + * Copyright 2024 Michael Rasmussen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + * + */ + +#ifndef _SCANNER_H_ +#define _SCANNER_H_ + + +#ifdef __cplusplus +extern "C" { +#endif +struct Scanner; +typedef struct Scanner Scanner_t; + +Scanner_t* scanner_create(const char* file); + +void scanner_destroy(Scanner_t* s); + +void scanner_do_work(Scanner_t* s); + +#ifdef __cplusplus +} +#endif + + +#endif /* _SCANNER_H_ */ diff --git a/src/Makefile.am b/src/Makefile.am index 65006d6..90d919b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,8 +1,17 @@ AUTOMAKE_OPTIONS = gnu +extra = + +if BUILD_EXIF_SCANNER +extra += \ + ${top_builddir}/lib/libscanner.a \ + ${top_builddir}/lib/exiftool/exiftool.a +endif + AM_CPPFLAGS = \ -I${top_srcdir} \ -I${top_builddir} \ + -I${top_srcdir}/lib \ @GLIB_CFLAGS@ \ @GTK_CFLAGS@ \ @SQLITE3_CFLAGS@ @@ -22,7 +31,10 @@ lensdb_SOURCES = \ lensdb_LDADD = \ @GLIB_LIBS@ \ @GTK_LIBS@ \ - @SQLITE3_LIBS@ + @SQLITE3_LIBS@ \ + ${extra} \ + @LIBS@ + lensdb_LDFLAGS = diff --git a/src/main.c b/src/main.c index 9ad343a..351c627 100644 --- a/src/main.c +++ b/src/main.c @@ -30,11 +30,17 @@ #include #include "sqlite-storage.h" #include "gui.h" - +#include "lib/scanner.h" gint main(gint argc, gchar** argv) { gint status = 0; +// Scanner_t* sc = scanner_create("/home/mir/Pictures/Pictures/20211019_0012.jpg"); +// Scanner_t* sc = scanner_create("/home/mir/Pictures/Pictures/2024/09/14/Lndskrona Foto Festival/Lndskrona Foto Festival-8.ORF"); + Scanner_t* sc = scanner_create("/home/mir/Pictures/Pictures/2024/08/21/Fototur til Hejresøen/Fototur til Hejresøen-6.NEF"); + scanner_do_work(sc); + scanner_destroy(sc); + gchar* path = g_path_get_dirname(argv[0]); if ((status = init_storage(path)) > 0) { fprintf(stderr, "%s: Failed to open DB\n", path); -- 2.47.3