Skip to content

Commit

Permalink
fix div/0, fix warnings, 4:3 thumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
woelper committed Dec 14, 2024
1 parent 94edf43 commit 521f42f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/file_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use image::{DynamicImage, ImageEncoder};
use notan::egui::Ui;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::{BufWriter, Cursor, Write};
use std::io::BufWriter;
use std::path::Path;
use strum::{Display, EnumIter};

Expand Down
24 changes: 14 additions & 10 deletions src/filebrowser.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::icons::*;
use crate::file_encoder::FileEncoder;
use crate::settings::VolatileSettings;
use crate::thumbnails::{Thumbnails, THUMB_SIZE};
use crate::ui::{render_file_icon, EguiExt, BUTTON_HEIGHT_LARGE, BUTTON_HEIGHT_SMALL};
use crate::thumbnails::{Thumbnails, THUMB_CAPTION_HEIGHT, THUMB_SIZE};
use crate::ui::{render_file_icon, EguiExt, BUTTON_HEIGHT_LARGE};

use anyhow::{Context, Result};
use dirs;
use log::{debug, info};
use log::debug;
use notan::egui::{self, *};
use std::io::Write;
use std::{
Expand Down Expand Up @@ -379,14 +379,12 @@ pub fn browse<F: FnMut(&PathBuf)>(
false => Color32::from_gray(217),
};

const THUMB_HEIGHT_WITH_CAPTION: u32 = THUMB_SIZE[1] + 32;

let r = ui.available_rect_before_wrap();
let spacing = ui.style().spacing.item_spacing.x;
let w = r.width() - spacing * 3.;

let thumbs_per_row = (w / (THUMB_SIZE[0] as f32 + spacing)).floor();
let num_rows = entries.len() / thumbs_per_row as usize;
let thumbs_per_row = (w / (THUMB_SIZE[0] as f32 + spacing)).floor().max(1.);
let num_rows = entries.len() / (thumbs_per_row as usize).max(1);

// info!("tpr {thumbs_per_row} {w}, rows: {num_rows}");

Expand All @@ -400,10 +398,9 @@ pub fn browse<F: FnMut(&PathBuf)>(
.auto_shrink([false, false])
.show_rows(
ui,
THUMB_HEIGHT_WITH_CAPTION as f32,
(THUMB_SIZE[1] + THUMB_CAPTION_HEIGHT) as f32,
num_rows,
|ui, row_range| {
info!("range {:?}", row_range);
let entries = entries
.clone()
.drain(
Expand All @@ -416,7 +413,14 @@ pub fn browse<F: FnMut(&PathBuf)>(
} else {
ui.horizontal_wrapped(|ui| {
if entries.is_empty() {
ui.label("Empty directory");
let r = ui.label("Empty directory");
let r = r.interact(Sense::click());
if r.clicked() {
if let Some(parent) = path.parent() {
*path = parent.to_path_buf();
}

}
} else {
for de in entries.iter().filter(|e| e.is_dir()) {
if render_file_icon(&de, ui, &mut state.thumbnails)
Expand Down
1 change: 0 additions & 1 deletion src/image_editing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::{filebrowser, utils::SUPPORTED_EXTENSIONS};
use anyhow::{bail, Result};
use evalexpr::*;
use fast_image_resize::{self as fr, ResizeOptions};
use image::codecs::png::FilterType;
use image::{imageops, ColorType, DynamicImage, Rgba, RgbaImage};
use imageproc::geometric_transformations::Interpolation;
use log::{debug, error, info};
Expand Down
8 changes: 4 additions & 4 deletions src/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use dds::DDS;
use exr::prelude as exrs;
use exr::prelude::*;
use image::{
buffer, DynamicImage, EncodableLayout, GrayAlphaImage, GrayImage, ImageDecoder, ImageReader,
DynamicImage, EncodableLayout, GrayAlphaImage, GrayImage, ImageDecoder, ImageReader,
Rgb32FImage, RgbImage, RgbaImage,
};
use jxl_oxide::{JxlImage, PixelFormat};
use quickraw::{data, DemosaicingMethod, Export, Input, Output, OutputType};
use rayon::prelude::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
use quickraw::Export;
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use rgb::*;
use std::fs::File;
use std::io::{BufReader, Read};
Expand Down Expand Up @@ -759,7 +759,7 @@ fn u16_to_u8(p: u16) -> u8 {

fn load_raw(img_location: &Path) -> Result<RgbaImage> {
let raw_data = std::fs::read(img_location)?;
let (thumbnail_data, orientation) = Export::export_thumbnail_data(&raw_data).unwrap();
let (thumbnail_data, _orientation) = Export::export_thumbnail_data(&raw_data)?;

let i = image::load_from_memory(&thumbnail_data)?;
Ok(i.to_rgba8())
Expand Down
1 change: 0 additions & 1 deletion src/texture_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use log::error;
use log::warn;
use notan::draw::*;
use notan::prelude::{BlendMode, Graphics, ShaderSource, Texture, TextureFilter};
use zune_png::zune_core::bit_depth::ByteEndian;
pub struct TexWrap {
texture_array: Vec<TexturePair>,
pub col_count: u32,
Expand Down
7 changes: 4 additions & 3 deletions src/thumbnails.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub const THUMB_SIZE: [u32; 2] = [120, 80];
pub const THUMB_SIZE: [u32; 2] = [120, 90];
pub const THUMB_CAPTION_HEIGHT: u32 = 32;

use std::{
fs::create_dir_all,
Expand Down Expand Up @@ -84,7 +85,7 @@ pub fn generate<P: AsRef<Path>>(source_path: P) -> Result<()> {

pub fn from_existing<P: AsRef<Path>>(dest_path: P, image: &DynamicImage) -> Result<()> {
let (mut width, mut height) = image.dimensions();
let mut x = 0;
let x = 0;
let mut y = 0;

if width < height {
Expand Down Expand Up @@ -120,6 +121,6 @@ fn test_thumbs() {
std::env::set_var("RUST_LOG", "debug");
let _ = env_logger::try_init();
let mut thumbs = Thumbnails::default();
thumbs.get("tests/rust.png");
thumbs.get("tests/rust.png").unwrap();
std::thread::sleep_ms(3000);
}
25 changes: 13 additions & 12 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ use crate::{
set_zoom,
settings::{set_system_theme, ColorTheme, PersistentSettings, VolatileSettings},
shortcuts::{key_pressed, keypresses_as_string, lookup},
thumbnails::{Thumbnails, THUMB_SIZE},
thumbnails::{Thumbnails, THUMB_CAPTION_HEIGHT, THUMB_SIZE},
utils::{
clipboard_copy, disp_col, disp_col_norm, fix_exif, highlight_bleed, highlight_semitrans,
load_image_from_path, next_image, prev_image, send_extended_info, set_title, solo_channel,
toggle_fullscreen, unpremult, ColorChannel, ImageExt,
},
};
#[cfg(not(feature = "file_open"))]
use crate::{filebrowser, SUPPORTED_EXTENSIONS};
use crate::{filebrowser};

use std::io::Write;

Expand All @@ -32,7 +32,7 @@ use ase_swatch::types::{Color, ObjectColor};
use egui_plot::{Line, Plot, PlotPoints};
use epaint::TextShape;
use image::{ColorType, GenericImageView, RgbaImage};
use log::{debug, error, info, warn};
use log::{debug, error, info};
#[cfg(not(any(target_os = "netbsd", target_os = "freebsd")))]
use mouse_position::mouse_position::Mouse;
use notan::{
Expand Down Expand Up @@ -1424,7 +1424,7 @@ pub fn edit_ui(app: &mut App, ctx: &Context, state: &mut OculanteState, gfx: &mu
// Do the processing

// If expensive operations happened (modifying image geometry), process them here
let mut message: Option<String> = None;
let message: Option<String> = None;
if image_changed {
if let Some(img) = &mut state.current_image {
let stamp = Instant::now();
Expand All @@ -1435,7 +1435,6 @@ pub fn edit_ui(app: &mut App, ctx: &Context, state: &mut OculanteState, gfx: &mu
if let Err(e) = operation.process_image(&mut state.edit_state.result_image_op) {
error!("{e}");
state.send_message_warn(&format!("{e}"));
message = Some(e.to_string())
}
}
debug!(
Expand Down Expand Up @@ -1463,8 +1462,6 @@ pub fn edit_ui(app: &mut App, ctx: &Context, state: &mut OculanteState, gfx: &mu
let ops = &state.edit_state.pixel_op_stack;
if let Err(e) = process_pixels(&mut state.edit_state.result_pixel_op, ops) {
state.send_message_warn(&format!("{e}"));
message = Some(e.to_string())

}
}

Expand Down Expand Up @@ -2667,15 +2664,15 @@ pub fn drag_area(ui: &mut Ui, state: &mut OculanteState, app: &mut App) {
}
}
pub fn render_file_icon(icon_path: &Path, ui: &mut Ui, thumbnails: &mut Thumbnails) -> Response {
let caption_size = 24.;
// let caption_size = 24.;
let mut zoom = ui
.data_mut(|w| w.get_temp::<f32>("ZM".into()))
.unwrap_or(0.997);
let delta = ui.input(|r| r.zoom_delta()).clamp(0.9999, 1.0001);
.unwrap_or(1.);
let delta = ui.input(|r| r.zoom_delta()).clamp(0.999, 1.001);
zoom *= delta;
zoom = zoom.clamp(0.5, 1.3);
ui.data_mut(|w| w.insert_temp("ZM".into(), zoom));
let size = Vec2::new(THUMB_SIZE[0] as f32, THUMB_SIZE[1] as f32 + 32.) * zoom;
let size = Vec2::new(THUMB_SIZE[0] as f32, (THUMB_SIZE[1] + THUMB_CAPTION_HEIGHT) as f32) * zoom;
let response = ui.allocate_response(size, Sense::click());
let rounding = Rounding::same(4.);
let margin = 4.0;
Expand All @@ -2690,7 +2687,11 @@ pub fn render_file_icon(icon_path: &Path, ui: &mut Ui, thumbnails: &mut Thumbnai
);
} else {
let mut image_rect = response.rect;
image_rect.set_bottom(image_rect.max.y - caption_size);
image_rect.max.x = image_rect.max.x.round();
image_rect.max.y = image_rect.max.y.round();
image_rect.min.y = image_rect.min.y.round();
image_rect.min.x = image_rect.min.x.round();
image_rect.set_bottom(image_rect.max.y - THUMB_CAPTION_HEIGHT as f32);

match thumbnails.get(icon_path) {
Ok(tp) => {
Expand Down

0 comments on commit 521f42f

Please sign in to comment.