-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimage.cpp
More file actions
76 lines (63 loc) · 2.33 KB
/
image.cpp
File metadata and controls
76 lines (63 loc) · 2.33 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
#include "image.h"
#include "png/lodepng.h"
void encodeOneStep(const char* filename, std::vector<unsigned char>& image, unsigned width, unsigned height) {
//Encode the image
unsigned error = lodepng::encode(filename, image, width, height);
//if there's an error, display it
if (error) std::cerr << "encoder error " << error << ": " << lodepng_error_text(error) << std::endl;
}
Image::Image(const unsigned int h, const unsigned int l) {
hauteur = h;
largeur = l;
pixels = new glm::vec3[largeur * hauteur];
}
Image::~Image() {
delete pixels;
}
void Image::Save(std::string name) {
std::cout << "Saving:" << name << "..." << std::endl;
unsigned width = largeur, height = hauteur;
std::vector<unsigned char> image;
image.resize(width * height * 4);
#pragma omp parallel for collapse(2)
for (unsigned y = 0; y < height; y++)
for (unsigned x = 0; x < width; x++) {
glm::vec3 tmp = pixels[x + largeur * y];
tmp.x = glm::max(glm::min(1.0f, tmp.x), 0.0f);
tmp.y = glm::max(glm::min(1.0f, tmp.y), 0.0f);
tmp.z = glm::max(glm::min(1.0f, tmp.z), 0.0f);
image[4 * width * y + 4 * x + 0] = (unsigned char) (round(tmp.x * 255.0));
image[4 * width * y + 4 * x + 1] = (unsigned char) (round(tmp.y * 255.0));
image[4 * width * y + 4 * x + 2] = (unsigned char) (round(tmp.z * 255.0));
image[4 * width * y + 4 * x + 3] = 255;
}
encodeOneStep(name.c_str(), image, width, height);
std::cout << "Done!" << std::endl;
}
Image::Image(std::string name) {
std::vector<unsigned char> image; //the raw pixels
unsigned width, height;
std::cout << "Loading:" << name.c_str() << std::endl;
unsigned error = lodepng::decode(image, width, height, name);
//if there's an error, display it
if (error) std::cout << "encoder error " << error << ": " << lodepng_error_text(error) << std::endl;
hauteur = height;
largeur = width;
pixels = new glm::vec3[largeur * hauteur];
#pragma omp parallel for collapse(2)
for (unsigned y = 0; y < height; y++)
for (unsigned x = 0; x < width; x++) {
pixels[x + largeur * y] = glm::vec3(image[4 * width * y + 4 * x + 0] / 255.0,
image[4 * width * y + 4 * x + 1] / 255.0,
image[4 * width * y + 4 * x + 2] / 255.0);
}
}
int Image::getHauteur() {
return hauteur;
}
int Image::getLargeur() {
return largeur;
}
void Image::setPixel(int x, int y, glm::vec3 c) {
pixels[x + largeur * y] = c;
}