-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_loader.cpp
More file actions
240 lines (193 loc) · 7.37 KB
/
image_loader.cpp
File metadata and controls
240 lines (193 loc) · 7.37 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <filesystem>
#define STB_IMAGE_IMPLEMENTATION
#include "../stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "../stb/stb_image_write.h"
#include "image_loader.h"
////////////////////////////////////////////////////////////////////////////
float clamp(float u, float a, float b) { return std::min(std::max(a, u), b); }
TEX_FORMAT getFormatFromExtension(const std::string &filename)
{
auto found = filename.find_last_of('.');
std::string ext = filename.substr(found, filename.size());
if(ext == ".image4ub")
return IMG_IMAGE4UB;
else if(ext == ".image4f")
return IMG_IMAGE4F;
else if(ext == ".png")
return IMG_PNG;
else
{
std::cerr << "Error: Format " << ext << " not support" << std::endl;
return IMG_OTHER;
}
}
ImageFileInfo getImageInfo(const std::string& a_filename)
{
ImageFileInfo res = {};
res.path = a_filename;
res.is_ok = false;
res.channels = 4;
const size_t fileSize = std::filesystem::file_size(a_filename);
std::ifstream file(a_filename, std::ios::binary);
if(!file.good())
{
file.close();
return res;
}
auto tex_format = getFormatFromExtension(a_filename);
if (tex_format == IMG_IMAGE4UB)
{
file.read((char*)&res.width, sizeof(uint32_t));
file.read((char*)&res.height, sizeof(uint32_t));
res.bytesPerChannel = sizeof(unsigned char);
res.channels = 4;
if(res.width > 0 && res.height > 0)
res.is_ok = true;
}
else if (tex_format == IMG_IMAGE4F)
{
// Чтение ширины и высоты
file.read((char*)&res.width, sizeof(uint32_t));
file.read((char*)&res.height, sizeof(uint32_t));
// Расчёт ожидаемого размера для 3 и 4 каналов
size_t expectedSize3 = res.width * res.height * 3 * sizeof(float) + 8;
size_t expectedSize4 = res.width * res.height * 4 * sizeof(float) + 8;
// Определение числа каналов
if (fileSize == expectedSize3)
res.channels = 3;
else if (fileSize == expectedSize4)
res.channels = 4;
else {
std::cerr << "Некорректный размер файла: " << a_filename << std::endl;
return res;
}
res.bytesPerChannel = sizeof(float);
res.is_ok = true;
return res;
}
else if (tex_format == IMG_PNG)
{
stbi_info(a_filename.c_str(), &res.width, &res.height, &res.channels);
res.bytesPerChannel = sizeof(unsigned char);
if(res.width > 0 && res.height > 0 && res.channels > 0)
res.is_ok = true;
}
else
{
std::cerr << "Error: Format " << a_filename << " not support" << std::endl;
res.is_ok = false;
}
file.close();
return res;
}
void flipImageVertically(std::vector<unsigned char>& data, int width, int height, int channels) {
const int rowSize = width * channels;
#pragma omp parallel for
for (int y = 0; y < height/2; y++) {
unsigned char* top = data.data() + y * rowSize;
unsigned char* bottom = data.data() + (height - 1 - y) * rowSize;
std::swap_ranges(top, top + rowSize, bottom);
}
}
void flipImageVertically(std::vector<float>& data, int width, int height, int channels) {
const int rowSize = width * channels;
for (int y = 0; y < height/2; y++) {
float* top = data.data() + y * rowSize;
float* bottom = data.data() + (height - 1 - y) * rowSize;
std::swap_ranges(top, top + rowSize, bottom);
}
}
std::vector<unsigned char> loadImage4ub(const std::string &filename)
{
std::ifstream infile(filename, std::ios::binary);
std::vector<unsigned char> result;
if (infile.good())
{
uint32_t w, h;
infile.read((char*)&w, sizeof(uint32_t));
infile.read((char*)&h, sizeof(uint32_t));
if (!infile) {
std::cerr << "Ошибка чтения данных из файла!" << std::endl;
return result;
}
result.resize(w * h * 4);
infile.read((char*)result.data(), w * h * 4);
flipImageVertically(result, w, h, 4);
}
return result;
}
std::vector<unsigned char> image4fToUchar(const std::string &filename, int& outChannels) {
std::ifstream infile(filename, std::ios::binary);
std::vector<float> result_float;
std::vector<unsigned char> result;
if (infile.good())
{
uint32_t w, h;
infile.read((char*)&w, sizeof(uint32_t));
infile.read((char*)&h, sizeof(uint32_t));
if (outChannels == 4)
{
result_float.resize(w * h * 4);
infile.read((char*)result_float.data(), w * h * 4 * sizeof(float));
if (!infile) {
std::cerr << "Error reading data from a file!" << std::endl;
return result;
}
}
else if (outChannels == 3)
{
// It is not possible to save the 3-channel float4. Perhaps this is a format bug.
std::cerr << "Error: image4 should contain 4 channels, now 3" << std::endl;
outChannels = 4;
return result;
// std::vector<float> result3(w * h * 3);
// infile.read((char*)result3.data(), w * h * 3 * sizeof(float));
// result.resize(w * h * 4);
// for (size_t srcIdx = 0, dstIdx = 0; srcIdx < result3.size(); srcIdx += 3, dstIdx += 4)
// {
// // RGB → RGBA
// const float r = clamp(result3[srcIdx], 0.0f, 1.0f);
// const float g = clamp(result3[srcIdx + 1], 0.0f, 1.0f);
// const float b = clamp(result3[srcIdx + 2], 0.0f, 1.0f);
// result[dstIdx] = r;
// result[dstIdx+1] = g;
// result[dstIdx+2] = b;
// result[dstIdx+3] = 1.0f;
// }
// outChannels = 4;
}
result.resize(w * h * 4);
#pragma omp parallel for
for (size_t i = 0; i < result_float.size(); i ++)
result[i] = static_cast<unsigned char>(clamp(result_float[i] * 255, 0.0, 255.0f));
flipImageVertically(result, w, h, 4);
}
return result;
}
std::vector<unsigned char> loadImageLDR(const ImageFileInfo& info)
{
int w, h, channels, req_channels;
if(info.channels == 3)
req_channels = 4;
else
req_channels = info.channels;
unsigned char *pixels = stbi_load(info.path.c_str(), &w, &h, &channels, req_channels);
if (!pixels) {
std::cerr << "Error: Failed to load image data for " << info.path << std::endl;
return std::vector<unsigned char>(); // Return empty vector
}
std::vector<unsigned char> result(w * h * req_channels);
memcpy(result.data(), pixels, result.size());
stbi_image_free(pixels);
return result;
}
bool saveImageLDR(const std::string& a_filename, const std::vector<unsigned char> &a_data,
int width, int height, int channels)
{
return stbi_write_png(a_filename.c_str(), width, height, channels, a_data.data(), width * channels);
}