-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresize.cpp
More file actions
67 lines (50 loc) · 1.77 KB
/
resize.cpp
File metadata and controls
67 lines (50 loc) · 1.77 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
#include <Magick++.h>
#include <iostream>
#include <list>
#include "resize.h"
int init_resize(int memory_limit) {
try {
Magick::InitializeMagick(nullptr);
Magick::ResourceLimits::memory(memory_limit);
} catch (Magick::Exception &error) {
std::cerr << " Error: " << error.what() << std::endl;
return 1;
}
return 0;
}
int resize(char* input_filename, char* output_filename, int targetWidth, int targetHeight) {
std::cout << " Input " << input_filename << std::endl;
try {
std::list<Magick::Image> frames;
Magick::readImages(&frames, input_filename);
if (frames.empty()) {
std::cerr << " Error: No frames found in the input file." << std::endl;
return 1;
}
std::list<Magick::Image> cFrames;
if (frames.size() > 1) {
std::cout << " Multiple frames detected: " << frames.size() << std::endl;
Magick::coalesceImages(&cFrames, frames.begin(), frames.end());
} else {
cFrames = frames;
}
for (auto &frame : cFrames) {
frame.autoOrient();
if (targetWidth > frame.columns()) {
targetWidth = frame.columns();
}
if (targetHeight > frame.rows()) {
targetHeight = frame.rows();
}
frame.resize(Magick::Geometry(targetWidth, targetHeight));
frame.quality(70);
frame.magick("WEBP");
}
writeImages(cFrames.begin(), cFrames.end(), output_filename);
std::cout << " Done. Saved to " << output_filename << std::endl;
} catch (Magick::Exception &error) {
std::cerr << " Error: " << error.what() << std::endl;
return 1;
}
return 0;
}