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

source

pub fn new(width: u32, height: u32) -> Self

Create a black image with the specified resolution.

source

pub fn shape(&self) -> (u32, u32)

Get pixels matrix shape (width, heigth).

source

fn pixel_offset(&self, x: u32, y: u32) -> usize

Return the position in the 1D array of the specified pixel.

source

fn valid_coordinates(&self, x: u32, y: u32) -> bool

Return true if (x, y) are coordinates within the 2D matrix.

source

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).

source

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).

source

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.

source

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 magic
  • width height - image shape (note: space separated)
  • ±1.0 - endianness (+1 big endian, -1 little endian) float codification
  • pixels matrix - matrix of RGB float pixels encoded as function of endianness

Possible read failures, in precedence order:

  1. Lack of first end line (check_eol)
  2. Invalid magic (no PF)
  3. Lack of second end line (check_eol)
  4. Invalid image shape (parse_img_shape)
  5. Lack of third end line (check_eol)
  6. Invalid endianness (parse_endianness)
  7. Invalid pixels matrix (error parsing float RGB)
  8. 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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn clamp_image(&mut self)

Adjust the color levels of the brightest pixels in the image.

source

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.

Trait Implementations§

source§

impl Clone for HdrImage

source§

fn clone(&self) -> HdrImage

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for HdrImage

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq for HdrImage

source§

fn eq(&self, other: &HdrImage) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for HdrImage

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.