-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathactions.cpp
More file actions
2262 lines (2064 loc) · 79.7 KB
/
Copy pathactions.cpp
File metadata and controls
2262 lines (2064 loc) · 79.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ***************************************************************** -*- C++ -*-
/*
* Copyright (C) 2004-2021 Exiv2 authors
* This program is part of the Exiv2 distribution.
*
* 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, 5th Floor, Boston, MA 02110-1301 USA.
*/
/*
File: actions.cpp
*/
// *****************************************************************************
// included header files
#include "config.h"
#include "actions.hpp"
#include "exiv2app.hpp"
#include "image.hpp"
#include "jpgimage.hpp"
#include "xmpsidecar.hpp"
#include "utils.hpp"
#include "types.hpp"
#include "exif.hpp"
#include "easyaccess.hpp"
#include "iptc.hpp"
#include "xmp_exiv2.hpp"
#include "preview.hpp"
#include "futils.hpp"
#include "i18n.h" // NLS support.
// + standard includes
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cassert>
#include <stdexcept>
#include <sys/types.h> // for stat()
#include <sys/stat.h> // for stat()
#ifdef EXV_HAVE_UNISTD_H
# include <unistd.h> // for stat()
#endif
#ifdef _MSC_VER
# include <sys/utime.h>
#else
# include <utime.h>
#endif
#if !defined(__MINGW__) && !defined(_MSC_VER)
#define _fileno(a) a
#define _setmode(a,b)
#endif
// *****************************************************************************
// local declarations
namespace {
//! Helper class to set the timestamp of a file to that of another file
class Timestamp {
public:
//! C'tor
Timestamp() : actime_(0), modtime_(0) {}
//! Read the timestamp of a file
int read(const std::string& path);
//! Read the timestamp from a broken-down time in buffer \em tm.
int read(struct tm* tm);
//! Set the timestamp of a file
int touch(const std::string& path);
private:
time_t actime_;
time_t modtime_;
};
/*!
@brief Convert a string "YYYY:MM:DD HH:MI:SS" to a struct tm type,
returns 0 if successful
*/
int str2Tm(const std::string& timeStr, struct tm* tm);
//! Convert a localtime to a string "YYYY:MM:DD HH:MI:SS", "" on error
std::string time2Str(time_t time);
//! Convert a tm structure to a string "YYYY:MM:DD HH:MI:SS", "" on error
std::string tm2Str(const struct tm* tm);
/*!
@brief Copy metadata from source to target according to Params::copyXyz
@param source Source file path
@param target Target file path. An *.exv file is created if target doesn't
exist.
@param targetType Image type for the target image in case it needs to be
created.
@param preserve Indicates if existing metadata in the target file should
be kept.
@return 0 if successful, else an error code
*/
int metacopy(const std::string& source,
const std::string& target,
int targetType,
bool preserve);
/*!
@brief Rename a file according to a timestamp value.
@param path The original file path. Contains the new path on exit.
@param tm Pointer to a buffer with the broken-down time to rename
the file to.
@return 0 if successful, -1 if the file was skipped, 1 on error.
*/
int renameFile(std::string& path, const struct tm* tm);
/*!
@brief Make a file path from the current file path, destination
directory (if any) and the filename extension passed in.
@param path Path of the existing file
@param ext New filename extension (incl. the dot '.' if required)
@return 0 if successful, 1 if the new file exists and the user
chose not to overwrite it.
*/
std::string newFilePath(const std::string& path, const std::string& ext);
/*!
@brief Check if file \em path exists and whether it should be
overwritten. Ask user if necessary. Return 1 if the file
exists and shouldn't be overwritten, else 0.
*/
int dontOverwrite(const std::string& path);
/*!
@brief Output a text with a given minimum number of chars, honoring
multi-byte characters correctly. Replace code in the form
os << setw(width) << myString
with
os << make_pair( myString, width)
*/
std::ostream& operator<<( std::ostream& os, std::pair<std::string, int> strAndWidth);
//! Print image Structure information
int printStructure(std::ostream& out, Exiv2::PrintStructureOption option, const std::string &path);
}
// *****************************************************************************
// class member definitions
namespace Action {
Task::~Task()
{
}
Task::AutoPtr Task::clone() const
{
return AutoPtr(clone_());
}
TaskFactory* TaskFactory::instance_ = 0;
TaskFactory& TaskFactory::instance()
{
if (0 == instance_) {
instance_ = new TaskFactory;
}
return *instance_;
} // TaskFactory::instance
void TaskFactory::cleanup()
{
if (instance_ != 0) {
Registry::iterator e = registry_.end();
for (Registry::iterator i = registry_.begin(); i != e; ++i) {
delete i->second;
}
delete instance_;
instance_ = 0;
}
} //TaskFactory::cleanup
void TaskFactory::registerTask(TaskType type, Task::AutoPtr task)
{
Registry::iterator i = registry_.find(type);
if (i != registry_.end()) {
delete i->second;
}
registry_[type] = task.release();
} // TaskFactory::registerTask
TaskFactory::TaskFactory()
{
// Register a prototype of each known task
registerTask(adjust, Task::AutoPtr(new Adjust));
registerTask(print, Task::AutoPtr(new Print));
registerTask(rename, Task::AutoPtr(new Rename));
registerTask(erase, Task::AutoPtr(new Erase));
registerTask(extract, Task::AutoPtr(new Extract));
registerTask(insert, Task::AutoPtr(new Insert));
registerTask(modify, Task::AutoPtr(new Modify));
registerTask(fixiso, Task::AutoPtr(new FixIso));
registerTask(fixcom, Task::AutoPtr(new FixCom));
} // TaskFactory c'tor
Task::AutoPtr TaskFactory::create(TaskType type)
{
Registry::const_iterator i = registry_.find(type);
if (i != registry_.end() && i->second != 0) {
Task* t = i->second;
return t->clone();
}
return Task::AutoPtr(0);
} // TaskFactory::create
Print::~Print()
{
}
int setModeAndPrintStructure(Exiv2::PrintStructureOption option, const std::string& path,bool binary)
{
int result = 0 ;
if ( binary && option == Exiv2::kpsIccProfile ) {
std::stringstream output(std::stringstream::out|std::stringstream::binary);
result = printStructure(output, option, path);
if ( result == 0 ) {
size_t size = (long) output.str().size();
Exiv2::DataBuf iccProfile((long)size);
Exiv2::DataBuf ascii((long)(size * 3 + 1));
ascii.pData_[size * 3] = 0;
::memcpy(iccProfile.pData_,output.str().c_str(),size);
if ( Exiv2::base64encode(iccProfile.pData_,size,(char*)ascii.pData_,size*3) ) {
long chunk = 60 ;
std::string code = std::string("data:") + std::string((char*)ascii.pData_);
long length = (long) code.size() ;
for ( long start = 0 ; start < length ; start += chunk ) {
long count = (start+chunk) < length ? chunk : length - start ;
std::cout << code.substr(start,count) << std::endl;
}
}
}
} else {
_setmode(_fileno(stdout),O_BINARY);
result = printStructure(std::cout, option, path);
}
return result;
}
int Print::run(const std::string& path)
{
try {
path_ = path;
int rc = 0;
Exiv2::PrintStructureOption option = Exiv2::kpsNone ;
switch (Params::instance().printMode_) {
case Params::pmSummary: rc = Params::instance().greps_.empty() ? printSummary() : printList(); break;
case Params::pmList: rc = printList(); break;
case Params::pmComment: rc = printComment(); break;
case Params::pmPreview: rc = printPreviewList(); break;
case Params::pmStructure: rc = printStructure(std::cout,Exiv2::kpsBasic, path_) ; break;
case Params::pmRecursive: rc = printStructure(std::cout,Exiv2::kpsRecursive, path_) ; break;
case Params::pmXMP:
if (option == Exiv2::kpsNone)
option = Exiv2::kpsXMP;
rc = setModeAndPrintStructure(option, path_,binary());
break;
case Params::pmIccProfile:
if (option == Exiv2::kpsNone)
option = Exiv2::kpsIccProfile;
rc = setModeAndPrintStructure(option, path_,binary());
break;
}
return rc;
}
catch(const Exiv2::AnyError& e) {
std::cerr << "Exiv2 exception in print action for file "
<< path << ":\n" << e << "\n";
return 1;
}
catch(const std::overflow_error& e) {
std::cerr << "std::overflow_error exception in print action for file "
<< path << ":\n" << e.what() << "\n";
return 1;
}
}
int Print::printSummary()
{
if (!Exiv2::fileExists(path_, true)) {
std::cerr << path_ << ": "
<< _("Failed to open the file\n");
return -1;
}
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path_);
assert(image.get() != 0);
image->readMetadata();
Exiv2::ExifData& exifData = image->exifData();
align_ = 16;
// Filename
printLabel(_("File name"));
std::cout << path_ << std::endl;
// Filesize
struct stat buf;
if (0 == stat(path_.c_str(), &buf)) {
printLabel(_("File size"));
std::cout << buf.st_size << " " << _("Bytes") << std::endl;
}
// MIME type
printLabel(_("MIME type"));
std::cout << image->mimeType() << std::endl;
// Image size
printLabel(_("Image size"));
std::cout << image->pixelWidth() << " x " << image->pixelHeight() << std::endl;
if (exifData.empty()) {
std::cerr << path_ << ": "
<< _("No Exif data found in the file\n");
return -3;
}
// Thumbnail
printLabel(_("Thumbnail"));
Exiv2::ExifThumbC exifThumb(exifData);
std::string thumbExt = exifThumb.extension();
if (thumbExt.empty()) {
std::cout << _("None");
}
else {
Exiv2::DataBuf buf = exifThumb.copy();
if (buf.size_ == 0) {
std::cout << _("None");
}
else {
std::cout << exifThumb.mimeType() << ", "
<< buf.size_ << " " << _("Bytes");
}
}
std::cout << std::endl;
printTag(exifData, Exiv2::make , _("Camera make") );
printTag(exifData, Exiv2::model , _("Camera model") );
printTag(exifData, Exiv2::dateTimeOriginal , _("Image timestamp") );
printTag(exifData, "Exif.Canon.FileNumber" , _("File number") );
printTag(exifData, Exiv2::exposureTime , _("Exposure time") , Exiv2::shutterSpeedValue );
printTag(exifData, Exiv2::fNumber , _("Aperture") , Exiv2::apertureValue );
printTag(exifData, Exiv2::exposureBiasValue , _("Exposure bias") );
printTag(exifData, Exiv2::flash , _("Flash") );
printTag(exifData, Exiv2::flashBias , _("Flash bias") );
printTag(exifData, Exiv2::focalLength , _("Focal length") );
printTag(exifData, Exiv2::subjectDistance , _("Subject distance") );
printTag(exifData, Exiv2::isoSpeed , _("ISO speed") );
printTag(exifData, Exiv2::exposureMode , _("Exposure mode") );
printTag(exifData, Exiv2::meteringMode , _("Metering mode") );
printTag(exifData, Exiv2::macroMode , _("Macro mode") );
printTag(exifData, Exiv2::imageQuality , _("Image quality") );
printTag(exifData, Exiv2::whiteBalance , _("White balance") );
printTag(exifData, "Exif.Image.Copyright" , _("Copyright") );
printTag(exifData, "Exif.Photo.UserComment" , _("Exif comment") );
std::cout << std::endl;
return 0;
} // Print::printSummary
void Print::printLabel(const std::string& label) const
{
std::cout << std::setfill(' ') << std::left;
if (Params::instance().files_.size() > 1) {
std::cout << std::setw(20) << path_ << " ";
}
std::cout << std::make_pair( label, align_)
<< ": ";
}
int Print::printTag(const Exiv2::ExifData& exifData,
const std::string& key,
const std::string& label) const
{
int rc = 0;
if (!label.empty()) {
printLabel(label);
}
Exiv2::ExifKey ek(key);
Exiv2::ExifData::const_iterator md = exifData.findKey(ek);
if (md != exifData.end()) {
md->write(std::cout, &exifData);
rc = 1;
}
if (!label.empty()) std::cout << std::endl;
return rc;
} // Print::printTag
int Print::printTag(const Exiv2::ExifData& exifData,
EasyAccessFct easyAccessFct,
const std::string& label,
EasyAccessFct easyAccessFctFallback) const
{
int rc = 0;
if (!label.empty()) {
printLabel(label);
}
Exiv2::ExifData::const_iterator md = easyAccessFct(exifData);
if (md != exifData.end()) {
md->write(std::cout, &exifData);
rc = 1;
}
else if (NULL != easyAccessFctFallback)
{
md = easyAccessFctFallback(exifData);
if (md != exifData.end()) {
md->write(std::cout, &exifData);
rc = 1;
}
}
if (!label.empty()) std::cout << std::endl;
return rc;
} // Print::printTag
int Print::printList()
{
if (!Exiv2::fileExists(path_, true)) {
std::cerr << path_
<< ": " << _("Failed to open the file\n");
return -1;
}
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path_);
assert(image.get() != 0);
image->readMetadata();
// Set defaults for metadata types and data columns
if (Params::instance().printTags_ == Exiv2::mdNone) {
Params::instance().printTags_ = Exiv2::mdExif | Exiv2::mdIptc | Exiv2::mdXmp;
}
if (Params::instance().printItems_ == 0) {
Params::instance().printItems_ = Params::prKey | Params::prType | Params::prCount | Params::prTrans;
}
return printMetadata(image.get());
} // Print::printList
int Print::printMetadata(const Exiv2::Image* image)
{
bool ret = false;
bool noExif = false;
if (Params::instance().printTags_ & Exiv2::mdExif) {
const Exiv2::ExifData& exifData = image->exifData();
for (Exiv2::ExifData::const_iterator md = exifData.begin();
md != exifData.end(); ++md) {
ret |= printMetadatum(*md, image);
}
if (exifData.empty()) noExif = true;
}
bool noIptc = false;
if (Params::instance().printTags_ & Exiv2::mdIptc) {
const Exiv2::IptcData& iptcData = image->iptcData();
for (Exiv2::IptcData::const_iterator md = iptcData.begin();
md != iptcData.end(); ++md) {
ret |= printMetadatum(*md, image);
}
if (iptcData.empty()) noIptc = true;
}
bool noXmp = false;
if (Params::instance().printTags_ & Exiv2::mdXmp) {
const Exiv2::XmpData& xmpData = image->xmpData();
for (Exiv2::XmpData::const_iterator md = xmpData.begin();
md != xmpData.end(); ++md) {
ret |= printMetadatum(*md, image);
}
if (xmpData.empty()) noXmp = true;
}
// With -v, inform about the absence of any (requested) type of metadata
if (Params::instance().verbose_) {
if (noExif) std::cerr << path_ << ": " << _("No Exif data found in the file\n");
if (noIptc) std::cerr << path_ << ": " << _("No IPTC data found in the file\n");
if (noXmp) std::cerr << path_ << ": " << _("No XMP data found in the file\n");
}
// With -g or -K, return -3 if no matching tags were found
int rc = 0;
if ((!Params::instance().greps_.empty() || !Params::instance().keys_.empty()) && !ret) rc = 1;
return rc;
} // Print::printMetadata
bool Print::grepTag(const std::string& key)
{
bool result=Params::instance().greps_.empty();
for (Params::Greps::const_iterator g = Params::instance().greps_.begin();
!result && g != Params::instance().greps_.end(); ++g)
{
#if defined(EXV_HAVE_REGEX_H)
result = regexec( &(*g), key.c_str(), 0, NULL, 0) == 0 ;
#else
std::string Pattern(g->pattern_);
std::string Key(key);
if ( g->bIgnoreCase_ ) {
// https://notfaq.wordpress.com/2007/08/04/cc-convert-string-to-upperlower-case/
std::transform(Pattern.begin(), Pattern.end(),Pattern.begin(), ::tolower);
std::transform(Key.begin() , Key.end() ,Key.begin() , ::tolower);
}
result = Key.find(Pattern) != std::string::npos;
#endif
}
return result ;
}
bool Print::keyTag(const std::string& key)
{
bool result=Params::instance().keys_.empty();
for (Params::Keys::const_iterator k = Params::instance().keys_.begin();
!result && k != Params::instance().keys_.end(); ++k)
{
result = key.compare(*k) == 0;
}
return result ;
}
static void binaryOutput(const std::ostringstream& os)
{
std::cout << os.str();
}
bool Print::printMetadatum(const Exiv2::Metadatum& md, const Exiv2::Image* pImage)
{
if (!grepTag(md.key()))
return false;
if (!keyTag(md.key()))
return false;
if (Params::instance().unknown_ && md.tagName().substr(0, 2) == "0x") {
return false;
}
bool const manyFiles = Params::instance().files_.size() > 1;
if (manyFiles) {
std::cout << std::setfill(' ') << std::left << std::setw(20) << path_ << " ";
}
bool first = true;
if (Params::instance().printItems_ & Params::prTag) {
if (!first)
std::cout << " ";
first = false;
std::cout << "0x" << std::setw(4) << std::setfill('0') << std::right << std::hex << md.tag();
}
if (Params::instance().printItems_ & Params::prSet) {
if (!first)
std::cout << " ";
first = false;
std::cout << "set";
}
if (Params::instance().printItems_ & Params::prGroup) {
if (!first)
std::cout << " ";
first = false;
std::cout << std::setw(12) << std::setfill(' ') << std::left << md.groupName();
}
if (Params::instance().printItems_ & Params::prKey) {
if (!first)
std::cout << " ";
first = false;
std::cout << std::setfill(' ') << std::left << std::setw(44) << md.key();
}
if (Params::instance().printItems_ & Params::prName) {
if (!first)
std::cout << " ";
first = false;
std::cout << std::setw(27) << std::setfill(' ') << std::left << md.tagName();
}
if (Params::instance().printItems_ & Params::prLabel) {
if (!first)
std::cout << " ";
first = false;
std::cout << std::setw(30) << std::setfill(' ') << std::left << md.tagLabel();
}
if (Params::instance().printItems_ & Params::prType) {
if (!first)
std::cout << " ";
first = false;
std::cout << std::setw(9) << std::setfill(' ') << std::left;
const char* tn = md.typeName();
if (tn) {
std::cout << tn;
} else {
std::ostringstream os;
os << "0x" << std::setw(4) << std::setfill('0') << std::hex << md.typeId();
std::cout << os.str();
}
}
if (Params::instance().printItems_ & Params::prCount) {
if (!first)
std::cout << " ";
first = false;
std::cout << std::dec << std::setw(3) << std::setfill(' ') << std::right << md.count();
}
if (Params::instance().printItems_ & Params::prSize) {
if (!first)
std::cout << " ";
first = false;
std::cout << std::dec << std::setw(3) << std::setfill(' ') << std::right << md.size();
}
if (Params::instance().printItems_ & Params::prValue && md.size() > 0) {
if (!first)
std::cout << " ";
first = false;
std::ostringstream os;
// #1114 - show negative values for SByte
if (md.typeId() == Exiv2::signedByte) {
for ( long c = 0 ; c < md.value().count() ; c++ ) {
long value = md.value().toLong(c);
os << (c?" ":"") << std::dec << (value < 128 ? value : value - 256);
}
} else {
os << std::dec << md.value();
}
binaryOutput(os);
}
if (Params::instance().printItems_ & Params::prTrans) {
if (!first)
std::cout << " ";
first = false;
std::ostringstream os;
os << std::dec << md.print(&pImage->exifData());
binaryOutput(os) ;
}
if (Params::instance().printItems_ & Params::prHex) {
if (!first)
std::cout << std::endl;
first = false;
Exiv2::DataBuf buf(md.size());
md.copy(buf.pData_, pImage->byteOrder());
Exiv2::hexdump(std::cout, buf.pData_, buf.size_);
}
std::cout << std::endl;
return true;
} // Print::printMetadatum
int Print::printComment()
{
if (!Exiv2::fileExists(path_, true)) {
std::cerr << path_
<< ": " << _("Failed to open the file\n");
return -1;
}
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path_);
assert(image.get() != 0);
image->readMetadata();
if (Params::instance().verbose_) {
std::cout << _("JPEG comment") << ": ";
}
std::cout << image->comment() << std::endl;
return 0;
} // Print::printComment
int Print::printPreviewList()
{
if (!Exiv2::fileExists(path_, true)) {
std::cerr << path_
<< ": " << _("Failed to open the file\n");
return -1;
}
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path_);
assert(image.get() != 0);
image->readMetadata();
bool const manyFiles = Params::instance().files_.size() > 1;
int cnt = 0;
Exiv2::PreviewManager pm(*image);
Exiv2::PreviewPropertiesList list = pm.getPreviewProperties();
for (Exiv2::PreviewPropertiesList::const_iterator pos = list.begin(); pos != list.end(); ++pos) {
if (manyFiles) {
std::cout << std::setfill(' ') << std::left << std::setw(20)
<< path_ << " ";
}
std::cout << _("Preview") << " " << ++cnt << ": "
<< pos->mimeType_ << ", ";
if (pos->width_ != 0 && pos->height_ != 0) {
std::cout << pos->width_ << "x" << pos->height_ << " "
<< _("pixels") << ", ";
}
std::cout << pos->size_ << " " << _("bytes") << "\n";
}
return 0;
} // Print::printPreviewList
Print::AutoPtr Print::clone() const
{
return AutoPtr(clone_());
}
Print* Print::clone_() const
{
return new Print(*this);
}
Rename::~Rename()
{
}
int Rename::run(const std::string& path)
{
try {
if (!Exiv2::fileExists(path, true)) {
std::cerr << path
<< ": " << _("Failed to open the file\n");
return -1;
}
Timestamp ts;
if (Params::instance().preserve_) ts.read(path);
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path);
assert(image.get() != 0);
image->readMetadata();
Exiv2::ExifData& exifData = image->exifData();
if (exifData.empty()) {
std::cerr << path
<< ": " << _("No Exif data found in the file\n");
return -3;
}
Exiv2::ExifKey key("Exif.Photo.DateTimeOriginal");
Exiv2::ExifData::iterator md = exifData.findKey(key);
if (md == exifData.end()) {
key = Exiv2::ExifKey("Exif.Image.DateTime");
md = exifData.findKey(key);
}
if (md == exifData.end()) {
std::cerr << _("Neither tag") << " `Exif.Photo.DateTimeOriginal' "
<< _("nor") << " `Exif.Image.DateTime' "
<< _("found in the file") << " " << path << "\n";
return 1;
}
std::string v = md->toString();
if (v.length() == 0 || v[0] == ' ') {
std::cerr << _("Image file creation timestamp not set in the file")
<< " " << path << "\n";
return 1;
}
struct tm tm;
if (str2Tm(v, &tm) != 0) {
std::cerr << _("Failed to parse timestamp") << " `" << v
<< "' " << _("in the file") << " " << path << "\n";
return 1;
}
if ( Params::instance().timestamp_
|| Params::instance().timestampOnly_) {
ts.read(&tm);
}
int rc = 0;
std::string newPath = path;
if (Params::instance().timestampOnly_) {
if (Params::instance().verbose_) {
std::cout << _("Updating timestamp to") << " " << v << std::endl;
}
}
else {
rc = renameFile(newPath, &tm);
if (rc == -1) return 0; // skip
}
if ( 0 == rc
&& ( Params::instance().preserve_
|| Params::instance().timestamp_
|| Params::instance().timestampOnly_)) {
ts.touch(newPath);
}
return rc;
}
catch(const Exiv2::AnyError& e)
{
std::cerr << "Exiv2 exception in rename action for file " << path
<< ":\n" << e << "\n";
return 1;
}} // Rename::run
Rename::AutoPtr Rename::clone() const
{
return AutoPtr(clone_());
}
Rename* Rename::clone_() const
{
return new Rename(*this);
}
Erase::~Erase()
{
}
int Erase::run(const std::string& path)
try {
path_ = path;
if (!Exiv2::fileExists(path_, true)) {
std::cerr << path_
<< ": " << _("Failed to open the file\n");
return -1;
}
Timestamp ts;
if (Params::instance().preserve_) ts.read(path);
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path_);
assert(image.get() != 0);
image->readMetadata();
// Thumbnail must be before Exif
int rc = 0;
if (Params::instance().target_ & Params::ctThumb) {
rc = eraseThumbnail(image.get());
}
if (0 == rc && Params::instance().target_ & Params::ctExif) {
rc = eraseExifData(image.get());
}
if (0 == rc && Params::instance().target_ & Params::ctIptc) {
rc = eraseIptcData(image.get());
}
if (0 == rc && Params::instance().target_ & Params::ctComment) {
rc = eraseComment(image.get());
}
if (0 == rc && Params::instance().target_ & Params::ctXmp) {
rc = eraseXmpData(image.get());
}
if (0 == rc && Params::instance().target_ & Params::ctIccProfile) {
rc = eraseIccProfile(image.get());
}
if (0 == rc && Params::instance().target_ & Params::ctIptcRaw) {
rc = printStructure(std::cout,Exiv2::kpsIptcErase,path_);
}
if (0 == rc) {
image->writeMetadata();
if (Params::instance().preserve_) ts.touch(path);
}
return rc;
}
catch(const Exiv2::AnyError& e)
{
std::cerr << "Exiv2 exception in erase action for file " << path
<< ":\n" << e << "\n";
return 1;
} // Erase::run
int Erase::eraseThumbnail(Exiv2::Image* image) const
{
Exiv2::ExifThumb exifThumb(image->exifData());
std::string thumbExt = exifThumb.extension();
if (thumbExt.empty()) {
return 0;
}
exifThumb.erase();
if (Params::instance().verbose_) {
std::cout << _("Erasing thumbnail data") << std::endl;
}
return 0;
}
int Erase::eraseExifData(Exiv2::Image* image) const
{
if (Params::instance().verbose_ && image->exifData().count() > 0) {
std::cout << _("Erasing Exif data from the file") << std::endl;
}
image->clearExifData();
return 0;
}
int Erase::eraseIptcData(Exiv2::Image* image) const
{
if (Params::instance().verbose_ && image->iptcData().count() > 0) {
std::cout << _("Erasing IPTC data from the file") << std::endl;
}
image->clearIptcData();
return 0;
}
int Erase::eraseComment(Exiv2::Image* image) const
{
if (Params::instance().verbose_ && image->comment().size() > 0) {
std::cout << _("Erasing JPEG comment from the file") << std::endl;
}
image->clearComment();
return 0;
}
int Erase::eraseXmpData(Exiv2::Image* image) const
{
if (Params::instance().verbose_ && image->xmpData().count() > 0) {
std::cout << _("Erasing XMP data from the file") << std::endl;
}
image->clearXmpData(); // Quick fix for bug #612
image->clearXmpPacket();
return 0;
}
int Erase::eraseIccProfile(Exiv2::Image* image) const
{
if (Params::instance().verbose_ && image->iccProfileDefined() ) {
std::cout << _("Erasing ICC Profile data from the file") << std::endl;
}
image->clearIccProfile();
return 0;
}
Erase::AutoPtr Erase::clone() const
{
return AutoPtr(clone_());
}
Erase* Erase::clone_() const
{
return new Erase(*this);
}
Extract::~Extract()
{
}
int Extract::run(const std::string& path)
{
try {
path_ = path;
int rc = 0;
bool bStdout = Params::instance().target_ & Params::ctStdInOut ? true : false;
if (bStdout) {
_setmode(_fileno(stdout), _O_BINARY);
}
if (Params::instance().target_ & Params::ctThumb) {
rc = writeThumbnail();
}
if (!rc && Params::instance().target_ & Params::ctPreview) {
rc = writePreviews();
}
if (!rc && Params::instance().target_ & Params::ctXmpSidecar) {
std::string xmpPath = bStdout ? "-" : newFilePath(path_, ".xmp");
if (dontOverwrite(xmpPath))
return 0;
rc = metacopy(path_, xmpPath, Exiv2::ImageType::xmp, false);
}
if (!rc && Params::instance().target_ & Params::ctIccProfile) {
std::string iccPath = bStdout ? "-" : newFilePath(path_, ".icc");
rc = writeIccProfile(iccPath);
}
if (!rc
&& !(Params::instance().target_ & Params::ctXmpSidecar)
&& !(Params::instance().target_ & Params::ctThumb)
&& !(Params::instance().target_ & Params::ctPreview)
&& !(Params::instance().target_ & Params::ctIccProfile)) {
std::string exvPath = bStdout ? "-" : newFilePath(path_, ".exv");
if (dontOverwrite(exvPath))
return 0;
rc = metacopy(path_, exvPath, Exiv2::ImageType::exv, false);
}
return rc;
} catch (const Exiv2::AnyError& e) {
std::cerr << "Exiv2 exception in extract action for file " << path << ":\n" << e << "\n";
return 1;
}
}
int Extract::writeThumbnail() const
{
if (!Exiv2::fileExists(path_, true)) {
std::cerr << path_
<< ": " << _("Failed to open the file\n");
return -1;
}
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path_);
assert(image.get() != 0);
image->readMetadata();
Exiv2::ExifData& exifData = image->exifData();
if (exifData.empty()) {
std::cerr << path_
<< ": " << _("No Exif data found in the file\n");
return -3;
}
int rc = 0;
Exiv2::ExifThumb exifThumb(exifData);
std::string thumbExt = exifThumb.extension();
if (thumbExt.empty()) {
std::cerr << path_ << ": " << _("Image does not contain an Exif thumbnail\n");
}
else {
std::string thumb = newFilePath(path_, "-thumb");
std::string thumbPath = thumb + thumbExt;