| 1 |
#ifndef __MICROPY_INCLUDED_MICROBIT_IMAGE_H__ |
| 2 |
#define __MICROPY_INCLUDED_MICROBIT_IMAGE_H__ |
| 3 |
|
| 4 |
#include "py/runtime.h" |
| 5 |
|
| 6 |
#define MAX_BRIGHTNESS 9 |
| 7 |
|
| 8 |
/** Monochrome images are immutable, which means that |
| 9 |
* we only need one bit per pixel which saves quite a lot |
| 10 |
* of memory */ |
| 11 |
|
| 12 |
/* we reserve a couple of bits, so we won't need to modify the |
| 13 |
* layout if we need to add more functionality or subtypes. */ |
| 14 |
#define TYPE_AND_FLAGS \ |
| 15 |
mp_obj_base_t base; \ |
| 16 |
uint8_t five:1; \ |
| 17 |
uint8_t reserved1:1; \ |
| 18 |
uint8_t reserved2:1 |
| 19 |
|
| 20 |
typedef struct _image_base_t { |
| 21 |
TYPE_AND_FLAGS; |
| 22 |
} image_base_t; |
| 23 |
|
| 24 |
typedef struct _monochrome_5by5_t { |
| 25 |
TYPE_AND_FLAGS; |
| 26 |
uint8_t pixel44: 1; |
| 27 |
uint8_t bits24[3]; |
| 28 |
} monochrome_5by5_t; |
| 29 |
|
| 30 |
|
| 31 |
/* This is an internal method it is up to the caller to validate the inputs */ |
| 32 |
uint8_t monochromeGetPixelValue(monochrome_5by5_t * p_mono, mp_int_t x, mp_int_t y); |
| 33 |
|
| 34 |
typedef struct _greyscale_t { |
| 35 |
TYPE_AND_FLAGS; |
| 36 |
uint8_t height; |
| 37 |
uint8_t width; |
| 38 |
uint8_t byte_data[]; /* Static initializer for this will have to be C, not C++ */ |
| 39 |
} greyscale_t; |
| 40 |
|
| 41 |
#if 1 |
| 42 |
void clear(greyscale_t * p_greyscale); |
| 43 |
/* Thiese are internal methods and it is up to the caller to validate the inputs */ |
| 44 |
uint8_t greyscaleGetPixelValue(greyscale_t * p_greyscale, mp_int_t x, mp_int_t y); |
| 45 |
void greyscaleSetPixelValue(greyscale_t * p_greyscale, mp_int_t x, mp_int_t y, mp_int_t val); |
| 46 |
void greyscaleFill(greyscale_t * p_greyscale, mp_int_t val); |
| 47 |
#endif |
| 48 |
|
| 49 |
typedef union _microbit_image_obj_t { |
| 50 |
image_base_t base; |
| 51 |
monochrome_5by5_t monochrome_5by5; |
| 52 |
greyscale_t greyscale; |
| 53 |
} microbit_image_obj_t; |
| 54 |
|
| 55 |
#if 1 |
| 56 |
mp_int_t imageHeight(microbit_image_obj_t * p_image_obj); |
| 57 |
mp_int_t imageWidth(microbit_image_obj_t * p_image_obj); |
| 58 |
greyscale_t * imageCopy(microbit_image_obj_t * p_image_obj); |
| 59 |
greyscale_t * imageInvert(microbit_image_obj_t * p_image_obj); |
| 60 |
/* This is an internal method it is up to the caller to validate the inputs */ |
| 61 |
uint8_t imageGetPixelValue(microbit_image_obj_t * p_image_obj, mp_int_t x, mp_int_t y); |
| 62 |
#endif |
| 63 |
|
| 64 |
/** Return a facade object that presents the string as a sequence of images */ |
| 65 |
mp_obj_t microbit_string_facade(mp_obj_t string); |
| 66 |
|
| 67 |
void microbit_image_set_from_char(greyscale_t *img, char c); |
| 68 |
microbit_image_obj_t *microbit_image_for_char(char c); |
| 69 |
mp_obj_t microbit_image_slice(microbit_image_obj_t *img, mp_int_t start, mp_int_t width, mp_int_t stride); |
| 70 |
/* ref exists so that we can pull a string out of an object and not have it GC'ed while oterating over it */ |
| 71 |
mp_obj_t scrolling_string_image_iterable(const char* str, mp_uint_t len, mp_obj_t ref, bool monospace, bool repeat); |
| 72 |
|
| 73 |
#define SMALL_IMAGE(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p44) \ |
| 74 |
{ \ |
| 75 |
{ µbit_image_type }, \ |
| 76 |
1, 0, 0, (p44), \ |
| 77 |
{ \ |
| 78 |
(p0)|((p1)<<1)|((p2)<<2)|((p3)<<3)|((p4)<<4)|((p5)<<5)|((p6)<<6)|((p7)<<7), \ |
| 79 |
(p8)|((p9)<<1)|((p10)<<2)|((p11)<<3)|((p12)<<4)|((p13)<<5)|((p14)<<6)|((p15)<<7), \ |
| 80 |
(p16)|((p17)<<1)|((p18)<<2)|((p19)<<3)|((p20)<<4)|((p21)<<5)|((p22)<<6)|((p23)<<7) \ |
| 81 |
} \ |
| 82 |
} |
| 83 |
|
| 84 |
extern const monochrome_5by5_t microbit_blank_image; |
| 85 |
extern const monochrome_5by5_t microbit_const_image_heart_obj; |
| 86 |
extern const mp_obj_type_t microbit_image_type; |
| 87 |
|
| 88 |
#define BLANK_IMAGE (microbit_image_obj_t *)(µbit_blank_image) |
| 89 |
#define HEART_IMAGE (microbit_image_obj_t *)(µbit_const_image_heart_obj) |
| 90 |
#define HAPPY_IMAGE (microbit_image_obj_t *)(µbit_const_image_happy_obj) |
| 91 |
|
| 92 |
#if MICROPY_PY_BUILTINS_FLOAT |
| 93 |
microbit_image_obj_t *microbit_image_dim(microbit_image_obj_t *lhs, mp_float_t fval); |
| 94 |
#else |
| 95 |
microbit_image_obj_t *microbit_image_dim(microbit_image_obj_t *lhs, mp_int_t val); |
| 96 |
#endif |
| 97 |
microbit_image_obj_t *microbit_image_sum(microbit_image_obj_t *lhs, microbit_image_obj_t *rhs, bool add); |
| 98 |
|
| 99 |
#endif // __MICROPY_INCLUDED_MICROBIT_IMAGE_H__ |