-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.cpp
More file actions
234 lines (191 loc) · 6.83 KB
/
texture.cpp
File metadata and controls
234 lines (191 loc) · 6.83 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
#include "texture.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
#define TINYEXR_IMPLEMENTATION
#include "tinyexr/tinyexr.h"
float gammaTransform(float val) {
// Uncomment below to disable gamma transform during debugging
// return val;
float transformed = std::pow(val, 1 / 2.2);
float clamped = std::min(transformed, 1.0f);
clamped = std::max(clamped, 0.0f);
return clamped;
}
Texture::Texture(std::string pathToImage)
{
size_t pos = pathToImage.find(".exr");
if (pos > pathToImage.length()) {
this->type = TextureType::UNSIGNED_INTEGER_ALPHA;
pos = pathToImage.find(".png");
if (pos > pathToImage.length())
this->loadJpg(pathToImage);
else
this->loadPng(pathToImage);
}
else {
this->type = TextureType::FLOAT_ALPHA;
this->loadExr(pathToImage);
}
}
void Texture::allocate(TextureType type, Vector2i resolution)
{
this->resolution = resolution;
this->type = type;
if (this->type == TextureType::UNSIGNED_INTEGER_ALPHA) {
uint32_t* dpointer = (uint32_t*) malloc(this->resolution.x * this->resolution.y * sizeof(uint32_t));
this->data = (uint64_t)dpointer;
}
else if (this->type == TextureType::FLOAT_ALPHA) {
float* dpointer = (float*)malloc(this->resolution.x * this->resolution.y * 4 * sizeof(float));
this->data = (uint64_t)dpointer;
}
}
Vector3f Texture::nearestNeighbourFetch(Vector2f uv)
{
Vector3f rval(0.f, 0.f, 0.f);
int xIndex = (int) std::floor(uv.x * (this->resolution.x - 1));
int yIndex = (int) std::floor(uv.y * (this->resolution.y - 1));
if (this->type == TextureType::UNSIGNED_INTEGER_ALPHA) {
uint32_t* dpointer = (uint32_t*)this->data;
uint32_t val = dpointer[yIndex * this->resolution.x + xIndex];
uint32_t r = (val >> 0) & 255u;
uint32_t g = (val >> 8) & 255u;
uint32_t b = (val >> 16) & 255u;
rval.x = r / 255.f;
rval.y = g / 255.f;
rval.z = b / 255.f;
}
return rval;
}
void Texture::writePixelColor(Vector3f color, int x, int y)
{
if (this->type == TextureType::UNSIGNED_INTEGER_ALPHA) {
uint32_t* dpointer = (uint32_t*)this->data;
uint32_t r = static_cast<uint32_t>(gammaTransform(color.x) * 255.0f);
uint32_t g = static_cast<uint32_t>(gammaTransform(color.y) * 255.0f) << 8;
uint32_t b = static_cast<uint32_t>(gammaTransform(color.z) * 255.0f) << 16;
uint32_t a = 255 << 24;
uint32_t final = r | g | b | a;
dpointer[y * this->resolution.x + x] = final;
} else {
float *dpointer = (float *)this->data;
dpointer[y * resolution.x * 4 + x * 4 + 0] = color.x;
dpointer[y * resolution.x * 4 + x * 4 + 1] = color.y;
dpointer[y * resolution.x * 4 + x * 4 + 2] = color.z;
dpointer[y * resolution.x * 4 + x * 4 + 3] = 1.f;
}
}
void Texture::loadJpg(std::string pathToJpg)
{
Vector2i res;
int comp;
unsigned char* image = stbi_load(pathToJpg.c_str(), &res.x, &res.y, &comp, STBI_rgb_alpha);
int textureID = -1;
if (image) {
this->resolution = res;
this->data = (uint64_t)image;
/* iw - actually, it seems that stbi loads the pictures
mirrored along the y axis - mirror them here */
for (int y = 0; y < res.y / 2; y++) {
uint32_t* line_y = (uint32_t*)this->data + y * res.x;
uint32_t* mirrored_y = (uint32_t*)this->data + (res.y - 1 - y) * res.x;
int mirror_y = res.y - 1 - y;
for (int x = 0; x < res.x; x++) {
std::swap(line_y[x], mirrored_y[x]);
}
}
}
else {
std::cerr << "Could not load .jpg texture from " << pathToJpg << std::endl;
std::cerr << stbi_failure_reason() << std::endl;
exit(1);
}
}
void Texture::loadPng(std::string pathToPng)
{
Vector2i res;
int comp;
unsigned char* image = stbi_load(pathToPng.c_str(), &res.x, &res.y, &comp, STBI_rgb_alpha);
int textureID = -1;
if (image) {
this->resolution = res;
this->data = (uint64_t)image;
/* iw - actually, it seems that stbi loads the pictures
mirrored along the y axis - mirror them here */
for (int y = 0; y < res.y / 2; y++) {
uint32_t* line_y = (uint32_t*)this->data + y * res.x;
uint32_t* mirrored_y = (uint32_t*)this->data + (res.y - 1 - y) * res.x;
int mirror_y = res.y - 1 - y;
for (int x = 0; x < res.x; x++) {
std::swap(line_y[x], mirrored_y[x]);
}
}
}
else {
std::cerr << "Could not load .png texture from " << pathToPng << std::endl;
std::cerr << stbi_failure_reason() << std::endl;
exit(1);
}
}
void Texture::loadExr(std::string pathToExr)
{
int width;
int height;
const char* err = nullptr; // or nullptr in C++11
float* data;
int ret = LoadEXR(&data, &width, &height, pathToExr.c_str(), &err);
this->data = (uint64_t)data;
if (ret != TINYEXR_SUCCESS) {
std::cerr << "Could not load .exr texture map from " << pathToExr << std::endl;
exit(1);
}
else {
this->resolution = Vector2i(width, height);
}
}
void Texture::save(std::string path)
{
size_t pos = path.find(".png");
if (pos > path.length()) {
this->saveExr(path);
}
else {
this->savePng(path);
}
}
void Texture::saveExr(std::string path)
{
if (this->type == TextureType::FLOAT_ALPHA) {
uint64_t hostData = this->data;
const char* err = nullptr;
SaveEXR((float*)hostData, this->resolution.x, this->resolution.y, 4, 0, path.c_str(), &err);
if (err == nullptr)
std::cout << "Saved EXR: " << path << std::endl;
else
std::cerr << "Could not save EXR: " << err << std::endl;
}
else {
std::cerr << "Cannot save to EXR: texture is not of type float." << std::endl;
}
}
void Texture::savePng(std::string path)
{
if (this->type == TextureType::UNSIGNED_INTEGER_ALPHA) {
uint64_t hostData = this->data;
const uint32_t* data = (const uint32_t*)hostData;
std::vector<uint32_t> pixels;
for (int y = 0; y < this->resolution.y; y++) {
const uint32_t* line = data + (this->resolution.y - 1 - y) * this->resolution.x;
for (int x = 0; x < this->resolution.x; x++) {
pixels.push_back(line[x] | (0xff << 24));
}
}
stbi_write_png(path.c_str(), this->resolution.x, this->resolution.y, 4, data, this->resolution.x * sizeof(uint32_t));
std::cout << "Saved PNG: " << path << std::endl;
}
else {
std::cerr << "Cannot save to PNG: texture is not of type uint32." << std::endl;
}
}