@@ -2,7 +2,6 @@ extern crate proc_macro;
2
2
3
3
use image:: * ;
4
4
use proc_macro:: TokenStream ;
5
- use std:: collections:: HashMap ;
6
5
use std:: io:: Write ;
7
6
use syn:: { parse_macro_input, Ident , LitStr } ;
8
7
@@ -154,33 +153,29 @@ fn generate_bagl_glyph(frame: &GrayImage) -> Vec<u8> {
154
153
packed
155
154
}
156
155
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 ] ) ;
163
161
}
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 ) ;
168
163
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
- }
174
164
// 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 ;
177
172
}
178
173
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
+ }
184
179
}
185
180
186
181
let width = frame. width ( ) ;
@@ -224,10 +219,10 @@ fn generate_nbgl_glyph(frame: &mut GrayImage) -> (Vec<u8>, u8, bool) {
224
219
* pixel = Luma ( [ 0 ; 1 ] ) ;
225
220
}
226
221
} ) ;
227
- let ( packed, bpp) = image_to_packed_buffer ( & frame) ;
222
+ let ( packed, bpp) = image_to_packed_buffer ( frame) ;
228
223
return ( packed, bpp, false ) ;
229
224
}
230
- let ( packed, bpp) = image_to_packed_buffer ( & frame) ;
225
+ let ( packed, bpp) = image_to_packed_buffer ( frame) ;
231
226
let mut compressed_image: Vec < u8 > = Vec :: new ( ) ;
232
227
let mut full_uncompressed_size = packed. len ( ) ;
233
228
let mut i = 0 ;
0 commit comments