-
Notifications
You must be signed in to change notification settings - Fork 0
/
subpixeler.cpp
119 lines (94 loc) · 3.32 KB
/
subpixeler.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
#include <fstream>
#include <cmath>
#include <cstring>
#include <sstream>
int main(int argc, char* argv[]) {
std::string input_filename = "input.png";
if (argc > 1) {
input_filename = argv[1];
}
std::string input_command = "ffmpeg -loglevel quiet -i \"" + input_filename + "\" subpixel_input.ppm -y";
system(input_command.c_str());
std::ifstream input1("subpixel_input.ppm", std::ios_base::binary);
char magic_num1[3];
int width1 = 0, height1 = 0, max_val1 = 0;
input1 >> magic_num1;
input1 >> width1 >> height1 >> max_val1;
input1.get();
int temp_x = 3*int(std::floor(width1*0.3333333333333333));
int temp_y = 3*int(std::floor(height1*0.3333333333333333));
int overhang_x = width1 - temp_x;
width1 = temp_x;
height1 = temp_y;
int num_pixels1 = width1*height1;
std::ofstream output1("subpixel.ppm", std::ios_base::binary);
output1 << magic_num1 << "\n" << width1 << " " << height1 << "\n" << max_val1 << std::endl;
for (int i=0; i<num_pixels1; i++) {
if (i%width1 >= temp_x-1) {
if (overhang_x == 1) {
input1.get();
input1.get();
input1.get();
} else if (overhang_x == 2) {
input1.get();
input1.get();
input1.get();
input1.get();
input1.get();
input1.get();
}
}
unsigned char old_rgb[3];
unsigned char new_rgb[3];
old_rgb[0] = input1.get();
old_rgb[1] = input1.get();
old_rgb[2] = input1.get();
if ((i%width1)%3 == 0) {
new_rgb[0] = old_rgb[0];
new_rgb[1] = 0;
new_rgb[2] = 0;
} else if ((i%width1)%3 == 1) {
new_rgb[0] = 0;
new_rgb[1] = old_rgb[1];
new_rgb[2] = 0;
} else {
new_rgb[0] = 0;
new_rgb[1] = 0;
new_rgb[2] = old_rgb[2];
}
output1 << new_rgb[0] << new_rgb[1] << new_rgb[2];
}
output1.close();
input1.close();
int height_2 = int(std::floor(height1*0.3333333333333333));
std::stringstream ss;
ss << "ffmpeg -loglevel quiet -i subpixel.ppm -vf scale=" << width1 << ":" << height_2 << ":flags='lanczos' subpixel_scaled.ppm -y";
system(ss.str().c_str());
std::ifstream input("subpixel_scaled.ppm", std::ios_base::binary);
char magic_num[3];
int width = 0, height = 0, max_val = 0;
input >> magic_num;
input >> width >> height >> max_val;
input.get();
int num_pixels = width*height;
std::ofstream output("subpixel_output.ppm", std::ios_base::binary);
output << magic_num << "\n" << int(std::floor(width*0.3333333333333333)) << " " << height << "\n" << max_val << std::endl;
for (int i=0; i<num_pixels; i++) {
unsigned char rgb[3];
rgb[0] = input.get(); // R
input.get();
input.get();
input.get();
rgb[1] = input.get(); // G
input.get();
input.get();
input.get();
rgb[2] = input.get(); // B
output << rgb[0] << rgb[1] << rgb[2];
}
output.close();
input.close();
std::string output_command = "ffmpeg -loglevel quiet -i subpixel_output.ppm " + input_filename + " -y && del *.ppm";
system(output_command.c_str());
return 0;
}