77#include "py/runtime.h"
88#include "shared-bindings/tilepalettemapper/TilePaletteMapper.h"
99#include "shared-bindings/displayio/Palette.h"
10+ #include "shared-bindings/displayio/ColorConverter.h"
1011
1112void common_hal_tilepalettemapper_tilepalettemapper_construct (tilepalettemapper_tilepalettemapper_t * self ,
12- mp_obj_t palette , uint16_t input_color_count , uint16_t width , uint16_t height ) {
13+ mp_obj_t pixel_shader , uint16_t input_color_count , uint16_t width , uint16_t height ) {
1314
14- self -> palette = palette ;
15+ self -> pixel_shader = pixel_shader ;
1516 self -> width_in_tiles = width ;
1617 self -> height_in_tiles = height ;
1718 self -> input_color_count = input_color_count ;
1819 self -> needs_refresh = false;
1920 int mappings_len = width * height ;
20- self -> tile_mappings = (uint16_t * * )m_malloc (mappings_len * sizeof (uint16_t * ));
21+ self -> tile_mappings = (uint32_t * * )m_malloc (mappings_len * sizeof (uint32_t * ));
2122 for (int i = 0 ; i < mappings_len ; i ++ ) {
22- self -> tile_mappings [i ] = (uint16_t * )m_malloc (input_color_count * sizeof (uint16_t ));
23- for (uint16_t j = 0 ; j < input_color_count ; j ++ ) {
24- self -> tile_mappings [i ][j ] = j ;
23+ self -> tile_mappings [i ] = (uint32_t * )m_malloc (input_color_count * sizeof (uint32_t ));
24+ if (mp_obj_is_type (self -> pixel_shader , & displayio_palette_type )) {
25+ for (uint16_t j = 0 ; j < input_color_count ; j ++ ) {
26+ self -> tile_mappings [i ][j ] = j ;
27+ }
28+ } else if (mp_obj_is_type (self -> pixel_shader , & displayio_colorconverter_type )) {
29+ for (uint16_t j = 0 ; j < input_color_count ; j ++ ) {
30+ self -> tile_mappings [i ][j ] = 0 ;
31+ }
2532 }
2633 }
2734}
@@ -34,8 +41,8 @@ uint16_t common_hal_tilepalettemapper_tilepalettemapper_get_height(tilepalettema
3441 return self -> height_in_tiles ;
3542}
3643
37- mp_obj_t common_hal_tilepalettemapper_tilepalettemapper_get_palette (tilepalettemapper_tilepalettemapper_t * self ) {
38- return self -> palette ;
44+ mp_obj_t common_hal_tilepalettemapper_tilepalettemapper_get_pixel_shader (tilepalettemapper_tilepalettemapper_t * self ) {
45+ return self -> pixel_shader ;
3946}
4047
4148mp_obj_t common_hal_tilepalettemapper_tilepalettemapper_get_mapping (tilepalettemapper_tilepalettemapper_t * self , uint16_t x , uint16_t y ) {
@@ -48,25 +55,40 @@ mp_obj_t common_hal_tilepalettemapper_tilepalettemapper_get_mapping(tilepalettem
4855}
4956
5057void common_hal_tilepalettemapper_tilepalettemapper_set_mapping (tilepalettemapper_tilepalettemapper_t * self , uint16_t x , uint16_t y , size_t len , mp_obj_t * items ) {
51- uint32_t palette_len = common_hal_displayio_palette_get_len (self -> palette );
58+ uint32_t palette_max ;
59+ if (mp_obj_is_type (self -> pixel_shader , & displayio_palette_type )) {
60+ palette_max = common_hal_displayio_palette_get_len (self -> pixel_shader ) - 1 ;
61+ } else { // colorconverter type
62+ palette_max = 0xFFFFFF ;
63+ }
64+
5265 for (uint16_t i = 0 ; i < MIN (len , self -> input_color_count ); i ++ ) {
5366 int mapping_val = mp_arg_validate_type_int (items [i ], MP_QSTR_mapping_value );
54- mp_arg_validate_int_range (mapping_val , 0 , palette_len - 1 , MP_QSTR_mapping_value );
67+ mp_arg_validate_int_range (mapping_val , 0 , palette_max , MP_QSTR_mapping_value );
5568 self -> tile_mappings [y * self -> width_in_tiles + x ][i ] = mapping_val ;
5669 }
5770 self -> needs_refresh = true;
5871}
5972
6073void tilepalettemapper_tilepalettemapper_get_color (tilepalettemapper_tilepalettemapper_t * self , const _displayio_colorspace_t * colorspace , displayio_input_pixel_t * input_pixel , displayio_output_pixel_t * output_color , uint16_t x_tile_index , uint16_t y_tile_index ) {
6174 if (x_tile_index >= self -> width_in_tiles || y_tile_index >= self -> height_in_tiles ) {
62- displayio_palette_get_color (self -> palette , colorspace , input_pixel , output_color );
75+ if (mp_obj_is_type (self -> pixel_shader , & displayio_palette_type )) {
76+ displayio_palette_get_color (self -> pixel_shader , colorspace , input_pixel , output_color );
77+ } else if (mp_obj_is_type (self -> pixel_shader , & displayio_colorconverter_type )) {
78+ displayio_colorconverter_convert (self -> pixel_shader , colorspace , input_pixel , output_color );
79+ }
6380 return ;
6481 }
6582 uint16_t tile_index = y_tile_index * self -> width_in_tiles + x_tile_index ;
6683 uint32_t mapped_index = self -> tile_mappings [tile_index ][input_pixel -> pixel ];
6784 displayio_input_pixel_t tmp_pixel ;
6885 tmp_pixel .pixel = mapped_index ;
69- displayio_palette_get_color (self -> palette , colorspace , & tmp_pixel , output_color );
86+ if (mp_obj_is_type (self -> pixel_shader , & displayio_palette_type )) {
87+ displayio_palette_get_color (self -> pixel_shader , colorspace , & tmp_pixel , output_color );
88+ } else if (mp_obj_is_type (self -> pixel_shader , & displayio_colorconverter_type )) {
89+ displayio_colorconverter_convert (self -> pixel_shader , colorspace , & tmp_pixel , output_color );
90+ }
91+
7092}
7193
7294bool tilepalettemapper_tilepalettemapper_needs_refresh (tilepalettemapper_tilepalettemapper_t * self ) {
0 commit comments