-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertMode.cpp
100 lines (74 loc) · 2.69 KB
/
ConvertMode.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
#include <iostream>
#include "ConvertMode.h"
#include <stb_image.h>
#include <stb_image_write.h>
std::string ToString(ConvertMode::Format format)
{
switch(format)
{
case ConvertMode::Format::JPG:
return "jpg";
case ConvertMode::Format::PNG:
return "png";
default:
break;
}
return "";
}
std::ostream& operator<<(std::ostream& out, ConvertMode::Format format)
{
out << ToString(format);
return out;
}
ConvertMode::ConvertMode(const std::string& filter, const std::string& folder, Format fromFormat, Format toFormat, int quality)
: Mode{ filter, folder }
, m_FromFormat{ fromFormat }
, m_ToFormat{ toFormat }
, m_Quality{ quality }
{}
const std::string& ConvertMode::GetModeName() const
{
static const std::string ConvertModeName = "[Converter]: ";
return ConvertModeName;
}
void ConvertMode::RunImpl()
{
std::cout << GetModeName() << "Modo : Converter" << std::endl;
std::cout << GetModeName() << "Pasta : " << GetFolder() << std::endl;
std::cout << GetModeName() << "Filtro : " << GetFilter() << std::endl;
std::cout << GetModeName() << "Origem : " << m_FromFormat << std::endl;
std::cout << GetModeName() << "Destino : " << m_ToFormat << std::endl;
const std::filesystem::path fromExtension = "." + ToString(m_FromFormat);
for(const std::filesystem::path& filepath : GetFiles(fromExtension))
{
std::cout << GetModeName() << "Convertendo " << filepath << std::endl;
int width = 0;
int height = 0;
int numComp = 0;
const int numReqComp = 3;
if(unsigned char* data = stbi_load(filepath.string().c_str(), &width, &height, &numComp, numReqComp))
{
std::filesystem::path destFilepath = filepath;
destFilepath.replace_extension(ToString(m_ToFormat));
int writeResult = 0;
switch (m_ToFormat)
{
case Format::PNG:
writeResult = stbi_write_png(destFilepath.string().c_str(), width, height, numComp, data, 0);
break;
case Format::JPG:
writeResult = stbi_write_jpg(destFilepath.string().c_str(), width, height, numComp, data, m_Quality);
break;
default:
break;
}
if(writeResult == 0)
{
std::cout << GetModeName() << "Erro ao Converter " << filepath << std::endl;
}
stbi_image_free(data);
}else{
std::cout << GetModeName() << "Erro ao carregar " << filepath << std::endl;
}
}
}