Skip to content

Commit

Permalink
added cw/ccw 90deg rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
sonodima committed Sep 4, 2024
1 parent 2ed2491 commit 034b0f3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
38 changes: 35 additions & 3 deletions blurthing/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@ impl BlurThing {
self.state.components.1 = y;
self.compute_and_apply_blurhash();
}
Interaction::RotateCW => {
self.state.rotation = (self.state.rotation + 1) % 4;
self.compute_and_apply_blurhash();
self.history.push(self.state.clone());
}
Interaction::RotateCCW => {
self.state.rotation = (self.state.rotation + 3) % 4;
self.compute_and_apply_blurhash();
self.history.push(self.state.clone());
}
Interaction::UpBlur(blur) => {
self.state.blur = blur;
self.compute_and_apply_blurhash();
Expand Down Expand Up @@ -324,15 +334,22 @@ impl BlurThing {
.as_ref()
.ok_or_else(|| anyhow!("source image is not available"))?;

let buffer = img
let rotated = match self.state.rotation {
1 => &img.rotate90(),
2 => &img.rotate180(),
3 => &img.rotate270(),
_ => img,
};

let buffer = rotated
.blur(self.state.blur as f32)
.huerotate(self.state.hue_rotate)
.adjust_contrast(self.state.contrast as f32)
.brighten(self.state.brightness * 2)
.to_rgba8()
.to_vec();

let (width, height) = img.dimensions();
let (width, height) = rotated.dimensions();
let (x, y) = self.state.components;
// Encode the blurhash and decode it to a preview image for display.
let hash = blurhash::encode(x, y, width, height, &buffer)
Expand Down Expand Up @@ -449,6 +466,20 @@ impl BlurThing {
.on_release(Interaction::SaveParameters),
);

let tools = Row::new()
.push(
Column::new().push(Text::new("Rotation")).push(
Text::new("Rotate the input image by 90 degrees")
.style(styles::Text::Subtle)
.size(12),
),
)
.push(Space::with_width(Length::Fill))
.push(Button::new("↻").on_press(Interaction::RotateCW))
.push(Button::new("↺").on_press(Interaction::RotateCCW))
.spacing(8)
.padding([0, 0, 4, 0]);

let smoothness = Column::new()
.push(Text::new("Smoothness"))
.push(
Expand Down Expand Up @@ -495,13 +526,14 @@ impl BlurThing {
.size(12),
)
.push(
Slider::new(-100..=100, self.state.contrast, Interaction::UpContrast)
Slider::new(-40..=220, self.state.contrast, Interaction::UpContrast)
.on_release(Interaction::SaveParameters),
);

Column::new()
.push(x_components)
.push(y_components)
.push(tools)
.push(smoothness)
.push(hue_rotation)
.push(brightness)
Expand Down
2 changes: 2 additions & 0 deletions blurthing/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum Interaction {
SaveParameters,
UpX(u32),
UpY(u32),
RotateCW,
RotateCCW,
UpBlur(i32),
UpHue(i32),
UpBrightness(i32),
Expand Down
4 changes: 2 additions & 2 deletions blurthing/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[derive(Debug, Clone)]
pub struct State {
pub components: (u32, u32),
pub rotation: i8,
pub blur: i32,

pub hue_rotate: i32,
pub brightness: i32,
pub contrast: i32,
Expand All @@ -12,8 +12,8 @@ impl Default for State {
fn default() -> Self {
Self {
components: (4, 3),
rotation: 0,
blur: 0,

hue_rotate: 0,
brightness: 0,
contrast: 0,
Expand Down

0 comments on commit 034b0f3

Please sign in to comment.