Skip to content

Commit

Permalink
move float to bytes conversion in utils
Browse files Browse the repository at this point in the history
  • Loading branch information
PoneyUHC committed Aug 29, 2024
1 parent c68d964 commit e2f94a1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
16 changes: 0 additions & 16 deletions src/export/IExporter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,5 @@ class IExporter {

return 0;
}


static void rgb_normalized_to_8bits(const RGBColor& color, uint8_t *dest){
double r = color[0];
double g = color[1];
double b = color[2];

r = linear_to_gamma(r);
g = linear_to_gamma(g);
b = linear_to_gamma(b);

static const Interval intensity(0.000, 0.999);
dest[0] = uint8_t(256 * intensity.Clamp(r));
dest[1] = uint8_t(256 * intensity.Clamp(g));
dest[2] = uint8_t(256 * intensity.Clamp(b));
}

};
9 changes: 6 additions & 3 deletions src/export/png_exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,13 @@ void PngExporter::ImageToScanline(uint8_t filtering, uint8_t *dest) const
{
uint8_t *data_ptr = dest;
for(uint32_t j=0; j<m_height; ++j){
*data_ptr = filtering;
++data_ptr;
*data_ptr++ = filtering;
for(uint32_t i=0; i<m_width; ++i){
IExporter::rgb_normalized_to_8bits(m_data_buffer[j*m_width + i], data_ptr);
RGBColor& color = m_data_buffer[j*m_width + i];
color[0] = linear_to_gamma(color[0]);
color[1] = linear_to_gamma(color[1]);
color[2] = linear_to_gamma(color[2]);
rgb_normalized_to_8bits(color, data_ptr);
data_ptr += 3;
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/export/ppm_exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "export/ppm_exporter.hpp"
#include "renderer/camera.hpp"
#include "math/interval.hpp"
#include "utils.hpp"

#include <filesystem>
#include <iostream>
Expand Down Expand Up @@ -32,8 +33,12 @@ int PpmExporter::Export(int width, int height, std::shared_ptr<RGBColor[]> buffe
for(int i=0; i<width; ++i){

RGBColor& color = buffer[j* width + i];
color[0] = linear_to_gamma(color[0]);
color[1] = linear_to_gamma(color[1]);
color[2] = linear_to_gamma(color[2]);

uint8_t rgb[3];
IExporter::rgb_normalized_to_8bits(color, rgb);
rgb_normalized_to_8bits(color, rgb);

file << rgb[0] << ' ' << rgb[1] << ' ' << rgb[2] << '\n';
}
Expand Down
24 changes: 24 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@

#include "utils.hpp"

#include "math/interval.hpp"

#include <iostream>
#include <algorithm>


using namespace std;


float MAGENTA[3] = { 1.0f, 0.0f, 1.0f };


string get_env_var(const string& key)
{
char* val = getenv(key.c_str());
if(val == NULL){
clog << "Could not retrieve environment variable with key '" << key << "'" << endl;
}
return val == NULL ? string("") : string(val);
}

Expand All @@ -20,3 +31,16 @@ void change_endianess(uint32_t n, uint8_t *dest)
dest[sizeof(uint32_t) - i - 1] = data_ptr[i];
}
}


void rgb_normalized_to_8bits(const RGBColor& color, uint8_t *dest){
static const Interval intensity(0.000, 0.999);
dest[0] = uint8_t(256 * intensity.Clamp(color[0]));
dest[1] = uint8_t(256 * intensity.Clamp(color[1]));
dest[2] = uint8_t(256 * intensity.Clamp(color[2]));
}


uint8_t rgb_normalized_to_8bits(float value){
return static_cast<uint8_t>(255.999 * std::clamp(value, 0.f, 1.f));
}
11 changes: 9 additions & 2 deletions src/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@

#pragma once

#include <string>
#include "math/vec.hpp"

#include <string>
#include <cstdint>


std::string get_env_var(const std::string& key);

void change_endianess(uint32_t n, uint8_t *dest);
void change_endianess(uint32_t n, uint8_t *dest);

void rgb_normalized_to_8bits(const RGBColor& color, uint8_t *dest);

uint8_t rgb_normalized_to_8bits(float value);

extern float MAGENTA[3];

0 comments on commit e2f94a1

Please sign in to comment.