Skip to content

Commit

Permalink
[toimage] Optimize native pixel data cropping
Browse files Browse the repository at this point in the history
- do no copies if the raw pixel data is native and needed no processing
  • Loading branch information
Enet4 committed Oct 5, 2023
1 parent 5df7092 commit c45955d
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions toimage/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,21 @@ fn run(args: App) -> Result<(), Error> {
let frame_size = rows * columns * samples_per_pixel * ((bits_allocated + 7) / 8);

let frame = frame_number as usize;
Cow::Owned(
v.to_bytes()
.get((frame_size * frame)..(frame_size * (frame + 1)))
.context(FrameOutOfBoundsSnafu { frame_number })?
.to_vec(),
)
let mut data = v.to_bytes();
match &mut data {
Cow::Borrowed(data) => {
*data = data
.get((frame_size * frame)..(frame_size * (frame + 1)))
.context(FrameOutOfBoundsSnafu { frame_number })?;
}
Cow::Owned(data) => {
*data = data
.get((frame_size * frame)..(frame_size * (frame + 1)))
.context(FrameOutOfBoundsSnafu { frame_number })?
.to_vec();
}
}
data
}
_ => {
return UnexpectedPixelDataSnafu.fail();
Expand Down Expand Up @@ -289,7 +298,7 @@ fn run(args: App) -> Result<(), Error> {
}

let image = pixel
.to_dynamic_image_with_options(frame_number, &options)
.to_dynamic_image_with_options(0, &options)
.context(ConvertImageSnafu)?;

image.save(&output).context(SaveImageSnafu)?;
Expand Down

0 comments on commit c45955d

Please sign in to comment.