-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathimage.cpp
More file actions
769 lines (638 loc) · 23.3 KB
/
image.cpp
File metadata and controls
769 lines (638 loc) · 23.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
#include "mp_helpers.hpp"
#include "picovector.hpp"
extern "C" {
#include "py/stream.h"
#include "py/reader.h"
#include "py/runtime.h"
#include "py/objstr.h"
#include "psram_ops.h"
mp_obj_t image__del__(mp_obj_t self_in) {
self(self_in, image_obj_t);
if(self->image) {
//self->image->delete_palette();
m_del_class(image_t, self->image);
}
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(image__del___obj, image__del__);
MPY_BIND_NEW(image, {
image_obj_t *self = mp_obj_malloc_with_finaliser(image_obj_t, type);
int w = mp_obj_get_int(args[0]);
int h = mp_obj_get_int(args[1]);
if (n_args > 2) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_WRITE);
self->image = new(m_malloc(sizeof(image_t))) image_t(bufinfo.buf, w, h);
} else {
self->image = new(m_malloc(sizeof(image_t))) image_t(w, h);
// Clear new image to black, fully transparent. This should potentially
// be removed once we have a copy brush or other way to clear alpha.
psram_memset32(self->image->ptr(0, 0), 0, self->image->buffer_size() >> 2);
}
return MP_OBJ_FROM_PTR(self);
})
void image_open_helper(image_obj_t &target, mp_obj_t path_or_bytes_in, int target_width, int target_height) {
int status = 0;
if(mp_obj_is_str(path_or_bytes_in)) {
const char *path = mp_obj_str_get_str(path_or_bytes_in);
status = pngdec_open_file(target, path, target_width, target_height);
if(status == PNG_INVALID_FILE) {
status = jpegdec_open_file(target, path, target_width, target_height);
}
} else {
mp_buffer_info_t buf;
mp_get_buffer_raise(path_or_bytes_in, &buf, MP_BUFFER_READ);
status = pngdec_open_ram(target, buf.buf, buf.len, target_width, target_height);
if(status == PNG_INVALID_FILE) {
status = jpegdec_open_ram(target, buf.buf, buf.len, target_width, target_height);
}
}
if(status != 0) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("unable to read image! %d"), status);
}
}
MPY_BIND_STATICMETHOD_VAR(1, load, {
image_obj_t *result = mp_obj_malloc_with_finaliser(image_obj_t, &type_image);
result->image = nullptr;
int target_width = n_args >= 2 ? (int)mp_obj_get_float(args[1]) : 0;
int target_height = n_args >= 3 ? (int)mp_obj_get_float(args[2]) : 0;
image_open_helper(*result, args[0], target_width, target_height);
return MP_OBJ_FROM_PTR(result);
})
MPY_BIND_CLASSMETHOD_ARGS1(load_into, path_or_bytes_in, {
self(self_in, image_obj_t);
image_open_helper(*self, path_or_bytes_in, 0, 0);
return mp_const_none;
})
MPY_BIND_VAR(2, window, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
int x;
int y;
int w;
int h;
if (mp_obj_is_type(args[1], &type_rect)) {
const rect_obj_t *rect = (rect_obj_t *)MP_OBJ_TO_PTR(args[1]);
x = rect->r.x;
y = rect->r.y;
w = rect->r.w;
h = rect->r.h;
}else{
x = mp_obj_get_float(args[1]);
y = mp_obj_get_float(args[2]);
w = mp_obj_get_float(args[3]);
h = mp_obj_get_float(args[4]);
}
image_obj_t *result = mp_obj_malloc_with_finaliser(image_obj_t, &type_image);
result->image = new(m_malloc(sizeof(image_t))) image_t(self->image, rect_t(x, y, w, h));
result->parent = (void*)self;
return MP_OBJ_FROM_PTR(result);
})
MPY_BIND_VAR(2, shape, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
if (mp_obj_is_type(args[1], &type_shape)) {
const shape_obj_t *shape = (shape_obj_t *)MP_OBJ_TO_PTR(args[1]);
self->image->shape(shape->shape);
return mp_const_none;
}
if (mp_obj_is_type(args[1], &mp_type_list)) {
size_t len;
mp_obj_t *items;
mp_obj_list_get(args[1], &len, &items);
for(size_t i = 0; i < len; i++) {
const shape_obj_t *shape = (shape_obj_t *)MP_OBJ_TO_PTR(items[i]);
self->image->shape(shape->shape);
}
return mp_const_none;
}
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid parameters, expected either shape(s) or shape([s1, s2, s3, ...])"));
})
MPY_BIND_VAR(2, rectangle, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
if(mp_obj_is_rect(args[1])) {
self->image->rectangle(mp_obj_get_rect(args[1]));
return mp_const_none;
}
if(n_args == 5) {
self->image->rectangle(mp_obj_get_rect_from_xywh(&args[1]));
return mp_const_none;
}
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid parameters, expected either rectangle(r) or rectangle(x, y, w, h)"));
})
MPY_BIND_VAR(3, line, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
if(n_args == 3 && mp_obj_is_vec2(args[1]) && mp_obj_is_vec2(args[2])) {
vec2_t p1 = mp_obj_get_vec2(args[1]);
vec2_t p2 = mp_obj_get_vec2(args[2]);
self->image->line(p1, p2);
return mp_const_none;
}
if(n_args == 5) {
int x1 = mp_obj_get_float(args[1]);
int y1 = mp_obj_get_float(args[2]);
int x2 = mp_obj_get_float(args[3]);
int y2 = mp_obj_get_float(args[4]);
self->image->line(vec2_t(x1, y1), vec2_t(x2, y2));
return mp_const_none;
}
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid parameters, expected either line(p1, p2) or line(x1, y1, x2, y2)"));
})
MPY_BIND_VAR(3, circle, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
if(mp_obj_is_vec2(args[1])) {
vec2_t p = mp_obj_get_vec2(args[1]);
float r = mp_obj_get_float(args[2]);
self->image->circle(p, r);
return mp_const_none;
}
if(n_args == 4) {
int x = mp_obj_get_float(args[1]);
int y = mp_obj_get_float(args[2]);
int r = mp_obj_get_float(args[3]);
self->image->circle(vec2_t(x, y), r);
return mp_const_none;
}
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid parameters, expected either circle(p, r) or circle(x, y, r)"));
})
MPY_BIND_VAR(4, triangle, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
if(n_args == 4 && mp_obj_is_vec2(args[1]) && mp_obj_is_vec2(args[2]) && mp_obj_is_vec2(args[3])) {
vec2_t p1 = mp_obj_get_vec2(args[1]);
vec2_t p2 = mp_obj_get_vec2(args[2]);
vec2_t p3 = mp_obj_get_vec2(args[3]);
self->image->triangle(p1, p2, p3);
return mp_const_none;
}
if(n_args == 7) {
vec2_t p1 = mp_obj_get_vec2_from_xy(&args[1]);
vec2_t p2 = mp_obj_get_vec2_from_xy(&args[3]);
vec2_t p3 = mp_obj_get_vec2_from_xy(&args[5]);
self->image->triangle(p1, p2, p3);
return mp_const_none;
}
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid parameters, expected either triangle(p1, p2, p3) or triangle(x1, y1, x2, y2, x3, y3)"));
})
MPY_BIND_VAR(2, blur, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
float radius = mp_obj_get_float(args[1]);
self->image->blur(radius);
return mp_const_none;
})
MPY_BIND_VAR(1, dither, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
self->image->dither();
return mp_const_none;
})
MPY_BIND_VAR(1, onebit, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
self->image->onebit();
return mp_const_none;
})
MPY_BIND_VAR(1, monochrome, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
self->image->monochrome();
return mp_const_none;
})
// do we want to allow this with premultiplied alpha?
// would we undo the multiply and return rgba?
MPY_BIND_VAR(2, get, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
vec2_t point;
if(mp_obj_is_vec2(args[1])) {
point = mp_obj_get_vec2(args[1]);
} else {
point = mp_obj_get_vec2_from_xy(&args[1]);
}
color_obj_t *color = mp_obj_malloc(color_obj_t, &type_color);
uint32_t c = self->image->get(point.x, point.y);
color->c = new rgb_color_t(_r(c), _g(c), _b(c), _a(c));
return MP_OBJ_FROM_PTR(color);
})
MPY_BIND_VAR(2, put, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
vec2_t point;
if(mp_obj_is_vec2(args[1])) {
point = mp_obj_get_vec2(args[1]);
} else {
point = mp_obj_get_vec2_from_xy(&args[1]);
}
self->image->put(point.x, point.y);
return mp_const_none;
})
MPY_BIND_VAR(3, text, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
const char *text = mp_obj_str_get_str(args[1]);
if(!self->font && !self->pixel_font) {
mp_raise_msg_varg(&mp_type_OSError, MP_ERROR_TEXT("target image has no font"));
}
vec2_t point;
int arg_offset;
if(mp_obj_is_vec2(args[2])) {
point = mp_obj_get_vec2(args[2]);
arg_offset = 3;
} else {
point = mp_obj_get_vec2_from_xy(&args[2]);
arg_offset = 4;
}
if(self->font) {
float size = mp_obj_get_float(args[arg_offset]);
self->image->font()->draw(self->image, text, point.x, point.y, size);
}
if(self->pixel_font) {
self->image->pixel_font()->draw(self->image, text, point.x, point.y);
}
return mp_const_none;
})
MPY_BIND_VAR(2, measure_text, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
const char *text = mp_obj_str_get_str(args[1]);
if(!self->font && !self->pixel_font) {
mp_raise_msg_varg(&mp_type_OSError, MP_ERROR_TEXT("target image has no font"));
}
mp_obj_t result[2];
if(self->font) {
float size = mp_obj_get_float(args[2]);
rect_t r = self->image->font()->measure(self->image, text, size);
result[0] = mp_obj_new_float(r.w);
result[1] = mp_obj_new_float(r.h);
}
if(self->pixel_font) {
rect_t r = self->image->pixel_font()->measure(self->image, text);
result[0] = mp_obj_new_float(r.w);
result[1] = mp_obj_new_float(r.h);
}
return mp_obj_new_tuple(2, result);
})
// MPY_BIND_VAR(2, shape, {
// const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
// if (mp_obj_is_type(args[1], &type_shape)) {
// const shape_obj_t *shape = (shape_obj_t *)MP_OBJ_TO_PTR(args[1]);
// self->image->shape(shape->shape);
// return mp_const_none;
// }
// if (mp_obj_is_type(args[1], &mp_type_list)) {
// size_t len;
// mp_obj_t *items;
// mp_obj_list_get(args[1], &len, &items);
// for(size_t i = 0; i < len; i++) {
// const shape_obj_t *shape = (shape_obj_t *)MP_OBJ_TO_PTR(items[i]);
// self->image->shape(shape->shape);
// }
// return mp_const_none;
// }
// mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid parameters, expected either shape(s) or shape([s1, s2, s3, ...])"));
// })
MPY_BIND_VAR(6, blit_vspan, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
const image_obj_t *src = (image_obj_t *)MP_OBJ_TO_PTR(args[1]);
vec2_t p; // position on screen to start rendering span
int c; // height of span
vec2_t uv0; // start uv coordinate
vec2_t uv1; // end uv coordinate
if(n_args == 6) {
p = mp_obj_get_vec2(args[2]);
c = mp_obj_get_float(args[3]);
uv0 = mp_obj_get_vec2(args[4]);
uv1 = mp_obj_get_vec2(args[5]);
} else if(n_args == 9) {
p.x = mp_obj_get_float(args[2]);
p.y = mp_obj_get_float(args[3]);
c = mp_obj_get_float(args[4]);
uv0.x = mp_obj_get_float(args[5]);
uv0.y = mp_obj_get_float(args[6]);
uv1.x = mp_obj_get_float(args[7]);
uv1.y = mp_obj_get_float(args[8]);
} else {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid parameters, expected either blit_vspan(p, c, uvs, uve) or blit_vspan(x, y, x, uvsx, uvsy, uvex, uvey)"));
}
src->image->blit_vspan(self->image, p, c, uv0, uv1);
return mp_const_none;
})
MPY_BIND_VAR(6, blit_hspan, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
const image_obj_t *src = (image_obj_t *)MP_OBJ_TO_PTR(args[1]);
vec2_t p; // position on screen to start rendering span
int c; // height of span
vec2_t uvs; // start uv coordinate
vec2_t uve; // end uv coordinate
if(n_args == 6) {
p = mp_obj_get_vec2(args[2]);
c = mp_obj_get_float(args[3]);
uvs = mp_obj_get_vec2(args[4]);
uve = mp_obj_get_vec2(args[5]);
} else if(n_args == 9) {
p.x = mp_obj_get_float(args[2]);
p.y = mp_obj_get_float(args[3]);
c = mp_obj_get_float(args[4]);
uvs.x = mp_obj_get_float(args[5]);
uvs.y = mp_obj_get_float(args[6]);
uve.x = mp_obj_get_float(args[7]);
uve.y = mp_obj_get_float(args[8]);
} else {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid parameters, expected either blit_hspan(p, c, uvs, uve) or blit_hspan(x, y, x, uvsx, uvsy, uvex, uvey)"));
}
src->image->blit_hspan(self->image, p, c, uvs, uve);
return mp_const_none;
})
MPY_BIND_VAR(3, blit, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
if(mp_obj_is_type(args[1], &type_image)) {
const image_obj_t *src = (image_obj_t *)MP_OBJ_TO_PTR(args[1]);
if(n_args == 3 && mp_obj_is_vec2(args[2])) {
src->image->blit(self->image, mp_obj_get_vec2(args[2]));
return mp_const_none;
}
if(n_args == 4 && mp_obj_is_float(args[2]) && mp_obj_is_float(args[3])) {
vec2_t p = mp_obj_get_vec2_from_xy(&args[2]);
src->image->blit(self->image, p);
return mp_const_none;
}
if(n_args == 3 && mp_obj_is_rect(args[2])) {
src->image->blit(self->image, mp_obj_get_rect(args[2]));
return mp_const_none;
}
if(n_args == 4 && mp_obj_is_rect(args[2]) && mp_obj_is_rect(args[3])) {
src->image->blit(self->image, mp_obj_get_rect(args[2]), mp_obj_get_rect(args[3]));
return mp_const_none;
}
}
mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("invalid parameter, expected blit(image, point), blit(image, rect) or blit(image, source_rect, dest_rect)"));
})
MPY_BIND_VAR(1, clear, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
self->image->clear();
return mp_const_none;
})
MPY_BIND_ATTR(image, {
self(self_in, image_obj_t);
action_t action = m_attr_action(dest);
switch(attr) {
case MP_QSTR_raw: {
if(action == GET) {
mp_obj_t raw = mp_obj_new_bytearray_by_ref(self->image->buffer_size(), self->image->ptr(0, 0));
dest[0] = raw;
return;
}
};
case MP_QSTR_clip: {
if(action == GET) {
rect_obj_t *result = mp_obj_malloc(rect_obj_t, &type_rect);
result->r = self->image->clip();
dest[0] = MP_OBJ_FROM_PTR(result);
return;
}
if(action == SET) {
if(!mp_obj_is_type(dest[1], &type_rect)) {
mp_raise_TypeError(MP_ERROR_TEXT("value must be of type rect"));
}
rect_obj_t * r = (rect_obj_t *)dest[1];
self->image->clip(r->r);
dest[0] = MP_OBJ_NULL;
return;
}
};
case MP_QSTR_width: {
if(action == GET) {
dest[0] = mp_obj_new_int(self->image->bounds().w);
return;
}
};
case MP_QSTR_height: {
if(action == GET) {
dest[0] = mp_obj_new_int(self->image->bounds().h);
return;
}
};
case MP_QSTR_has_palette: {
if(action == GET) {
dest[0] = mp_obj_new_bool(self->image->has_palette());
return;
}
};
case MP_QSTR_antialias: {
if(action == GET) {
dest[0] = mp_obj_new_int(self->image->antialias());
return;
}
if(action == SET) {
self->image->antialias((antialias_t)mp_obj_get_int(dest[1]));
dest[0] = MP_OBJ_NULL;
return;
}
};
case MP_QSTR_alpha: {
if(action == GET) {
dest[0] = mp_obj_new_int(self->image->alpha());
return;
}
if(action == SET) {
self->image->alpha((int)mp_obj_get_int(dest[1]));
dest[0] = MP_OBJ_NULL;
return;
}
};
case MP_QSTR_pen: {
if(action == GET) {
if(self->brush) {
dest[0] = self->brush;
}else{
dest[0] = mp_const_none;
}
return;
}
if(action == SET) {
brush_obj_t *brush = mp_obj_to_brush(1, &dest[1]);
if(!brush){
mp_raise_TypeError(MP_ERROR_TEXT("value must be of type brush or color"));
}
self->brush = brush;
self->image->brush(brush->brush);
dest[0] = MP_OBJ_NULL;
return;
}
};
case MP_QSTR_font: {
if(action == GET) {
if(self->font || self->pixel_font) {
if(self->font) {
dest[0] = MP_OBJ_FROM_PTR(self->font);
}else{
dest[0] = MP_OBJ_FROM_PTR(self->pixel_font);
}
}else{
dest[0] = mp_const_none;
}
return;
}
if(action == SET) {
if(!mp_obj_is_type(dest[1], &type_font) && !mp_obj_is_type(dest[1], &type_pixel_font)) {
mp_raise_TypeError(MP_ERROR_TEXT("value must be of type Font or PixelFont"));
}
if(mp_obj_is_type(dest[1], &type_font)) {
self->font = (font_obj_t *)dest[1];
self->pixel_font = nullptr;
self->image->font(&self->font->font);
}
if(mp_obj_is_type(dest[1], &type_pixel_font)) {
self->pixel_font = (pixel_font_obj_t *)dest[1];
self->font = nullptr;
self->image->pixel_font(self->pixel_font->font);
}
dest[0] = MP_OBJ_NULL;
return;
}
};
}
// we didn't handle this, fall back to alternative methods
dest[1] = MP_OBJ_SENTINEL;
})
static void image_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
self(self_in, image_obj_t);
mp_printf(print, "image(%d x %d)", int(self->image->bounds().w), int(self->image->bounds().h));
}
static mp_int_t image_get_framebuffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
self(self_in, image_obj_t);
bufinfo->buf = self->image->ptr(0, 0);
bufinfo->len = self->image->buffer_size();
bufinfo->typecode = 'B';
return 0;
}
/*
takes a list of commands to perform, each command is a tuple containing the
method/attribute name and a single argument or tuple of arguments.
[
("clear"), # calling a method with no parameters
("pen", color.blue), # setting an attribute
("rectangle", (10, 10, 50, 50)), # calling a raster draw method
("circle", (50, 50, 20)), # calling another raster draw method
"dither" # also calling a method with no parameters
]
*/
MPY_BIND_VAR(2, batch, {
const image_obj_t *self = (image_obj_t *)MP_OBJ_TO_PTR(args[0]);
if (!mp_obj_is_type(args[1], &mp_type_list)) {
mp_raise_TypeError(MP_ERROR_TEXT("invalid parameters, expected list of draw commands"));
}
// dummy args object we use to call method handlers
mp_obj_t handler_args[32];
handler_args[0] = args[0]; // set "self" ahead of time
// get the list of commands
size_t ncommands;
mp_obj_t *commands;
mp_obj_list_get(args[1], &ncommands, &commands);
for(size_t i = 0; i < ncommands; i++) {
// command can either be a tuple with a qstr and parameters, or just a
// qstr if the method takes no parameters
if(!mp_obj_is_type(commands[i], &mp_type_tuple)) {
mp_raise_TypeError(MP_ERROR_TEXT("list entries must be tuples in the format (command, parameter1, parameter2, ...)"));
}
// get the list of commands
size_t ncommand;
mp_obj_t *command;
mp_obj_tuple_get(commands[i], &ncommand, &command);
qstr name = mp_obj_str_get_qstr(command[0]);
size_t nparameters = ncommand - 1;
for(size_t j = 1; j < ncommand; j++) {
handler_args[j] = command[j];
}
if(nparameters == 1) {
// if a single value provided, attempt to use that to set an
// attribute
mp_obj_t dest[2];
dest[0] = MP_OBJ_SENTINEL;
dest[1] = handler_args[1];
image_attr(handler_args[0], name, dest);
if(dest[0] == MP_OBJ_NULL) {
// attribute was found and set, skip to next command
continue;
}
}
// handler is also passed reference to "self"
size_t nhandler_args = nparameters + 1;
switch (name) {
case MP_QSTR_clear: {
mpy_binding_clear(nhandler_args, handler_args);
} break;
case MP_QSTR_rectangle: {
mpy_binding_rectangle(nhandler_args, handler_args);
} break;
case MP_QSTR_line: {
mpy_binding_line(nhandler_args, handler_args);
} break;
case MP_QSTR_circle: {
mpy_binding_circle(nhandler_args, handler_args);
} break;
case MP_QSTR_triangle: {
mpy_binding_triangle(nhandler_args, handler_args);
} break;
case MP_QSTR_put: {
mpy_binding_put(nhandler_args, handler_args);
} break;
case MP_QSTR_blur: {
mpy_binding_blur(nhandler_args, handler_args);
} break;
case MP_QSTR_dither: {
mpy_binding_dither(nhandler_args, handler_args);
} break;
case MP_QSTR_shape: {
mpy_binding_shape(nhandler_args, handler_args);
} break;
case MP_QSTR_text: {
mpy_binding_text(nhandler_args, handler_args);
} break;
case MP_QSTR_blit_vspan: {
mpy_binding_blit_vspan(nhandler_args, handler_args);
} break;
case MP_QSTR_blit_hspan: {
mpy_binding_blit_hspan(nhandler_args, handler_args);
} break;
case MP_QSTR_blit: {
mpy_binding_blit(nhandler_args, handler_args);
} break;
default: mp_raise_ValueError(MP_ERROR_TEXT("unknown method"));
}
}
return mp_const_none;
})
MPY_BIND_LOCALS_DICT(image,
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&image__del___obj) },
MPY_BIND_ROM_PTR_STATIC(load),
MPY_BIND_ROM_PTR(load_into),
MPY_BIND_ROM_PTR(window),
// primitives
MPY_BIND_ROM_PTR(clear),
MPY_BIND_ROM_PTR(rectangle),
MPY_BIND_ROM_PTR(line),
MPY_BIND_ROM_PTR(circle),
MPY_BIND_ROM_PTR(triangle),
MPY_BIND_ROM_PTR(get),
MPY_BIND_ROM_PTR(put),
MPY_BIND_ROM_PTR(blur),
MPY_BIND_ROM_PTR(dither),
MPY_BIND_ROM_PTR(onebit),
MPY_BIND_ROM_PTR(monochrome),
// vector
MPY_BIND_ROM_PTR(shape),
// text
MPY_BIND_ROM_PTR(text),
MPY_BIND_ROM_PTR(measure_text),
// blitting
MPY_BIND_ROM_PTR(blit_vspan),
MPY_BIND_ROM_PTR(blit_hspan),
MPY_BIND_ROM_PTR(blit),
MPY_BIND_ROM_PTR(batch),
// TODO: Just define these in MicroPython?
{ MP_ROM_QSTR(MP_QSTR_X4), MP_ROM_INT(antialias_t::X4)},
{ MP_ROM_QSTR(MP_QSTR_X2), MP_ROM_INT(antialias_t::X2)},
{ MP_ROM_QSTR(MP_QSTR_OFF), MP_ROM_INT(antialias_t::OFF)},
)
MP_DEFINE_CONST_OBJ_TYPE(
type_image,
MP_QSTR_image,
MP_TYPE_FLAG_NONE,
make_new, (const void *)image_new,
print, (const void *)image_print,
attr, (const void *)image_attr,
buffer, (const void *)image_get_framebuffer,
locals_dict, &image_locals_dict
);
}