Skip to content

Commit

Permalink
Delete unsafe scaling implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
lilith committed Aug 21, 2024
1 parent 0d8387a commit 3345a1d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1,309 deletions.
9 changes: 2 additions & 7 deletions imageflow_core/benches/bench_graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,8 @@ fn benchmark_scale_2d(ctx: &mut Criterion) {
} ));
}
group.bench_function("SafeRust", |b| b.iter(|| {
unsafe { assert_eq!(imageflow_core::graphics::scaling::flow_node_execute_scale2d_render1d(
bitmap_a.get_window_u8().unwrap(),bitmap_b.get_window_u8().unwrap(),&scale_rust,true), Ok(())) }
}));

group.bench_function("Rust", |b| b.iter(|| {
unsafe { assert_eq!(imageflow_core::graphics::scaling::flow_node_execute_scale2d_render1d(
bitmap_a.get_window_u8().unwrap(),bitmap_b.get_window_u8().unwrap(),&scale_rust,false), Ok(())) }
unsafe { assert_eq!(imageflow_core::graphics::scaling::scale_and_render(
bitmap_a.get_window_u8().unwrap(),bitmap_b.get_window_u8().unwrap(),&scale_rust), Ok(())) }
}));


Expand Down
57 changes: 18 additions & 39 deletions imageflow_core/src/flow/nodes/scale_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,27 +193,23 @@ impl NodeDefOneInputOneCanvas for DrawImageDef {

fn render(&self, c: &Context, canvas_key: BitmapKey, input_key: BitmapKey, p: &NodeParams) -> Result<()> {
if let NodeParams::Json(s::Node::DrawImageExact { x, y, w, h, ref hints, blend }) = *p {

unsafe {
let bitmaps = c.borrow_bitmaps()
.map_err(|e| e.at(here!()))?;
let mut canvas_bitmap = bitmaps.try_borrow_mut(canvas_key)
.map_err(|e| e.at(here!()))?;
let mut canvas = canvas_bitmap.get_window_u8().unwrap()
.to_bitmap_bgra().map_err(|e| e.at(here!()))?;

let mut input_bitmap = bitmaps.try_borrow_mut(input_key)
.map_err(|e| e.at(here!()))?;
let input = input_bitmap.get_window_u8().unwrap()
.to_bitmap_bgra().map_err(|e| e.at(here!()))?;


let hints = hints.as_ref();
if x + w > canvas.w || y + h > canvas.h {
return Err(nerror!(crate::ErrorKind::InvalidNodeParams, "DrawImageExact target rect x1={},y1={},w={},h={} does not fit canvas size {}x{}.", x,y, w, h, canvas.w, canvas.h));
if x + w > canvas_bitmap.w() || y + h > canvas_bitmap.h() {
return Err(nerror!(crate::ErrorKind::InvalidNodeParams, "DrawImageExact target rect x1={},y1={},w={},h={} does not fit canvas size {}x{}.",
x,y, w, h, canvas_bitmap.w(), canvas_bitmap.h()));
}
if input.fmt.bytes() != 4 || canvas.fmt.bytes() != 4 {
return Err(nerror!(crate::ErrorKind::InvalidNodeConnections, "DrawImageExact can only operate on Rgb32 and Rgba32 bitmaps. Input pixel format {:?}. Canvas pixel format {:?}.", input.fmt, canvas.fmt));
if input_bitmap.info().channels() != 4 || canvas_bitmap.info().channels() != 4 {
return Err(nerror!(crate::ErrorKind::InvalidNodeConnections,
"DrawImageExact can only operate on Rgb32 and Rgba32 bitmaps. Input pixel format {:?}. Canvas pixel format {:?}.", input_bitmap.frame_info().fmt, canvas_bitmap.frame_info().fmt));
}
match hints.and_then(|h| h.resample_when) {
Some(s::ResampleWhen::Always) | None => {},
Expand All @@ -222,9 +218,9 @@ impl NodeDefOneInputOneCanvas for DrawImageDef {
}
}

let upscaling = w > input.w || h > input.h;
let downscaling = w < input.w || h < input.h;
let size_differs = w != input.w || h != input.h;
let upscaling = w > input_bitmap.w() || h > input_bitmap.h();
let downscaling = w < input_bitmap.w() || h < input_bitmap.h();
let size_differs = w != input_bitmap.w() || h != input_bitmap.h();

let picked_filter = if upscaling {
hints.and_then(|h| h.up_filter).unwrap_or(s::Filter::Ginseng)
Expand Down Expand Up @@ -255,56 +251,39 @@ impl NodeDefOneInputOneCanvas for DrawImageDef {
let compose = blend.unwrap_or(::imageflow_types::CompositingMode::Compose) == s::CompositingMode::Compose;

// We can write to the temporary BitmapBgra field because we set it on the real bitmap later after we're done
if canvas.compositing_mode == crate::ffi::BitmapCompositingMode::ReplaceSelf && compose {
canvas.compositing_mode = crate::ffi::BitmapCompositingMode::BlendWithSelf;
if canvas_bitmap.info().compose() == &BitmapCompositing::ReplaceSelf && compose {
canvas_bitmap.set_compositing(BitmapCompositing::BlendWithSelf);
}
if canvas.compositing_mode == crate::ffi::BitmapCompositingMode::BlendWithMatte && !compose && canvas.fmt == PixelFormat::Bgra32 {
canvas.compositing_mode = crate::ffi::BitmapCompositingMode::ReplaceSelf;
if matches!(canvas_bitmap.info().compose(),BitmapCompositing::BlendWithMatte(_))
&& !compose
&& canvas_bitmap.frame_info().fmt == PixelFormat::Bgra32 {
canvas_bitmap.set_compositing(BitmapCompositing::ReplaceSelf);
}


// let ffi_struct = ffi::Scale2dRenderToCanvas1d {
// interpolation_filter: ffi::Filter::from(picked_filter),
// x,
// y,
// w,
// h,
// sharpen_percent_goal: sharpen_percent,
// scale_in_colorspace: crate::ffi::Floatspace::from(floatspace)
// };


// if !crate::ffi::flow_node_execute_scale2d_render1d(c.flow_c(),
// &mut input, &mut canvas, &ffi_struct as *const ffi::Scale2dRenderToCanvas1d) {
// return Err(cerror!(c, "Failed to execute Scale2D: "));
// }

let scale_and_render = ScaleAndRenderParams {
x,
y,
w,
interpolation_filter: Filter::from(picked_filter),
sharpen_percent_goal: sharpen_percent,
scale_in_colorspace: match floatspace{
scale_in_colorspace: match floatspace {
ScalingFloatspace::Srgb => WorkingFloatspace::StandardRGB,
ScalingFloatspace::Linear => WorkingFloatspace::LinearRGB
},
h,
};
match crate::graphics::scaling::flow_node_execute_scale2d_render1d(input_bitmap.get_window_u8().unwrap(),
canvas_bitmap.get_window_u8().unwrap(), &scale_and_render, true) {
match crate::graphics::scaling::scale_and_render(input_bitmap.get_window_u8().unwrap(),
canvas_bitmap.get_window_u8().unwrap(), &scale_and_render) {
Ok(_) => {},
Err(e) => {
return Err(e.at(here!()));
}
}
canvas_bitmap.set_compositing(BitmapCompositing::BlendWithSelf);
}

Ok(())
} else {
Err(nerror!(crate::ErrorKind::NodeParamsMismatch, "Need DrawImageExact, got {:?}",p))
Err(nerror!( crate::ErrorKind::NodeParamsMismatch, "Need DrawImageExact, got {:?}", p))
}
}
}
Loading

0 comments on commit 3345a1d

Please sign in to comment.