-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResizeMode.cpp
More file actions
118 lines (84 loc) · 3.63 KB
/
ResizeMode.cpp
File metadata and controls
118 lines (84 loc) · 3.63 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
#include <iostream>
#include "ResizeMode.h"
#include "Utils.h"
// #define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
// #define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
// #define STB_IMAGE_RESIZE_IMPLEMENTATION
#include <stb_image_resize.h>
ResizeMode::ResizeMode(const std::string& filter, const std::string& folder, int width, int height, int quality)
: Mode{ filter, folder }
, m_Width{ width }
, m_Height{ height }
, m_Quality{ quality }
{}
const std::string& ResizeMode::GetModeName() const
{
static const std::string ResizeModeName = "[Redimensionar]: ";
return ResizeModeName;
}
void ResizeMode::ResizeImage(const std::filesystem::path& filepath, int newWidth, int newHeight) const
{
// Ler a imagem
// Redimensionar em memória
// Escrevera imagem
int inputWidth = 0;
int inputHeight = 0;
int inputNumComp = 0;
const int numCompReq = 4;
if(unsigned char* inputData = stbi_load(filepath.string().c_str(), &inputWidth, &inputHeight, &inputNumComp, numCompReq))
{
const int numOutputPixels = newWidth * newHeight * numCompReq;
// unsigned char == std::uint8_t
std::vector<std::uint8_t> outputData(numOutputPixels, 0);
const int resizeResult = stbir_resize_uint8( inputData, inputWidth, inputHeight, 0,
outputData.data(), newWidth, newHeight, 0,
numCompReq
);
if(resizeResult == 1)
{
int writeResult = 1;
const std::filesystem::path extension = filepath.extension();
if( Utils::ToLower(extension.string()) == ".jpg" )
{
writeResult = stbi_write_jpg( filepath.string().c_str(),
newWidth, newHeight, numCompReq,
outputData.data(), m_Quality
);
} else if( Utils::ToLower(extension.string()) == ".png" ) {
writeResult = stbi_write_png( filepath.string().c_str(),
newWidth, newHeight, numCompReq,
outputData.data(), 0
);
} else {
std::cerr << GetModeName() << "Formato não suportado " << filepath << std::endl;
}
if (writeResult == 0)
{
std::cerr << GetModeName() << "Erro ao escrever " << filepath << std::endl;
}
} else {
std::cerr << GetModeName() << "Erro ao redimensionar " << filepath << std::endl;
}
stbi_image_free(inputData);
} else {
std::cerr << GetModeName() << "Erro ao carregar " << filepath << std::endl;
}
}
void ResizeMode::RunImpl()
{
std::cout << GetModeName() << "Modo : Redimensionar" << std::endl;
std::cout << GetModeName() << "Pasta : " << GetFolder() << std::endl;
std::cout << GetModeName() << "Filtro : " << GetFilter() << std::endl;
std::cout << GetModeName() << "Comprimento : " << m_Width << std::endl;
std::cout << GetModeName() << "Altura : " << m_Height << std::endl;
if(m_Width > 0 && m_Height > 0)
{
for(const std::filesystem::path& filepath : GetFiles())
{
std::cout << GetModeName() << "Redimensionando " << filepath << std::endl;
ResizeImage(filepath, m_Width, m_Height);
}
}
}