pub struct HdrImage {
width: u32,
height: u32,
pixels: Vec<Color>,
}
Expand description
High Dynamic Range Image struct.
Fields§
§width: u32
Number of columns in the 2D matrix of colors.
height: u32
Number of rows in the 2D matrix of colors.
pixels: Vec<Color>
The 2D matrix, represented as a 1D std::vec::Vec
of Color
.
Implementations§
source§impl HdrImage
impl HdrImage
sourcepub fn new(width: u32, height: u32) -> Self
pub fn new(width: u32, height: u32) -> Self
Create a black image with the specified resolution.
sourcefn pixel_offset(&self, x: u32, y: u32) -> usize
fn pixel_offset(&self, x: u32, y: u32) -> usize
Return the position in the 1D array of the specified pixel.
sourcefn valid_coordinates(&self, x: u32, y: u32) -> bool
fn valid_coordinates(&self, x: u32, y: u32) -> bool
Return true
if (x, y)
are coordinates within the 2D matrix.
sourcepub fn get_pixel(&self, x: u32, y: u32) -> Result<Color, HdrImageErr>
pub fn get_pixel(&self, x: u32, y: u32) -> Result<Color, HdrImageErr>
Return the Color
value for a pixel in the image inside a std::result::Result
.
For invalid (x, y)
coordinates the result is an HdrImageErr
error variant.
The pixel at the top-left corner has coordinates (0, 0)
.
sourcepub fn set_pixel(
&mut self,
x: u32,
y: u32,
new_color: Color
) -> Result<(), HdrImageErr>
pub fn set_pixel( &mut self, x: u32, y: u32, new_color: Color ) -> Result<(), HdrImageErr>
Set the new Color
for a pixel in the image.
For invalid (x, y)
coordinates the result is an HdrImageErr
error variant.
The pixel at the top-left corner has coordinates (0, 0)
.
sourcepub fn set_pixels(&mut self, pixels: Vec<Color>) -> Result<(), HdrImageErr>
pub fn set_pixels(&mut self, pixels: Vec<Color>) -> Result<(), HdrImageErr>
Set pixels matrix, from underlying Vec
structure, of the correct size.
Otherwise return HdrImageErr::InvalidPixelsSize
variant.
sourcefn read_pfm_image<R: BufRead>(buf_reader: &mut R) -> Result<Self, HdrImageErr>
fn read_pfm_image<R: BufRead>(buf_reader: &mut R) -> Result<Self, HdrImageErr>
Read a pfm image from buf_reader
with std::io::BufRead
trait implementation.
The expected input buffer must respect pfm format:
PF # ASCII text, '\n' as line separator
width height # ASCII text, '\n' as line separator
±1.0 # ASCII text, '\n' as line separator
pixels matrix # binary content (float RGB x #pixels)
Little description:
PF
- the magicwidth
height
- image shape (note: space separated)±1.0
- endianness (+1
big endian,-1
little endian) float codificationpixels matrix
- matrix of RGB float pixels encoded as function of endianness
Possible read failures, in precedence order:
- Lack of first end line (
check_eol
) - Invalid magic (no
PF
) - Lack of second end line (
check_eol
) - Invalid image shape (
parse_img_shape
) - Lack of third end line (
check_eol
) - Invalid endianness (
parse_endianness
) - Invalid pixels matrix (error parsing float RGB)
- Invalid EOF (unexpected binary content after pixels matrix read)
Parse of f32
from binary stream using
byteorder
library.
Return a HdrImage
object containing the image inside a std::result::Result
.
If an error occurs the result contains an HdrImageErr
error variant.
sourcepub fn read_pfm_file(path: &Path) -> Result<Self, HdrImageErr>
pub fn read_pfm_file(path: &Path) -> Result<Self, HdrImageErr>
Read a pfm image from path
, wrapper function around
read_pfm_image
.
Return a HdrImage
object containing the image inside a std::result::Result
.
If an error occurs the result contains an HdrImageErr
error variant.
sourcefn write_pfm_image<W: Write>(
&self,
stream: &mut W,
endianness: ByteOrder
) -> Result<(), HdrImageErr>
fn write_pfm_image<W: Write>( &self, stream: &mut W, endianness: ByteOrder ) -> Result<(), HdrImageErr>
Write a pfm image to stream
with std::io::Write
trait implementation.
The enum endianness
specifies the byte endianness
to be used in the file.
If an error occurs the result contains an HdrImageErr
error variant.
sourcepub fn write_pfm_file(
&self,
path: &Path,
endianness: ByteOrder
) -> Result<(), HdrImageErr>
pub fn write_pfm_file( &self, path: &Path, endianness: ByteOrder ) -> Result<(), HdrImageErr>
Write a pfm image to path
, wrapper function around
write_pfm_image
.
If an error occurs the result contains an HdrImageErr
error variant.
sourcefn average_luminosity(&self) -> f32
fn average_luminosity(&self) -> f32
Return the average luminosity of the image.
The DELTA
constant is used to prevent numerical problems
for under illuminated pixels.
sourcepub fn normalize_image(&mut self, factor: f32, luminosity: Luminosity)
pub fn normalize_image(&mut self, factor: f32, luminosity: Luminosity)
Normalize the image for a given luminosity.
factor
is normalization factor.
Different variants of luminosity
enum can be chosen.
sourcepub fn clamp_image(&mut self)
pub fn clamp_image(&mut self)
Adjust the color levels of the brightest pixels in the image.
sourcepub fn write_ldr_file(&self, path: &Path, gamma: f32) -> Result<(), HdrImageErr>
pub fn write_ldr_file(&self, path: &Path, gamma: f32) -> Result<(), HdrImageErr>
Save the image in a Low Dynamic Range (LDR) format,
using image
library.
gamma
is transfer function parameter.
Note: the output format is auto-detected from the file name extension,
only two LDR image format are supported .ff
(farbfeld
)
and .png
(PNG
).
Note: before calling this function, you should apply a
tone-mapping algorithm to the image and
be sure that the RGB values of the colors in the image are all in the range [0, 1]
.
Use normalize_image
and clamp_image
to do this.
In case of errors, std::result::Result
is an HdrImageErr
error variant.