From e7863402f35e558e1bd6c103765d5727e4e64033 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Mon, 1 Jan 2024 21:18:56 +0100 Subject: [PATCH] implement invert instruction --- crates/executor/src/debug/instructions/mod.rs | 15 ++++++++-- crates/executor/src/debug/mod.rs | 29 ++++++++++++------- crates/rpl/src/instructions/mod.rs | 6 ++++ testfiles/invert.rpl | 13 +++++++++ 4 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 testfiles/invert.rpl diff --git a/crates/executor/src/debug/instructions/mod.rs b/crates/executor/src/debug/instructions/mod.rs index 85ec631..0c4b611 100644 --- a/crates/executor/src/debug/instructions/mod.rs +++ b/crates/executor/src/debug/instructions/mod.rs @@ -1,4 +1,4 @@ -pub mod Read { +pub mod read { use image::{io::Reader as ImageReader, DynamicImage}; use rpl::instructions::read::{Read, SourceType}; @@ -12,7 +12,7 @@ pub mod Read { } } -pub mod Write { +pub mod write { use image::{io::Reader as ImageReader, DynamicImage, ImageFormat}; use rpl::instructions::write::{TargetFormat, TargetType, Write}; @@ -28,3 +28,14 @@ pub mod Write { ); } } + +pub mod filters { + pub mod invert { + use image::DynamicImage; + + pub fn invert(mut input_data: DynamicImage) -> DynamicImage { + input_data.invert(); + input_data + } + } +} diff --git a/crates/executor/src/debug/mod.rs b/crates/executor/src/debug/mod.rs index 61cae27..d56f546 100644 --- a/crates/executor/src/debug/mod.rs +++ b/crates/executor/src/debug/mod.rs @@ -1,19 +1,18 @@ +use rpl::instructions::{FilterInstruction, Instruction}; + use crate::{value::DynamicValue, Executor}; mod instructions; pub struct DebugExecutor; impl Executor for DebugExecutor { - fn execute( - instruction: rpl::instructions::Instruction, - input: Option, - ) -> Option { + fn execute(instruction: Instruction, input: Option) -> Option { match instruction { - rpl::instructions::Instruction::Read(read_instruction) => Some(DynamicValue::Image( - instructions::Read::read(read_instruction), + Instruction::Read(read_instruction) => Some(DynamicValue::Image( + instructions::read::read(read_instruction), )), - rpl::instructions::Instruction::Write(write_instruction) => { - instructions::Write::write( + Instruction::Write(write_instruction) => { + instructions::write::write( write_instruction, match input { Some(DynamicValue::Image(img)) => img, @@ -22,9 +21,17 @@ impl Executor for DebugExecutor { ); None } - rpl::instructions::Instruction::Math(_) => todo!(), - rpl::instructions::Instruction::Blend(_) => todo!(), - rpl::instructions::Instruction::Noise(_) => todo!(), + Instruction::Math(_) => todo!(), + Instruction::Blend(_) => todo!(), + Instruction::Noise(_) => todo!(), + Instruction::Filter(filter_instruction) => match filter_instruction { + FilterInstruction::Invert => Some(DynamicValue::Image( + instructions::filters::invert::invert(match input { + Some(DynamicValue::Image(img)) => img, + _ => panic!("invalid value type for invert"), + }), + )), + }, } } } diff --git a/crates/rpl/src/instructions/mod.rs b/crates/rpl/src/instructions/mod.rs index dee2608..9fb8a56 100644 --- a/crates/rpl/src/instructions/mod.rs +++ b/crates/rpl/src/instructions/mod.rs @@ -10,6 +10,7 @@ pub enum Instruction { Math(MathInstruction), Blend(BlendInstruction), Noise(NoiseInstruction), + Filter(FilterInstruction), } #[derive(Serialize, Deserialize, PartialEq, Eq, Debug)] @@ -39,3 +40,8 @@ pub enum NoiseInstruction { Simplex, Voronoi, } + +#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)] +pub enum FilterInstruction { + Invert, +} diff --git a/testfiles/invert.rpl b/testfiles/invert.rpl new file mode 100644 index 0000000..d17fe1a --- /dev/null +++ b/testfiles/invert.rpl @@ -0,0 +1,13 @@ +( + [ + Read(( + source: File("/home/jade/example/file.png"), + format: Png + )), + Filter(Invert), + Write(( + target: File("/home/jade/example/inverted.jpg"), + format: Jpeg + )) + ] +)