forked from Exiv2/exiv2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactions.hpp
440 lines (377 loc) · 15.2 KB
/
actions.hpp
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
// ***************************************************************** -*- 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.hpp
@brief Implements base class Task, TaskFactory and the various supported
actions (derived from Task).
@author Andreas Huggel (ahu)
<a href="mailto:ahuggel@gmx.net">ahuggel@gmx.net</a>
@date 11-Dec-03, ahu: created
*/
#ifndef ACTIONS_HPP_
#define ACTIONS_HPP_
// *****************************************************************************
// included header files
#include "exiv2app.hpp"
// *****************************************************************************
// class declarations
namespace Exiv2 {
class ExifData;
class Image;
class Metadatum;
class PreviewImage;
}
// *****************************************************************************
// namespace extensions
/*!
@brief Contains all action classes (task subclasses).
*/
namespace Action {
//! Enumerates all tasks
enum TaskType { none, adjust, print, rename, erase, extract, insert,
modify, fixiso, fixcom };
// *****************************************************************************
// class definitions
/*!
@brief Abstract base class for all concrete actions.
Task provides a simple interface that actions must implement and a few
commonly used helpers.
*/
class Task {
public:
//! Shortcut for an auto pointer.
using UniquePtr = std::unique_ptr<Task>;
//! Virtual destructor.
virtual ~Task() = default;
//! Virtual copy construction.
UniquePtr clone() const;
/*!
@brief Application interface to perform a task.
@param path Path of the file to process.
@return 0 if successful.
*/
virtual int run(const std::string& path) =0;
/*!
@brief Application interface to perform a task.
@param path Path of the file to process.
@return 0 if successful.
*/
bool setBinary(bool b) { bool bResult = binary_ ; binary_ = b ; return bResult ;}
bool binary() { return binary_ ; }
private:
//! Internal virtual copy constructor.
virtual Task* clone_() const =0;
//! copy binary_ from command-line params to task
bool binary_ {false} ;
}; // class Task
/*!
@brief Task factory.
Creates an instance of the task of the requested type. The factory is
implemented as a singleton, which can be accessed only through the static
member function instance().
*/
class TaskFactory {
public:
/*!
@brief Get access to the task factory.
Clients access the task factory exclusively through
this method.
*/
static TaskFactory& instance();
//! Prevent copy construction: not implemented.
TaskFactory(const TaskFactory& rhs) = delete;
//! Destructor
void cleanup();
/*!
@brief Create a task.
@param type Identifies the type of task to create.
@return An auto pointer that owns a task of the requested type. If
the task type is not supported, the pointer is 0.
@remark The caller of the function should check the content of the
returned auto pointer and take appropriate action (e.g., throw
an exception) if it is 0.
*/
Task::UniquePtr create(TaskType type);
/*!
@brief Register a task prototype together with its type.
The task factory creates new tasks of a given type by cloning its
associated prototype. Additional tasks can be registered. If called
for a type which already exists in the list, the corresponding
prototype is replaced.
@param type Task type.
@param task Pointer to the prototype. Ownership is transferred to the
task factory. That's what the auto pointer indicates.
*/
void registerTask(TaskType type, Task::UniquePtr task);
private:
//! Prevent construction other than through instance().
TaskFactory();
//! Pointer to the one and only instance of this class.
static TaskFactory* instance_;
//! Type used to store Task prototype classes
using Registry = std::map<TaskType, Task*>;
//! List of task types and corresponding prototypes.
Registry registry_;
}; // class TaskFactory
//! %Print the Exif (or other metadata) of a file to stdout
class Print : public Task {
public:
~Print() override = default;
int run(const std::string& path) override;
using UniquePtr = std::unique_ptr<Print>;
UniquePtr clone() const;
//! Print the Jpeg comment
int printComment();
//! Print list of available preview images
int printPreviewList();
//! Print Exif summary information
int printSummary();
//! Print Exif, IPTC and XMP metadata in user defined format
int printList();
//! Return true if key should be printed, else false
static bool grepTag(const std::string& key);
//! Return true if key should be printed, else false
static bool keyTag(const std::string& key);
//! Print all metadata in a user defined format
int printMetadata(const Exiv2::Image* image);
//! Print a metadatum in a user defined format, return true if something was printed
bool printMetadatum(const Exiv2::Metadatum& md, const Exiv2::Image* image);
//! Print the label for a summary line
void printLabel(const std::string& label) const;
/*!
@brief Print one summary line with a label (if provided) and requested
data. A line break is printed only if a label is provided.
@return 1 if a line was written, 0 if the key was not found.
*/
int printTag(const Exiv2::ExifData& exifData,
const std::string& key,
const std::string& label ="") const;
//! Type for an Exiv2 Easy access function
using EasyAccessFct = Exiv2::ExifData::const_iterator (*)(const Exiv2::ExifData& ed);
/*!
@brief Print one summary line with a label (if provided) and requested
data. A line break is printed only if a label is provided.
@return 1 if a line was written, 0 if the information was not found.
*/
int printTag(const Exiv2::ExifData& exifData,
EasyAccessFct easyAccessFct,
const std::string& label ="",
EasyAccessFct easyAccessFctFallback =NULL) const;
private:
Print* clone_() const override;
std::string path_;
int align_{0}; // for the alignment of the summary output
}; // class Print
/*!
@brief %Rename a file to its metadate creation timestamp,
in the specified format.
*/
class Rename : public Task {
public:
~Rename() override = default;
int run(const std::string& path) override;
using UniquePtr = std::unique_ptr<Rename>;
UniquePtr clone() const;
private:
Rename* clone_() const override;
}; // class Rename
//! %Adjust the Exif (or other metadata) timestamps
class Adjust : public Task {
public:
~Adjust() override = default;
int run(const std::string& path) override;
using UniquePtr = std::unique_ptr<Adjust>;
UniquePtr clone() const;
private:
Adjust* clone_() const override;
int adjustDateTime(Exiv2::ExifData& exifData,
const std::string& key,
const std::string& path) const;
long adjustment_ {0};
long yearAdjustment_ {0};
long monthAdjustment_ {0};
long dayAdjustment_ {0};
}; // class Adjust
/*!
@brief %Erase the entire exif data or only the thumbnail section.
*/
class Erase : public Task {
public:
~Erase() override = default;
int run(const std::string& path) override;
using UniquePtr = std::unique_ptr<Erase>;
UniquePtr clone() const;
/*!
@brief Delete the thumbnail image, incl IFD1 metadata from the file.
*/
static int eraseThumbnail(Exiv2::Image* image);
/*!
@brief Erase the complete Exif data block from the file.
*/
static int eraseExifData(Exiv2::Image* image);
/*!
@brief Erase all Iptc data from the file.
*/
static int eraseIptcData(Exiv2::Image* image);
/*!
@brief Erase Jpeg comment from the file.
*/
static int eraseComment(Exiv2::Image* image);
/*!
@brief Erase XMP packet from the file.
*/
static int eraseXmpData(Exiv2::Image* image);
/*!
@brief Erase ICCProfile from the file.
*/
static int eraseIccProfile(Exiv2::Image* image);
private:
Erase* clone_() const override;
std::string path_;
}; // class Erase
/*!
@brief %Extract the entire exif data or only the thumbnail section.
*/
class Extract : public Task {
public:
~Extract() override = default;
int run(const std::string& path) override;
using UniquePtr = std::unique_ptr<Extract>;
UniquePtr clone() const;
/*!
@brief Write the thumbnail image to a file. The filename is composed by
removing the suffix from the image filename and appending
"-thumb" and the appropriate suffix (".jpg" or ".tif"), depending
on the format of the Exif thumbnail image.
*/
int writeThumbnail() const;
/*!
@brief Write preview images to files.
*/
int writePreviews() const;
/*!
@brief Write one preview image to a file. The filename is composed by
removing the suffix from the image filename and appending
"-preview<num>" and the appropriate suffix (".jpg" or ".tif"),
depending on the format of the Exif thumbnail image.
*/
void writePreviewFile(const Exiv2::PreviewImage& pvImg, int num) const;
/*!
@brief Write embedded iccProfile files.
*/
int writeIccProfile(const std::string& target) const;
private:
Extract* clone_() const override;
std::string path_;
}; // class Extract
/*!
@brief %Insert the Exif data from corresponding *.exv files.
*/
class Insert : public Task {
public:
~Insert() override = default;
int run(const std::string& path) override;
using UniquePtr = std::unique_ptr<Insert>;
UniquePtr clone() const;
/*!
@brief Insert a Jpeg thumbnail image from a file into file \em path.
The filename of the thumbnail is expected to be the image
filename (\em path) minus its suffix plus "-thumb.jpg".
*/
static int insertThumbnail(const std::string& path);
/*!
@brief Insert an XMP packet from a xmpPath into file \em path.
*/
static int insertXmpPacket(const std::string& path,const std::string& xmpPath) ;
/*!
@brief Insert xmp from a DataBuf into file \em path.
*/
static int insertXmpPacket(const std::string& path, const Exiv2::DataBuf& xmpBlob, bool usePacket = false);
/*!
@brief Insert an ICC profile from iccPath into file \em path.
*/
static int insertIccProfile(const std::string& path,const std::string& iccPath) ;
/*!
@brief Insert an ICC profile from binary DataBuf into file \em path.
*/
static int insertIccProfile(const std::string& path, Exiv2::DataBuf& iccProfileBlob);
private:
Insert* clone_() const override;
}; // class Insert
/*!
@brief %Modify the Exif data according to the commands in the
modification table.
*/
class Modify : public Task {
public:
~Modify() override = default;
int run(const std::string& path) override;
using UniquePtr = std::unique_ptr<Modify>;
UniquePtr clone() const;
Modify() {}
//! Apply modification commands to the \em pImage, return 0 if successful.
static int applyCommands(Exiv2::Image* pImage);
private:
Modify* clone_() const override;
//! Copy constructor needed because of UniquePtr member
Modify(const Modify& /*src*/) = default;
//! Add a metadatum to \em pImage according to \em modifyCmd
static int addMetadatum(Exiv2::Image* pImage,
const ModifyCmd& modifyCmd);
//! Set a metadatum in \em pImage according to \em modifyCmd
static int setMetadatum(Exiv2::Image* pImage,
const ModifyCmd& modifyCmd);
//! Delete a metadatum from \em pImage according to \em modifyCmd
static void delMetadatum(Exiv2::Image* pImage,
const ModifyCmd& modifyCmd);
//! Register an XMP namespace according to \em modifyCmd
static void regNamespace(const ModifyCmd& modifyCmd);
}; // class Modify
/*!
@brief %Copy ISO settings from any of the Nikon makernotes to the
regular Exif tag, Exif.Photo.ISOSpeedRatings.
*/
class FixIso : public Task {
public:
~FixIso() override = default;
int run(const std::string& path) override;
using UniquePtr = std::unique_ptr<FixIso>;
UniquePtr clone() const;
private:
FixIso* clone_() const override;
std::string path_;
}; // class FixIso
/*!
@brief Fix the character encoding of Exif UNICODE user comments.
Decodes the comment using the auto-detected or specified
character encoding and writes it back in UCS-2.
*/
class FixCom : public Task {
public:
~FixCom() override = default;
int run(const std::string& path) override;
using UniquePtr = std::unique_ptr<FixCom>;
UniquePtr clone() const;
private:
FixCom* clone_() const override;
std::string path_;
}; // class FixCom
} // namespace Action
#endif // #ifndef ACTIONS_HPP_