summary refs log tree commit diff
path: root/src/GFX/Image/RawImage.cpp
blob: 1222fab5b92862a89241e7a953e3b819cd402fac (plain)
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
#include "RawImage.hpp"

namespace MC::GFX::Image {

RawImage::RawImage(Util::Sampler<2, float>& sampler, uint32_t width, uint32_t height)
    : m_pixels(), m_width(width), m_height(height)
{
    m_pixels.reserve(width * height);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            auto result = sampler.sample({(float)x, (float)y});
            auto intensity = (uint8_t)(result * 255);
            add({intensity, intensity, intensity});
        }
    }
}

void RawImage::add(RawImage::Pixel pixel) {
    m_pixels.push_back(pixel);
}

size_t RawImage::size() const {
    return m_pixels.size();
}

uint8_t* RawImage::raw() const {
    return (uint8_t*)m_pixels.data();
}

uint32_t RawImage::width() const {
    return m_width;
}

uint32_t RawImage::height() const {
    return m_height;
}

uint8_t RawImage::channels() const {
    return m_channels;
}

std::string RawImage::string() const {
    std::stringstream str{};

    bool comma = false;
    str << "[";
    for (const auto& pixel : m_pixels) {
        if (comma) { str << ", "; }
        str << "{r=" << (uint)pixel.r << ", g=" << (uint)pixel.g << ", b=" << (uint)pixel.r << "}";
        comma = true;
    }
    str << "]";

    return str.str();
}

}