lutgen-studio 0.1.0

Offical GUI for Lutgen, the best way to apply popular colorschemes to any image or wallpaper!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#![warn(clippy::all)]
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release

use egui::Widget;
use log::{debug, error};
use strum::VariantArray;

use crate::palette::DynamicPalette;
use crate::state::{LutAlgorithm, UiState};
use crate::worker::{BackendEvent, LutAlgorithmArgs, Worker, WorkerHandle};

mod palette;
mod state;
mod worker;

pub struct App {
    state: UiState,
    worker: WorkerHandle,
}

impl App {
    pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
        egui_extras::install_image_loaders(&cc.egui_ctx);

        cc.egui_ctx.set_visuals(egui::Visuals {
            dark_mode: true,
            // bg
            window_fill: egui::Color32::from_rgb(0x16, 0x16, 0x16),
            code_bg_color: egui::Color32::from_rgb(0x0b, 0x0b, 0x0b),
            faint_bg_color: egui::Color32::from_rgb(0x26, 0x26, 0x26),
            extreme_bg_color: egui::Color32::from_rgb(0x39, 0x39, 0x39),
            // fg
            override_text_color: Some(egui::Color32::from_rgb(0xf4, 0xf4, 0xf4)),
            hyperlink_color: egui::Color32::from_rgb(0x45, 0x89, 0xff),
            warn_fg_color: egui::Color32::from_rgb(0xfd, 0xdc, 0x69),
            error_fg_color: egui::Color32::from_rgb(0xfa, 0x4d, 0x56),
            ..Default::default()
        });

        // Load previous app state (if any).
        let state = cc
            .storage
            .and_then(|storage| eframe::get_value::<UiState>(storage, eframe::APP_KEY))
            .unwrap_or_default();

        // Spawn background worker thread
        let worker = Worker::spawn(cc.egui_ctx.clone());

        let this = Self { state, worker };

        // Load last opened image and apply settings
        if let Some(path) = &this.state.current_image {
            this.worker.load_file(path);
            this.apply();
        }

        this
    }

    /// Handle any incoming events from the backend
    fn poll_worker_events(&mut self, ctx: &egui::Context) {
        if let Some(event) = self.worker.poll_event() {
            self.state.last_event = event.to_string();
            debug!("{}", self.state.last_event);

            match event {
                BackendEvent::Error(e) => {
                    error!("{e}");
                },
                BackendEvent::PickFile(path_buf, _) => {
                    self.state.current_image = Some(path_buf);
                },
                BackendEvent::SetImage(image, width, height, uri) => {
                    let ci = egui::ColorImage::from_rgba_unmultiplied(
                        [height as usize, width as usize],
                        &image,
                    );
                    let texture = ctx.load_texture(uri, ci, egui::TextureOptions::default());
                    self.state.image_texture = Some(texture);
                },
                _ => {},
            }
        }
    }

    /// Get the currently set lut arguments for worker requests
    fn apply(&self) {
        let args = match self.state.current_alg {
            LutAlgorithm::GaussianRbf => LutAlgorithmArgs::GaussianRbf {
                rbf: self.state.common_rbf,
                args: self.state.guassian_rbf,
            },
            LutAlgorithm::ShepardsMethod => LutAlgorithmArgs::ShepardsMethod {
                rbf: self.state.common_rbf,
                args: self.state.shepards_method,
            },
            LutAlgorithm::GaussianSampling => LutAlgorithmArgs::GaussianSampling {
                args: self.state.guassian_sampling,
            },
            LutAlgorithm::NearestNeighbor => LutAlgorithmArgs::NearestNeighbor,
        };
        self.worker
            .apply_palette(self.state.palette.clone(), self.state.common, args);
    }
}

impl eframe::App for App {
    /// Called by the frame work to save state before shutdown.
    fn save(&mut self, storage: &mut dyn eframe::Storage) {
        eframe::set_value(storage, eframe::APP_KEY, &self.state)
    }

    /// Called each time the UI needs repainting, which may be many times per second.
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        self.poll_worker_events(ctx);

        egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
            egui::menu::bar(ui, |ui| {
                ui.label("Lutgen Studio");
                ui.add_space(5.);
                ui.menu_button("File", |ui| {
                    if ui.button("Open").clicked() {
                        self.worker.pick_file();
                    }
                    if ui.button("Save As").clicked() {
                        self.worker.save_as();
                    }
                    if ui.button("Quit").clicked() {
                        ctx.send_viewport_cmd(egui::ViewportCommand::Close);
                    }
                });

                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    egui::widgets::global_theme_preference_buttons(ui);
                });
            });
        });

        // side panel for lut args
        egui::SidePanel::left("args")
            .resizable(false)
            .show(ctx, |ui| {
                egui::ScrollArea::vertical().show(ui, |ui| {
                    ui.horizontal_wrapped(|ui| {
                        ui.label("Palette:");
                        egui::ComboBox::from_id_salt("palette")
                            .width(150.)
                            .selected_text(format!("{}", self.state.palette_selection))
                            .show_ui(ui, |ui| {
                                let val = ui.selectable_value(
                                    &mut self.state.palette_selection,
                                    palette::DynamicPalette::Custom,
                                    "- custom -",
                                );
                                val.gained_focus()
                                    .then(|| ui.scroll_to_cursor(Some(egui::Align::Center)));

                                for p in lutgen_palettes::Palette::VARIANTS {
                                    let val = ui.selectable_value(
                                        &mut self.state.palette_selection,
                                        palette::DynamicPalette::Builtin(*p),
                                        p.to_string(),
                                    );
                                    val.clicked().then(|| {
                                        self.state.palette =
                                            self.state.palette_selection.get().to_vec();
                                        self.apply()
                                    });
                                    val.gained_focus()
                                        .then(|| ui.scroll_to_cursor(Some(egui::Align::Center)));
                                }
                            });
                    });

                    ui.group(|ui| {
                        ui.horizontal_wrapped(|ui| {
                            let mut res = Vec::new();
                            for color in self.state.palette.iter_mut() {
                                res.push(egui::widgets::color_picker::color_edit_button_srgb(
                                    ui, color,
                                ));
                            }
                            for (i, res) in res.iter().enumerate() {
                                if res.changed() {
                                    self.state.palette_selection = DynamicPalette::Custom;
                                    self.apply();
                                }
                                if res.secondary_clicked() {
                                    self.state.palette.remove(i);
                                    self.state.palette_selection = DynamicPalette::Custom;
                                    self.apply();
                                }
                            }

                            if egui::Button::new("+")
                                .min_size(egui::vec2(40., 0.))
                                .ui(ui)
                                .clicked()
                            {
                                self.state.palette.push([0u8; 3]);
                            };
                        });
                    });

                    ui.separator();

                    egui::ComboBox::from_id_salt("algorithm")
                        .width(200.)
                        .selected_text(format!("{:?}", self.state.current_alg))
                        .show_ui(ui, |ui| {
                            for alg in LutAlgorithm::VARIANTS {
                                let val = ui.selectable_value(
                                    &mut self.state.current_alg,
                                    *alg,
                                    alg.to_string(),
                                );
                                val.clicked().then(|| self.apply());
                                val.gained_focus()
                                    .then(|| ui.scroll_to_cursor(Some(egui::Align::Center)));
                            }
                        });

                    ui.group(|ui| {
                        ui.heading("Common Arguments");
                        ui.add_space(10.);

                        ui.label("Hald-Clut Level");
                        let res = ui.add(egui::Slider::new(&mut self.state.common.level, 4..=16));
                        if res.drag_stopped() || res.lost_focus() {
                            self.apply()
                        }

                        ui.label("Luminosity Factor");
                        let res = ui.add(egui::Slider::new(
                            &mut self.state.common.lum_factor.0,
                            0.0001..=2.,
                        ));
                        if res.drag_stopped() || res.lost_focus() {
                            self.apply()
                        }

                        match self.state.current_alg {
                            LutAlgorithm::GaussianRbf | LutAlgorithm::ShepardsMethod => {
                                ui.separator();
                                ui.heading("Rbf Arguments");
                                ui.add_space(10.);

                                ui.toggle_value(
                                    &mut self.state.common_rbf.preserve,
                                    "Preserve Luminosity",
                                )
                                .changed()
                                .then(|| self.apply());

                                ui.label("Nearest Colors");
                                let res = ui.add(
                                    egui::Slider::new(&mut self.state.common_rbf.nearest, 0..=32)
                                        .step_by(1.),
                                );
                                if res.drag_stopped() || res.lost_focus() {
                                    self.apply()
                                }
                            },
                            _ => {},
                        }

                        match self.state.current_alg {
                            LutAlgorithm::GaussianRbf => {
                                ui.separator();
                                ui.heading("Gaussian Arguments");
                                ui.add_space(10.);

                                ui.label("Shape");
                                let res = ui.add(egui::Slider::new(
                                    &mut self.state.guassian_rbf.shape.0,
                                    0.0001..=512.,
                                ));
                                if res.drag_stopped() || res.lost_focus() {
                                    self.apply()
                                }
                            },
                            LutAlgorithm::ShepardsMethod => {
                                ui.separator();
                                ui.heading("Shepard's Method Arguments");
                                ui.add_space(10.);

                                ui.label("Power");
                                let res = ui.add(egui::Slider::new(
                                    &mut self.state.shepards_method.power.0,
                                    0.0001..=512.,
                                ));
                                if res.drag_stopped() || res.lost_focus() {
                                    self.apply()
                                }
                            },
                            LutAlgorithm::GaussianSampling => {
                                ui.separator();
                                ui.heading("Guassian Sampling Arguments");
                                ui.add_space(10.);

                                ui.label("Mean");
                                let res = ui.add(egui::Slider::new(
                                    &mut self.state.guassian_sampling.mean.0,
                                    -128.0..=128.,
                                ));
                                if res.drag_stopped() || res.lost_focus() {
                                    self.apply()
                                }

                                ui.label("Standard Deviation");
                                let res = ui.add(egui::Slider::new(
                                    &mut self.state.guassian_sampling.std_dev.0,
                                    1.0..=128.,
                                ));
                                if res.drag_stopped() || res.lost_focus() {
                                    self.apply()
                                }

                                ui.label("Iterations");
                                let res = ui.add(egui::Slider::new(
                                    &mut self.state.guassian_sampling.iterations,
                                    1..=1024,
                                ));
                                if res.drag_stopped() || res.lost_focus() {
                                    self.apply()
                                }

                                ui.label("RNG Seed");
                                let res = ui.add(egui::Slider::new(
                                    &mut self.state.guassian_sampling.seed,
                                    0..=u64::MAX,
                                ));
                                if res.drag_stopped() || res.lost_focus() {
                                    self.apply()
                                }
                            },
                            _ => {},
                        }
                    });
                });
            });

        // statusline with events and other info
        egui::TopBottomPanel::bottom("statusline").show(ctx, |ui| {
            ui.label(&self.state.last_event);
        });

        // main app panel
        egui::CentralPanel::default().show(ctx, |ui| {
            // current image
            ui.vertical_centered(|ui| {
                let available_size = ui.available_size();

                let rect = if let Some(texture) = &self.state.image_texture {
                    ui.add(
                        egui::Image::from_texture(texture)
                            .max_size(available_size)
                            .fit_to_exact_size(available_size)
                            .corner_radius(10.0),
                    )
                    .rect
                } else {
                    let (rect, _response) =
                        ui.allocate_exact_size(available_size, egui::Sense::hover());
                    ui.painter().text(
                        rect.center(),
                        egui::Align2::CENTER_CENTER,
                        "No image loaded",
                        egui::FontId::default(),
                        egui::Color32::GRAY,
                    );
                    rect
                };
                // paint border
                ui.painter().rect_stroke(
                    rect,
                    10.0,
                    egui::Stroke::new(1.0, egui::Color32::GRAY),
                    egui::StrokeKind::Middle,
                );
            });

            ui.with_layout(egui::Layout::bottom_up(egui::Align::RIGHT), |ui| {
                egui::warn_if_debug_build(ui);
            });
        });
    }
}
fn main() -> eframe::Result {
    env_logger::init();
    let native_options = eframe::NativeOptions {
        viewport: egui::ViewportBuilder::default()
            .with_inner_size([400.0, 300.0])
            .with_min_inner_size([300.0, 220.0])
            .with_icon(
                eframe::icon_data::from_png_bytes(&include_bytes!("../assets/logo.png")[..])
                    .expect("Failed to load icon"),
            ),
        persist_window: true,
        centered: true,
        ..Default::default()
    };
    eframe::run_native(
        "lutgen-studio",
        native_options,
        Box::new(|cc| Ok(Box::new(App::new(cc)))),
    )
}