Skip to content

Commit

Permalink
update image dependency to 0.24 and remove checked_int_cast
Browse files Browse the repository at this point in the history
close #54, close #59
  • Loading branch information
kennytm committed Dec 11, 2023
1 parent f20cdeb commit 10e6b4c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 41 deletions.
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
name = "qrcode"
description = "QR code encoder in Rust"
license = "MIT OR Apache-2.0"
version = "0.12.0"
edition = "2018"
version = "0.13.0"
edition = "2021"
authors = ["kennytm <kennytm@gmail.com>"]
keywords = ["qrcode"]
repository = "https://github.com/kennytm/qrcode-rust"
Expand All @@ -21,11 +21,10 @@ is-it-maintained-open-issues = { repository = "kennytm/qrcode-rust" }
maintenance = { status = "passively-maintained" }

[dependencies]
image = { version = "0.23", default-features = false, optional = true }
checked_int_cast = "1"
image = { version = "0.24", default-features = false, optional = true }

[dev-dependencies]
image = "0.23"
image = "0.24"

[features]
default = ["image", "svg"]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ Cargo.toml

```toml
[dependencies]
qrcode = "0.12"
qrcode = "0.13"
```

The default settings will depend on the `image` crate. If you don't need image generation capability, disable the `default-features`:

```toml
[dependencies]
qrcode = { version = "0.12", default-features = false }
qrcode = { version = "0.13", default-features = false }
```

Example
Expand Down
30 changes: 5 additions & 25 deletions src/cast.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
use std::fmt::Display;

#[cfg(debug_assertions)]
use checked_int_cast::CheckedIntCast;

pub trait Truncate {
fn truncate_as_u8(self) -> u8;
}
Expand All @@ -22,43 +17,28 @@ pub trait As {
fn as_isize(self) -> isize;
}

trait ExpectOrOverflow {
type Output;
fn expect_or_overflow<D: Display>(self, value: D, ty: &str) -> Self::Output;
}

impl<T> ExpectOrOverflow for Option<T> {
type Output = T;
fn expect_or_overflow<D: Display>(self, value: D, ty: &str) -> Self::Output {
match self {
Some(v) => v,
None => panic!("{} overflows {}", value, ty),
}
}
}

macro_rules! impl_as {
($ty:ty) => {
#[cfg(debug_assertions)]
impl As for $ty {
fn as_u16(self) -> u16 {
self.as_u16_checked().expect_or_overflow(self, "u16")
u16::try_from(self).unwrap()
}

fn as_i16(self) -> i16 {
self.as_i16_checked().expect_or_overflow(self, "i16")
i16::try_from(self).unwrap()
}

fn as_u32(self) -> u32 {
self.as_u32_checked().expect_or_overflow(self, "u32")
u32::try_from(self).unwrap()
}

fn as_usize(self) -> usize {
self.as_usize_checked().expect_or_overflow(self, "usize")
usize::try_from(self).unwrap()
}

fn as_isize(self) -> isize {
self.as_isize_checked().expect_or_overflow(self, "usize")
isize::try_from(self).unwrap()
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ pub use crate::types::{Color, EcLevel, QrResult, Version};

use crate::cast::As;
use crate::render::{Pixel, Renderer};
use checked_int_cast::CheckedIntCast;

/// The encoded QR code symbol.
#[derive(Clone)]
Expand Down Expand Up @@ -186,8 +185,8 @@ impl QrCode {
/// Checks whether a module at coordinate (x, y) is a functional module or
/// not.
pub fn is_functional(&self, x: usize, y: usize) -> bool {
let x = x.as_i16_checked().expect("coordinate is too large for QR code");
let y = y.as_i16_checked().expect("coordinate is too large for QR code");
let x = x.try_into().expect("coordinate is too large for QR code");
let y = y.try_into().expect("coordinate is too large for QR code");
canvas::is_functional(self.version, self.version.width(), x, y)
}

Expand Down Expand Up @@ -325,7 +324,7 @@ mod image_tests {
fn test_annex_i_qr_as_image() {
let code = QrCode::new(b"01234567").unwrap();
let image = code.render::<Luma<u8>>().build();
let expected = load_from_memory(include_bytes!("test_annex_i_qr_as_image.png")).unwrap().to_luma();
let expected = load_from_memory(include_bytes!("test_annex_i_qr_as_image.png")).unwrap().into_luma8();
assert_eq!(image.dimensions(), expected.dimensions());
assert_eq!(image.into_raw(), expected.into_raw());
}
Expand All @@ -339,7 +338,7 @@ mod image_tests {
.dark_color(Rgb([128, 0, 0]))
.light_color(Rgb([255, 255, 128]))
.build();
let expected = load_from_memory(include_bytes!("test_annex_i_micro_qr_as_image.png")).unwrap().to_rgb();
let expected = load_from_memory(include_bytes!("test_annex_i_micro_qr_as_image.png")).unwrap().into_rgb8();
assert_eq!(image.dimensions(), expected.dimensions());
assert_eq!(image.into_raw(), expected.into_raw());
}
Expand Down
14 changes: 10 additions & 4 deletions src/render/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
use crate::render::{Canvas, Pixel};
use crate::types::Color;

use image::{ImageBuffer, Luma, LumaA, Pixel as ImagePixel, Primitive, Rgb, Rgba};
use image::{ImageBuffer, Luma, LumaA, Primitive, Rgb, Rgba};

// need to keep using this macro to implement Pixel separately for each color model,
// otherwise we'll have conflicting impl with `impl Pixel for impl Element` 🤷
macro_rules! impl_pixel_for_image_pixel {
($p:ident<$s:ident>: $c:pat => $d:expr) => {
impl<$s: Primitive + 'static> Pixel for $p<$s> {
type Image = ImageBuffer<Self, Vec<S>>;
impl<$s> Pixel for $p<$s>
where
$s: Primitive + 'static,
$p<$s>: image::Pixel<Subpixel = $s>,
{
type Image = ImageBuffer<Self, Vec<$s>>;
type Canvas = (Self, Self::Image);

fn default_color(color: Color) -> Self {
Expand All @@ -25,7 +31,7 @@ impl_pixel_for_image_pixel! { LumaA<S>: p => [p, S::max_value()] }
impl_pixel_for_image_pixel! { Rgb<S>: p => [p, p, p] }
impl_pixel_for_image_pixel! { Rgba<S>: p => [p, p, p, S::max_value()] }

impl<P: ImagePixel + 'static> Canvas for (P, ImageBuffer<P, Vec<P::Subpixel>>) {
impl<P: image::Pixel + 'static> Canvas for (P, ImageBuffer<P, Vec<P::Subpixel>>) {
type Pixel = P;
type Image = ImageBuffer<P, Vec<P::Subpixel>>;

Expand Down

0 comments on commit 10e6b4c

Please sign in to comment.