Skip to content

Commit e68b083

Browse files
committed
Optimize drawing loop
Signed-off-by: Cléo Rebert <cleo.rebert@gmail.com>
1 parent 17844d6 commit e68b083

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

qrcodegen-image/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [1.3.0](https://github.com/constantoine/totp-rs/releases/tag/qrcodegen-image%2Fv1.3.0) (19/01/2024)
4+
5+
### What's new
6+
7+
- `draw_canvas` is now 70%(!) faster on my machine after optimizing the "drawing_square" loop.
8+
39
## [1.2.0](https://github.com/constantoine/totp-rs/releases/tag/qrcodegen-image%2Fv1.2.0) (14/09/2023)
410

511
### What's new

qrcodegen-image/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "qrcodegen-image"
3-
version = "1.2.0"
3+
version = "1.3.0"
44
edition = "2021"
55
authors = ["Cleo Rebert <cleo.rebert@gmail.com>"]
66
rust-version = "1.61"

qrcodegen-image/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub fn draw_canvas(qr: qrcodegen::QrCode) -> image::ImageBuffer<Luma<u8>, Vec<u8
1818
*pixel = Luma([255]);
1919
}
2020

21+
let raw = canvas.as_mut();
22+
2123
// The QR inside the white border
2224
for x_qr in 0..size {
2325
for y_qr in 0..size {
@@ -32,10 +34,9 @@ pub fn draw_canvas(qr: qrcodegen::QrCode) -> image::ImageBuffer<Luma<u8>, Vec<u8
3234
let y_start = y_qr * 8 + 8 * 4;
3335

3436
// Draw a 8-pixels-wide square
35-
for x_img in x_start..x_start + 8 {
36-
for y_img in y_start..y_start + 8 {
37-
canvas.put_pixel(x_img, y_img, Luma([val]));
38-
}
37+
for y_img in y_start..y_start + 8 {
38+
let start = (x_start + y_img * image_size) as usize;
39+
raw[start..start + 8].copy_from_slice(&[val; 8]);
3940
}
4041
}
4142
}
@@ -99,5 +100,5 @@ pub fn draw_png(text: &str) -> Result<Vec<u8>, String> {
99100
#[cfg(feature = "base64")]
100101
pub fn draw_base64(text: &str) -> Result<String, String> {
101102
use base64::{engine::general_purpose, Engine as _};
102-
Ok(draw_png(text).map(|vec| general_purpose::STANDARD.encode(vec))?)
103+
draw_png(text).map(|vec| general_purpose::STANDARD.encode(vec))
103104
}

0 commit comments

Comments
 (0)