Move color palette code to helper methods

This commit is contained in:
Kwabena W. Agyeman 2020-12-30 12:25:46 -08:00
parent 8ccc9f1011
commit 43aef07ae6
5 changed files with 79 additions and 144 deletions

View File

@ -599,48 +599,8 @@ mp_obj_t py_fir_draw_ir(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "0 <= alpha <= 256!")); nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "0 <= alpha <= 256!"));
} }
const uint16_t *color_palette = rainbow_table; const uint16_t *color_palette = py_helper_keyword_color_palette(n_args, args, offset + 5, kw_args, rainbow_table);
int palette; const uint8_t *alpha_palette = py_helper_keyword_alpha_palette(n_args, args, offset + 6, kw_args, NULL);
uint arg_index = offset + 5;
mp_map_elem_t *kw_arg = mp_map_lookup(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_color_palette), MP_MAP_LOOKUP);
if (kw_arg && MP_OBJ_IS_TYPE(kw_arg->value, mp_const_none)) {
color_palette = NULL;
} else if ((n_args > arg_index) && MP_OBJ_IS_TYPE(args[arg_index], mp_const_none)) {
color_palette = NULL;
} else if (py_helper_keyword_int_maybe(n_args, args, arg_index, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_color_palette), &palette)) {
if (palette == COLOR_PALETTE_RAINBOW) {
color_palette = rainbow_table;
} else if (palette == COLOR_PALETTE_IRONBOW) {
color_palette = ironbow_table;
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid pre-defined color palette!"));
}
} else {
image_t *arg_color_palette = py_helper_keyword_to_image_mutable_color_palette(n_args, args, arg_index, kw_args);
if (arg_color_palette) {
if (arg_color_palette->bpp != IMAGE_BPP_RGB565) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Color palette must be RGB565!"));
}
if ((arg_color_palette->w * arg_color_palette->h) != 256) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Color palette must be 256 pixels!"));
}
color_palette = (uint16_t *) arg_color_palette->data;
}
}
const uint8_t *alpha_palette = NULL;
image_t *arg_alpha_palette = py_helper_keyword_to_image_mutable_alpha_palette(n_args, args, offset + 6, kw_args);
if (arg_alpha_palette) {
if (arg_alpha_palette->bpp != IMAGE_BPP_GRAYSCALE) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Alpha palette must be GRAYSCALE!"));
}
if ((arg_alpha_palette->w * arg_alpha_palette->h) != 256) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Alpha palette must be 256 pixels!"));
}
alpha_palette = (uint8_t *) arg_alpha_palette->data;
}
image_hint_t hint = py_helper_keyword_int(n_args, args, offset + 7, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_hint), 0); image_hint_t hint = py_helper_keyword_int(n_args, args, offset + 7, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_hint), 0);
@ -740,49 +700,8 @@ mp_obj_t py_fir_snapshot(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "0 <= alpha <= 256!")); nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "0 <= alpha <= 256!"));
} }
const uint16_t *color_palette = rainbow_table; const uint16_t *color_palette = py_helper_keyword_color_palette(n_args, args, 8, kw_args, rainbow_table);
int palette; const uint8_t *alpha_palette = py_helper_keyword_alpha_palette(n_args, args, 9, kw_args, NULL);
uint arg_index = 8;
mp_map_elem_t *kw_arg = mp_map_lookup(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_color_palette), MP_MAP_LOOKUP);
if (kw_arg && MP_OBJ_IS_TYPE(kw_arg->value, mp_const_none)) {
color_palette = NULL;
} else if ((n_args > arg_index) && MP_OBJ_IS_TYPE(args[arg_index], mp_const_none)) {
color_palette = NULL;
} else if (py_helper_keyword_int_maybe(n_args, args, arg_index, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_color_palette), &palette)) {
if (palette == COLOR_PALETTE_RAINBOW) {
color_palette = rainbow_table;
} else if (palette == COLOR_PALETTE_IRONBOW) {
color_palette = ironbow_table;
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid pre-defined color palette!"));
}
} else {
image_t *arg_color_palette = py_helper_keyword_to_image_mutable_color_palette(n_args, args, arg_index, kw_args);
if (arg_color_palette) {
if (arg_color_palette->bpp != IMAGE_BPP_RGB565) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Color palette must be RGB565!"));
}
if ((arg_color_palette->w * arg_color_palette->h) != 256) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Color palette must be 256 pixels!"));
}
color_palette = (uint16_t *) arg_color_palette->data;
}
}
const uint8_t *alpha_palette = NULL;
image_t *arg_alpha_palette = py_helper_keyword_to_image_mutable_alpha_palette(n_args, args, 9, kw_args);
if (arg_alpha_palette) {
if (arg_alpha_palette->bpp != IMAGE_BPP_GRAYSCALE) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Alpha palette must be GRAYSCALE!"));
}
if ((arg_alpha_palette->w * arg_alpha_palette->h) != 256) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Alpha palette must be 256 pixels!"));
}
alpha_palette = (uint8_t *) arg_alpha_palette->data;
}
image_hint_t hint = py_helper_keyword_int(n_args, args, 10, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_hint), 0); image_hint_t hint = py_helper_keyword_int(n_args, args, 10, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_hint), 0);

