Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ default = [ "util" ]
[dependencies]
hidapi = "2.4"
log = "0.4.8"
image = "0.25.1"
image = "0.25.2"
imageproc = "0.24.0"
thiserror = "1.0.30"
ab_glyph = "0.2.25"
Expand Down
22 changes: 6 additions & 16 deletions src/images.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;

use image::codecs::jpeg::JpegEncoder;
use image::io::Reader;
use image::ImageReader;
use image::{imageops::FilterType, Pixel, Rgba};
use image::{DynamicImage, ExtendedColorType};

Expand All @@ -28,7 +28,7 @@ impl FromStr for Colour {

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() != 6 && s.len() != 8 {
return Err(format!("Expected colour in the hex form: RRGGBB"));
return Err("Expected colour in the hex form: RRGGBB".to_string());
}

let r =
Expand All @@ -43,7 +43,7 @@ impl FromStr for Colour {
}

/// Options for image loading and editing
#[derive(Debug)]
#[derive(Debug, Default)]
#[cfg_attr(feature = "structopt", derive(structopt::StructOpt))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct ImageOptions {
Expand All @@ -62,15 +62,6 @@ impl ImageOptions {
}
}

impl Default for ImageOptions {
fn default() -> Self {
Self {
background: None,
invert: false,
}
}
}

pub(crate) fn apply_transform(
image: DynamicImage,
rotation: Rotation,
Expand All @@ -82,13 +73,12 @@ pub(crate) fn apply_transform(
Rotation::Rot180 => image.rotate180(),
Rotation::Rot270 => image.rotate270(),
};
let image = match mirroring {
match mirroring {
Mirroring::None => image,
Mirroring::X => image.flipv(),
Mirroring::Y => image.fliph(),
Mirroring::Both => image.flipv().fliph(),
};
image
}
}

/// Load an image from a file, resize to defined x and y, and apply the provided options
Expand All @@ -102,7 +92,7 @@ pub(crate) fn load_image(
colour_order: ColourOrder,
) -> Result<Vec<u8>, Error> {
// Open image reader
let reader = match Reader::open(path) {
let reader = match ImageReader::open(path) {
Ok(v) => v,
Err(e) => {
error!("error loading file '{}': {:?}", path, e);
Expand Down
6 changes: 2 additions & 4 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum ImageMode {

/// Stream Deck color mode
#[derive(Debug, Clone, PartialEq)]
#[allow(clippy::upper_case_acronyms)]
pub(crate) enum ColourOrder {
RGB,
BGR,
Expand Down Expand Up @@ -159,10 +160,7 @@ impl Kind {
}

pub(crate) fn is_v2(&self) -> bool {
match self {
Kind::OriginalV2 | Kind::Xl | Kind::Mk2 => true,
_ => false,
}
matches!(self, Kind::OriginalV2 | Kind::Xl | Kind::Mk2)
}
}

Expand Down
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,17 @@ impl StreamDeck {
let mut image = vec![0u8; self.kind.image_size_bytes()];
let colour_order = self.kind.image_colour_order();

for i in 0..image.len() {
for (i, image_byte) in image.iter_mut().enumerate() {
match i % 3 {
0 => {
image[i] = match colour_order {
*image_byte = match colour_order {
ColourOrder::BGR => colour.b,
ColourOrder::RGB => colour.r,
}
}
1 => image[i] = colour.g,
1 => *image_byte = colour.g,
2 => {
image[i] = match colour_order {
*image_byte = match colour_order {
ColourOrder::BGR => colour.r,
ColourOrder::RGB => colour.b,
}
Expand Down Expand Up @@ -328,7 +328,7 @@ impl StreamDeck {
match pos {
TextPosition::Absolute { x, y } => {
let mut y = *y;
text.to_string().split("\n").for_each(|txt| {
text.split("\n").for_each(|txt| {
draw_text_mut(&mut image, colour, *x, y, opts.scale, font, txt);
y += (opts.scale.y * opts.line_height).round() as i32;
});
Expand Down Expand Up @@ -376,7 +376,7 @@ impl StreamDeck {
KeyDirection::LeftToRight => key + self.kind.key_index_offset(),
// The original Streamdeck uses 1-indexed right-to-left
KeyDirection::RightToLeft => {
let cols = self.kind.key_columns() as u8;
let cols = self.kind.key_columns();
let col = key % cols;
let row = key / cols;
row * cols + cols - col
Expand Down Expand Up @@ -480,12 +480,14 @@ impl StreamDeck {
}

/// TextPosition is how to position text via set_button_text
#[derive(Debug, Clone)]
pub enum TextPosition {
/// Absolute positioning
Absolute { x: i32, y: i32 },
}

/// Text Options provide values for text buttons
#[derive(Debug, Clone)]
pub struct TextOptions {
foreground: Colour,
background: Colour,
Expand Down Expand Up @@ -518,7 +520,7 @@ impl Default for TextOptions {
}

// Convert RGB image data to BGR
fn rgb_to_bgr(data: &mut Vec<u8>) {
fn rgb_to_bgr(data: &mut [u8]) {
for chunk in data.chunks_exact_mut(3) {
chunk.swap(0, 2);
}
Expand Down