pub struct HdrEncoder<'a> { /* private fields */ }Expand description
A simple HDR encoder
Data is expected to be in f32 and its size should be
width*height*3
Implementations§
Source§impl<'a> HdrEncoder<'a>
impl<'a> HdrEncoder<'a>
Sourcepub fn new(data: &'a [f32], options: EncoderOptions) -> HdrEncoder<'a>
pub fn new(data: &'a [f32], options: EncoderOptions) -> HdrEncoder<'a>
Create a new HDR encoder context that can encode the provided data
§Arguments
data: Data to encodeoptions: Contains metadata for data, including width and height
Sourcepub fn add_headers(&mut self, headers: &'a HashMap<String, String>)
pub fn add_headers(&mut self, headers: &'a HashMap<String, String>)
Sourcepub fn expected_buffer_size(&self) -> Option<usize>
pub fn expected_buffer_size(&self) -> Option<usize>
Calculate buffer with padding size needed for encoding this into a vec
This is a given upper limit and doesn’t specifically mean that your buffer should exactly be that size.
The size of the output will depend on the nature of your data
Sourcepub fn encode<T: ZByteWriterTrait>(
&self,
out: T,
) -> Result<usize, HdrEncodeErrors>
pub fn encode<T: ZByteWriterTrait>( &self, out: T, ) -> Result<usize, HdrEncodeErrors>
Encode into a sink
The encoder expects colorspace to be in RGB and the length to match
otherwise it’s an error to try and encode
The floating point data is expected to be normalized between 0.0 and 1.0, it will not be clipped if not in this range
The size of output cannot be determined up until compression is over hence we cannot be sure if the output buffer will be enough.
The library provides expected_buffer_size which guarantees that if your buffer is that big, encoding will always succeed
§Arguments:
- out: The output buffer to write bytes into
§Returns
- Ok(usize): The number of bytes written into out
- Err(HdrEncodeErrors): An error if something occurred
§Examples
- Encode a black image of 10x10
use zune_core::bit_depth::BitDepth;
use zune_core::colorspace::ColorSpace;
use zune_core::options::EncoderOptions;
use zune_hdr::HdrEncoder;
let w = 10;
let h = 10;
let comp = 3;
let data = vec![0.0_f32;w*h*comp];
let opts = EncoderOptions::new(w,h,ColorSpace::RGB,BitDepth::Float32);
let encoder = HdrEncoder::new(&data,opts);
// create output buffer , this is the upper limit on it
let mut output = Vec::with_capacity(encoder.expected_buffer_size().unwrap());
let size = encoder.encode(&mut output).unwrap();- Encode but directly write to a file
use std::fs::OpenOptions;
use std::io::{BufReader, BufWriter};
use zune_core::bit_depth::BitDepth;
use zune_core::colorspace::ColorSpace;
use zune_core::options::EncoderOptions;
use zune_hdr::HdrEncoder;
let w = 10;
let h = 10;
let comp = 3;
let data = vec![0.0_f32;w*h*comp];
let opts = EncoderOptions::new(w,h,ColorSpace::RGB,BitDepth::Float32);
let encoder = HdrEncoder::new(&data,opts);
// create output buffer , this is the upper limit on it
let mut output = OpenOptions::new().create(true).write(true).truncate(true).open("./black.hdr").unwrap();
let mut buffered_output = BufWriter::new(output);
let size = encoder.encode(&mut buffered_output).unwrap();