| 1 |
/* |
| 2 |
* This file is part of the MicroPython project, http://micropython.org/ |
| 3 |
* |
| 4 |
* The MIT License (MIT) |
| 5 |
* |
| 6 |
* Copyright (c) 2015 Damien George, Mark Shannon |
| 7 |
* |
| 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
* of this software and associated documentation files (the "Software"), to deal |
| 10 |
* in the Software without restriction, including without limitation the rights |
| 11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
* copies of the Software, and to permit persons to whom the Software is |
| 13 |
* furnished to do so, subject to the following conditions: |
| 14 |
* |
| 15 |
* The above copyright notice and this permission notice shall be included in |
| 16 |
* all copies or substantial portions of the Software. |
| 17 |
* |
| 18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
* THE SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <string.h> |
| 28 |
#include "py/runtime.h" |
| 29 |
#include "microbitimage.h" |
| 30 |
#include "microbitconstimage.h" |
| 31 |
#include "py/runtime0.h" |
| 32 |
#include "microbitfont.h" |
| 33 |
|
| 34 |
#define min(a,b) (((a)<(b))?(a):(b)) |
| 35 |
#define max(a,b) (((a)>(b))?(a):(b)) |
| 36 |
|
| 37 |
const monochrome_5by5_t microbit_blank_image = { |
| 38 |
{ µbit_image_type }, |
| 39 |
1, 0, 0, 0, |
| 40 |
{ 0, 0, 0 } |
| 41 |
}; |
| 42 |
|
| 43 |
static void microbit_image_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 44 |
microbit_image_obj_t *self = (microbit_image_obj_t*)self_in; |
| 45 |
mp_printf(print, "Image("); |
| 46 |
if (kind == PRINT_STR) |
| 47 |
mp_printf(print, "\n "); |
| 48 |
mp_printf(print, "'"); |
| 49 |
for (int y = 0; y < imageHeight(self); ++y) { |
| 50 |
for (int x = 0; x < imageWidth(self); ++x) { |
| 51 |
mp_printf(print, "%c", "0123456789"[imageGetPixelValue(self, x, y)]); |
| 52 |
} |
| 53 |
mp_printf(print, ":"); |
| 54 |
if (kind == PRINT_STR && y < imageHeight(self)-1) |
| 55 |
mp_printf(print, "'\n '"); |
| 56 |
} |
| 57 |
mp_printf(print, "'"); |
| 58 |
if (kind == PRINT_STR) |
| 59 |
mp_printf(print, "\n"); |
| 60 |
mp_printf(print, ")"); |
| 61 |
} |
| 62 |
|
| 63 |
uint8_t monochromeGetPixelValue(monochrome_5by5_t * p_mono, mp_int_t x, mp_int_t y) { |
| 64 |
unsigned int index = y*5+x; |
| 65 |
if (index == 24) |
| 66 |
return p_mono->pixel44; |
| 67 |
return (p_mono->bits24[index>>3] >> (index&7))&1; |
| 68 |
} |
| 69 |
|
| 70 |
uint8_t greyscaleGetPixelValue(greyscale_t * p_greyscale, mp_int_t x, mp_int_t y) { |
| 71 |
unsigned int index = y*p_greyscale->width+x; |
| 72 |
unsigned int shift = ((index<<2)&4); |
| 73 |
return (p_greyscale->byte_data[index>>1] >> shift)&15; |
| 74 |
} |
| 75 |
|
| 76 |
void greyscaleSetPixelValue(greyscale_t * p_greyscale, mp_int_t x, mp_int_t y, mp_int_t val) { |
| 77 |
unsigned int index = y*p_greyscale->width+x; |
| 78 |
unsigned int shift = ((index<<2)&4); |
| 79 |
uint8_t mask = 240 >> shift; |
| 80 |
p_greyscale->byte_data[index>>1] = (p_greyscale->byte_data[index>>1] & mask) | (val << shift); |
| 81 |
} |
| 82 |
|
| 83 |
void greyscaleFill(greyscale_t * p_greyscale, mp_int_t val) { |
| 84 |
mp_int_t byte = (val<<4) | val; |
| 85 |
for (int i = 0; i < ((p_greyscale->width*p_greyscale->height+1)>>1); i++) { |
| 86 |
p_greyscale->byte_data[i] = byte; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
void greyscaleClear(greyscale_t * p_greyscale) { |
| 91 |
memset(&p_greyscale->byte_data, 0, (p_greyscale->width*p_greyscale->height+1)>>1); |
| 92 |
} |
| 93 |
|
| 94 |
uint8_t imageGetPixelValue(microbit_image_obj_t * p_image, mp_int_t x, mp_int_t y) { |
| 95 |
if (p_image->base.five) |
| 96 |
return monochromeGetPixelValue(&p_image->monochrome_5by5, x, y)*MAX_BRIGHTNESS; |
| 97 |
else |
| 98 |
return greyscaleGetPixelValue(&p_image->greyscale, x, y); |
| 99 |
} |
| 100 |
|
| 101 |
mp_int_t imageWidth(microbit_image_obj_t * p_image) { |
| 102 |
if (p_image->base.five) |
| 103 |
return 5; |
| 104 |
else |
| 105 |
return p_image->greyscale.width; |
| 106 |
} |
| 107 |
|
| 108 |
mp_int_t imageHeight(microbit_image_obj_t * p_image) { |
| 109 |
if (p_image->base.five) |
| 110 |
return 5; |
| 111 |
else |
| 112 |
return p_image->greyscale.height; |
| 113 |
} |
| 114 |
|
| 115 |
static greyscale_t *greyscale_new(mp_int_t w, mp_int_t h) { |
| 116 |
greyscale_t *result = mp_obj_malloc_var(greyscale_t, byte_data, uint8_t, (w*h+1)>>1, µbit_image_type); |
| 117 |
result->five = 0; |
| 118 |
result->width = w; |
| 119 |
result->height = h; |
| 120 |
return result; |
| 121 |
} |
| 122 |
|
| 123 |
greyscale_t * imageCopy(microbit_image_obj_t * p_image) { |
| 124 |
mp_int_t w = imageWidth(p_image); |
| 125 |
mp_int_t h = imageHeight(p_image); |
| 126 |
greyscale_t *result = greyscale_new(w, h); |
| 127 |
for (mp_int_t y = 0; y < h; y++) { |
| 128 |
for (mp_int_t x = 0; x < w; ++x) { |
| 129 |
greyscaleSetPixelValue(result, x,y, imageGetPixelValue(p_image, x,y)); |
| 130 |
} |
| 131 |
} |
| 132 |
return result; |
| 133 |
} |
| 134 |
|
| 135 |
greyscale_t * imageInvert(microbit_image_obj_t * p_image) { |
| 136 |
mp_int_t w = imageWidth(p_image); |
| 137 |
mp_int_t h = imageHeight(p_image); |
| 138 |
greyscale_t *result = greyscale_new(w, h); |
| 139 |
for (mp_int_t y = 0; y < h; y++) { |
| 140 |
for (mp_int_t x = 0; x < w; ++x) { |
| 141 |
greyscaleSetPixelValue(result, x,y, MAX_BRIGHTNESS - imageGetPixelValue(p_image, x,y)); |
| 142 |
} |
| 143 |
} |
| 144 |
return result; |
| 145 |
} |
| 146 |
|
| 147 |
static microbit_image_obj_t *image_from_parsed_str(const char *s, mp_int_t len) { |
| 148 |
mp_int_t w = 0; |
| 149 |
mp_int_t h = 0; |
| 150 |
mp_int_t line_len = 0; |
| 151 |
greyscale_t *result; |
| 152 |
/*First pass -- Establish metadata */ |
| 153 |
for (int i = 0; i < len; i++) { |
| 154 |
char c = s[i]; |
| 155 |
if (c == '\n' || c == ':') { |
| 156 |
w = max(line_len, w); |
| 157 |
line_len = 0; |
| 158 |
++h; |
| 159 |
} else if (c == ' ') { |
| 160 |
++line_len; |
| 161 |
} else if ('c' >= '0' && c <= '9') { |
| 162 |
++line_len; |
| 163 |
} else { |
| 164 |
mp_raise_ValueError(MP_ERROR_TEXT("Unexpected character in Image definition.")); |
| 165 |
} |
| 166 |
} |
| 167 |
if (line_len) { |
| 168 |
// Omitted trailing terminator |
| 169 |
++h; |
| 170 |
w = max(line_len, w); |
| 171 |
} |
| 172 |
result = greyscale_new(w, h); |
| 173 |
mp_int_t x = 0; |
| 174 |
mp_int_t y = 0; |
| 175 |
/* Second pass -- Fill in data */ |
| 176 |
for (int i = 0; i < len; i++) { |
| 177 |
char c = s[i]; |
| 178 |
if (c == '\n' || c == ':') { |
| 179 |
while (x < w) { |
| 180 |
greyscaleSetPixelValue(result, x, y, 0); |
| 181 |
x++; |
| 182 |
} |
| 183 |
++y; |
| 184 |
x = 0; |
| 185 |
} else if (c == ' ') { |
| 186 |
/* Treat spaces as 0 */ |
| 187 |
greyscaleSetPixelValue(result, x, y, 0); |
| 188 |
++x; |
| 189 |
} else if ('c' >= '0' && c <= '9') { |
| 190 |
greyscaleSetPixelValue(result, x, y, c - '0'); |
| 191 |
++x; |
| 192 |
} |
| 193 |
} |
| 194 |
if (y < h) { |
| 195 |
while (x < w) { |
| 196 |
greyscaleSetPixelValue(result, x, y, 0); |
| 197 |
x++; |
| 198 |
} |
| 199 |
} |
| 200 |
return (microbit_image_obj_t *)result; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
static mp_obj_t microbit_image_make_new(const mp_obj_type_t *type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { |
| 205 |
(void)type_in; |
| 206 |
mp_arg_check_num(n_args, n_kw, 0, 3, false); |
| 207 |
|
| 208 |
switch (n_args) { |
| 209 |
case 0: { |
| 210 |
greyscale_t *image = greyscale_new(5, 5); |
| 211 |
greyscaleClear(image); |
| 212 |
return image; |
| 213 |
} |
| 214 |
|
| 215 |
case 1: { |
| 216 |
if (mp_obj_is_str(args[0])) { |
| 217 |
// arg is a string object |
| 218 |
size_t len; |
| 219 |
const char *str = mp_obj_str_get_data(args[0], &len); |
| 220 |
// make image from string |
| 221 |
if (len == 1) { |
| 222 |
/* For a single character, return the font glyph */ |
| 223 |
return microbit_image_for_char(str[0]); |
| 224 |
} else { |
| 225 |
/* Otherwise parse the image description string */ |
| 226 |
return image_from_parsed_str(str, len); |
| 227 |
} |
| 228 |
} else { |
| 229 |
mp_raise_TypeError(MP_ERROR_TEXT("Image(s) takes a string.")); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
case 2: |
| 234 |
case 3: { |
| 235 |
mp_int_t w = mp_obj_get_int(args[0]); |
| 236 |
mp_int_t h = mp_obj_get_int(args[1]); |
| 237 |
greyscale_t *image = greyscale_new(w, h); |
| 238 |
if (n_args == 2) { |
| 239 |
greyscaleClear(image); |
| 240 |
} else { |
| 241 |
mp_buffer_info_t bufinfo; |
| 242 |
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ); |
| 243 |
|
| 244 |
if (w < 0 || h < 0 || (size_t)(w * h) != bufinfo.len) { |
| 245 |
mp_raise_ValueError(MP_ERROR_TEXT("image data is incorrect size")); |
| 246 |
} |
| 247 |
mp_int_t i = 0; |
| 248 |
for (mp_int_t y = 0; y < h; y++) { |
| 249 |
for (mp_int_t x = 0; x < w; ++x) { |
| 250 |
uint8_t val = min(((const uint8_t*)bufinfo.buf)[i], MAX_BRIGHTNESS); |
| 251 |
greyscaleSetPixelValue(image, x, y, val); |
| 252 |
++i; |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
return image; |
| 257 |
} |
| 258 |
|
| 259 |
default: { |
| 260 |
mp_raise_TypeError(MP_ERROR_TEXT("Image() takes 0 to 3 arguments")); |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
static void clear_rect(greyscale_t *img, mp_int_t x0, mp_int_t y0,mp_int_t x1, mp_int_t y1) { |
| 266 |
for (int i = x0; i < x1; ++i) { |
| 267 |
for (int j = y0; j < y1; ++j) { |
| 268 |
greyscaleSetPixelValue(img, i, j, 0); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
static void image_blit(microbit_image_obj_t *src, greyscale_t *dest, mp_int_t x, mp_int_t y, mp_int_t w, mp_int_t h, mp_int_t xdest, mp_int_t ydest) { |
| 274 |
if (w < 0) |
| 275 |
w = 0; |
| 276 |
if (h < 0) |
| 277 |
h = 0; |
| 278 |
mp_int_t intersect_x0 = max(max(0, x), -xdest); |
| 279 |
mp_int_t intersect_y0 = max(max(0, y), -ydest); |
| 280 |
mp_int_t intersect_x1 = min(min(dest->width+x-xdest, imageWidth(src)), x+w); |
| 281 |
mp_int_t intersect_y1 = min(min(dest->height+y-ydest, imageHeight(src)), y+h); |
| 282 |
mp_int_t xstart, xend, ystart, yend, xdel, ydel; |
| 283 |
mp_int_t clear_x0 = max(0, xdest); |
| 284 |
mp_int_t clear_y0 = max(0, ydest); |
| 285 |
mp_int_t clear_x1 = min(dest->width, xdest+w); |
| 286 |
mp_int_t clear_y1 = min(dest->height, ydest+h); |
| 287 |
if (intersect_x0 >= intersect_x1 || intersect_y0 >= intersect_y1) { |
| 288 |
// Nothing to copy |
| 289 |
clear_rect(dest, clear_x0, clear_y0, clear_x1, clear_y1); |
| 290 |
return; |
| 291 |
} |
| 292 |
if (x > xdest) { |
| 293 |
xstart = intersect_x0; xend = intersect_x1; xdel = 1; |
| 294 |
} else { |
| 295 |
xstart = intersect_x1-1; xend = intersect_x0-1; xdel = -1; |
| 296 |
} |
| 297 |
if (y > ydest) { |
| 298 |
ystart = intersect_y0; yend = intersect_y1; ydel = 1; |
| 299 |
} else { |
| 300 |
ystart = intersect_y1-1; yend = intersect_y0-1; ydel = -1; |
| 301 |
} |
| 302 |
for (int i = xstart; i != xend; i += xdel) { |
| 303 |
for (int j = ystart; j != yend; j += ydel) { |
| 304 |
int val = imageGetPixelValue(src, i, j); |
| 305 |
greyscaleSetPixelValue(dest, i+xdest-x, j+ydest-y, val); |
| 306 |
} |
| 307 |
} |
| 308 |
// Adjust intersection rectangle to dest |
| 309 |
intersect_x0 += xdest-x; |
| 310 |
intersect_y0 += ydest-y; |
| 311 |
intersect_x1 += xdest-x; |
| 312 |
intersect_y1 += ydest-y; |
| 313 |
// Clear four rectangles in the cleared area surrounding the copied area. |
| 314 |
clear_rect(dest, clear_x0, clear_y0, intersect_x0, intersect_y1); |
| 315 |
clear_rect(dest, clear_x0, intersect_y1, intersect_x1, clear_y1); |
| 316 |
clear_rect(dest, intersect_x1, intersect_y0, clear_x1, clear_y1); |
| 317 |
clear_rect(dest, intersect_x0, clear_y0, clear_x1, intersect_y0); |
| 318 |
} |
| 319 |
|
| 320 |
greyscale_t *image_shift(microbit_image_obj_t *self, mp_int_t x, mp_int_t y) { |
| 321 |
greyscale_t *result = greyscale_new(imageWidth(self), imageWidth(self)); |
| 322 |
image_blit(self, result, x, y, imageWidth(self), imageWidth(self), 0, 0); |
| 323 |
return result; |
| 324 |
} |
| 325 |
|
| 326 |
static microbit_image_obj_t *image_crop(microbit_image_obj_t *img, mp_int_t x, mp_int_t y, mp_int_t w, mp_int_t h) { |
| 327 |
if (w < 0) |
| 328 |
w = 0; |
| 329 |
if (h < 0) |
| 330 |
h = 0; |
| 331 |
greyscale_t *result = greyscale_new(w, h); |
| 332 |
image_blit(img, result, x, y, w, h, 0, 0); |
| 333 |
return (microbit_image_obj_t *)result; |
| 334 |
} |
| 335 |
|
| 336 |
mp_obj_t microbit_image_width(mp_obj_t self_in) { |
| 337 |
microbit_image_obj_t *self = (microbit_image_obj_t*)self_in; |
| 338 |
return MP_OBJ_NEW_SMALL_INT(imageWidth(self)); |
| 339 |
} |
| 340 |
MP_DEFINE_CONST_FUN_OBJ_1(microbit_image_width_obj, microbit_image_width); |
| 341 |
|
| 342 |
mp_obj_t microbit_image_height(mp_obj_t self_in) { |
| 343 |
microbit_image_obj_t *self = (microbit_image_obj_t*)self_in; |
| 344 |
return MP_OBJ_NEW_SMALL_INT(imageHeight(self)); |
| 345 |
} |
| 346 |
MP_DEFINE_CONST_FUN_OBJ_1(microbit_image_height_obj, microbit_image_height); |
| 347 |
|
| 348 |
mp_obj_t microbit_image_get_pixel(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in) { |
| 349 |
microbit_image_obj_t *self = (microbit_image_obj_t*)self_in; |
| 350 |
mp_int_t x = mp_obj_get_int(x_in); |
| 351 |
mp_int_t y = mp_obj_get_int(y_in); |
| 352 |
if (x < 0 || y < 0) { |
| 353 |
mp_raise_ValueError(MP_ERROR_TEXT("index can't be negative")); |
| 354 |
} |
| 355 |
if (x < imageWidth(self) && y < imageHeight(self)) { |
| 356 |
return MP_OBJ_NEW_SMALL_INT(imageGetPixelValue(self, x, y)); |
| 357 |
} |
| 358 |
mp_raise_ValueError(MP_ERROR_TEXT("index too large")); |
| 359 |
} |
| 360 |
MP_DEFINE_CONST_FUN_OBJ_3(microbit_image_get_pixel_obj, microbit_image_get_pixel); |
| 361 |
|
| 362 |
/* Raise an exception if not mutable */ |
| 363 |
static void check_mutability(microbit_image_obj_t *self) { |
| 364 |
if (self->base.five) { |
| 365 |
mp_raise_TypeError(MP_ERROR_TEXT("image can't be modified (try copying first)")); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
mp_obj_t microbit_image_set_pixel(mp_uint_t n_args, const mp_obj_t *args) { |
| 371 |
(void)n_args; |
| 372 |
microbit_image_obj_t *self = (microbit_image_obj_t*)args[0]; |
| 373 |
check_mutability(self); |
| 374 |
mp_int_t x = mp_obj_get_int(args[1]); |
| 375 |
mp_int_t y = mp_obj_get_int(args[2]); |
| 376 |
if (x < 0 || y < 0) { |
| 377 |
mp_raise_ValueError(MP_ERROR_TEXT("index can't be negative")); |
| 378 |
} |
| 379 |
mp_int_t bright = mp_obj_get_int(args[3]); |
| 380 |
if (bright < 0 || bright > MAX_BRIGHTNESS) |
| 381 |
mp_raise_ValueError(MP_ERROR_TEXT("brightness out of bounds.")); |
| 382 |
if (x < imageWidth(self) && y < imageHeight(self)) { |
| 383 |
greyscaleSetPixelValue(&(self->greyscale), x, y, bright); |
| 384 |
return mp_const_none; |
| 385 |
} |
| 386 |
mp_raise_ValueError(MP_ERROR_TEXT("index too large")); |
| 387 |
} |
| 388 |
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(microbit_image_set_pixel_obj, 4, 4, microbit_image_set_pixel); |
| 389 |
|
| 390 |
mp_obj_t microbit_image_fill(mp_obj_t self_in, mp_obj_t n_in) { |
| 391 |
microbit_image_obj_t *self = (microbit_image_obj_t*)self_in; |
| 392 |
check_mutability(self); |
| 393 |
mp_int_t n = mp_obj_get_int(n_in); |
| 394 |
if (n < 0 || n > MAX_BRIGHTNESS) { |
| 395 |
mp_raise_ValueError(MP_ERROR_TEXT("brightness out of bounds.")); |
| 396 |
} |
| 397 |
greyscaleFill(&self->greyscale, n); |
| 398 |
return mp_const_none; |
| 399 |
} |
| 400 |
MP_DEFINE_CONST_FUN_OBJ_2(microbit_image_fill_obj, microbit_image_fill); |
| 401 |
|
| 402 |
mp_obj_t microbit_image_blit(mp_uint_t n_args, const mp_obj_t *args) { |
| 403 |
microbit_image_obj_t *self = (microbit_image_obj_t*)args[0]; |
| 404 |
check_mutability(self); |
| 405 |
|
| 406 |
mp_obj_t src = args[1]; |
| 407 |
if (mp_obj_get_type(src) != µbit_image_type) { |
| 408 |
mp_raise_TypeError(MP_ERROR_TEXT("expecting an image")); |
| 409 |
} |
| 410 |
if (n_args == 7) { |
| 411 |
mp_raise_TypeError(MP_ERROR_TEXT("must specify both offsets")); |
| 412 |
} |
| 413 |
mp_int_t x = mp_obj_get_int(args[2]); |
| 414 |
mp_int_t y = mp_obj_get_int(args[3]); |
| 415 |
mp_int_t w = mp_obj_get_int(args[4]); |
| 416 |
mp_int_t h = mp_obj_get_int(args[5]); |
| 417 |
if (w < 0 || h < 0) { |
| 418 |
mp_raise_ValueError(MP_ERROR_TEXT("size can't be negative")); |
| 419 |
} |
| 420 |
mp_int_t xdest; |
| 421 |
mp_int_t ydest; |
| 422 |
if (n_args == 6) { |
| 423 |
xdest = 0; |
| 424 |
ydest = 0; |
| 425 |
} else { |
| 426 |
xdest = mp_obj_get_int(args[6]); |
| 427 |
ydest = mp_obj_get_int(args[7]); |
| 428 |
} |
| 429 |
image_blit((microbit_image_obj_t *)src, &(self->greyscale), x, y, w, h, xdest, ydest); |
| 430 |
return mp_const_none; |
| 431 |
} |
| 432 |
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(microbit_image_blit_obj, 6, 8, microbit_image_blit); |
| 433 |
|
| 434 |
mp_obj_t microbit_image_crop(mp_uint_t n_args, const mp_obj_t *args) { |
| 435 |
(void)n_args; |
| 436 |
microbit_image_obj_t *self = (microbit_image_obj_t*)args[0]; |
| 437 |
mp_int_t x0 = mp_obj_get_int(args[1]); |
| 438 |
mp_int_t y0 = mp_obj_get_int(args[2]); |
| 439 |
mp_int_t x1 = mp_obj_get_int(args[3]); |
| 440 |
mp_int_t y1 = mp_obj_get_int(args[4]); |
| 441 |
return image_crop(self, x0, y0, x1, y1); |
| 442 |
} |
| 443 |
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(microbit_image_crop_obj, 5, 5, microbit_image_crop); |
| 444 |
|
| 445 |
mp_obj_t microbit_image_shift_left(mp_obj_t self_in, mp_obj_t n_in) { |
| 446 |
microbit_image_obj_t *self = (microbit_image_obj_t*)self_in; |
| 447 |
mp_int_t n = mp_obj_get_int(n_in); |
| 448 |
return image_shift(self, n, 0); |
| 449 |
} |
| 450 |
MP_DEFINE_CONST_FUN_OBJ_2(microbit_image_shift_left_obj, microbit_image_shift_left); |
| 451 |
|
| 452 |
mp_obj_t microbit_image_shift_right(mp_obj_t self_in, mp_obj_t n_in) { |
| 453 |
microbit_image_obj_t *self = (microbit_image_obj_t*)self_in; |
| 454 |
mp_int_t n = mp_obj_get_int(n_in); |
| 455 |
return image_shift(self, -n, 0); |
| 456 |
} |
| 457 |
MP_DEFINE_CONST_FUN_OBJ_2(microbit_image_shift_right_obj, microbit_image_shift_right); |
| 458 |
|
| 459 |
mp_obj_t microbit_image_shift_up(mp_obj_t self_in, mp_obj_t n_in) { |
| 460 |
microbit_image_obj_t *self = (microbit_image_obj_t*)self_in; |
| 461 |
mp_int_t n = mp_obj_get_int(n_in); |
| 462 |
return image_shift(self, 0, n); |
| 463 |
} |
| 464 |
MP_DEFINE_CONST_FUN_OBJ_2(microbit_image_shift_up_obj, microbit_image_shift_up); |
| 465 |
|
| 466 |
mp_obj_t microbit_image_shift_down(mp_obj_t self_in, mp_obj_t n_in) { |
| 467 |
microbit_image_obj_t *self = (microbit_image_obj_t*)self_in; |
| 468 |
mp_int_t n = mp_obj_get_int(n_in); |
| 469 |
return image_shift(self, 0, -n); |
| 470 |
} |
| 471 |
MP_DEFINE_CONST_FUN_OBJ_2(microbit_image_shift_down_obj, microbit_image_shift_down); |
| 472 |
|
| 473 |
mp_obj_t microbit_image_copy(mp_obj_t self_in) { |
| 474 |
microbit_image_obj_t *self = (microbit_image_obj_t*)self_in; |
| 475 |
return imageCopy(self); |
| 476 |
} |
| 477 |
MP_DEFINE_CONST_FUN_OBJ_1(microbit_image_copy_obj, microbit_image_copy); |
| 478 |
|
| 479 |
mp_obj_t microbit_image_invert(mp_obj_t self_in) { |
| 480 |
microbit_image_obj_t *self = (microbit_image_obj_t*)self_in; |
| 481 |
return imageInvert(self); |
| 482 |
} |
| 483 |
MP_DEFINE_CONST_FUN_OBJ_1(microbit_image_invert_obj, microbit_image_invert); |
| 484 |
|
| 485 |
|
| 486 |
static const mp_rom_map_elem_t microbit_image_locals_dict_table[] = { |
| 487 |
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(µbit_image_width_obj) }, |
| 488 |
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(µbit_image_height_obj) }, |
| 489 |
{ MP_ROM_QSTR(MP_QSTR_get_pixel), MP_ROM_PTR(µbit_image_get_pixel_obj) }, |
| 490 |
{ MP_ROM_QSTR(MP_QSTR_set_pixel), MP_ROM_PTR(µbit_image_set_pixel_obj) }, |
| 491 |
{ MP_ROM_QSTR(MP_QSTR_shift_left), MP_ROM_PTR(µbit_image_shift_left_obj) }, |
| 492 |
{ MP_ROM_QSTR(MP_QSTR_shift_right), MP_ROM_PTR(µbit_image_shift_right_obj) }, |
| 493 |
{ MP_ROM_QSTR(MP_QSTR_shift_up), MP_ROM_PTR(µbit_image_shift_up_obj) }, |
| 494 |
{ MP_ROM_QSTR(MP_QSTR_shift_down), MP_ROM_PTR(µbit_image_shift_down_obj) }, |
| 495 |
{ MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(µbit_image_copy_obj) }, |
| 496 |
{ MP_ROM_QSTR(MP_QSTR_crop), MP_ROM_PTR(µbit_image_crop_obj) }, |
| 497 |
{ MP_ROM_QSTR(MP_QSTR_invert), MP_ROM_PTR(µbit_image_invert_obj) }, |
| 498 |
{ MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(µbit_image_fill_obj) }, |
| 499 |
{ MP_ROM_QSTR(MP_QSTR_blit), MP_ROM_PTR(µbit_image_blit_obj) }, |
| 500 |
|
| 501 |
{ MP_ROM_QSTR(MP_QSTR_HEART), MP_ROM_PTR(µbit_const_image_heart_obj) }, |
| 502 |
{ MP_ROM_QSTR(MP_QSTR_HEART_SMALL), MP_ROM_PTR(µbit_const_image_heart_small_obj) }, |
| 503 |
{ MP_ROM_QSTR(MP_QSTR_HAPPY), MP_ROM_PTR(µbit_const_image_happy_obj) }, |
| 504 |
{ MP_ROM_QSTR(MP_QSTR_SMILE), MP_ROM_PTR(µbit_const_image_smile_obj) }, |
| 505 |
{ MP_ROM_QSTR(MP_QSTR_SAD), MP_ROM_PTR(µbit_const_image_sad_obj) }, |
| 506 |
{ MP_ROM_QSTR(MP_QSTR_CONFUSED), MP_ROM_PTR(µbit_const_image_confused_obj) }, |
| 507 |
{ MP_ROM_QSTR(MP_QSTR_ANGRY), MP_ROM_PTR(µbit_const_image_angry_obj) }, |
| 508 |
{ MP_ROM_QSTR(MP_QSTR_ASLEEP), MP_ROM_PTR(µbit_const_image_asleep_obj) }, |
| 509 |
{ MP_ROM_QSTR(MP_QSTR_SURPRISED), MP_ROM_PTR(µbit_const_image_surprised_obj) }, |
| 510 |
{ MP_ROM_QSTR(MP_QSTR_SILLY), MP_ROM_PTR(µbit_const_image_silly_obj) }, |
| 511 |
{ MP_ROM_QSTR(MP_QSTR_FABULOUS), MP_ROM_PTR(µbit_const_image_fabulous_obj) }, |
| 512 |
{ MP_ROM_QSTR(MP_QSTR_MEH), MP_ROM_PTR(µbit_const_image_meh_obj) }, |
| 513 |
{ MP_ROM_QSTR(MP_QSTR_YES), MP_ROM_PTR(µbit_const_image_yes_obj) }, |
| 514 |
{ MP_ROM_QSTR(MP_QSTR_NO), MP_ROM_PTR(µbit_const_image_no_obj) }, |
| 515 |
{ MP_ROM_QSTR(MP_QSTR_CLOCK12), MP_ROM_PTR(µbit_const_image_clock12_obj) }, |
| 516 |
{ MP_ROM_QSTR(MP_QSTR_CLOCK1), MP_ROM_PTR(µbit_const_image_clock1_obj) }, |
| 517 |
{ MP_ROM_QSTR(MP_QSTR_CLOCK2), MP_ROM_PTR(µbit_const_image_clock2_obj) }, |
| 518 |
{ MP_ROM_QSTR(MP_QSTR_CLOCK3), MP_ROM_PTR(µbit_const_image_clock3_obj) }, |
| 519 |
{ MP_ROM_QSTR(MP_QSTR_CLOCK4), MP_ROM_PTR(µbit_const_image_clock4_obj) }, |
| 520 |
{ MP_ROM_QSTR(MP_QSTR_CLOCK5), MP_ROM_PTR(µbit_const_image_clock5_obj) }, |
| 521 |
{ MP_ROM_QSTR(MP_QSTR_CLOCK6), MP_ROM_PTR(µbit_const_image_clock6_obj) }, |
| 522 |
{ MP_ROM_QSTR(MP_QSTR_CLOCK7), MP_ROM_PTR(µbit_const_image_clock7_obj) }, |
| 523 |
{ MP_ROM_QSTR(MP_QSTR_CLOCK8), MP_ROM_PTR(µbit_const_image_clock8_obj) }, |
| 524 |
{ MP_ROM_QSTR(MP_QSTR_CLOCK9), MP_ROM_PTR(µbit_const_image_clock9_obj) }, |
| 525 |
{ MP_ROM_QSTR(MP_QSTR_CLOCK10), MP_ROM_PTR(µbit_const_image_clock10_obj) }, |
| 526 |
{ MP_ROM_QSTR(MP_QSTR_CLOCK11), MP_ROM_PTR(µbit_const_image_clock11_obj) }, |
| 527 |
{ MP_ROM_QSTR(MP_QSTR_ARROW_N), MP_ROM_PTR(µbit_const_image_arrow_n_obj) }, |
| 528 |
{ MP_ROM_QSTR(MP_QSTR_ARROW_NE), MP_ROM_PTR(µbit_const_image_arrow_ne_obj) }, |
| 529 |
{ MP_ROM_QSTR(MP_QSTR_ARROW_E), MP_ROM_PTR(µbit_const_image_arrow_e_obj) }, |
| 530 |
{ MP_ROM_QSTR(MP_QSTR_ARROW_SE), MP_ROM_PTR(µbit_const_image_arrow_se_obj) }, |
| 531 |
{ MP_ROM_QSTR(MP_QSTR_ARROW_S), MP_ROM_PTR(µbit_const_image_arrow_s_obj) }, |
| 532 |
{ MP_ROM_QSTR(MP_QSTR_ARROW_SW), MP_ROM_PTR(µbit_const_image_arrow_sw_obj) }, |
| 533 |
{ MP_ROM_QSTR(MP_QSTR_ARROW_W), MP_ROM_PTR(µbit_const_image_arrow_w_obj) }, |
| 534 |
{ MP_ROM_QSTR(MP_QSTR_ARROW_NW), MP_ROM_PTR(µbit_const_image_arrow_nw_obj) }, |
| 535 |
{ MP_ROM_QSTR(MP_QSTR_TRIANGLE), MP_ROM_PTR(µbit_const_image_triangle_obj) }, |
| 536 |
{ MP_ROM_QSTR(MP_QSTR_TRIANGLE_LEFT), MP_ROM_PTR(µbit_const_image_triangle_left_obj) }, |
| 537 |
{ MP_ROM_QSTR(MP_QSTR_CHESSBOARD), MP_ROM_PTR(µbit_const_image_chessboard_obj) }, |
| 538 |
{ MP_ROM_QSTR(MP_QSTR_DIAMOND), MP_ROM_PTR(µbit_const_image_diamond_obj) }, |
| 539 |
{ MP_ROM_QSTR(MP_QSTR_DIAMOND_SMALL), MP_ROM_PTR(µbit_const_image_diamond_small_obj) }, |
| 540 |
{ MP_ROM_QSTR(MP_QSTR_SQUARE), MP_ROM_PTR(µbit_const_image_square_obj) }, |
| 541 |
{ MP_ROM_QSTR(MP_QSTR_SQUARE_SMALL), MP_ROM_PTR(µbit_const_image_square_small_obj) }, |
| 542 |
{ MP_ROM_QSTR(MP_QSTR_RABBIT), MP_ROM_PTR(µbit_const_image_rabbit) }, |
| 543 |
{ MP_ROM_QSTR(MP_QSTR_COW), MP_ROM_PTR(µbit_const_image_cow) }, |
| 544 |
{ MP_ROM_QSTR(MP_QSTR_MUSIC_CROTCHET), MP_ROM_PTR(µbit_const_image_music_crotchet_obj) }, |
| 545 |
{ MP_ROM_QSTR(MP_QSTR_MUSIC_QUAVER), MP_ROM_PTR(µbit_const_image_music_quaver_obj) }, |
| 546 |
{ MP_ROM_QSTR(MP_QSTR_MUSIC_QUAVERS), MP_ROM_PTR(µbit_const_image_music_quavers_obj) }, |
| 547 |
{ MP_ROM_QSTR(MP_QSTR_PITCHFORK), MP_ROM_PTR(µbit_const_image_pitchfork_obj) }, |
| 548 |
{ MP_ROM_QSTR(MP_QSTR_XMAS), MP_ROM_PTR(µbit_const_image_xmas_obj) }, |
| 549 |
{ MP_ROM_QSTR(MP_QSTR_PACMAN), MP_ROM_PTR(µbit_const_image_pacman_obj) }, |
| 550 |
{ MP_ROM_QSTR(MP_QSTR_TARGET), MP_ROM_PTR(µbit_const_image_target_obj) }, |
| 551 |
{ MP_ROM_QSTR(MP_QSTR_ALL_CLOCKS), MP_ROM_PTR(µbit_const_image_all_clocks_tuple_obj) }, |
| 552 |
{ MP_ROM_QSTR(MP_QSTR_ALL_ARROWS), MP_ROM_PTR(µbit_const_image_all_arrows_tuple_obj) }, |
| 553 |
{ MP_ROM_QSTR(MP_QSTR_TSHIRT), MP_ROM_PTR(µbit_const_image_tshirt_obj) }, |
| 554 |
{ MP_ROM_QSTR(MP_QSTR_ROLLERSKATE), MP_ROM_PTR(µbit_const_image_rollerskate_obj) }, |
| 555 |
{ MP_ROM_QSTR(MP_QSTR_DUCK), MP_ROM_PTR(µbit_const_image_duck_obj) }, |
| 556 |
{ MP_ROM_QSTR(MP_QSTR_HOUSE), MP_ROM_PTR(µbit_const_image_house_obj) }, |
| 557 |
{ MP_ROM_QSTR(MP_QSTR_TORTOISE), MP_ROM_PTR(µbit_const_image_tortoise_obj) }, |
| 558 |
{ MP_ROM_QSTR(MP_QSTR_BUTTERFLY), MP_ROM_PTR(µbit_const_image_butterfly_obj) }, |
| 559 |
{ MP_ROM_QSTR(MP_QSTR_STICKFIGURE), MP_ROM_PTR(µbit_const_image_stickfigure_obj) }, |
| 560 |
{ MP_ROM_QSTR(MP_QSTR_GHOST), MP_ROM_PTR(µbit_const_image_ghost_obj) }, |
| 561 |
{ MP_ROM_QSTR(MP_QSTR_SWORD), MP_ROM_PTR(µbit_const_image_sword_obj) }, |
| 562 |
{ MP_ROM_QSTR(MP_QSTR_GIRAFFE), MP_ROM_PTR(µbit_const_image_giraffe_obj) }, |
| 563 |
{ MP_ROM_QSTR(MP_QSTR_SKULL), MP_ROM_PTR(µbit_const_image_skull_obj) }, |
| 564 |
{ MP_ROM_QSTR(MP_QSTR_UMBRELLA), MP_ROM_PTR(µbit_const_image_umbrella_obj) }, |
| 565 |
{ MP_ROM_QSTR(MP_QSTR_SNAKE), MP_ROM_PTR(µbit_const_image_snake_obj) }, |
| 566 |
}; |
| 567 |
|
| 568 |
static MP_DEFINE_CONST_DICT(microbit_image_locals_dict, microbit_image_locals_dict_table); |
| 569 |
|
| 570 |
#define THE_FONT font_pendolino3_5x5_pad3msb |
| 571 |
|
| 572 |
#define ASCII_START 32 |
| 573 |
#define ASCII_END 126 |
| 574 |
|
| 575 |
static const unsigned char *get_font_data_from_char(char c) { |
| 576 |
if (c < ASCII_START || c > ASCII_END) { |
| 577 |
c = '?'; |
| 578 |
} |
| 579 |
int offset = (c-ASCII_START) * 5; |
| 580 |
return THE_FONT + offset; |
| 581 |
} |
| 582 |
|
| 583 |
static mp_int_t get_pixel_from_font_data(const unsigned char *data, int x, int y) { |
| 584 |
/* The following logic belongs in MicroBitFont */ |
| 585 |
return ((data[y]>>(4-x))&1); |
| 586 |
} |
| 587 |
|
| 588 |
void microbit_image_set_from_char(greyscale_t *img, char c) { |
| 589 |
const unsigned char *data = get_font_data_from_char(c); |
| 590 |
for (int x = 0; x < 5; ++x) { |
| 591 |
for (int y = 0; y < 5; ++y) { |
| 592 |
greyscaleSetPixelValue(img, x, y, get_pixel_from_font_data(data, x, y)*MAX_BRIGHTNESS); |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
microbit_image_obj_t *microbit_image_for_char(char c) { |
| 599 |
greyscale_t *result = greyscale_new(5,5); |
| 600 |
microbit_image_set_from_char(result, c); |
| 601 |
return (microbit_image_obj_t *)result; |
| 602 |
} |
| 603 |
|
| 604 |
#if MICROPY_PY_BUILTINS_FLOAT |
| 605 |
microbit_image_obj_t *microbit_image_dim(microbit_image_obj_t *lhs, mp_float_t fval) { |
| 606 |
#else |
| 607 |
microbit_image_obj_t *microbit_image_dim(microbit_image_obj_t *lhs, mp_int_t fval) { |
| 608 |
#endif |
| 609 |
if (fval < 0) |
| 610 |
mp_raise_ValueError(MP_ERROR_TEXT("Brightness multiplier must not be negative.")); |
| 611 |
greyscale_t *result = greyscale_new(imageWidth(lhs), imageHeight(lhs)); |
| 612 |
for (int x = 0; x < imageWidth(lhs); ++x) { |
| 613 |
for (int y = 0; y < imageWidth(lhs); ++y) { |
| 614 |
#if MICROPY_PY_BUILTINS_FLOAT |
| 615 |
int val = min((int)imageGetPixelValue(lhs, x,y)*fval+0.5, MAX_BRIGHTNESS); |
| 616 |
#else |
| 617 |
int val = min((int)imageGetPixelValue(lhs, x,y)*fval, MAX_BRIGHTNESS); |
| 618 |
#endif |
| 619 |
greyscaleSetPixelValue(result, x, y, val); |
| 620 |
} |
| 621 |
} |
| 622 |
return (microbit_image_obj_t *)result; |
| 623 |
} |
| 624 |
|
| 625 |
microbit_image_obj_t *microbit_image_sum(microbit_image_obj_t *lhs, microbit_image_obj_t *rhs, bool add) { |
| 626 |
mp_int_t h = imageHeight(lhs); |
| 627 |
mp_int_t w = imageWidth(lhs); |
| 628 |
if (imageHeight(rhs) != h || imageWidth(lhs) != w) { |
| 629 |
// TODO: verify that image width in test above should really test (lhs != w) |
| 630 |
mp_raise_ValueError(MP_ERROR_TEXT("Images must be the same size.")); |
| 631 |
} |
| 632 |
greyscale_t *result = greyscale_new(w, h); |
| 633 |
for (int x = 0; x < w; ++x) { |
| 634 |
for (int y = 0; y < h; ++y) { |
| 635 |
int val; |
| 636 |
int lval = imageGetPixelValue(lhs, x,y); |
| 637 |
int rval = imageGetPixelValue(rhs, x,y); |
| 638 |
if (add) |
| 639 |
val = min(lval + rval, MAX_BRIGHTNESS); |
| 640 |
else |
| 641 |
val = max(0, lval - rval); |
| 642 |
greyscaleSetPixelValue(result, x, y, val); |
| 643 |
} |
| 644 |
} |
| 645 |
return (microbit_image_obj_t *)result; |
| 646 |
} |
| 647 |
|
| 648 |
static mp_obj_t image_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
| 649 |
if (mp_obj_get_type(lhs_in) != µbit_image_type) { |
| 650 |
return MP_OBJ_NULL; // op not supported |
| 651 |
} |
| 652 |
microbit_image_obj_t *lhs = (microbit_image_obj_t *)lhs_in; |
| 653 |
switch(op) { |
| 654 |
case MP_BINARY_OP_ADD: |
| 655 |
case MP_BINARY_OP_SUBTRACT: |
| 656 |
break; |
| 657 |
case MP_BINARY_OP_MULTIPLY: |
| 658 |
#if MICROPY_PY_BUILTINS_FLOAT |
| 659 |
return microbit_image_dim(lhs, mp_obj_get_float(rhs_in)); |
| 660 |
#else |
| 661 |
return microbit_image_dim(lhs, mp_obj_get_int(rhs_in) * 10); |
| 662 |
#endif |
| 663 |
case MP_BINARY_OP_TRUE_DIVIDE: |
| 664 |
#if MICROPY_PY_BUILTINS_FLOAT |
| 665 |
return microbit_image_dim(lhs, 1.0/mp_obj_get_float(rhs_in)); |
| 666 |
#else |
| 667 |
break; |
| 668 |
case MP_BINARY_OP_FLOOR_DIVIDE: |
| 669 |
return microbit_image_dim(lhs, (100/mp_obj_get_int(rhs_in) + 5) / 10); |
| 670 |
#endif |
| 671 |
default: |
| 672 |
return MP_OBJ_NULL; // op not supported |
| 673 |
} |
| 674 |
if (mp_obj_get_type(rhs_in) != µbit_image_type) { |
| 675 |
return MP_OBJ_NULL; // op not supported |
| 676 |
} |
| 677 |
return microbit_image_sum(lhs, (microbit_image_obj_t *)rhs_in, op == MP_BINARY_OP_ADD); |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
MP_DEFINE_CONST_OBJ_TYPE( |
| 682 |
microbit_image_type, |
| 683 |
MP_QSTR_MicroBitImage, |
| 684 |
MP_TYPE_FLAG_NONE, |
| 685 |
make_new, microbit_image_make_new, |
| 686 |
print, microbit_image_print, |
| 687 |
binary_op, image_binary_op, |
| 688 |
locals_dict, µbit_image_locals_dict |
| 689 |
); |
| 690 |
|
| 691 |
typedef struct _scrolling_string_t { |
| 692 |
mp_obj_base_t base; |
| 693 |
char const *str; |
| 694 |
mp_uint_t len; |
| 695 |
mp_obj_t ref; |
| 696 |
bool monospace; |
| 697 |
bool repeat; |
| 698 |
} scrolling_string_t; |
| 699 |
|
| 700 |
typedef struct _scrolling_string_iterator_t { |
| 701 |
mp_obj_base_t base; |
| 702 |
mp_obj_t ref; |
| 703 |
greyscale_t *img; |
| 704 |
char const *next_char; |
| 705 |
char const *start; |
| 706 |
char const *end; |
| 707 |
uint8_t offset; |
| 708 |
uint8_t offset_limit; |
| 709 |
bool monospace; |
| 710 |
bool repeat; |
| 711 |
char right; |
| 712 |
} scrolling_string_iterator_t; |
| 713 |
|
| 714 |
extern const mp_obj_type_t microbit_scrolling_string_type; |
| 715 |
extern const mp_obj_type_t microbit_scrolling_string_iterator_type; |
| 716 |
|
| 717 |
mp_obj_t scrolling_string_image_iterable(const char* str, mp_uint_t len, mp_obj_t ref, bool monospace, bool repeat) { |
| 718 |
scrolling_string_t *result = mp_obj_malloc(scrolling_string_t, µbit_scrolling_string_type); |
| 719 |
result->str = str; |
| 720 |
result->len = len; |
| 721 |
result->ref = ref; |
| 722 |
result->monospace = monospace; |
| 723 |
result->repeat = repeat; |
| 724 |
return result; |
| 725 |
} |
| 726 |
|
| 727 |
static int font_column_non_blank(const unsigned char *font_data, unsigned int col) { |
| 728 |
for (int y = 0; y < 5; ++y) { |
| 729 |
if (get_pixel_from_font_data(font_data, col, y)) { |
| 730 |
return 1; |
| 731 |
} |
| 732 |
} |
| 733 |
return 0; |
| 734 |
} |
| 735 |
|
| 736 |
/* Not strictly the rightmost non-blank column, but the rightmost in columns 2,3 or 4. */ |
| 737 |
static unsigned int rightmost_non_blank_column(const unsigned char *font_data) { |
| 738 |
if (font_column_non_blank(font_data, 4)) { |
| 739 |
return 4; |
| 740 |
} |
| 741 |
if (font_column_non_blank(font_data, 3)) { |
| 742 |
return 3; |
| 743 |
} |
| 744 |
return 2; |
| 745 |
} |
| 746 |
|
| 747 |
static void restart(scrolling_string_iterator_t *iter) { |
| 748 |
iter->next_char = iter->start; |
| 749 |
iter->offset = 0; |
| 750 |
if (iter->start < iter->end) { |
| 751 |
iter->right = *iter->next_char; |
| 752 |
if (iter->monospace) { |
| 753 |
iter->offset_limit = 5; |
| 754 |
} else { |
| 755 |
iter->offset_limit = rightmost_non_blank_column(get_font_data_from_char(iter->right)) + 1; |
| 756 |
} |
| 757 |
} else { |
| 758 |
iter->right = ' '; |
| 759 |
iter->offset_limit = 5; |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
static mp_obj_t get_microbit_scrolling_string_iter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { |
| 764 |
(void)iter_buf; |
| 765 |
scrolling_string_t *str = (scrolling_string_t *)o_in; |
| 766 |
scrolling_string_iterator_t *result = mp_obj_malloc(scrolling_string_iterator_t, µbit_scrolling_string_iterator_type); |
| 767 |
result->img = greyscale_new(5,5); |
| 768 |
result->start = str->str; |
| 769 |
result->ref = str->ref; |
| 770 |
result->monospace = str->monospace; |
| 771 |
result->end = result->start + str->len; |
| 772 |
result->repeat = str->repeat; |
| 773 |
restart(result); |
| 774 |
return result; |
| 775 |
} |
| 776 |
|
| 777 |
static mp_obj_t microbit_scrolling_string_iter_next(mp_obj_t o_in) { |
| 778 |
scrolling_string_iterator_t *iter = (scrolling_string_iterator_t *)o_in; |
| 779 |
if (iter->next_char == iter->end && iter->offset == 5) { |
| 780 |
if (iter->repeat) { |
| 781 |
restart(iter); |
| 782 |
greyscaleClear(iter->img); |
| 783 |
} else { |
| 784 |
return MP_OBJ_STOP_ITERATION; |
| 785 |
} |
| 786 |
} |
| 787 |
for (int x = 0; x < 4; x++) { |
| 788 |
for (int y = 0; y < 5; y++) { |
| 789 |
greyscaleSetPixelValue(iter->img, x, y, greyscaleGetPixelValue(iter->img, x+1, y)); |
| 790 |
} |
| 791 |
} |
| 792 |
for (int y = 0; y < 5; y++) { |
| 793 |
greyscaleSetPixelValue(iter->img, 4, y, 0); |
| 794 |
} |
| 795 |
const unsigned char *font_data; |
| 796 |
if (iter->offset < iter->offset_limit) { |
| 797 |
font_data = get_font_data_from_char(iter->right); |
| 798 |
for (int y = 0; y < 5; ++y) { |
| 799 |
int pix = get_pixel_from_font_data(font_data, iter->offset, y)*MAX_BRIGHTNESS; |
| 800 |
greyscaleSetPixelValue(iter->img, 4, y, pix); |
| 801 |
} |
| 802 |
} else if (iter->offset == iter->offset_limit) { |
| 803 |
++iter->next_char; |
| 804 |
if (iter->next_char == iter->end) { |
| 805 |
iter->right = ' '; |
| 806 |
iter->offset_limit = 5; |
| 807 |
iter->offset = 0; |
| 808 |
} else { |
| 809 |
iter->right = *iter->next_char; |
| 810 |
font_data = get_font_data_from_char(iter->right); |
| 811 |
if (iter->monospace) { |
| 812 |
iter->offset = -1; |
| 813 |
iter->offset_limit = 5; |
| 814 |
} else { |
| 815 |
iter->offset = -font_column_non_blank(font_data, 0); |
| 816 |
iter->offset_limit = rightmost_non_blank_column(font_data)+1; |
| 817 |
} |
| 818 |
} |
| 819 |
} |
| 820 |
++iter->offset; |
| 821 |
return iter->img; |
| 822 |
} |
| 823 |
|
| 824 |
MP_DEFINE_CONST_OBJ_TYPE( |
| 825 |
microbit_scrolling_string_type, |
| 826 |
MP_QSTR_ScrollingString, |
| 827 |
MP_TYPE_FLAG_ITER_IS_GETITER, |
| 828 |
iter, get_microbit_scrolling_string_iter |
| 829 |
); |
| 830 |
|
| 831 |
MP_DEFINE_CONST_OBJ_TYPE( |
| 832 |
microbit_scrolling_string_iterator_type, |
| 833 |
MP_QSTR_iterator, |
| 834 |
MP_TYPE_FLAG_ITER_IS_ITERNEXT, |
| 835 |
iter, microbit_scrolling_string_iter_next |
| 836 |
); |
| 837 |
|
| 838 |
/** Facade types to present a string as a sequence of images. |
| 839 |
* These are necessary to avoid allocation during iteration, |
| 840 |
* which may happen in interrupt handlers. |
| 841 |
*/ |
| 842 |
|
| 843 |
typedef struct _string_image_facade_t { |
| 844 |
mp_obj_base_t base; |
| 845 |
mp_obj_t string; |
| 846 |
greyscale_t *image; |
| 847 |
} string_image_facade_t; |
| 848 |
|
| 849 |
static mp_obj_t string_image_facade_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) { |
| 850 |
if (value == MP_OBJ_SENTINEL) { |
| 851 |
// Fill in image |
| 852 |
string_image_facade_t *self = (string_image_facade_t *)self_in; |
| 853 |
size_t len; |
| 854 |
const char *text = mp_obj_str_get_data(self->string, &len); |
| 855 |
mp_uint_t index = mp_get_index(self->base.type, len, index_in, false); |
| 856 |
microbit_image_set_from_char(self->image, text[index]); |
| 857 |
return self->image; |
| 858 |
} else { |
| 859 |
return MP_OBJ_NULL; // op not supported |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
static mp_obj_t facade_unary_op(mp_unary_op_t op, mp_obj_t self_in) { |
| 864 |
string_image_facade_t *self = (string_image_facade_t *)self_in; |
| 865 |
switch (op) { |
| 866 |
case MP_UNARY_OP_LEN: |
| 867 |
return mp_obj_len(self->string); |
| 868 |
default: return MP_OBJ_NULL; // op not supported |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
static mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in, mp_obj_iter_buf_t *iter_buf); |
| 873 |
|
| 874 |
MP_DEFINE_CONST_OBJ_TYPE( |
| 875 |
string_image_facade_type, |
| 876 |
MP_QSTR_Facade, |
| 877 |
MP_TYPE_FLAG_ITER_IS_GETITER, |
| 878 |
unary_op, facade_unary_op, |
| 879 |
subscr, string_image_facade_subscr, |
| 880 |
iter, microbit_facade_iterator |
| 881 |
); |
| 882 |
|
| 883 |
|
| 884 |
typedef struct _facade_iterator_t { |
| 885 |
mp_obj_base_t base; |
| 886 |
mp_obj_t string; |
| 887 |
mp_uint_t index; |
| 888 |
greyscale_t *image; |
| 889 |
} facade_iterator_t; |
| 890 |
|
| 891 |
mp_obj_t microbit_string_facade(mp_obj_t string) { |
| 892 |
string_image_facade_t *result = mp_obj_malloc(string_image_facade_t, &string_image_facade_type); |
| 893 |
result->string = string; |
| 894 |
result->image = greyscale_new(5,5); |
| 895 |
return result; |
| 896 |
} |
| 897 |
|
| 898 |
static mp_obj_t microbit_facade_iter_next(mp_obj_t iter_in) { |
| 899 |
facade_iterator_t *iter = (facade_iterator_t *)iter_in; |
| 900 |
size_t len; |
| 901 |
const char *text = mp_obj_str_get_data(iter->string, &len); |
| 902 |
if (iter->index >= len) { |
| 903 |
return MP_OBJ_STOP_ITERATION; |
| 904 |
} |
| 905 |
microbit_image_set_from_char(iter->image, text[iter->index]); |
| 906 |
iter->index++; |
| 907 |
return iter->image; |
| 908 |
} |
| 909 |
|
| 910 |
MP_DEFINE_CONST_OBJ_TYPE( |
| 911 |
microbit_facade_iterator_type, |
| 912 |
MP_QSTR_iterator, |
| 913 |
MP_TYPE_FLAG_ITER_IS_ITERNEXT, |
| 914 |
iter, microbit_facade_iter_next |
| 915 |
); |
| 916 |
|
| 917 |
mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in, mp_obj_iter_buf_t *iter_buf) { |
| 918 |
(void)iter_buf; |
| 919 |
facade_iterator_t *result = m_new_obj(facade_iterator_t); |
| 920 |
string_image_facade_t *iterable = (string_image_facade_t *)iterable_in; |
| 921 |
result->base.type = µbit_facade_iterator_type; |
| 922 |
result->string = iterable->string; |
| 923 |
result->image = iterable->image; |
| 924 |
result->index = 0; |
| 925 |
return result; |
| 926 |
} |