Skip to content

Commit

Permalink
Uniformly refer to the individual parts of a pixel as 'components', r…
Browse files Browse the repository at this point in the history
…ather than 'channels'
  • Loading branch information
adamkewley committed Aug 28, 2024
1 parent a44c545 commit 8cc16c8
Show file tree
Hide file tree
Showing 20 changed files with 225 additions and 225 deletions.
2 changes: 1 addition & 1 deletion src/oscar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ add_library(oscar STATIC
Graphics/Snorm8.h
Graphics/SourceBlendingFactor.h
Graphics/SubMeshDescriptor.h
Graphics/TextureChannelFormat.h
Graphics/TextureComponentFormat.h
Graphics/TextureDimensionality.h
Graphics/TextureFilterMode.h
Graphics/TextureFormat.h
Expand Down
24 changes: 12 additions & 12 deletions src/oscar/Formats/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ namespace
}

Vec2i dimensions{};
int num_channels = 0;
int num_components = 0;
const std::unique_ptr<float, decltype(&stbi_image_free)> pixel_data = {
stbi_loadf_from_callbacks(&c_stbi_stream_callbacks, &in, &dimensions.x, &dimensions.y, &num_channels, 0),
stbi_loadf_from_callbacks(&c_stbi_stream_callbacks, &in, &dimensions.x, &dimensions.y, &num_components, 0),
stbi_image_free,
};

Expand All @@ -98,19 +98,19 @@ namespace
}

const std::optional<TextureFormat> texture_format = to_texture_format(
static_cast<size_t>(num_channels),
TextureChannelFormat::Float32
static_cast<size_t>(num_components),
TextureComponentFormat::Float32
);

if (not texture_format) {
std::stringstream ss;
ss << input_name << ": error loading HDR image: no TextureFormat exists for " << num_channels << " floating-point channel images";
ss << input_name << ": error loading HDR image: no TextureFormat exists for " << num_components << "-floating-point component images";
throw std::runtime_error{std::move(ss).str()};
}

const std::span<const float> pixel_span{
pixel_data.get(),
static_cast<size_t>(dimensions.x*dimensions.y*num_channels)
static_cast<size_t>(dimensions.x*dimensions.y*num_components)
};

Texture2D rv{dimensions, *texture_format, color_space};
Expand All @@ -131,9 +131,9 @@ namespace
}

Vec2i dimensions{};
int num_channels = 0;
int num_components = 0;
const std::unique_ptr<stbi_uc, decltype(&stbi_image_free)> pixel_data = {
stbi_load_from_callbacks(&c_stbi_stream_callbacks, &in, &dimensions.x, &dimensions.y, &num_channels, 0),
stbi_load_from_callbacks(&c_stbi_stream_callbacks, &in, &dimensions.x, &dimensions.y, &num_components, 0),
stbi_image_free,
};

Expand All @@ -148,18 +148,18 @@ namespace
}

const std::optional<TextureFormat> texture_format = to_texture_format(
static_cast<size_t>(num_channels),
TextureChannelFormat::Uint8
static_cast<size_t>(num_components),
TextureComponentFormat::Uint8
);

if (not texture_format) {
std::stringstream ss;
ss << input_name << ": error loading non-HDR image: no TextureFormat exists for " << num_channels << " 8-bit channel images";
ss << input_name << ": error loading non-HDR image: no TextureFormat exists for " << num_components << "-8-bit component images";
throw std::runtime_error{std::move(ss).str()};
}

Texture2D rv{dimensions, *texture_format, color_space};
rv.set_pixel_data({pixel_data.get(), static_cast<size_t>(dimensions.x*dimensions.y*num_channels)});
rv.set_pixel_data({pixel_data.get(), static_cast<size_t>(dimensions.x*dimensions.y*num_components)});
return rv;
}

