From 7f67d5ad290af136ce75080cb23076fc30a04909 Mon Sep 17 00:00:00 2001 From: Piyush Mishra Date: Mon, 28 Feb 2022 19:38:46 +0530 Subject: [PATCH] Export image with image crate --- Cargo.lock | 51 +-------------------------------------------------- Cargo.toml | 3 +-- src/utils.rs | 13 ++++++++----- 3 files changed, 10 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b28cec6..e2122e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,12 +29,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "anyhow" -version = "1.0.55" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "159bb86af3a200e19a068f4224eae4c8bb2d0fa054c7e5d1cacd5cef95e684cd" - [[package]] name = "atty" version = "0.2.14" @@ -632,7 +626,7 @@ dependencies = [ [[package]] name = "post_maker" -version = "0.2.5" +version = "0.2.6" dependencies = [ "clap", "dirs", @@ -647,7 +641,6 @@ dependencies = [ "serde_json", "simplelog", "textwrap", - "turbojpeg", "webbrowser", ] @@ -939,26 +932,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "thiserror" -version = "1.0.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "tiff" version = "0.6.1" @@ -986,28 +959,6 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e5d7cd7ab3e47dda6e56542f4bbf3824c15234958c6e1bd6aaa347e93499fdc" -[[package]] -name = "turbojpeg" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "111671fdf720903270c8c19fd641048793a4f8f69abc184e4e7ea9228619c65e" -dependencies = [ - "image", - "libc", - "thiserror", - "turbojpeg-sys", -] - -[[package]] -name = "turbojpeg-sys" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03ee42589a950ef4f0f03752c3878676ffe6ab5a468132784ba483b0efc257e6" -dependencies = [ - "anyhow", - "libc", -] - [[package]] name = "unicode-linebreak" version = "0.1.2" diff --git a/Cargo.toml b/Cargo.toml index bd7ee91..634e234 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "post_maker" -version = "0.2.5" +version = "0.2.6" edition = "2021" description = "Post Maker helps you to make post for Instagram and other Social Media apps easily." authors = ["PiyushXCoder "] @@ -17,7 +17,6 @@ simplelog = "0.11" fltk = "1.2" fltk-theme = "0.4" image = "0.23" -turbojpeg = { version = "^0.2", features = ["image"] } imageproc = "0.22" rusttype = "0.9" serde_json = "1.0" diff --git a/src/utils.rs b/src/utils.rs index cccf5f0..590bbc2 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -13,7 +13,7 @@ */ use std::{ - fs, + fs::{self, File}, path::{Path, PathBuf}, sync::{Arc, RwLock}, }; @@ -236,16 +236,19 @@ impl ImageContainer { prop.original_dimension.1, ); - let comp = match turbojpeg::compress_image(&img.into_rgb8(), 95, turbojpeg::Subsamp::None) { + let mut output = match File::create(&export) { Ok(a) => a, Err(e) => { - dialog::alert_default("Failed to compress jpeg!"); - warn!("Failed to compress jpeg!\n{:?}", e); + dialog::alert_default("Failed to write to disk!"); + warn!("Failed to write to disk!\n{:?}", e); return; } }; - if let Err(e) = std::fs::write(&export, comp) { + let mut encoder = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut output, 100); + encoder.set_pixel_density(image::codecs::jpeg::PixelDensity::dpi(300)); + + if let Err(e) = encoder.encode_image(&img) { dialog::alert_default("Failed to export Image!"); warn!("Failed to export Image!\n{:?}", e); }