From 5b1c67240502167d3fe222992d40a3a79ca89ff0 Mon Sep 17 00:00:00 2001
From: Stephen D <webmaster@scd31.com>
Date: Sat, 27 May 2023 13:05:47 -0300
Subject: [PATCH] allow configuring image size

---
 config.toml.example |  3 +++
 src/config.rs       |  1 +
 src/control.rs      |  2 +-
 src/img.rs          | 19 ++++++++++++-------
 4 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/config.toml.example b/config.toml.example
index 7d9e794..109e6d2 100644
--- a/config.toml.example
+++ b/config.toml.example
@@ -11,6 +11,9 @@ paths = [
 # must be 6 characters or less
 callsign = "NOCALL"
 
+# Max image dimension. Comment this out to send full-sized images
+max_img_dimension = 1024
+
 # Comment this section out if you're not using
 # APRS for control
 [control]
diff --git a/src/config.rs b/src/config.rs
index 97de0df..f9057d1 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -14,6 +14,7 @@ pub struct ControlConfig {
 pub struct Config {
     pub paths: Vec<PathBuf>,
     pub callsign: String,
+    pub max_img_dimension: Option<u32>,
 
     pub control: Option<ControlConfig>,
 }
diff --git a/src/control.rs b/src/control.rs
index 2d38893..62acf44 100644
--- a/src/control.rs
+++ b/src/control.rs
@@ -41,7 +41,7 @@ impl Controller {
         let mut manager = ImgManager::new(self.config.paths.clone(), cmd_rx);
 
         loop {
-            while let Some((idx, bytes)) = manager.next() {
+            while let Some((idx, bytes)) = manager.next(self.config.max_img_dimension) {
                 if let Err(e) = self.process_image(&bytes, idx, &img_tx) {
                     eprintln!("Error processing image: {e}");
                 }
diff --git a/src/img.rs b/src/img.rs
index 61ceb08..607dbf4 100644
--- a/src/img.rs
+++ b/src/img.rs
@@ -1,7 +1,6 @@
 use image::{imageops::FilterType, io::Reader as ImageReader, ImageOutputFormat};
 use std::{cmp::max, fs, io::Cursor, path::PathBuf, sync::mpsc::Receiver};
 
-const IMG_DIM: u32 = 1024;
 const IMG_MAX_SIZE: u32 = 2048;
 const JPEG_QUALITY: u8 = 100; // let ssdv do the compression
 
@@ -15,12 +14,12 @@ struct ImgInfo {
 }
 
 impl ImgInfo {
-    pub fn compressed_jpeg_bytes(&self) -> anyhow::Result<Vec<u8>> {
+    pub fn compressed_jpeg_bytes(&self, dim: u32) -> anyhow::Result<Vec<u8>> {
         let img = ImageReader::open(&self.path)?.decode()?;
 
         // want a imagine that's 512 on one side
         // resize so that the larger dimension is 512
-        let (mut w, mut h) = resize_dimensions(img.width(), img.height(), IMG_DIM, IMG_DIM, false);
+        let (mut w, mut h) = resize_dimensions(img.width(), img.height(), dim, dim, false);
 
         let img = img.resize_exact(w, h, FilterType::Triangle);
 
@@ -65,7 +64,7 @@ pub struct ImgManager {
     path_idx: usize,
     found: bool,
     tx_idx: u8,
-    // map between sent image ids, the the images
+    // map between sent image ids, and the images
     // in the array above
     // used for decoding rx requests
     img_map: [usize; 256],
@@ -89,7 +88,7 @@ impl ImgManager {
     // not making this an iterator since
     // it's entirely valid to have a Some(...)
     // after a None
-    pub fn next(&mut self) -> Option<(u8, Vec<u8>)> {
+    pub fn next(&mut self, dim: Option<u32>) -> Option<(u8, Vec<u8>)> {
         loop {
             if let Ok(req) = self.rx.try_recv() {
                 let img = self
@@ -123,7 +122,10 @@ impl ImgManager {
 
             if let Some((new_id, new)) = new {
                 new.txed = true;
-                let bytes = new.compressed_jpeg_bytes();
+                let bytes = match dim {
+                    Some(d) => new.compressed_jpeg_bytes(d),
+                    None => new.lossless_jpeg_bytes(),
+                };
 
                 self.inc_iter(true);
 
@@ -145,7 +147,10 @@ impl ImgManager {
 
             if let Some((old_idx, old)) = old {
                 old.txed = true;
-                let bytes = old.compressed_jpeg_bytes();
+                let bytes = match dim {
+                    Some(d) => old.compressed_jpeg_bytes(d),
+                    None => old.lossless_jpeg_bytes(),
+                };
 
                 self.inc_iter(true);
 
-- 
GitLab