This commit is contained in:
Sweetbread 2025-03-24 17:22:18 +03:00
parent 84099d4928
commit c1ccad6229

View File

@ -1,3 +1,4 @@
use gtk::builders::ImageBuilder;
use gtk::gio::Cancellable;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Picture, Scale, Label, FileDialog};
@ -39,13 +40,13 @@ fn build_ui(app: &Application) {
// Создание кнопки для открытия диалога выбора файла
let button = Button::with_label("Select Image");
let picture = Picture::new();
picture.set_size_request(400, 400);
// let picture = Picture::new();
// picture.set_size_request(400, 400);
{
let window = window.clone();
let selected_file = selected_file.clone();
let picture = picture.clone();
// let picture = picture.clone();
button.connect_clicked(move |_| {
// Создание FileDialog
let dialog = FileDialog::builder()
@ -62,7 +63,7 @@ fn build_ui(app: &Application) {
// Показ диалога и обработка результата
let window = window.clone();
let selected_file = selected_file.clone();
let picture = picture.clone();
// let picture = picture.clone();
dialog.open(Some(&window), None::<&gtk::gio::Cancellable>, move |result| {
if let Ok(file) = result {
// Получение выбранного файла
@ -74,7 +75,7 @@ fn build_ui(app: &Application) {
println!("Image loaded successfully: {}x{}", pixbuf.width(), pixbuf.height());
let _ = fs::copy(&path, "/tmp/modified.png");
*selected_file.borrow_mut() = Some(file.clone());
picture.set_pixbuf(Some(&pixbuf));
// picture.set_pixbuf(Some(&pixbuf));
} else {
eprintln!("Failed to load image.");
@ -90,11 +91,17 @@ fn build_ui(app: &Application) {
button.set_visible(false);
}
let br_slider = Scale::with_range(Orientation::Horizontal, -128., 128., 1.);
let picture = Picture::for_filename("/tmp/modified.png");
let image = image::open("/tmp/modified.png")
.map(|img| img.to_rgb8())
.map_err(|e| eprintln!("Failed to load image: {}", e))
.ok().unwrap();
let br_slider = Scale::with_range(Orientation::Horizontal, -128., 128., 10.);
br_slider.set_value(0.);
br_slider.set_hexpand(true);
let br_text = Label::new(Some("Brightness: 0.00"));
let br_text = Label::new(Some("Brightness: +000"));
let br_container = GtkBox::new(Orientation::Horizontal, 5);
br_container.append(&br_text);
@ -102,8 +109,11 @@ fn build_ui(app: &Application) {
{
br_slider.clone().connect_change_value(move |_, _, _| {
let value = br_slider.value();
br_text.set_text(&format!("Brightness: {:.2}", value));
let mut value = br_slider.value();
value = value.round();
br_slider.set_value(value);
br_text.set_text(&format!("Brightness: {:+04.0}", value));
edit_image(&mut image, value as i8);
// Продолжаем обработку события
Propagation::Proceed
@ -154,14 +164,11 @@ fn build_ui(app: &Application) {
window.show();
}
// fn edit_image(img: &mut RgbImage) {
// for (x, y, pixel) in img.enumerate_pixels_mut() {
// // Пример: инвертируем цвета
// pixel[0] = 255 - pixel[0]; // Красный
// pixel[1] = 255 - pixel[1]; // Зеленый
// pixel[2] = 255 - pixel[2]; // Синий
// }
// }
fn edit_image(img: &mut RgbImage, brightness: i8) {
for (x, y, pixel) in img.enumerate_pixels_mut() {
pixel[0] = (pixel[0] as i16 + brightness as i16) as u8;
}
}
// fn update_image(picture: &Picture, img: &DynamicImage) {
// img.save("tmp.png").unwrap();