This repository was archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
136 lines (112 loc) · 3.76 KB
/
main.cpp
File metadata and controls
136 lines (112 loc) · 3.76 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
#include <cstdio>
#include <fstream>
#include <iostream>
#include <map>
#include "conv.h"
#include "qoi.h"
void QoiToPnm(std::string fType, bool omitted) {
auto fName = "temp." + fType;
std::fstream temp;
temp.open(fName, std::ios::out | std::ios::binary | std::ios::trunc);
auto backup = std::cout.rdbuf();
auto target = temp.rdbuf();
uint32_t width, height;
uint8_t channels, colorspace;
// .qoi -> .rgb(a)
std::cout.rdbuf(target); // redirect std::cout to ofstream temp
QoiDecode(width, height, channels, colorspace);
std::cout.rdbuf(backup); // restore std::cout buffer
// image type check
if (fType == "rgb" && channels != 3) {
std::cerr << "image type doesnt match the channel number" << std::endl;
return ;
}
if (fType == "rgba" && channels != 4) {
std::cerr << "image type doesnt match the channel number" << std::endl;
return;
}
// .rgb(a) -> .ppm/pam
temp.close();
temp.open(fName, std::ios::in | std::ios::binary);
try {
if (fType == "rgb") {
RgbToPpm(temp, std::cout, width, height);
} else {
RgbaToPam(temp, std::cout, width, height);
}
} catch (const char *msg) {
std::cerr << msg << std::endl;
return;
} catch (...) {
std::cerr << "an error is raised during conversion" << std::endl;
}
temp.close();
if (omitted) std::remove(fName.c_str());
}
void PnmToQoi(std::string fType, bool omitted) {
auto fName = "temp." + fType;
std::fstream temp;
temp.open(fName, std::ios::out | std::ios::binary | std::ios::trunc);
uint32_t width, height;
uint8_t channels, colorspace;
// .ppm/pam -> .rgb(a)
try {
if (fType == "rgb") {
PpmToRgb(std::cin, temp, width, height);
channels = 3u;
} else {
PamToRgba(std::cin, temp, width, height);
channels = 4u;
}
} catch (const char *msg) {
std::cerr << msg << std::endl;
return;
} catch (...) {
std::cerr << "an error is raised during conversion" << std::endl;
}
temp.close();
temp.open(fName, std::ios::in | std::ios::binary);
auto backup = std::cin.rdbuf();
auto target = temp.rdbuf();
// .rgb(a) -> .qoi
std::cin.rdbuf(target); // redirect std::cin to ifstream temp
QoiEncode(width, height, channels, 0u);
std::cin.rdbuf(backup); // restore std::cin buffer
temp.close();
if (omitted) std::remove(fName.c_str());
}
int main(int argc, char *argv[]) {
if (argc <= 1) {
std::cerr << "too few arguments" << std::endl;
return 0;
}
std::map<std::string, bool> args;
for (int i = 1; i < argc; ++i) args[argv[i]] = true;
if (args["-h"]) {
std::string helpinfo =
R"(Hi, im a command line qoi <-> ppm/pam converter :-)
Usage: ./conv [options...] [<path/to/image] [>path/to/image]
-h Get help information
-d Convert qoi to ppm/pam
-e Convert ppm/pam to qoi
-3 Prompt me that im converting rgb images
-4 Prompt me that im converting rgba images
-o Omit temporary files generated by the intermediate process
<path/to/image Redirect stdin to an image file
>path/to/image Redirect stdout to an image file
Examples:
qoi -> ppm: ./conv -d -3 <sample/rgb/kodim23.qoi >kodim23.ppm
pam -> qoi: ./conv -e -4 -o <sample/rgba/dice.pam >dice.qoi
)";
std::cerr << helpinfo;
return 0;
}
if (args["-e"] && args["-d"]) {
std::cerr << "-d and -e options conflict with one anthor, please choose only one." << std::endl;
return 0;
}
std::string type = args["-4"] ? "rgba" : "rgb";
if (args["-e"]) PnmToQoi(type, args["-o"]);
if (args["-d"]) QoiToPnm(type, args["-o"]);
return 0;
}