/
CImage.cpp
269 lines (223 loc) · 7.94 KB
/
CImage.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
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
#include "CImage.h"
#include "./exceptions/ImageNotSupportedException.h"
#include "exceptions/ImageTooBigException.h"
#include <QDir>
#include <QImageReader>
#include <QSettings>
#include <QTemporaryFile>
#include <cmath>
CImage::CImage(const QString& path)
{
QFileInfo fileInfo = QFileInfo(path);
auto* imageReader = new QImageReader(path);
auto format = imageReader->format().toLower();
if (!supportedFormats.contains(format)) {
throw ImageNotSupportedException();
}
this->size = fileInfo.size();
if (this->size > 104857600) {
throw ImageTooBigException();
}
this->fullPath = fileInfo.canonicalFilePath();
this->fileName = fileInfo.fileName();
this->compressedSize = this->size;
QSize imageSize = imageReader->size();
this->width = imageSize.width();
this->height = imageSize.height();
this->compressedWidth = this->width;
this->compressedHeight = this->height;
this->status = CImageStatus::UNCOMPRESSED;
delete imageReader;
}
bool operator==(const CImage& c1, const CImage& c2)
{
return (c1.fullPath == c2.fullPath);
}
bool operator!=(const CImage& c1, const CImage& c2)
{
return !(c1 == c2);
}
QString CImage::getFormattedSize()
{
size_t s = this->status == CImageStatus::COMPRESSED ? this->compressedSize : this->size;
return toHumanSize((double)s);
}
QString CImage::getRichFormattedSize()
{
if (this->status == CImageStatus::COMPRESSED && this->size != this->compressedSize) {
return "<small><s>" + toHumanSize((double)this->size) + "</s></small> " + toHumanSize((double)this->compressedSize);
}
return toHumanSize((double)this->size);
}
QString CImage::getResolution()
{
if (this->status == CImageStatus::COMPRESSED) {
return QString::number(this->compressedWidth) + "x" + QString::number(this->compressedHeight);
}
return QString::number(this->width) + "x" + QString::number(this->height);
}
QString CImage::getRichResolution()
{
if (this->status == CImageStatus::COMPRESSED && (this->width != this->compressedWidth || this->height != this->compressedHeight)) {
return "<small><s>" + QString::number(this->width) + "x" + QString::number(this->height) + "</s></small> " + QString::number(this->compressedWidth) + "x" + QString::number(this->compressedHeight);
}
return QString::number(this->width) + "x" + QString::number(this->height);
}
QString CImage::getFileName() const
{
return fileName;
}
QString CImage::getFullPath() const
{
return this->fullPath;
}
bool CImage::compress(const CompressionOptions& compressionOptions)
{
QSettings settings;
QString outputPath = compressionOptions.sameFolderAsInput ? QFileInfo(this->getFullPath()).absoluteDir().absolutePath() : compressionOptions.outputPath;
QString inputFullPath = this->getFullPath();
QString suffix = compressionOptions.suffix;
QFileInfo inputFileInfo = QFileInfo(inputFullPath);
if (!inputFileInfo.exists()) {
return false;
}
QString fullFileName = inputFileInfo.completeBaseName() + suffix + "." + inputFileInfo.suffix();
FileDates inputFileDates = {
inputFileInfo.fileTime(QFile::FileBirthTime),
inputFileInfo.fileTime(QFile::FileModificationTime),
inputFileInfo.fileTime(QFile::FileAccessTime)
};
if (compressionOptions.keepStructure && !compressionOptions.sameFolderAsInput) {
outputPath = inputFileInfo.absolutePath().remove(0, compressionOptions.basePath.length());
outputPath = QDir::cleanPath(compressionOptions.outputPath + QDir::separator() + outputPath);
}
QDir outputDir(outputPath);
if (!outputDir.exists()) {
if (!outputDir.mkpath(outputPath)) {
return false;
}
}
QString outputFullPath = outputDir.absoluteFilePath(fullFileName);
QString tempFileFullPath = "";
bool outputAlreadyExists = QFile(outputFullPath).exists();
QTemporaryFile tempFile(outputPath + QDir::separator() + inputFileInfo.completeBaseName() + ".XXXXXXXX." + inputFileInfo.suffix());
if (tempFile.open()) {
tempFileFullPath = tempFile.fileName();
}
if (tempFileFullPath.isEmpty()) {
return false;
}
tempFile.close();
bool lossless = compressionOptions.lossless;
bool keepMetadata = compressionOptions.keepMetadata;
CCSParameters r_parameters = {
keepMetadata,
static_cast<unsigned int>(compressionOptions.jpeg_quality),
static_cast<unsigned int>(compressionOptions.png_level),
false,
20,
static_cast<unsigned int>(compressionOptions.webp_quality),
lossless,
0,
0
};
QImageReader::setAllocationLimit(512);
// Resize
if (compressionOptions.resize) {
QImageReader imageReader(this->getFullPath());
QSize originalSize = imageReader.size();
std::tuple<unsigned int, unsigned int> dimensions = cResize(originalSize,
compressionOptions.fitTo,
compressionOptions.width,
compressionOptions.height,
compressionOptions.size,
compressionOptions.doNotEnlarge);
if (std::get<0>(dimensions) != originalSize.width() || std::get<1>(dimensions) != originalSize.height()) {
r_parameters.width = std::get<0>(dimensions);
r_parameters.height = std::get<1>(dimensions);
}
}
bool result = c_compress(inputFullPath.toUtf8().constData(), tempFileFullPath.toUtf8().constData(), r_parameters);
if (result) {
QFileInfo outputInfo(tempFileFullPath);
bool outputIsBiggerThanInput = outputInfo.size() >= inputFileInfo.size();
bool copyResult;
if (outputAlreadyExists) {
QFile::remove(outputFullPath);
}
if (!outputIsBiggerThanInput) {
copyResult = QFile::copy(tempFileFullPath, outputFullPath);
} else {
copyResult = QFile::copy(inputFullPath, outputFullPath);
}
if (!copyResult) {
return false;
}
QFileInfo outputFileInfo = QFileInfo(outputFullPath);
if (compressionOptions.keepDates) {
this->setFileDates(outputFileInfo, compressionOptions, inputFileDates);
}
this->setCompressedInfo(outputFileInfo);
}
return result;
}
void CImage::setCompressedInfo(QFileInfo fileInfo)
{
QImage compressedImage(fileInfo.absoluteFilePath());
this->compressedSize = fileInfo.size();
this->compressedFullPath = fileInfo.absoluteFilePath();
this->compressedWidth = compressedImage.width();
this->compressedHeight = compressedImage.height();
}
void CImage::setFileDates(QFileInfo fileInfo, CompressionOptions compressionOptions, FileDates inputFileDates)
{
QFile outputFile(fileInfo.absoluteFilePath());
outputFile.open(QIODevice::ReadWrite);
if (compressionOptions.datesMap.keepCreation) {
outputFile.setFileTime(inputFileDates.creation, QFileDevice::FileBirthTime);
}
if (compressionOptions.datesMap.keepLastModified) {
outputFile.setFileTime(inputFileDates.lastModified, QFileDevice::FileModificationTime);
}
if (compressionOptions.datesMap.keepLastAccess) {
outputFile.setFileTime(inputFileDates.lastAccess, QFileDevice::FileAccessTime);
}
outputFile.close();
}
QString CImage::getCompressedFullPath() const
{
return compressedFullPath;
}
double CImage::getRatio() const
{
return (double)this->compressedSize / this->size;
}
QString CImage::getFormattedSavedRatio()
{
return QString::number(round(100 - (this->getRatio() * 100))) + "%";
}
QString CImage::getRichFormattedSavedRatio()
{
// TODO
return this->getFormattedSavedRatio();
}
CImageStatus CImage::getStatus() const
{
return status;
}
void CImage::setStatus(const CImageStatus& value)
{
status = value;
}
size_t CImage::getOriginalSize() const
{
return this->size;
}
size_t CImage::getCompressedSize() const
{
return this->compressedSize;
}
size_t CImage::getTotalPixels() const
{
return this->width * this->height;
}