Expand Down
2 changes: 1 addition & 1 deletion src/oscar/Formats/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace osc
// loads the given (named) image stream into a `Texture2D`
//
// throws if the image data isn't representable as a GPU texture (e.g. because it has
// an incorrect number of color channels)
// an incorrect number of components)
Texture2D load_texture2D_from_image(
std::istream&,
std::string_view input_name,
Expand Down
2 changes: 1 addition & 1 deletion src/oscar/Graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#include <oscar/Graphics/Snorm8.h>
#include <oscar/Graphics/SourceBlendingFactor.h>
#include <oscar/Graphics/SubMeshDescriptor.h>
#include <oscar/Graphics/TextureChannelFormat.h>
#include <oscar/Graphics/TextureComponentFormat.h>
#include <oscar/Graphics/TextureDimensionality.h>
#include <oscar/Graphics/TextureFilterMode.h>
#include <oscar/Graphics/TextureFormat.h>
Expand Down
20 changes: 10 additions & 10 deletions src/oscar/Graphics/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,23 @@ std::ostream& osc::operator<<(std::ostream& out, const Color& color)
// - https://registry.khronos.org/OpenGL/extensions/ARB/ARB_framebuffer_sRGB.txt
//
// (because I am a lazy bastard)
float osc::to_linear_colorspace(float color_channel_value)
float osc::to_linear_colorspace(float srgb_component_value)
{
if (color_channel_value <= 0.04045f) {
return color_channel_value / 12.92f;
if (srgb_component_value <= 0.04045f) {
return srgb_component_value / 12.92f;
}
else {
return pow((color_channel_value + 0.055f) / 1.055f, 2.4f);
return pow((srgb_component_value + 0.055f) / 1.055f, 2.4f);
}
}

float osc::to_srgb_colorspace(float color_channel_value)
float osc::to_srgb_colorspace(float linear_component_value)
{
if (color_channel_value <= 0.0031308f) {
return color_channel_value * 12.92f;
if (linear_component_value <= 0.0031308f) {
return linear_component_value * 12.92f;
}
else {
return pow(color_channel_value, 1.0f/2.4f)*1.055f - 0.055f;
return pow(linear_component_value, 1.0f/2.4f)*1.055f - 0.055f;
}
}

Expand Down Expand Up @@ -239,8 +239,8 @@ std::string osc::to_html_string_rgba(const Color& color)
std::string rv;
rv.reserve(9);
rv.push_back('#');
for (auto channel : to_color32(color)) {
auto [nibble_1, nibble_2] = to_hex_chars(static_cast<uint8_t>(channel));
for (auto component : to_color32(color)) {
auto [nibble_1, nibble_2] = to_hex_chars(static_cast<uint8_t>(component));
rv.push_back(nibble_1);
rv.push_back(nibble_2);
}
Expand Down
12 changes: 6 additions & 6 deletions src/oscar/Graphics/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ namespace osc

std::ostream& operator<<(std::ostream&, const Color&);

// returns the linear version of one (presumed to be) sRGB color channel value
float to_linear_colorspace(float color_channel_value);
// returns the linearized version of a sRGB component value
float to_linear_colorspace(float srgb_component_value);

// returns the linear version of one (presumed to be) linear color channel value
float to_srgb_colorspace(float color_channel_value);
// returns the sRGB version of a linearized component value
float to_srgb_colorspace(float linear_component_value);

// returns the linear version of a (presumed to be) sRGB color
Color to_linear_colorspace(const Color&);
Expand Down Expand Up @@ -277,7 +277,7 @@ namespace osc
return &color.r;
}

// linearly interpolates all color channels and alpha of `a` and `b` by `t`
// linearly interpolates all components of `a` and `b` by the interpolant `t`
//
// `t` is clamped to [0.0f, 1.0f]. When `t` is 0, returns `a`. When `t` is 1, returns `b`
Color lerp(const Color& a, const Color& b, float t);
Expand Down Expand Up @@ -307,7 +307,7 @@ namespace osc
// multiplying its luminance (L) by `factor`, and converting it back to RGBA
Color multiply_luminance(const Color& color, float factor);

// when handled as a tuple-like object, a `Color` decomposes into its channels (incl. alpha)
// when handled as a tuple-like object, a `Color` decomposes into its components (incl. alpha)

template<size_t I>
constexpr const float& get(const Color& color) { return color[I]; }
Expand Down
4 changes: 2 additions & 2 deletions src/oscar/Graphics/ColorSpace.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ namespace osc
// into a shader.
enum class ColorSpace {

// Indicates that the color channels are encoded in a linear color space. This
// Indicates that the color components are encoded in a linear color space. This
// encoding typical for (e.g.) normal maps and other vector-encoded image formats.
Linear,

// Indicates that the color channels are encoded in a sRGB color space. This
// Indicates that the color components are encoded in a sRGB color space. This
// implies that (e.g.) shaders need to perform sRGB <-> linear conversion when
// sampling from, or rendering to, the texture. This encoding is typical for
// (e.g.) albedo textures.
Expand Down
4 changes: 2 additions & 2 deletions src/oscar/Graphics/Cubemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ namespace osc
TextureFilterMode filter_mode() const;
void set_filter_mode(TextureFilterMode);

// the provided bytes must match the channel layout, bytes per channel, and
// width*height of the cubemap, or an exception will be thrown
// The number of provided bytes must match the provided `width*width` and
// `TextureFormat` of this `Cubemap`, or an exception will be thrown.
void set_pixel_data(CubemapFace, std::span<const uint8_t>);

friend bool operator==(const Cubemap&, const Cubemap&) = default;
Expand Down
30 changes: 15 additions & 15 deletions src/oscar/Graphics/Detail/TextureFormatTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <oscar/Graphics/Detail/CPUDataType.h>
#include <oscar/Graphics/Detail/CPUImageFormat.h>
#include <oscar/Graphics/TextureChannelFormat.h>
#include <oscar/Graphics/TextureComponentFormat.h>
#include <oscar/Graphics/TextureFormat.h>

#include <cstddef>
Expand All @@ -16,55 +16,55 @@ namespace osc::detail
struct TextureFormatTraits<TextureFormat::R8> {
static inline constexpr CPUDataType equivalent_cpu_datatype = CPUDataType::UnsignedByte;
static inline constexpr CPUImageFormat equivalent_cpu_image_format = CPUImageFormat::R8;
static inline constexpr size_t num_channels = 1;
static inline constexpr TextureChannelFormat channel_format = TextureChannelFormat::Uint8;
static inline constexpr size_t num_components = 1;
static inline constexpr TextureComponentFormat component_format = TextureComponentFormat::Uint8;
};

template<>
struct TextureFormatTraits<TextureFormat::RG16> {
static inline constexpr CPUDataType equivalent_cpu_datatype = CPUDataType::UnsignedByte;
static inline constexpr CPUImageFormat equivalent_cpu_image_format = CPUImageFormat::RG;
static inline constexpr size_t num_channels = 2;
static inline constexpr TextureChannelFormat channel_format = TextureChannelFormat::Uint8;
static inline constexpr size_t num_components = 2;
static inline constexpr TextureComponentFormat component_format = TextureComponentFormat::Uint8;
};

template<>
struct TextureFormatTraits<TextureFormat::RGB24> {
static inline constexpr CPUDataType equivalent_cpu_datatype = CPUDataType::UnsignedByte;
static inline constexpr CPUImageFormat equivalent_cpu_image_format = CPUImageFormat::RGB;
static inline constexpr size_t num_channels = 3;
static inline constexpr TextureChannelFormat channel_format = TextureChannelFormat::Uint8;
static inline constexpr size_t num_components = 3;
static inline constexpr TextureComponentFormat component_format = TextureComponentFormat::Uint8;
};

template<>
struct TextureFormatTraits<TextureFormat::RGBA32> {
static inline constexpr CPUDataType equivalent_cpu_datatype = CPUDataType::UnsignedByte;
static inline constexpr CPUImageFormat equivalent_cpu_image_format = CPUImageFormat::RGBA;
static inline constexpr size_t num_channels = 4;
static inline constexpr TextureChannelFormat channel_format = TextureChannelFormat::Uint8;
static inline constexpr size_t num_components = 4;
static inline constexpr TextureComponentFormat component_format = TextureComponentFormat::Uint8;
};

template<>
struct TextureFormatTraits<TextureFormat::RGFloat> {
static inline constexpr CPUDataType equivalent_cpu_datatype = CPUDataType::Float;
static inline constexpr CPUImageFormat equivalent_cpu_image_format = CPUImageFormat::RG;
static inline constexpr size_t num_channels = 2;
static inline constexpr TextureChannelFormat channel_format = TextureChannelFormat::Float32;
static inline constexpr size_t num_components = 2;
static inline constexpr TextureComponentFormat component_format = TextureComponentFormat::Float32;
};

template<>
struct TextureFormatTraits<TextureFormat::RGBFloat> {
static inline constexpr CPUDataType equivalent_cpu_datatype = CPUDataType::Float;
static inline constexpr CPUImageFormat equivalent_cpu_image_format = CPUImageFormat::RGB;
static inline constexpr size_t num_channels = 3;
static inline constexpr TextureChannelFormat channel_format = TextureChannelFormat::Float32;
static inline constexpr size_t num_components = 3;
static inline constexpr TextureComponentFormat component_format = TextureComponentFormat::Float32;
};

template<>
struct TextureFormatTraits<TextureFormat::RGBAFloat> {
static inline constexpr CPUDataType equivalent_cpu_datatype = CPUDataType::Float;
static inline constexpr CPUImageFormat equivalent_cpu_image_format = CPUImageFormat::RGBA;
static inline constexpr size_t num_channels = 4;
static inline constexpr TextureChannelFormat channel_format = TextureChannelFormat::Float32;
static inline constexpr size_t num_components = 4;
static inline constexpr TextureComponentFormat component_format = TextureComponentFormat::Float32;
};
}
Loading

0 comments on commit 8cc16c8

Please sign in to comment.