25
25
*/
26
26
27
27
#include "shared-bindings/displayio/OnDiskBitmap.h"
28
+ #include "shared-bindings/displayio/ColorConverter.h"
29
+ #include "shared-bindings/displayio/Palette.h"
30
+ #include "shared-module/displayio/ColorConverter.h"
31
+ #include "shared-module/displayio/Palette.h"
28
32
29
33
#include <string.h>
30
34
@@ -63,6 +67,11 @@ void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self,
63
67
self -> width = read_word (bmp_header , 9 );
64
68
self -> height = read_word (bmp_header , 11 );
65
69
70
+ displayio_colorconverter_t * colorconverter = m_new_obj (displayio_colorconverter_t );
71
+ colorconverter -> base .type = & displayio_colorconverter_type ;
72
+ common_hal_displayio_colorconverter_construct (colorconverter , false, DISPLAYIO_COLORSPACE_RGB888 );
73
+ self -> colorconverter = colorconverter ;
74
+
66
75
if (bits_per_pixel == 16 ) {
67
76
if (((header_size >= 56 )) || (self -> bitfield_compressed )) {
68
77
self -> r_bitmask = read_word (bmp_header , 27 );
@@ -74,25 +83,41 @@ void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self,
74
83
self -> g_bitmask = 0x3e0 ;
75
84
self -> b_bitmask = 0x1f ;
76
85
}
77
- } else if (indexed && self -> bits_per_pixel != 1 ) {
86
+ } else if (indexed ) {
78
87
if (number_of_colors == 0 ) {
79
88
number_of_colors = 1 << bits_per_pixel ;
80
89
}
81
- uint16_t palette_size = number_of_colors * sizeof (uint32_t );
82
- uint16_t palette_offset = 0xe + header_size ;
83
90
84
- self -> palette_data = m_malloc (palette_size , false);
91
+ displayio_palette_t * palette = m_new_obj (displayio_palette_t );
92
+ palette -> base .type = & displayio_palette_type ;
93
+ common_hal_displayio_palette_construct (palette , number_of_colors );
85
94
86
- f_rewind (& self -> file -> fp );
87
- f_lseek (& self -> file -> fp , palette_offset );
95
+ if (number_of_colors > 1 ) {
96
+ uint16_t palette_size = number_of_colors * sizeof (uint32_t );
97
+ uint16_t palette_offset = 0xe + header_size ;
88
98
89
- UINT palette_bytes_read ;
90
- if (f_read (& self -> file -> fp , self -> palette_data , palette_size , & palette_bytes_read ) != FR_OK ) {
91
- mp_raise_OSError (MP_EIO );
92
- }
93
- if (palette_bytes_read != palette_size ) {
94
- mp_raise_ValueError (translate ("Unable to read color palette data" ));
99
+ uint32_t * palette_data = m_malloc (palette_size , false);
100
+
101
+ f_rewind (& self -> file -> fp );
102
+ f_lseek (& self -> file -> fp , palette_offset );
103
+
104
+ UINT palette_bytes_read ;
105
+ if (f_read (& self -> file -> fp , palette_data , palette_size , & palette_bytes_read ) != FR_OK ) {
106
+ mp_raise_OSError (MP_EIO );
107
+ }
108
+ if (palette_bytes_read != palette_size ) {
109
+ mp_raise_ValueError (translate ("Unable to read color palette data" ));
110
+ }
111
+ for (uint16_t i = 0 ; i < palette_size ; i ++ ) {
112
+ common_hal_displayio_palette_set_color (palette , i , palette_data [i ]);
113
+ }
114
+ m_free (palette_data );
115
+ } else {
116
+ common_hal_displayio_palette_set_color (palette , 0 , 0x0 );
117
+ common_hal_displayio_palette_set_color (palette , 1 , 0xffffff );
95
118
}
119
+ self -> palette = palette ;
120
+
96
121
} else if (!(header_size == 12 || header_size == 40 || header_size == 108 || header_size == 124 )) {
97
122
mp_raise_ValueError_varg (translate ("Only Windows format, uncompressed BMP supported: given header size is %d" ), header_size );
98
123
}
@@ -148,15 +173,7 @@ uint32_t common_hal_displayio_ondiskbitmap_get_pixel(displayio_ondiskbitmap_t *s
148
173
uint8_t offset = (x % pixels_per_byte ) * self -> bits_per_pixel ;
149
174
uint8_t mask = (1 << self -> bits_per_pixel ) - 1 ;
150
175
151
- uint8_t index = (pixel_data >> ((8 - self -> bits_per_pixel ) - offset )) & mask ;
152
- if (self -> bits_per_pixel == 1 ) {
153
- if (index == 1 ) {
154
- return 0xFFFFFF ;
155
- } else {
156
- return 0x000000 ;
157
- }
158
- }
159
- return self -> palette_data [index ];
176
+ return (pixel_data >> ((8 - self -> bits_per_pixel ) - offset )) & mask ;
160
177
} else if (bytes_per_pixel == 2 ) {
161
178
if (self -> g_bitmask == 0x07e0 ) { // 565
162
179
red = ((pixel_data & self -> r_bitmask ) >> 11 );
@@ -185,3 +202,7 @@ uint16_t common_hal_displayio_ondiskbitmap_get_height(displayio_ondiskbitmap_t *
185
202
uint16_t common_hal_displayio_ondiskbitmap_get_width (displayio_ondiskbitmap_t * self ) {
186
203
return self -> width ;
187
204
}
205
+
206
+ mp_obj_t common_hal_displayio_ondiskbitmap_get_pixel_shader (displayio_ondiskbitmap_t * self ) {
207
+ return MP_OBJ_FROM_PTR (self -> pixel_shader_base );
208
+ }
0 commit comments