View File

@ -407,6 +407,73 @@ mp_obj_t py_helper_keyword_object(uint n_args, const mp_obj_t *args,
} }
} }
const uint16_t *py_helper_keyword_color_palette(uint n_args, const mp_obj_t *args,
uint arg_index, mp_map_t *kw_args, const uint16_t *default_color_palette)
{
int palette;
mp_map_elem_t *kw_arg =
mp_map_lookup(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_color_palette), MP_MAP_LOOKUP);
if (kw_arg && MP_OBJ_IS_TYPE(kw_arg->value, mp_const_none)) {
default_color_palette = NULL;
} else if ((n_args > arg_index) && MP_OBJ_IS_TYPE(args[arg_index], mp_const_none)) {
default_color_palette = NULL;
} else if (py_helper_keyword_int_maybe(n_args, args, arg_index, kw_args,
MP_OBJ_NEW_QSTR(MP_QSTR_color_palette), &palette)) {
if (palette == COLOR_PALETTE_RAINBOW) {
default_color_palette = rainbow_table;
} else if (palette == COLOR_PALETTE_IRONBOW) {
default_color_palette = ironbow_table;
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"Invalid pre-defined color palette!"));
}
} else {
image_t *arg_color_palette =
py_helper_keyword_to_image_mutable_color_palette(n_args, args, arg_index, kw_args);
if (arg_color_palette) {
if (arg_color_palette->bpp != IMAGE_BPP_RGB565) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"Color palette must be RGB565!"));
}
if ((arg_color_palette->w * arg_color_palette->h) != 256) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"Color palette must be 256 pixels!"));
}
default_color_palette = (uint16_t *) arg_color_palette->data;
}
}
return default_color_palette;
}
const uint8_t *py_helper_keyword_alpha_palette(uint n_args, const mp_obj_t *args,
uint arg_index, mp_map_t *kw_args, const uint8_t *default_alpha_palette)
{
image_t *arg_alpha_palette =
py_helper_keyword_to_image_mutable_alpha_palette(n_args, args, 9, kw_args);
if (arg_alpha_palette) {
if (arg_alpha_palette->bpp != IMAGE_BPP_GRAYSCALE) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"Alpha palette must be GRAYSCALE!"));
}
if ((arg_alpha_palette->w * arg_alpha_palette->h) != 256) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"Alpha palette must be 256 pixels!"));
}
default_alpha_palette = (uint8_t *) arg_alpha_palette->data;
}
return default_alpha_palette;
}
bool py_helper_is_equal_to_framebuffer(image_t *img) bool py_helper_is_equal_to_framebuffer(image_t *img)
{ {
return framebuffer_get_buffer() == img->data; return framebuffer_get_buffer() == img->data;

View File

@ -11,7 +11,6 @@
#ifndef __PY_HELPER_H__ #ifndef __PY_HELPER_H__
#define __PY_HELPER_H__ #define __PY_HELPER_H__
#include "imlib.h" #include "imlib.h"
extern const mp_obj_fun_builtin_var_t py_func_unavailable_obj; extern const mp_obj_fun_builtin_var_t py_func_unavailable_obj;
image_t *py_helper_arg_to_image_mutable(const mp_obj_t arg); image_t *py_helper_arg_to_image_mutable(const mp_obj_t arg);
image_t *py_helper_arg_to_image_mutable_bayer(const mp_obj_t arg); image_t *py_helper_arg_to_image_mutable_bayer(const mp_obj_t arg);
@ -55,6 +54,10 @@ int py_helper_arg_to_ksize(const mp_obj_t arg);
int py_helper_ksize_to_n(int ksize); int py_helper_ksize_to_n(int ksize);
mp_obj_t py_helper_keyword_object(uint n_args, const mp_obj_t *args, mp_obj_t py_helper_keyword_object(uint n_args, const mp_obj_t *args,
uint arg_index, mp_map_t *kw_args, mp_obj_t kw, mp_obj_t default_val); uint arg_index, mp_map_t *kw_args, mp_obj_t kw, mp_obj_t default_val);
const uint16_t *py_helper_keyword_color_palette(uint n_args, const mp_obj_t *args,
uint arg_index, mp_map_t *kw_args, const uint16_t *default_color_palette);
const uint8_t *py_helper_keyword_alpha_palette(uint n_args, const mp_obj_t *args,
uint arg_index, mp_map_t *kw_args, const uint8_t *default_alpha_palette);
bool py_helper_is_equal_to_framebuffer(image_t *img); bool py_helper_is_equal_to_framebuffer(image_t *img);
void py_helper_update_framebuffer(image_t *img); void py_helper_update_framebuffer(image_t *img);
void py_helper_set_to_framebuffer(image_t *img); void py_helper_set_to_framebuffer(image_t *img);

View File

@ -1890,35 +1890,8 @@ STATIC mp_obj_t py_image_draw_image(uint n_args, const mp_obj_t *args, mp_map_t
int arg_alpha = py_helper_keyword_int(n_args, args, offset + 4, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_alpha), 256); int arg_alpha = py_helper_keyword_int(n_args, args, offset + 4, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_alpha), 256);
if ((arg_alpha < 0) || (256 < arg_alpha)) nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "0 <= alpha <= 256!")); if ((arg_alpha < 0) || (256 < arg_alpha)) nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "0 <= alpha <= 256!"));
const uint16_t *color_palette = NULL; const uint16_t *color_palette = py_helper_keyword_color_palette(n_args, args, offset + 5, kw_args, NULL);
{ const uint8_t *alpha_palette = py_helper_keyword_alpha_palette(n_args, args, offset + 6, kw_args, NULL);
int palette;
if (py_helper_keyword_int_maybe(n_args, args, offset + 5, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_color_palette), &palette)) {
if (palette == COLOR_PALETTE_RAINBOW) color_palette = rainbow_table;
else if (palette == COLOR_PALETTE_IRONBOW) color_palette = ironbow_table;
else nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid pre-defined color palette!"));
} else {
image_t *arg_color_palette = py_helper_keyword_to_image_mutable_color_palette(n_args, args, offset + 5, kw_args);
if (arg_color_palette) {
if (arg_color_palette->bpp != IMAGE_BPP_RGB565) nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Color palette must be RGB565!"));
if ((arg_color_palette->w * arg_color_palette->h) != 256) nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Color palette must be 256 pixels!"));
color_palette = (uint16_t *) arg_color_palette->data;
}
}
}
const uint8_t *alpha_palette = NULL;
{
image_t *arg_alpha_palette = py_helper_keyword_to_image_mutable_alpha_palette(n_args, args, offset + 6, kw_args);
if (arg_alpha_palette) {
if (arg_alpha_palette->bpp != IMAGE_BPP_GRAYSCALE) nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Alpha palette must be GRAYSCALE!"));
if ((arg_alpha_palette->w * arg_alpha_palette->h) != 256) nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Alpha palette must be 256 pixels!"));
alpha_palette = (uint8_t *) arg_alpha_palette->data;
}
}
image_hint_t hint = py_helper_keyword_int(n_args, args, offset + 7, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_hint), 0); image_hint_t hint = py_helper_keyword_int(n_args, args, offset + 7, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_hint), 0);

