-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcr3-to-jpeg.cpp
126 lines (104 loc) · 3.72 KB
/
cr3-to-jpeg.cpp
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
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
bool extractThumbnail(const char *inputFile, const char *outputFile) {
// Open the input CR3 file
ifstream inFile(inputFile, ios::binary);
if (!inFile) {
cerr << "Error opening input file: " << inputFile << endl;
return false;
}
// Get the file size
inFile.seekg(0, ios::end);
size_t fileSize = inFile.tellg();
inFile.seekg(0, ios::beg);
// Read the entire file into a buffer
char *buf = new char[fileSize];
inFile.read(buf, fileSize);
inFile.close();
// Variables to find the JPEG thumbnail
bool found = false;
size_t n = 0;
while (n < fileSize - 4 && !found) {
if (memcmp(&buf[n], "mdat", 4) == 0) {
if (memcmp(&buf[n + 12], "\xFF\xD8", 2) == 0) { // JPEG start marker
size_t i = 0;
while (!found && (n + 12 + i < fileSize - 1)) {
if (memcmp(&buf[n + 12 + i], "\xFF\xD9", 2) == 0) { // JPEG end marker
found = true;
}
i++;
}
if (found) {
// Write the thumbnail to the output file
ofstream outFile(outputFile, ios::binary);
if (!outFile) {
cerr << "Error creating output file: " << outputFile << endl;
delete[] buf;
return false;
}
// Write JPEG header and data
outFile.write(&buf[n + 12], i + 2); // +2 for the end marker
outFile.close();
}
} else {
break;
}
} else {
n++;
}
}
// Clean up
delete[] buf;
if (!found) {
cerr << "No JPEG thumbnail found in: " << inputFile << endl;
return false;
}
cout << "Thumbnail extracted as '" << outputFile << "'" << endl;
return true;
}
string getOutputFileName(const fs::path& inputFile) {
return inputFile.stem().string() + ".jpg"; // Use stem() to get the filename without extension
}
bool copyExifData(const std::string& sourceFile, const std::string& destinationFile) {
// Build the command string
std::string command = "exiftool -overwrite_original -tagsFromFile \"" + sourceFile + "\" -all:all \"" + destinationFile + "\"";
// Execute the command
int result = system(command.c_str());
// Check if the command was successful
if (result != 0) {
cerr << "Error executing command: " << command << endl;
return false;
}
cout << "EXIF data copied from " << sourceFile << " to " << destinationFile << endl;
return true;
}
int main(int argc, char **argv) {
if (argc != 2) {
cerr << "Usage: " << argv[0] << " <input_directory>" << endl;
return 1;
}
fs::path inputDir(argv[1]);
if (!fs::is_directory(inputDir)) {
cerr << "Error: " << argv[1] << " is not a valid directory." << endl;
return 1;
}
for (const auto& entry : fs::directory_iterator(inputDir)) {
if (entry.is_regular_file() && entry.path().extension() == ".CR3") {
const auto& inputFile = entry.path();
string outputFile = getOutputFileName(inputFile);
outputFile = inputDir / outputFile; // Output in the same directory
if (!extractThumbnail(inputFile.string().c_str(), outputFile.c_str())) {
return 1;
}
if (!copyExifData(inputFile.string().c_str(), outputFile.c_str())) {
return 1;
}
}
}
return 0;
}