hdr_image.cpp 4.34 KB
Newer Older
TheNumbat's avatar
TheNumbat committed
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

#include "hdr_image.h"
#include "../lib/log.h"

#include <sf_libs/stb_image.h>
#include <sf_libs/tinyexr.h>

HDR_Image::HDR_Image() : w(0), h(0) {}

HDR_Image::HDR_Image(size_t w, size_t h) : w(w), h(h) {
    assert(w > 0 && h > 0);
    pixels.resize(w * h);
}

HDR_Image HDR_Image::copy() const {
    HDR_Image ret;
    ret.resize(w, h);
    ret.pixels.insert(ret.pixels.begin(), pixels.begin(), pixels.end());
    ret.last_path = last_path;
    ret.dirty = true;
    ret.exposure = exposure;
    return ret;
}

std::pair<size_t,size_t> HDR_Image::dimension() const {
    return {w,h};
}

void HDR_Image::resize(size_t _w, size_t _h) {
    w = _w;
    h = _h;
    pixels.clear();
    pixels.resize(w * h);
    dirty = true;
}

void HDR_Image::clear(Spectrum color) {
    for(auto& s : pixels) s = color;
    dirty = true;
}

Spectrum& HDR_Image::at(size_t i) {
    assert(i < w * h);
    dirty = true;
    return pixels[i];
}

Spectrum HDR_Image::at(size_t i) const {
    assert(i < w * h);
    return pixels[i];
}

Spectrum& HDR_Image::at(size_t x, size_t y) {
    assert(x < w && y < h);
    size_t idx = y * w + x;
    dirty = true;
    return pixels[idx];
}

Spectrum HDR_Image::at(size_t x, size_t y) const {
    assert(x < w && y < h);
    size_t idx = y * w + x;
    return pixels[idx];
}

std::string HDR_Image::load_from(std::string file) {

    if(IsEXR(file.c_str()) == TINYEXR_SUCCESS) {

        int n_w, n_h;
        float* data;
        const char* err = nullptr;

        int ret = LoadEXR(&data, &n_w, &n_h, file.c_str(), &err);

        if (ret != TINYEXR_SUCCESS) {

            if (err) {
                std::string err_s(err);
                FreeEXRErrorMessage(err);
                return err_s;
            } else return "Unknown failure.";

        } else {

            resize(n_w, n_h);

            for(size_t j = 0; j < h; j++) {
                for(size_t i = 0; i < w; i++) {
                    size_t didx = 4 * (j * w + i);
                    size_t pidx = (h - j - 1) * w + i;
                    pixels[pidx] = Spectrum(data[didx],data[didx+1],data[didx+2]);
                }
            }

            free(data);
        }

    } else {

        stbi_set_flip_vertically_on_load(true);
        
        int n_w, n_h, channels;
        unsigned char *data = stbi_load(file.c_str(), &n_w, &n_h, &channels, 0);

        if(!data) return std::string(stbi_failure_reason());
        if(channels < 3) return "Image has less than 3 color channels.";

        resize(n_w, n_h);

        for(size_t i = 0; i < w * h * channels; i += channels) {
            float r = data[i] / 255.0f;
            float g = data[i+1] / 255.0f;
            float b = data[i+2] / 255.0f;
            pixels[i / channels] = Spectrum(r,g,b);
        }

        stbi_image_free(data);
    }

    for(size_t i = 0; i < pixels.size(); i++) {
        pixels[i].make_linear();
        if(!pixels[i].valid()) pixels[i] = {};
    }

    last_path = file;
    dirty = true;
    return {};
}

std::string HDR_Image::loaded_from() const {
    return last_path;
}

void HDR_Image::tonemap(float e) const {

    if(e <= 0.0f) {
        e = exposure;
    } else if(e != exposure) {
        exposure = e;
        dirty = true;
    }

    if(!dirty) return;

    std::vector<unsigned char> data;
    tonemap_to(data, e);
    render_tex.image((int)w, (int)h, data.data());

    dirty = false;
}

const GL::Tex2D& HDR_Image::get_texture(float e) const {
    tonemap(e);
    return render_tex;
}

void HDR_Image::tonemap_to(std::vector<unsigned char>& data, float e) const {
    
    if(e <= 0.0f) {
        e = exposure;
    }

    if(data.size() != w * h * 4)
        data.resize(w * h * 4);

    for(size_t j = 0; j < h; j++) {
        for(size_t i = 0; i < w; i++) {
            
            size_t pidx = (h - j - 1) * w + i;
            const Spectrum& sample = pixels[pidx];
  
            float r = 1.0f - std::exp(-sample.r * exposure);
            float g = 1.0f - std::exp(-sample.g * exposure);
            float b = 1.0f - std::exp(-sample.b * exposure);
            
            Spectrum out(r,g,b);
            out.make_srgb();
  
            size_t didx = 4 * (j * w + i);
            data[didx] = (unsigned char)std::round(out.r * 255.0f);
            data[didx+1] = (unsigned char)std::round(out.g * 255.0f);
            data[didx+2] = (unsigned char)std::round(out.b * 255.0f);
            data[didx+3] = 255;
        }
    }
}