-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
181 lines (159 loc) · 5.54 KB
/
Copy pathmain.cpp
File metadata and controls
181 lines (159 loc) · 5.54 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
// Copyright © 2021 Smbat Makiyan (a.k.a. idimus, a.k.a. simfeo). All rights reserved.
#include <heifreader.h>
#include <heifboxes.h>
#include <TinyEXIF.h>
#include <argparse.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
bool check_extension(std::string const& img_file, std::string const& extension)
{
if (img_file.length() >= extension.length())
{
return (0 == img_file.compare(img_file.length() - extension.length(), extension.length(), extension));
}
else
{
return false;
}
}
int main(int argc, char** argv)
{
ArgumentParser parser;
parser.addArgument("-i", "--input", 1);
parser.addArgument("-o", "--output", 1);
parser.addArgument("--help");
//parser.addArgument("--license");
try
{
if (argc > 1)
{
const char** ptr = new const char* [argc];
for (int i = 0; i < argc; ++i)
{
ptr[i] = argv[i];
}
parser.parse(static_cast<size_t>(argc), ptr);
delete[] ptr;
}
else
{
std::cout << parser.usage() << std::endl;
return 0;
}
}
catch (const std::exception&)
{
std::cout << parser.usage() << std::endl;
return 1;
}
if (parser.count("help"))
{
std::cout << parser.usage() << std::endl;
return 0;
}
//if (parser.exists("license"))
//{
// return 0;
//}
if (!parser.count("input") || !parser.count("output"))
{
std::cerr << "you should specify both input and output files" << std::endl;
std::cout << parser.usage() << std::endl;
return 2;
}
std::string input_file = parser.retrieve<std::string>("input");
std::string output_file = parser.retrieve<std::string>("output");
std::string input_file_lower;
input_file_lower.resize(input_file.size());
std::transform(input_file.begin(), input_file.end(), input_file_lower.begin(),
[](unsigned char c) -> unsigned char { return std::tolower(c); });
if ( !check_extension(input_file_lower, ".jpg")
&& !check_extension(input_file_lower, ".jpeg")
&& !check_extension(input_file_lower, ".heic"))
{
std::cerr << R"(this application works only with ".jpg", ".jpeg" and ".heic" files.)" << std::endl;
return 3;
}
if (check_extension(input_file_lower, ".heic"))
{
HeifReader heif;
if (HeifHelpers::OperationResult::Ok != heif.load(input_file.c_str()))
{
std::cerr << "cannot read input file" << std::endl;
return 3;
}
SefdBox sf = heif.getSefdBox();
if (sf.getSize() != 0
&& sf.getFtyp().getSize() != 0
&& sf.getFtyp().GetMajorBand() == "mp42"
&& sf.getMdat().getSize() != 0
&& sf.getMdat().startPosition() != 0
&& sf.getMdat().endPosition() != 0)
{
std::vector<uint8_t> videoData;
videoData.resize(sf.getSize() - sf.getFtypStartPos());
std::shared_ptr<std::ifstream> imgFile(new std::ifstream(input_file.c_str(), std::ifstream::in | std::ifstream::binary), [](std::ifstream* p) {if (p) { p->close(), delete p; }});
if (imgFile->bad())
{
std::cerr << "cannot read input file" << std::endl;
return 3;
}
imgFile->seekg(heif.getSefdOffset() + sf.getFtypStartPos());
imgFile->read(reinterpret_cast<char*>(&videoData[0]), videoData.size());
std::ofstream ofile(output_file.c_str(), std::ios::binary);
if (ofile.bad())
{
std::cerr << "cannot open out file" << std::endl;
return 5;
}
ofile.write((char*)(videoData.data()), videoData.size());
ofile.close();
}
else
{
std::cerr << "there is no any video in this file" << std::endl;
return 4;
}
}
else
{
std::shared_ptr<std::ifstream> imgFile(new std::ifstream(input_file.c_str(), std::ifstream::in | std::ifstream::binary), [](std::ifstream* p) {if (p) { p->close(), delete p; }});
if (imgFile->bad())
{
std::cerr << "cannot read input file" << std::endl;
return 3;
}
imgFile->seekg(0, std::ios::end);
std::streampos length = imgFile->tellg();
imgFile->seekg(0, std::ios::beg);
std::vector<uint8_t> data(length);
imgFile->read((char*)data.data(), length);
auto exif_info = TinyEXIF::EXIFInfo(data.data(), length);
if (exif_info.Fields)
{
exif_info.parseFromXMPSegment(data.data(), length);
if (exif_info.MicroVideo.HasMicroVideo)
{
size_t offset = data.size() - exif_info.MicroVideo.MicroVideoOffset;
std::ofstream ofile(output_file.c_str(), std::ios::binary);
if (ofile.bad())
{
std::cerr << "cannot open out file" << std::endl;
return 5;
}
offset = data.size() - exif_info.MicroVideo.MicroVideoOffset;
ofile.write((char*)(data.data() + offset), data.size() - offset);
ofile.close();
}
else
{
std::cerr << "there is no any video in this file" << std::endl;
return 4;
}
}
}
std::cout << "job is done" << std::endl;
return 0;
}