Extracts data from image in JSON format.
#include <exiv2/exiv2.hpp>
#include "Jzon.h"
#include <iostream>
#include <iomanip>
#include <cassert>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <cstdlib>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined(__MINGW32__) || defined(__MINGW64__)
# ifndef __MINGW__
# define __MINGW__
# endif
#endif
#if defined(_MSC_VER) || defined(__MINGW__)
#include <windows.h>
#ifndef PATH_MAX
# define PATH_MAX 512
#endif
const char* realpath(const char* file,char* path)
{
GetFullPathName(file,PATH_MAX,path,NULL);
return path;
}
#else
#include <unistd.h>
#endif
struct Token {
std::string n;
bool a;
int i;
};
using Tokens = std::vector<Token>;
bool getToken(std::string& in, Token& token, std::set<std::string>* pNS = nullptr)
{
bool result = false;
bool ns = false;
token.n = "" ;
token.a = false ;
token.i = 0 ;
while ( !result && in.length() ) {
std::string c = in.substr(0,1);
char C = c.at(0);
in = in.substr(1,std::string::npos);
if ( in.length() == 0 && C != ']' ) token.n += c;
if ( C == '/' || C == '[' || C == ':' || C == '.' || C == ']' || in.length() == 0 ) {
ns |= C == '/' ;
token.a = C == '[' ;
if ( C == ']' ) token.i = std::atoi(token.n.c_str());
result = token.n.length() > 0 ;
} else {
token.n += c;
}
}
if (ns && pNS) pNS->insert(token.n);
return result;
}
Jzon::Node& addToTree(Jzon::Node& r1, const Token& token)
{
Jzon::Object object ;
Jzon::Array array ;
std::string key = token.n ;
size_t index = token.i-1;
auto& empty = token.a ? static_cast<Jzon::Node&>(array) : static_cast<Jzon::Node&>(object);
if ( r1.IsObject() ) {
Jzon::Object& o1 = r1.AsObject();
if ( !o1.Has(key) ) o1.Add(key,empty);
return o1.Get(key);
}
if (r1.IsArray()) {
Jzon::Array& a1 = r1.AsArray();
while ( a1.GetCount() <= index ) a1.Add(empty);
return a1.Get(index);
}
return r1;
}
Jzon::Node& recursivelyBuildTree(Jzon::Node& root,Tokens& tokens,size_t k)
{
return addToTree( k==0 ? root : recursivelyBuildTree(root,tokens,k-1), tokens.at(k) );
}
Jzon::Node& objectForKey(const std::string& Key, Jzon::Object& root, std::string& name,
std::set<std::string>* pNS = nullptr)
{
Tokens tokens ;
Token token ;
std::string input = Key ;
while ( getToken(input,token,pNS) ) tokens.push_back(token);
size_t l = tokens.size()-1;
name = tokens.at(l).n ;
if ( pNS && tokens.size() > 1 ) pNS->insert(tokens[1].n);
return recursivelyBuildTree(root,tokens,l-1);
#if 0
if ( l == 1 ) return addToTree(root,tokens[0]);
if ( l == 2 ) return addToTree(addToTree(root,tokens[0]),tokens[1]);
if ( l == 3 ) return addToTree(addToTree(addToTree(root,tokens[0]),tokens[1]),tokens[2]);
if ( l == 4 ) return addToTree(addToTree(addToTree(addToTree(root,tokens[0]),tokens[1]),tokens[2]),tokens[3]);
...
#endif
}
bool isObject(std::string& value)
{
return value == std::string("type=\"Struct\"");
}
bool isArray(std::string& value)
{
return value == "type=\"Seq\"" || value == "type=\"Bag\"" || value == "type=\"Alt\"";
}
#define STORE(node,key,value) \
if (node.IsObject()) node.AsObject().Add(key,value);\
else node.AsArray() .Add( value)
template <class T>
void push(Jzon::Node& node,const std::string& key,T i)
{
#define ABORT_IF_I_EMTPY \
if (i->value().size() == 0) { \
return; \
}
std::string value = i->value().toString();
switch ( i->typeId() ) {
if ( ::isObject(value) ) {
Jzon::Object v;
STORE(node,key,v);
} else if ( ::isArray(value) ) {
Jzon::Array v;
STORE(node,key,v);
} else {
STORE(node,key,value);
}
break;
STORE(node,key,std::atoi(value.c_str()) );
break;
STORE(node,key,std::atof(value.c_str()) );
break;
ABORT_IF_I_EMTPY
Jzon::Array arr;
arr.Add(rat.first );
arr.Add(rat.second);
STORE(node,key,arr);
} break;
ABORT_IF_I_EMTPY
Jzon::Object l ;
for (auto&& lang : langs.value_) {
l.Add(lang.first, lang.second);
}
Jzon::Object o ;
o.Add("lang",l);
STORE(node,key,o);
}
break;
default:
if ( key == "UserComment" ) {
size_t pos = value.find('\0') ;
if ( pos != std::string::npos )
value = value.substr(0,pos);
}
if ( key == "MakerNote") return;
STORE(node,key,value);
break;
}
}
void fileSystemPush(const char* path,Jzon::Node& nfs)
{
auto& fs = dynamic_cast<Jzon::Object&>(nfs);
fs.Add("path",path);
char resolved_path[2000];
fs.Add("realpath",realpath(path,resolved_path));
struct stat buf;
memset(&buf,0,sizeof(buf));
stat(path,&buf);
fs.Add("st_dev", static_cast<int>(buf.st_dev));
fs.Add("st_ino", static_cast<int>(buf.st_ino));
fs.Add("st_mode", static_cast<int>(buf.st_mode));
fs.Add("st_nlink", static_cast<int>(buf.st_nlink));
fs.Add("st_uid", static_cast<int>(buf.st_uid));
fs.Add("st_gid", static_cast<int>(buf.st_gid));
fs.Add("st_rdev", static_cast<int>(buf.st_rdev));
fs.Add("st_size", static_cast<int>(buf.st_size));
fs.Add("st_atime", static_cast<int>(buf.st_atime));
fs.Add("st_mtime", static_cast<int>(buf.st_mtime));
fs.Add("st_ctime", static_cast<int>(buf.st_ctime));
#if defined(_MSC_VER) || defined(__MINGW__)
size_t blksize = 1024;
size_t blocks = (buf.st_size+blksize-1)/blksize;
#else
size_t blksize = buf.st_blksize;
size_t blocks = buf.st_blocks ;
#endif
fs.Add("st_blksize", static_cast<int>(blksize));
fs.Add("st_blocks", static_cast<int>(blocks));
}
int main(int argc, char* const argv[])
{
#ifdef EXV_ENABLE_BMFF
Exiv2::enableBMFF();
#endif
try {
if (argc < 2 || argc > 3) {
std::cout << "Usage: " << argv[0] << " [-option] file" << std::endl;
std::cout << "Option: all | exif | iptc | xmp | filesystem" << std::endl;
return 1;
}
const char* path = argv[argc-1];
const char* opt = argc == 3 ? argv[1] : "-all" ;
while (opt[0] == '-') opt++ ;
char option = opt[0];
assert(image.get() != 0);
image->readMetadata();
if ( option == 'f' ) {
const char* Fs="FS";
Jzon::Object fs ;
fileSystemPush(path,
root.Get(Fs));
}
if ( option == 'a' || option == 'e' ) {
for (
auto i = exifData.
begin(); i != exifData.
end() ; ++i ) {
std::string name ;
Jzon::Node& object = objectForKey(i->key(),root,name);
push(object,name,i);
}
}
if ( option == 'a' || option == 'i' ) {
std::string name ;
Jzon::Node& object = objectForKey(i->key(),root,name);
push(object,name,i);
}
}
#ifdef EXV_HAVE_XMP_TOOLKIT
if ( option == 'a' || option == 'x' ) {
if ( !xmpData.
empty() ) {
std::set<std::string> namespaces;
for (
auto i = xmpData.
begin(); i != xmpData.
end(); ++i) {
std::string name ;
Jzon::Node& object = objectForKey(i->key(),root,name,&namespaces);
push(object,name,i);
}
Jzon::Object xmlns;
for (auto&& ns : namespaces) {
xmlns.Add(ns, nsDict[ns]);
}
root.Get(
"Xmp").AsObject().Add(
"xmlns",xmlns);
}
}
#endif
Jzon::Writer writer(root, Jzon::StandardFormat);
writer.Write();
std::cout << writer.GetResult() << std::endl;
return EXIT_SUCCESS;
}
std::cout <<
"Caught Exiv2 exception '" << e.
what() <<
"'\n";
return EXIT_FAILURE;
}
}
Simple error class used for exceptions. An output operator is provided to print errors to a stream.
Definition: error.hpp:264
const char * what() const noexcept override
Return the error message as a C-string. The pointer returned by what() is valid only as long as the B...
Definition: error.hpp:379
A container for Exif data. This is a top-level class of the Exiv2 library. The container holds Exifda...
Definition: exif.hpp:434
iterator begin()
Begin of the metadata.
Definition: exif.hpp:490
iterator end()
End of the metadata.
Definition: exif.hpp:492
static Image::UniquePtr open(const std::string &path, bool useCurl=true)
Create an Image subclass of the appropriate type by reading the specified file. Image type is derived...
Definition: image.cpp:894
std::unique_ptr< Image > UniquePtr
Image auto_ptr type.
Definition: image.hpp:81
A container for IPTC data. This is a top-level class of the Exiv2 library.
Definition: iptc.hpp:169
iterator begin()
Begin of the metadata.
Definition: iptc.hpp:220
iterator end()
End of the metadata.
Definition: iptc.hpp:222
IptcMetadata::const_iterator const_iterator
IptcMetadata const iterator type.
Definition: iptc.hpp:174
Value type for XMP language alternative properties.
Definition: value.hpp:877
A container for XMP data. This is a top-level class of the Exiv2 library.
Definition: xmp_exiv2.hpp:165
bool empty() const
Return true if there is no XMP metadata.
Definition: xmp.cpp:528
iterator end()
End of the metadata.
Definition: xmp.cpp:543
iterator begin()
Begin of the metadata.
Definition: xmp.cpp:538
static bool initialize(XmpParser::XmpLockFct xmpLockFct=nullptr, void *pLockData=nullptr)
Initialize the XMP Toolkit.
Definition: xmp.cpp:641
static void terminate()
Terminate the XMP Toolkit and unregister custom namespaces.
Definition: xmp.cpp:703
static void registeredNamespaces(Exiv2::Dictionary &nsDict)
Get all registered namespaces (for both Exiv2 and XMPsdk)
const uint32_t root
Special tag: root IFD.
Definition: tiffcomposite_int.hpp:74
@ unsignedShort
Exif SHORT type, 16-bit (2-byte) unsigned integer.
Definition: types.hpp:103
@ date
IPTC date type.
Definition: types.hpp:118
@ signedRational
Exif SRATIONAL type, two SLONGs: numerator and denumerator of a fraction.
Definition: types.hpp:110
@ string
IPTC string type.
Definition: types.hpp:117
@ unsignedLong
Exif LONG type, 32-bit (4-byte) unsigned integer.
Definition: types.hpp:104
@ signedShort
Exif SSHORT type, a 16-bit (2-byte) signed (twos-complement) integer.
Definition: types.hpp:108
@ signedLong
Exif SLONG type, a 32-bit (4-byte) signed (twos-complement) integer.
Definition: types.hpp:109
@ langAlt
XMP language alternative type.
Definition: types.hpp:126
@ xmpAlt
XMP alternative type.
Definition: types.hpp:123
@ unsignedByte
Exif BYTE type, 8-bit unsigned integer.
Definition: types.hpp:101
@ signedByte
Exif SBYTE type, an 8-bit signed (twos-complement) integer.
Definition: types.hpp:106
@ asciiString
Exif ASCII type, 8-bit byte.
Definition: types.hpp:102
@ xmpText
XMP text type.
Definition: types.hpp:122
@ time
IPTC time type.
Definition: types.hpp:119
@ xmpSeq
XMP sequence type.
Definition: types.hpp:125
@ comment
Exiv2 type for the Exif user comment.
Definition: types.hpp:120
@ tiffDouble
TIFF DOUBLE type, double precision (8-byte) IEEE format.
Definition: types.hpp:112
@ undefined
Exif UNDEFINED type, an 8-bit byte that may contain anything.
Definition: types.hpp:107
@ xmpBag
XMP bag type.
Definition: types.hpp:124
@ tiffFloat
TIFF FLOAT type, single precision (4-byte) IEEE format.
Definition: types.hpp:111
@ unsignedRational
Exif RATIONAL type, two LONGs: numerator and denumerator of a fraction.
Definition: types.hpp:105
@ tiffIfd
TIFF IFD type, 32-bit (4-byte) unsigned integer.
Definition: types.hpp:113
@ directory
Exiv2 type for a CIFF directory.
Definition: types.hpp:121
std::pair< int32_t, int32_t > Rational
8 byte signed rational type.
Definition: types.hpp:80
std::map< std::string, std::string > Dictionary
typedef for string:string map
Definition: datasets.hpp:347