[−][src]Module cvr::rgb
rgb
houses functions for working primarily in the 8-bit sRGB
color space but also
supports various other operations like color space conversions.
It's worth noting for those who are unfamiliar with the sRGB
color space, it's one of the
most widely used and popular color spaces.
If, for example, a user reads in a .png
image file, it should be assumed that its color
values are encoded as sRGB
and as such, the image doesn't natively support linear math.
This is because the sRGB
space is encoded using a transfer function which gives it
non-linear properties so even simple operations like r_1 + r_2
can have undesirable
results.
Functions like srgb_to_linear
aim to solve these kinds of issues while functions like
linear_to_srgb
enable users to convert from something they can perform linear operations
on to something that they can make suitable for displaying and storing.
Read more on sRGB
and its usages here.
How to Convert sRGB
to Linear
use cvr::rgb::iter::SRGBLinearIterator; // `cvr` emphasizes supporting channel-major ordering of image data // this is done for better interop with GPU-based code // let r = [1u8, 2, 3]; let g = [4u8, 5, 6]; let b = [7u8, 8, 9]; cvr::rgb::Iter::new(&r, &g, &b) .srgb_to_linear() .enumerate() .for_each(|(idx, [r, g, b])| { // can now use the (r, g, b) values for pixel `idx` }); // but `cvr` also aims to help support packed pixel formats wherever it can! // let pixels = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; pixels .iter() .copied() .srgb_to_linear() .enumerate() .for_each(|(idx, [r, g, b])| { // can now use the (r, g, b) values for pixel `idx` });
While most users would expect to be operating off the 8-bit values directly, working in floating point has several attractive features. Namely, it enables your image processing to retain accuracy and it keeps values consistent across different bit depths. For example, while 0.5 always represents something half as bright as 1.0, 128 will not always be the midpoint depending on the bit-depth of the image (8-bit vs 16-bit). Other operations like white balancing are also simplified.
It's worth noting that not all 8-bit RGB values are sRGB
. For example, certain cameras
enable you to capture images as raw sensor values which can be interpreted linearly without
loss of accuracy. Most cameras (including machine vision ones) do support sRGB
though and
in some cases, it is the default setting to have sRGB
encoding enabled.
Modules
iter |
Structs
Iter |
|
RGBA |
Functions
linear_to_gray | |
linear_to_srgb |
|
srgb_to_linear |
|