From 61136405cd2d21e494c33967217330edd9831186 Mon Sep 17 00:00:00 2001
From: Yoel <s73017@beuth-hochschule.de>
Date: Fri, 12 Feb 2021 15:16:40 +0100
Subject: [PATCH] New readImage-method

---
 RayTracer/sampling/Image.cpp | 21 +++++++++++++++++++++
 RayTracer/sampling/Image.h   |  1 +
 2 files changed, 22 insertions(+)

diff --git a/RayTracer/sampling/Image.cpp b/RayTracer/sampling/Image.cpp
index 7f839df..bb2ff0b 100644
--- a/RayTracer/sampling/Image.cpp
+++ b/RayTracer/sampling/Image.cpp
@@ -5,6 +5,8 @@
 
 #include "../tools/Threadpool.h"
 #include "StratifiedSampler.h"
+#define STB_IMAGE_IMPLEMENTATION
+#include "../tools/stb_image.h"
 
 namespace util {
 Image::Image(int width, int height) : width(width), height(height) {
@@ -150,4 +152,23 @@ void writeBmp(const char* filename, Image img) {
 
 	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
\ No newline at end of file
diff --git a/RayTracer/sampling/Image.h b/RayTracer/sampling/Image.h
index 902dca6..be2138a 100644
--- a/RayTracer/sampling/Image.h
+++ b/RayTracer/sampling/Image.h
@@ -30,5 +30,6 @@ class Image {
 Image raytrace(size_t threadcount, const cam::CamObs& cam,
                const std::shared_ptr<Sampler>& sampler, size_t n);
 void writeBmp(const char* filename, Image img);
+Image readImage(const char* filename);
 
 }  // namespace util
\ No newline at end of file
-- 
GitLab