Skip to content

Commit

Permalink
[toimage] Set filename for single dicom to image
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBou91 committed Aug 5, 2023
1 parent 3c62392 commit b54b8b8
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions toimage/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use dicom_pixeldata::{ConvertOptions, PixelDecoder};
use regex::Regex;
use snafu::prelude::*;
use snafu::ResultExt;
use tracing::{error, info, Level};
use tracing::{error, info, warn, Level};
use walkdir::WalkDir;

/// Convert a DICOM file into an image file
Expand All @@ -24,6 +24,12 @@ struct App {
#[arg(short = 'o', long = "out-dir", default_value = ".")]
out_dir: PathBuf,

/// Set a custom output file name for a single input file, if unset, the input file name will
/// be used with the extension replaced by the target format
/// The filename should not include the target format extension
#[arg(short = 'O', long)]
out_file_name: PathBuf,

/// Target format of the output image file
#[arg(short = 'f', long = "format", default_value = "png")]
format: String,
Expand Down Expand Up @@ -90,6 +96,7 @@ fn main() -> Result<(), Error> {
let App {
files,
out_dir,
out_file_name,
format,
all_frames,
verbose,
Expand All @@ -116,11 +123,17 @@ fn main() -> Result<(), Error> {
info!("Converting {} DICOM files to images", dicom_files.len());
}

let mut custom_file_name: bool = !out_file_name.as_os_str().is_empty();
if dicom_files.len() > 1 && custom_file_name {
warn!("Output file name will be ignored for multiple input files");
custom_file_name = false;
}

// compiled once as advised in https://docs.rs/regex/latest/regex/struct.Regex.html#method.new
let filename_increment_regex = Regex::new(r"\(\d+\)")
.whatever_context("Could not compile regex for filename increment")?;

for file in dicom_files.into_iter() {
for mut file in dicom_files.into_iter() {
let obj = open_file(&file).context(OpenFileSnafu)?;

let pixel = match obj.decode_pixel_data() {
Expand Down Expand Up @@ -187,6 +200,10 @@ fn main() -> Result<(), Error> {
}
};

if custom_file_name {
file = out_file_name.clone();
}

let mut image_path = match compute_image_path(&file, &out_dir, &format, frame_number, i)
{
Ok(path) => path,
Expand Down

0 comments on commit b54b8b8

Please sign in to comment.