Skip to content

Commit

Permalink
bin: Allow input from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
etemesi254 committed Jul 27, 2024
1 parent 33db34c commit 734bdc7
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 38 deletions.
5 changes: 2 additions & 3 deletions crates/zune-bin/src/cmd_parsers/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use clap::ArgMatches;
use log::debug;
use zune_image::pipelines::Pipeline;
use zune_image::traits::IntoImage;
use zune_imageprocs::box_blur::BoxBlur;
use zune_imageprocs::convolve::Convolve;
use zune_imageprocs::gaussian_blur::GaussianBlur;
Expand All @@ -21,8 +20,8 @@ use zune_imageprocs::spatial_ops::SpatialOperations;
use zune_imageprocs::unsharpen::Unsharpen;
//use zune_opencl::ocl_sobel::OclSobel;

pub fn parse_options<T: IntoImage>(
workflow: &mut Pipeline<T>, argument: &str, args: &ArgMatches
pub fn parse_options(
workflow: &mut Pipeline, argument: &str, args: &ArgMatches
) -> Result<(), String> {
if argument == "box-blur" {
let radius = *args.get_one::<usize>(argument).unwrap();
Expand Down
5 changes: 2 additions & 3 deletions crates/zune-bin/src/cmd_parsers/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use zune_core::colorspace::ColorSpace;
use zune_image::core_filters::colorspace::ColorspaceConv;
use zune_image::core_filters::depth::Depth;
use zune_image::pipelines::Pipeline;
use zune_image::traits::IntoImage;
use zune_imageprocs::brighten::Brighten;
use zune_imageprocs::contrast::Contrast;
use zune_imageprocs::crop::Crop;
Expand All @@ -33,8 +32,8 @@ use zune_imageprocs::transpose::Transpose;

use crate::cmd_args::arg_parsers::IColorSpace;

pub fn parse_options<T: IntoImage>(
workflow: &mut Pipeline<T>, argument: &str, args: &ArgMatches
pub fn parse_options(
workflow: &mut Pipeline, argument: &str, args: &ArgMatches
) -> Result<(), String> {
if argument == "flip" {
debug!("Added flip operation");
Expand Down
21 changes: 18 additions & 3 deletions crates/zune-bin/src/file_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*
* You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license
*/

use std::ffi::OsString;
use std::fs::File;
use std::io::BufReader;

use zune_core::bytestream::ZCursor;
use zune_core::options::DecoderOptions;
use zune_image::errors::ImageErrors;
use zune_image::image::Image;
Expand All @@ -27,10 +27,25 @@ impl ZuneFile {
}

impl IntoImage for ZuneFile {
fn into_image(self) -> Result<Image, ImageErrors> {
fn into_image(&mut self) -> Result<Image, ImageErrors> {
// read file
let fd = BufReader::new(File::open(self.file_path)?);
let fd = BufReader::new(File::open(self.file_path.clone())?);

Image::read(fd, self.options)
}
}

pub struct ZuneMem<T: AsRef<[u8]>> {
source: T,
options: DecoderOptions
}
impl<T: AsRef<[u8]>> ZuneMem<T> {
pub fn new(source: T, options: DecoderOptions) -> ZuneMem<T> {
ZuneMem { source, options }
}
}
impl<T: AsRef<[u8]>> IntoImage for ZuneMem<T> {
fn into_image(&mut self) -> Result<Image, ImageErrors> {
Image::read(ZCursor::new(self.source.as_ref()), self.options)
}
}
1 change: 1 addition & 0 deletions crates/zune-bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*
* You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license
*/
extern crate core;

use std::process::exit;

Expand Down
59 changes: 37 additions & 22 deletions crates/zune-bin/src/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@ use log::{debug, error, info, trace};
use zune_image::codecs::ImageFormat;
use zune_image::errors::ImageErrors;
use zune_image::pipelines::Pipeline;
use zune_image::traits::IntoImage;

use crate::cmd_args::CmdImageFormats;
use crate::cmd_parsers::global_options::CmdOptions;
use crate::cmd_parsers::{decoder_options, encoder_options};
use crate::file_io::ZuneFile;
use crate::file_io::{ZuneFile, ZuneMem};
use crate::probe_files::probe_input_files;
use crate::show_gui::open_in_default_app;

struct CmdPipeline<T: IntoImage> {
inner: Pipeline<T>,
struct CmdPipeline {
inner: Pipeline,
formats: Vec<ImageFormat>
}
impl<T: IntoImage> CmdPipeline<T> {
pub fn new() -> CmdPipeline<T> {
impl CmdPipeline {
pub fn new() -> CmdPipeline {
CmdPipeline {
inner: Pipeline::new(),
formats: vec![]
Expand All @@ -58,22 +57,40 @@ pub(crate) fn create_and_exec_workflow_from_cmd(
let mut buf = [0; 30];

for in_file in args.get_raw("in").unwrap() {
let mut workflow: CmdPipeline<ZuneFile> = CmdPipeline::new();

File::open(in_file)?.read(&mut buf)?;

add_operations(args, &mut workflow.inner)?;

if let Some((format, _)) = ImageFormat::guess_format(std::io::Cursor::new(&buf)) {
if format.has_decoder() {
workflow
.inner
.chain_decoder(ZuneFile::new(in_file.to_os_string(), decoder_options));
let mut workflow: CmdPipeline = CmdPipeline::new();

if in_file == "-" {
// handle stdin
let mut data = Vec::new();
let bytes_read = std::io::stdin().read_to_end(&mut data)?;
if let Some((format, _)) = ImageFormat::guess_format(std::io::Cursor::new(&data)) {
if format.has_decoder() {
workflow
.inner
.chain_decoder(Box::new(ZuneMem::new(data, decoder_options)));
} else {
return Err(ImageErrors::ImageDecoderNotImplemented(format));
}
} else {
return Err(ImageErrors::ImageDecoderNotImplemented(format));
return Err(ImageErrors::ImageDecoderNotIncluded(ImageFormat::Unknown));
}
} else {
return Err(ImageErrors::ImageDecoderNotIncluded(ImageFormat::Unknown));
File::open(in_file)?.read(&mut buf)?;

add_operations(args, &mut workflow.inner)?;

if let Some((format, _)) = ImageFormat::guess_format(std::io::Cursor::new(&buf)) {
if format.has_decoder() {
workflow.inner.chain_decoder(Box::new(ZuneFile::new(
in_file.to_os_string(),
decoder_options
)));
} else {
return Err(ImageErrors::ImageDecoderNotImplemented(format));
}
} else {
return Err(ImageErrors::ImageDecoderNotIncluded(ImageFormat::Unknown));
}
}

let options = encoder_options(args);
Expand Down Expand Up @@ -173,9 +190,7 @@ pub(crate) fn create_and_exec_workflow_from_cmd(
Ok(())
}

pub fn add_operations<T: IntoImage>(
args: &ArgMatches, workflow: &mut Pipeline<T>
) -> Result<(), String> {
pub fn add_operations(args: &ArgMatches, workflow: &mut Pipeline) -> Result<(), String> {
for id in args.ids() {
if args.try_get_many::<clap::Id>(id.as_str()).is_ok() {
// ignore groups
Expand Down
8 changes: 1 addition & 7 deletions crates/zune-image/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,5 @@ pub trait DecodeInto {
/// Convert something into an image by consuming it
pub trait IntoImage {
/// Consumes this and returns an image
fn into_image(self) -> Result<Image, ImageErrors>;
}

impl IntoImage for Image {
fn into_image(self) -> Result<Image, ImageErrors> {
Ok(self)
}
fn into_image(&mut self) -> Result<Image, ImageErrors>;
}

0 comments on commit 734bdc7

Please sign in to comment.