Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce low-level byte fiddling in openexr encoder #2012

Merged
merged 3 commits into from
Oct 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions src/codecs/openexr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,27 +272,22 @@ fn write_buffer(
}
}

// bytes might be unaligned so we cannot cast the whole thing, instead lookup each f32 individually
let lookup_f32 = move |f32_index: usize| {
let unaligned_f32_bytes_slice = &unaligned_bytes[f32_index * 4..(f32_index + 1) * 4];
let f32_bytes_array = unaligned_f32_bytes_slice
.try_into()
.expect("indexing error");
f32::from_ne_bytes(f32_bytes_array)
};
let bytes_per_pixel = color_type.bytes_per_pixel() as usize;

match color_type {
ColorType::Rgb32F => {
exr::prelude::Image // TODO compression method zip??
::from_channels(
(width, height),
SpecificChannels::rgb(|pixel: Vec2<usize>| {
let pixel_index = 3 * pixel.flat_index_for_size(Vec2(width, height));
(
lookup_f32(pixel_index),
lookup_f32(pixel_index + 1),
lookup_f32(pixel_index + 2),
)
let pixel_index = pixel.flat_index_for_size(Vec2(width, height));
let start_byte = pixel_index * bytes_per_pixel;

let [r,g,b]: [f32; 3] = bytemuck::pod_read_unaligned(&unaligned_bytes[
start_byte .. start_byte + bytes_per_pixel
]);

(r,g,b)
}),
)
.write()
Expand All @@ -306,13 +301,14 @@ fn write_buffer(
::from_channels(
(width, height),
SpecificChannels::rgba(|pixel: Vec2<usize>| {
let pixel_index = 4 * pixel.flat_index_for_size(Vec2(width, height));
(
lookup_f32(pixel_index),
lookup_f32(pixel_index + 1),
lookup_f32(pixel_index + 2),
lookup_f32(pixel_index + 3),
)
let pixel_index = pixel.flat_index_for_size(Vec2(width, height));
let start_byte = pixel_index * bytes_per_pixel;

let [r,g,b, a]: [f32; 4] = bytemuck::pod_read_unaligned(&unaligned_bytes[
start_byte .. start_byte + bytes_per_pixel
]);

(r,g,b,a)
}),
)
.write()
Expand Down
Loading