Skip to content
Snippets Groups Projects
Commit 61136405 authored by Yoel's avatar Yoel
Browse files

New readImage-method

parent 8e693c75
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include "../tools/Threadpool.h" #include "../tools/Threadpool.h"
#include "StratifiedSampler.h" #include "StratifiedSampler.h"
#define STB_IMAGE_IMPLEMENTATION
#include "../tools/stb_image.h"
namespace util { namespace util {
Image::Image(int width, int height) : width(width), height(height) { Image::Image(int width, int height) : width(width), height(height) {
...@@ -150,4 +152,23 @@ void writeBmp(const char* filename, Image img) { ...@@ -150,4 +152,23 @@ void writeBmp(const char* filename, Image img) {
ofile.close(); ofile.close();
} }
Image readImage(const char* filename) {
int width, height, channels;
unsigned char* img = stbi_load(filename, &width, &height, &channels, 0);
Image result(width, height);
if (channels != 3)
std::cout << "Careful! Loaded image has " << channels << " channels"
<< std::endl;
size_t img_size = width * height * channels;
int i = 0;
for (unsigned char* p = img; p != img + img_size; p += channels) {
float x = (float)*(p + 0) / 255;
float y = (float)*(p + 1) / 255;
float z = (float)*(p + 2) / 255;
result[{i, 0}] = Vec3(x, y, z);
i++;
}
return result;
}
} // namespace util } // namespace util
\ No newline at end of file
...@@ -30,5 +30,6 @@ class Image { ...@@ -30,5 +30,6 @@ class Image {
Image raytrace(size_t threadcount, const cam::CamObs& cam, Image raytrace(size_t threadcount, const cam::CamObs& cam,
const std::shared_ptr<Sampler>& sampler, size_t n); const std::shared_ptr<Sampler>& sampler, size_t n);
void writeBmp(const char* filename, Image img); void writeBmp(const char* filename, Image img);
Image readImage(const char* filename);
} // namespace util } // namespace util
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment