1
0
Fork 0
mirror of synced 2024-06-09 22:14:42 +12:00

Webp Heic

This commit is contained in:
Rafał Mikrut 2022-06-08 16:39:09 +02:00
parent fb1acb017f
commit 061463a2cc
3 changed files with 92 additions and 39 deletions

View file

@ -1,13 +1,18 @@
use std::cell::RefCell;
use std::rc::Rc;
use czkawka_core::common::get_dynamic_image_from_heic;
use gdk4::gdk_pixbuf::{InterpType, Pixbuf};
use gtk4::prelude::*;
use gtk4::{CheckButton, Image, ListStore, Orientation, ScrolledWindow, TreeIter, TreeModel, TreePath, TreeSelection, Widget};
use image::DynamicImage;
use crate::flg;
use crate::gui_structs::gui_data::GuiData;
use crate::help_functions::{count_number_of_groups, get_all_children, get_full_name_from_path_name, get_max_file_name, resize_pixbuf_dimension, NotebookObject, NOTEBOOKS_INFOS};
use crate::help_functions::{
count_number_of_groups, get_all_children, get_full_name_from_path_name, get_max_file_name, get_pixbuf_from_dynamic_image, resize_pixbuf_dimension, NotebookObject,
NOTEBOOKS_INFOS,
};
use crate::localizer_core::generate_translation_hashmap;
const BIG_PREVIEW_SIZE: i32 = 600;
@ -88,7 +93,6 @@ pub fn connect_button_compare(gui_data: &GuiData) {
let image_compare_left = gui_data.compare_images.image_compare_left.clone();
let image_compare_right = gui_data.compare_images.image_compare_right.clone();
window_compare.connect_close_request(move |window_compare| {
// TODO GTK4
window_compare.hide();
*shared_image_cache.borrow_mut() = Vec::new();
*shared_current_path.borrow_mut() = None;
@ -347,35 +351,80 @@ fn generate_cache_for_results(vector_with_path: Vec<(String, String, TreePath)>)
let small_img = Image::new();
let big_img = Image::new();
match Pixbuf::from_file(&full_path) {
Ok(pixbuf) =>
{
#[allow(clippy::never_loop)]
loop {
let pixbuf_big = match resize_pixbuf_dimension(pixbuf, (BIG_PREVIEW_SIZE, BIG_PREVIEW_SIZE), InterpType::Nearest) {
None => {
println!("Failed to resize image {}.", full_path);
break;
let mut pixbuf = get_pixbuf_from_dynamic_image(&DynamicImage::new_rgb8(1, 1)).unwrap();
if name.to_lowercase().ends_with(".heic") || name.to_lowercase().ends_with(".webp") {
#[allow(clippy::never_loop)]
'czystka: loop {
if name.ends_with(".heic") {
match get_dynamic_image_from_heic(&full_path) {
Ok(t) => {
match get_pixbuf_from_dynamic_image(&t) {
Ok(t) => {
pixbuf = t;
}
Err(e) => {
println!("Failed to open image {}, reason {}", full_path, e);
}
};
}
Some(pixbuf) => pixbuf,
};
let pixbuf_small = match resize_pixbuf_dimension(pixbuf_big.clone(), (SMALL_PREVIEW_SIZE, SMALL_PREVIEW_SIZE), InterpType::Nearest) {
None => {
println!("Failed to resize image {}.", full_path);
break;
Err(e) => {
println!("Failed to open image {}, reason {}", full_path, e);
}
Some(pixbuf) => pixbuf,
};
break 'czystka;
}
if name.ends_with(".webp") {
match image::open(&full_path) {
Ok(t) => {
match get_pixbuf_from_dynamic_image(&t) {
Ok(t) => {
pixbuf = t;
}
Err(e) => {
println!("Failed to open image {}, reason {}", full_path, e);
}
};
}
Err(e) => {
println!("Failed to open image {}, reason {}", full_path, e);
}
};
break 'czystka;
}
break 'czystka;
}
} else {
match Pixbuf::from_file(&full_path) {
Ok(t) => {
pixbuf = t;
}
Err(e) => {
println!("Failed to open image {}, reason {}", full_path, e);
}
};
}
big_img.set_from_pixbuf(Some(&pixbuf_big));
small_img.set_from_pixbuf(Some(&pixbuf_small));
#[allow(clippy::never_loop)]
loop {
let pixbuf_big = match resize_pixbuf_dimension(pixbuf, (BIG_PREVIEW_SIZE, BIG_PREVIEW_SIZE), InterpType::Nearest) {
None => {
println!("Failed to resize image {}.", full_path);
break;
}
}
Err(e) => {
println!("Failed to open image {}, reason {}", full_path, e);
}
};
Some(pixbuf) => pixbuf,
};
let pixbuf_small = match resize_pixbuf_dimension(pixbuf_big.clone(), (SMALL_PREVIEW_SIZE, SMALL_PREVIEW_SIZE), InterpType::Nearest) {
None => {
println!("Failed to resize image {}.", full_path);
break;
}
Some(pixbuf) => pixbuf,
};
big_img.set_from_pixbuf(Some(&pixbuf_big));
small_img.set_from_pixbuf(Some(&pixbuf_small));
break;
}
cache_all_images.push((full_path, name, big_img, small_img, tree_path));
}

View file

@ -3,8 +3,12 @@ use std::collections::HashMap;
use std::path::PathBuf;
use gdk4::gdk_pixbuf::{InterpType, Pixbuf};
use glib::Error;
use gtk4::prelude::*;
use gtk4::{ListStore, TextView, TreeView, Widget};
use image::codecs::jpeg::JpegEncoder;
use image::{DynamicImage, EncodableLayout};
use once_cell::sync::OnceCell;
use czkawka_core::bad_extensions::BadExtensions;
use czkawka_core::big_file::BigFile;
@ -857,3 +861,16 @@ pub fn set_icon_of_button<P: IsA<Widget>>(button: &P, data: &'static [u8]) {
let pixbuf = pixbuf.scale_simple(SIZE_OF_ICON, SIZE_OF_ICON, TYPE_OF_INTERPOLATION).unwrap();
image.set_from_pixbuf(Some(&pixbuf));
}
static mut IMAGE_PREVIEW_ARRAY: OnceCell<Vec<u8>> = OnceCell::new();
pub fn get_pixbuf_from_dynamic_image(dynamic_image: &DynamicImage) -> Result<Pixbuf, Error> {
let mut output = Vec::new();
JpegEncoder::new(&mut output).encode_image(dynamic_image).unwrap();
let arra;
unsafe {
IMAGE_PREVIEW_ARRAY.take();
IMAGE_PREVIEW_ARRAY.set(output).unwrap();
arra = IMAGE_PREVIEW_ARRAY.get().unwrap().as_bytes();
}
Pixbuf::from_read(arra)
}

View file

@ -7,9 +7,6 @@ use gdk4::gdk_pixbuf::Pixbuf;
use gtk4::gdk_pixbuf::InterpType;
use gtk4::prelude::*;
use gtk4::{CheckButton, Image, SelectionMode, TextView, TreeView};
use image::codecs::jpeg::JpegEncoder;
use image::EncodableLayout;
use once_cell::sync::OnceCell;
use czkawka_core::common::{get_dynamic_image_from_heic, HEIC_EXTENSIONS, IMAGE_RS_EXTENSIONS, RAW_IMAGE_EXTENSIONS};
use czkawka_core::similar_images::SIMILAR_VALUES;
@ -28,8 +25,6 @@ use crate::localizer_core::generate_translation_hashmap;
use crate::notebook_enums::NotebookMainEnum;
use crate::opening_selecting_records::*;
static mut IMAGE_PREVIEW_ARRAY: OnceCell<Vec<u8>> = OnceCell::new();
pub fn initialize_gui(gui_data: &mut GuiData) {
//// Initialize button
{
@ -751,15 +746,7 @@ fn show_preview(
panic!("");
};
let mut output = Vec::new();
JpegEncoder::new(&mut output).encode_image(&image).unwrap();
let arra;
unsafe {
IMAGE_PREVIEW_ARRAY.take();
IMAGE_PREVIEW_ARRAY.set(output).unwrap();
arra = IMAGE_PREVIEW_ARRAY.get().unwrap().as_bytes();
}
match Pixbuf::from_read(arra) {
match get_pixbuf_from_dynamic_image(&image) {
Ok(t) => t,
Err(e) => {
add_text_to_text_view(