From f5fcf0b0c008cd4438d10910da8f4a46a4c869bc Mon Sep 17 00:00:00 2001
From: Yoel <s73017@beuth-hochschule.de>
Date: Fri, 16 Oct 2020 16:06:04 +0200
Subject: [PATCH] Added new function to gammaCorrect the whole image vector

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

diff --git a/RayTracer/sampling/Image.cpp b/RayTracer/sampling/Image.cpp
index 8655512..b82813c 100644
--- a/RayTracer/sampling/Image.cpp
+++ b/RayTracer/sampling/Image.cpp
@@ -35,7 +35,18 @@ void Image::setPixels(size_t threadcount, std::shared_ptr<Sampler> sampler) {
 }
 
 void Image::setPixelsTask(int x, int y, std::shared_ptr<Sampler> sampler) {
-	setPixel(x, y, sampler->color(x, y));
+	Vec3 v = sampler->color(x, y);
+	setPixel(x, y, v);
+}
+
+void Image::gammaCorrect(float gamma) {
+	// correct the whole data-array with the given gamma
+	std::transform(vec.begin(), vec.end(), vec.begin(),
+	               [gamma](util::Vec3 v) -> util::Vec3 {
+		               return util::Vec3(std::powf(v.x(), 1 / gamma),
+		                                 std::powf(v.y(), 1 / gamma),
+		                                 std::powf(v.z(), 1 / gamma));
+	               });
 }
 Vec3 Image::operator[](const std::array<int, 2>& i) const {
 	return vec[width * i[1] + i[0]];
@@ -51,6 +62,7 @@ Image raytrace(size_t threadcount, const cam::CamObs& cam,
 	                                  StratifiedSampler(sampler, n))
 	                 // sampler
 	);
+	result.gammaCorrect(2.2);
 	return result;
 }
 }  // namespace util
\ No newline at end of file
diff --git a/RayTracer/sampling/Image.h b/RayTracer/sampling/Image.h
index c23b1f1..10f95f8 100644
--- a/RayTracer/sampling/Image.h
+++ b/RayTracer/sampling/Image.h
@@ -18,6 +18,7 @@ class Image {
 
 	const int width;
 	const int height;
+	void gammaCorrect(float gamma);
 
    protected:
 	void setPixelsTask(int x, int y, std::shared_ptr<Sampler> sampler);
-- 
GitLab