From 6c5375a636feb9f759cc23a9f5c578e13e3e83e6 Mon Sep 17 00:00:00 2001
From: Janne Mareike Koschinski <janne@kuschku.de>
Date: Mon, 9 Mar 2020 17:46:09 +0100
Subject: [PATCH] make format and scaler configurable

---
 src/ingest/extract.rs     |  5 ++++-
 src/ingest/spritesheet.rs | 15 ++++++++++-----
 src/main.rs               | 24 ++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/src/ingest/extract.rs b/src/ingest/extract.rs
index 28b5be0..0c29b0c 100644
--- a/src/ingest/extract.rs
+++ b/src/ingest/extract.rs
@@ -15,6 +15,8 @@ pub fn extract(
     frame_interval: MediaTime,
     input_file: &Path,
     output_folder: &Path,
+    format: impl AsRef<str>,
+    scaler: SwsScaler,
 ) -> Result<(), Error> {
     let mut avformat_context = AVFormatContext::new()
         .map_err(|error| format_err!("Could not open video input: {}", error))?;
@@ -32,6 +34,7 @@ pub fn extract(
         frame_interval,
         spritesheet_path,
         "preview",
+        format
     );
 
     let mut stream: AVStream = avformat_context
@@ -117,7 +120,7 @@ pub fn extract(
                                     format_err!("Could not init output frame: {}", error)
                                 })?;
                             scale_context
-                                .reinit(&frame, &output_frame, SwsScaler::FastBilinear)
+                                .reinit(&frame, &output_frame, scaler)
                                 .map_err(|error| {
                                     format_err!("Could not reinit scale context: {}", error)
                                 })?;
diff --git a/src/ingest/spritesheet.rs b/src/ingest/spritesheet.rs
index 9a6daa9..5771668 100644
--- a/src/ingest/spritesheet.rs
+++ b/src/ingest/spritesheet.rs
@@ -18,7 +18,8 @@ pub struct SpritesheetManager {
     frame_interval: MediaTime,
     metadata: WebVTTFile,
     output_path: PathBuf,
-    name: std::string::String,
+    name: String,
+    format: String,
     initialized: bool,
 }
 
@@ -30,6 +31,7 @@ impl SpritesheetManager {
         frame_interval: MediaTime,
         output_path: impl Into<PathBuf>,
         name: impl AsRef<str>,
+        format: impl AsRef<str>
     ) -> SpritesheetManager {
         SpritesheetManager {
             num_horizontal,
@@ -43,7 +45,8 @@ impl SpritesheetManager {
             frame_interval,
             metadata: WebVTTFile::new(),
             output_path: output_path.into(),
-            name: std::string::String::from(name.as_ref()),
+            name: String::from(name.as_ref()),
+            format: String::from(format.as_ref()),
             initialized: false,
         }
     }
@@ -135,9 +138,10 @@ impl SpritesheetManager {
             self.last_timestamp,
             timestamp,
             format!(
-                "{}_{}.jpg#xywh={},{},{},{}",
+                "{}_{}.{}#xywh={},{},{},{}",
                 self.name,
                 self.spritesheet_index(self.current_image - 1),
+                self.format,
                 self.x(self.current_image - 1),
                 self.y(self.current_image - 1),
                 self.sprite_width,
@@ -149,9 +153,10 @@ impl SpritesheetManager {
     fn save_spritesheet(&mut self) -> Result<(), Error> {
         self.spritesheet
             .save(self.output_path.join(format!(
-                "{}_{}.jpg",
+                "{}_{}.{}",
                 self.name,
-                self.spritesheet_index(self.current_image)
+                self.spritesheet_index(self.current_image),
+                self.format
             )))
             .map_err(|error| format_err!("Could not write spritesheet: {}", error))?;
         self.reinit_buffer();
diff --git a/src/main.rs b/src/main.rs
index 8db4ba3..788a78b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -10,6 +10,24 @@ use failure::Error;
 use structopt::StructOpt;
 
 use crate::util::media_time::MediaTime;
+use crate::ffmpeg_api::enums::SwsScaler;
+
+fn parse_scaler(src: &str) -> Result<SwsScaler, String> {
+    match src {
+        "fast_bilinear" => Ok(SwsScaler::FastBilinear),
+        "bilinear" => Ok(SwsScaler::Bilinear),
+        "bicubic" => Ok(SwsScaler::Bicubic),
+        "x" => Ok(SwsScaler::X),
+        "point" => Ok(SwsScaler::Point),
+        "area" => Ok(SwsScaler::Area),
+        "bicublin" => Ok(SwsScaler::Bicublin),
+        "gauss" => Ok(SwsScaler::Gauss),
+        "sinc" => Ok(SwsScaler::Sinc),
+        "lanczos" => Ok(SwsScaler::Lanczos),
+        "spline" => Ok(SwsScaler::Spline),
+        _ => Err(format!("Invalid scaler: {}", src))
+    }
+}
 
 #[derive(StructOpt, Debug)]
 #[structopt(author, about)]
@@ -24,6 +42,10 @@ struct Options {
     num_vertical: u32,
     #[structopt(long = "max-size", default_value = "160")]
     max_size: u32,
+    #[structopt(long = "format", default_value = "jpg")]
+    format: String,
+    #[structopt(long = "scaler", default_value = "bilinear", parse(try_from_str = parse_scaler))]
+    scaler: SwsScaler,
 }
 
 fn main() -> Result<(), Error> {
@@ -36,6 +58,8 @@ fn main() -> Result<(), Error> {
         MediaTime::from_seconds(options.frame_interval),
         Path::new(&options.input),
         Path::new(&options.output),
+        options.format,
+        options.scaler,
     )?;
 
     Ok(())
-- 
GitLab