-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cc
More file actions
110 lines (97 loc) · 3.33 KB
/
example.cc
File metadata and controls
110 lines (97 loc) · 3.33 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
#include<iostream>
#include<vector>
#include<lodepng.h>
#include<iomanip>
#include <smartResize.hpp>
using namespace std;
void encodeOneStep(const char* filename, std::vector<unsigned char>& image, unsigned width, unsigned height) {
//Encode the image
unsigned error = lodepng::encode(filename, image, width, height);
//if there's an error, display it
if(error) std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
}
void printHelp(){
cout << "Usage example:" << endl;
cout << " resize inpute.png output.png 'width' 'height' <mask.png>" << endl;
cout << "\"inpute.png\" path to png input inage, at least 10x10 pixels" << endl;
cout << "\"output.png\" name of png result inage" << endl;
cout << "'width' integer higher then 10 - desired image width" << endl;
cout << "'height' integer higher then 10 - desired image width" << endl;
cout << "\"mask.png\" optional mask image " << endl;
}
int main(int argc, char *argv[]){
if(argc < 5){
printHelp();
return 1;
}
const char* inputImageFileName = argv[1];
const char* outputImageFileName = argv[2];
const char* maskImageFileName = nullptr;
if(argc == 6){
maskImageFileName = argv[5];
}
std::vector<unsigned char> sourceImage; //raw pixels
std::vector<unsigned char> mask;
unsigned imageWidth, imageHeight;
unsigned maskWidth, maskHeight;
unsigned error = lodepng::decode(sourceImage, imageWidth, imageHeight, inputImageFileName);
if(error) {
cout << "Could not read image file" << endl;
std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
return 1;
}
if(maskImageFileName != nullptr){
error = lodepng::decode(mask, maskWidth, maskHeight, maskImageFileName);
if(error) {
cout << "Could not read mask file" << endl;
std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
return 1;
}
}
int desiredWidth, desiredHeight;
std::string arg(argv[3]);
try {
std::size_t pos;
desiredWidth = std::stoi(arg, &pos);
if (pos < arg.size()) {
std::cerr << "Trailing characters after number: " << arg << '\n';
}
} catch (std::invalid_argument const &ex) {
std::cerr << "Invalid number: " << arg << '\n';
return 1;
} catch (std::out_of_range const &ex) {
std::cerr << "Number out of range: " << arg << '\n';
return 1;
}
std::string arg2(argv[4]);
try {
std::size_t pos;
desiredHeight = std::stoi(arg2, &pos);
if (pos < arg2.size()) {
std::cerr << "Trailing characters after number: " << arg2 << '\n';
}
} catch (std::invalid_argument const &ex) {
std::cerr << "Invalid number: " << arg2 << '\n';
return 1;
} catch (std::out_of_range const &ex) {
std::cerr << "Number out of range: " << arg2 << '\n';
return 1;
}
if(desiredWidth < 10 || desiredHeight < 10){
printHelp();
return 1;
}
if(imageHeight < 10 || imageWidth < 10){
printHelp();
return 1;
}
///////////////////////////////////////////////////////////////
std::vector<unsigned char> output;
if(maskImageFileName != nullptr){
output = smartResize(&sourceImage, imageWidth, desiredWidth, desiredHeight, &mask);
}else{
output = smartResize(&sourceImage, imageWidth, desiredWidth, desiredHeight);
}
if(output.size() > 0) encodeOneStep(outputImageFileName, output, desiredWidth, desiredHeight);
return 0;
}