diff --git a/Cargo.lock b/Cargo.lock index c20a8e5df9d9bc679a23bc7aacf6e7cef1755e61..907a651061bff557bd9d071dfa879b361833fe85 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -31,7 +31,7 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "balloon_tx_monolith" -version = "0.1.0" +version = "0.1.1" dependencies = [ "anyhow", "aprs-parser", diff --git a/Cargo.toml b/Cargo.toml index 18d3f051bb81a40fe492d6790eaf5e133488dea3..8f15f50b0c38527e9cab166e4306d62f9858bfab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "balloon_tx_monolith" -version = "0.1.0" +version = "0.1.1" edition = "2021" license = "MIT" diff --git a/src/img.rs b/src/img.rs index 607dbf4b9afeb2015ae93d6233f498f647425b76..6afd2fd64c21b52124330c5fc909e5bb1d3edb54 100644 --- a/src/img.rs +++ b/src/img.rs @@ -15,13 +15,19 @@ struct ImgInfo { impl ImgInfo { pub fn compressed_jpeg_bytes(&self, dim: u32) -> anyhow::Result<Vec<u8>> { - let img = ImageReader::open(&self.path)?.decode()?; + let mut 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(), dim, dim, false); + // want an image that's $dim on one side + // resize so that the larger dimension is $dim + let (mut w, mut h) = if img.width() > dim || img.height() > dim { + let (w, h) = resize_dimensions(img.width(), img.height(), dim, dim, false); - let img = img.resize_exact(w, h, FilterType::Triangle); + img = img.resize_exact(w, h, FilterType::Triangle); + + (w, h) + } else { + (img.width(), img.height()) + }; // make both dimensions divisible by 16 (SSDV requirement) let wdiff = w % 16; @@ -43,12 +49,30 @@ impl ImgInfo { } pub fn lossless_jpeg_bytes(&self) -> anyhow::Result<Vec<u8>> { - let img = ImageReader::open(&self.path)?.decode()?; + let mut img = ImageReader::open(&self.path)?.decode()?; + + if img.width() > IMG_MAX_SIZE || img.height() > IMG_MAX_SIZE { + img = img.resize(IMG_MAX_SIZE, IMG_MAX_SIZE, FilterType::Lanczos3); + }; + + // make both dimensions divisible by 16 (SSDV requirement) + let mut w = img.width(); + let mut h = img.height(); + let wdiff = w % 16; + let hdiff = h % 16; + w -= wdiff; + h -= hdiff; - let img = img.resize(IMG_MAX_SIZE, IMG_MAX_SIZE, FilterType::Lanczos3); + let img = img.crop_imm(wdiff / 2, hdiff / 2, w, h); let mut bytes = vec![]; - img.write_to(&mut Cursor::new(&mut bytes), ImageOutputFormat::Jpeg(100))?; + + let img = img.to_rgb8(); + img.write_to( + &mut Cursor::new(&mut bytes), + ImageOutputFormat::Jpeg(JPEG_QUALITY), + )?; + Ok(bytes) } }