Skip to content

Commit cdb3efd

Browse files
authored
Merge pull request #285 from LedgerHQ/y333/fix_include_gif
Invert image when bpp == 1
2 parents 47b71d0 + 354f04c commit cdb3efd

File tree

4 files changed

+25
-30
lines changed

4 files changed

+25
-30
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

include_gif/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "include_gif"
3-
version = "1.2.2"
3+
version = "1.2.3"
44
edition = "2021"
55
license.workspace = true
66
repository.workspace = true

include_gif/src/lib.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ extern crate proc_macro;
22

33
use image::*;
44
use proc_macro::TokenStream;
5-
use std::collections::HashMap;
65
use std::io::Write;
76
use syn::{parse_macro_input, Ident, LitStr};
87

@@ -154,33 +153,29 @@ fn generate_bagl_glyph(frame: &GrayImage) -> Vec<u8> {
154153
packed
155154
}
156155

157-
// Get the palette of colors of a grayscale image
158-
fn get_palette<'a>(img: &'a GrayImage) -> Vec<u8> {
159-
let mut palette = HashMap::new();
160-
// Count the number of occurrences of each color
161-
for &pixel in img.pixels() {
162-
*palette.entry(pixel[0]).or_insert(0) += 1;
156+
fn image_to_packed_buffer(frame: &mut GrayImage) -> (Vec<u8>, u8) {
157+
// Count the number of colors in the image (max 16 supported)
158+
let mut color_count = std::collections::HashSet::new();
159+
for pixel in frame.pixels() {
160+
color_count.insert(pixel.0[0]);
163161
}
164-
let palette: Vec<_> = palette.into_iter().collect();
165-
// Collect all colors in a vector
166-
palette.into_iter().map(|(luma, _)| luma).collect()
167-
}
162+
let mut colors = std::cmp::min(16u8, color_count.len() as u8);
168163

169-
fn image_to_packed_buffer(frame: &GrayImage) -> (Vec<u8>, u8) {
170-
let mut colors = get_palette(&frame).len() as u8;
171-
if colors > 16 {
172-
colors = 16;
173-
}
174164
// Round number of colors to a power of 2
175-
if !(colors != 0 && colors.count_ones() == 1) {
176-
colors = (2.0_f64.powf((colors as f64).log2().ceil())) as u8;
165+
colors = colors.next_power_of_two();
166+
167+
// Compute number of bits per pixel from number of colors (1, 2 or 4)
168+
let mut bits_per_pixel = std::cmp::min(4, (colors as f64).log(2.0).ceil() as u8);
169+
// 2 is not supported
170+
if bits_per_pixel == 2 {
171+
bits_per_pixel = 4;
177172
}
178173

179-
let mut bits_per_pixel: u8 = (colors as f32).log2().floor() as u8;
180-
match bits_per_pixel {
181-
0 => bits_per_pixel = 1,
182-
3 => bits_per_pixel = 4,
183-
_ => (),
174+
// Invert if bpp is 1
175+
if bits_per_pixel == 1 {
176+
for pixel in frame.pixels_mut() {
177+
pixel.0[0] = 255 - pixel.0[0];
178+
}
184179
}
185180

186181
let width = frame.width();
@@ -224,10 +219,10 @@ fn generate_nbgl_glyph(frame: &mut GrayImage) -> (Vec<u8>, u8, bool) {
224219
*pixel = Luma([0; 1]);
225220
}
226221
});
227-
let (packed, bpp) = image_to_packed_buffer(&frame);
222+
let (packed, bpp) = image_to_packed_buffer(frame);
228223
return (packed, bpp, false);
229224
}
230-
let (packed, bpp) = image_to_packed_buffer(&frame);
225+
let (packed, bpp) = image_to_packed_buffer(frame);
231226
let mut compressed_image: Vec<u8> = Vec::new();
232227
let mut full_uncompressed_size = packed.len();
233228
let mut i = 0;

ledger_device_sdk/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ledger_device_sdk"
3-
version = "1.24.4"
3+
version = "1.24.5"
44
authors = ["yhql", "yogh333", "agrojean-ledger", "kingofpayne"]
55
edition = "2021"
66
license.workspace = true
@@ -15,7 +15,7 @@ ledger_device_sdk = { path = ".", features = ["speculos"] }
1515
testmacro = { path = "../testmacro", version = "0.1.0"}
1616

1717
[dependencies]
18-
include_gif = {path = "../include_gif", version = "1.2.2"}
18+
include_gif = {path = "../include_gif", version = "1.2.3"}
1919
num-traits = { version = "0.2.14", default-features = false }
2020
rand_core = { version = "0.6.3", default-features = false }
2121
zeroize = { version = "1.6.0", default-features = false }

0 commit comments

Comments
 (0)