View File

@ -1587,35 +1587,8 @@ STATIC mp_obj_t py_lcd_display(uint n_args, const mp_obj_t *args, mp_map_t *kw_a
int arg_alpha = py_helper_keyword_int(n_args, args, offset + 4, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_alpha), 256); int arg_alpha = py_helper_keyword_int(n_args, args, offset + 4, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_alpha), 256);
if ((arg_alpha < 0) || (256 < arg_alpha)) nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "0 <= alpha <= 256!")); if ((arg_alpha < 0) || (256 < arg_alpha)) nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "0 <= alpha <= 256!"));
const uint16_t *color_palette = NULL; const uint16_t *color_palette = py_helper_keyword_color_palette(n_args, args, offset + 5, kw_args, NULL);
{ const uint8_t *alpha_palette = py_helper_keyword_alpha_palette(n_args, args, offset + 6, kw_args, NULL);
int palette;
if (py_helper_keyword_int_maybe(n_args, args, offset + 5, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_color_palette), &palette)) {
if (palette == COLOR_PALETTE_RAINBOW) color_palette = rainbow_table;
else if (palette == COLOR_PALETTE_IRONBOW) color_palette = ironbow_table;
else nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid pre-defined color palette!"));
} else {
image_t *arg_color_palette = py_helper_keyword_to_image_mutable_color_palette(n_args, args, offset + 5, kw_args);
if (arg_color_palette) {
if (arg_color_palette->bpp != IMAGE_BPP_RGB565) nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Color palette must be RGB565!"));
if ((arg_color_palette->w * arg_color_palette->h) != 256) nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Color palette must be 256 pixels!"));
color_palette = (uint16_t *) arg_color_palette->data;
}
}
}
const uint8_t *alpha_palette = NULL;
{
image_t *arg_alpha_palette = py_helper_keyword_to_image_mutable_alpha_palette(n_args, args, offset + 6, kw_args);
if (arg_alpha_palette) {
if (arg_alpha_palette->bpp != IMAGE_BPP_GRAYSCALE) nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Alpha palette must be GRAYSCALE!"));
if ((arg_alpha_palette->w * arg_alpha_palette->h) != 256) nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Alpha palette must be 256 pixels!"));
alpha_palette = (uint8_t *) arg_alpha_palette->data;
}
}
image_hint_t hint = py_helper_keyword_int(n_args, args, offset + 7, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_hint), 0); image_hint_t hint = py_helper_keyword_int(n_args, args, offset + 7, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_hint), 0);