feat: graph-ir (continueing #6) #10
7 changed files with 37 additions and 36 deletions
|
@ -2,8 +2,9 @@ pub mod read {
|
|||
use image::{io::Reader as ImageReader, DynamicImage};
|
||||
use rpl::instructions::read::{Read, SourceType};
|
||||
|
||||
pub fn read(Read { source, format }: Read) -> DynamicImage {
|
||||
let mut img = ImageReader::open(match source {
|
||||
pub fn read(Read { source, format: _ }: Read) -> DynamicImage {
|
||||
// TODO: actual error handling
|
||||
let img = ImageReader::open(match source {
|
||||
SourceType::File(path) => path,
|
||||
})
|
||||
.expect("something went wrong :(((");
|
||||
|
@ -13,19 +14,21 @@ pub mod read {
|
|||
}
|
||||
|
||||
pub mod write {
|
||||
use image::{io::Reader as ImageReader, DynamicImage, ImageFormat};
|
||||
use image::{DynamicImage, ImageFormat};
|
||||
use rpl::instructions::write::{TargetFormat, TargetType, Write};
|
||||
|
||||
pub fn write(Write { target, format }: Write, input_data: DynamicImage) {
|
||||
input_data.save_with_format(
|
||||
match target {
|
||||
TargetType::File(path) => path,
|
||||
},
|
||||
match format {
|
||||
TargetFormat::Jpeg => ImageFormat::Jpeg,
|
||||
TargetFormat::Png => ImageFormat::Png,
|
||||
},
|
||||
);
|
||||
pub fn write(Write { target, format }: Write, input_data: &DynamicImage) {
|
||||
input_data
|
||||
.save_with_format(
|
||||
match target {
|
||||
TargetType::File(path) => path,
|
||||
},
|
||||
match format {
|
||||
TargetFormat::Jpeg => ImageFormat::Jpeg,
|
||||
TargetFormat::Png => ImageFormat::Png,
|
||||
},
|
||||
)
|
||||
.expect("couldn't save file — come back later and handle me properly please uwu");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,21 +1,21 @@
|
|||
use rpl::instructions::{FilterInstruction, Instruction};
|
||||
|
||||
use crate::{value::DynamicValue, Executor};
|
||||
use crate::value::Dynamic;
|
||||
mod instructions;
|
||||
|
||||
pub struct DebugExecutor;
|
||||
pub struct Executor;
|
||||
|
||||
impl Executor for DebugExecutor {
|
||||
fn execute(instruction: Instruction, input: Option<DynamicValue>) -> Option<DynamicValue> {
|
||||
impl crate::Executor for Executor {
|
||||
fn execute(instruction: Instruction, input: Option<Dynamic>) -> Option<Dynamic> {
|
||||
match instruction {
|
||||
Instruction::Read(read_instruction) => Some(DynamicValue::Image(
|
||||
instructions::read::read(read_instruction),
|
||||
)),
|
||||
Instruction::Read(read_instruction) => {
|
||||
Some(Dynamic::Image(instructions::read::read(read_instruction)))
|
||||
}
|
||||
Instruction::Write(write_instruction) => {
|
||||
instructions::write::write(
|
||||
write_instruction,
|
||||
match input {
|
||||
Some(DynamicValue::Image(img)) => img,
|
||||
Some(Dynamic::Image(ref img)) => img,
|
||||
_ => panic!("awawwawwa"),
|
||||
},
|
||||
);
|
||||
|
@ -25,9 +25,9 @@ impl Executor for DebugExecutor {
|
|||
Instruction::Blend(_) => todo!(),
|
||||
Instruction::Noise(_) => todo!(),
|
||||
Instruction::Filter(filter_instruction) => match filter_instruction {
|
||||
FilterInstruction::Invert => Some(DynamicValue::Image(
|
||||
FilterInstruction::Invert => Some(Dynamic::Image(
|
||||
instructions::filters::invert::invert(match input {
|
||||
Some(DynamicValue::Image(img)) => img,
|
||||
Some(Dynamic::Image(img)) => img,
|
||||
_ => panic!("invalid value type for invert"),
|
||||
}),
|
||||
)),
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use rpl::instructions::Instruction;
|
||||
use value::DynamicValue;
|
||||
use value::Dynamic;
|
||||
|
||||
mod debug;
|
||||
mod value;
|
||||
|
@ -17,14 +17,14 @@ pub enum Executors {
|
|||
}
|
||||
|
||||
trait Executor {
|
||||
fn execute(instruction: Instruction, input: Option<DynamicValue>) -> Option<DynamicValue>;
|
||||
fn execute(instruction: Instruction, input: Option<Dynamic>) -> Option<Dynamic>;
|
||||
}
|
||||
|
||||
pub fn execute_all(instructions: Vec<Instruction>) {
|
||||
let mut tmp = None;
|
||||
|
||||
for instruction in instructions {
|
||||
tmp = debug::DebugExecutor::execute(instruction, tmp);
|
||||
tmp = debug::Executor::execute(instruction, tmp);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use image::DynamicImage;
|
||||
|
||||
pub enum DynamicValue {
|
||||
pub enum Dynamic {
|
||||
Image(DynamicImage),
|
||||
}
|
||||
|
|
|
@ -1,16 +1,14 @@
|
|||
use instructions::Instruction;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::instructions::{
|
||||
read::{SourceFormat, SourceType},
|
||||
write::{TargetFormat, TargetType},
|
||||
MathInstruction,
|
||||
};
|
||||
|
||||
pub mod instructions;
|
||||
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if deserialization fails. lol.
|
||||
#[must_use]
|
||||
pub fn from_ron(raw: &str) -> Rpl {
|
||||
ron::from_str(raw).unwrap()
|
||||
ron::from_str(raw).expect("come back later and handle me correctly")
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
(
|
||||
[
|
||||
Read((
|
||||
source: File("/home/jade/example/file.png"),
|
||||
source: File("testfiles/juan.jpg"),
|
||||
format: Png
|
||||
)),
|
||||
Filter(Invert),
|
||||
Write((
|
||||
target: File("/home/jade/example/inverted.jpg"),
|
||||
target: File("testfiles/inverted.jpg"),
|
||||
format: Jpeg
|
||||
))
|
||||
]
|
||||
|
|
BIN
testfiles/juan.jpg
Normal file
BIN
testfiles/juan.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 89 KiB |
Loading…
Reference in a new issue