Skip to content

Commit 0d02d9b

Browse files
committed
UI: implemented image.draw(), fix image.save in andoid #115
1 parent 4596fb0 commit 0d02d9b

File tree

7 files changed

+65
-24
lines changed

7 files changed

+65
-24
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2021-06-06 (12.22)
2+
UI: implemented image.draw(), fix image.save in andoid #115
3+
14
2021-06-05 (12.22)
25
COMMON: Fixes 'Socket Client doesn't receive byte with value 13' #112
36
COMMON: Fixes TSAVE of arrays includes extra null character. #119

src/ui/ansiwidget.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ void AnsiWidget::drawEllipse(int xc, int yc, int rx, int ry, int fill) {
139139
flush(false, false, MAX_PENDING_GRAPHICS);
140140
}
141141

142+
void AnsiWidget::drawImage(ImageDisplay &image) {
143+
_back->drawImage(image);
144+
flush(false, false, MAX_PENDING_GRAPHICS);
145+
}
146+
142147
// draw a line onto the offscreen buffer
143148
void AnsiWidget::drawLine(int x1, int y1, int x2, int y2) {
144149
_back->drawLine(x1, y1, x2, y2);

src/ui/ansiwidget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct AnsiWidget {
4141
void draw();
4242
void drawArc(int xc, int yc, double r, double start, double end, double aspect);
4343
void drawEllipse(int xc, int yc, int rx, int ry, int fill);
44+
void drawImage(ImageDisplay &image);
4445
void drawOverlay(bool vscroll) { _back->drawOverlay(vscroll); }
4546
void drawLine(int x1, int y1, int x2, int y2);
4647
void drawRect(int x1, int y1, int x2, int y2);

src/ui/image.cpp

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -301,56 +301,70 @@ ImageBuffer *load_xpm_image(char **data) {
301301
return result;
302302
}
303303

304-
void cmd_image_show(var_s *self, var_s *) {
305-
ImageDisplay image;
306-
image._bid = map_get_int(self, IMG_BID, -1);
304+
void get_image_display(var_s *self, ImageDisplay *image) {
305+
image->_bid = map_get_int(self, IMG_BID, -1);
307306

308307
List_each(ImageBuffer *, it, cache) {
309308
ImageBuffer *next = (*it);
310-
if (next->_bid == image._bid) {
311-
image._buffer = next;
309+
if (next->_bid == image->_bid) {
310+
image->_buffer = next;
312311
break;
313312
}
314313
}
315314

316315
var_int_t x, y, z, op;
317316
int count = par_massget("iiii", &x, &y, &z, &op);
318317

319-
if (prog_error || image._buffer == nullptr || count == 1 || count > 4) {
318+
if (prog_error || image->_buffer == nullptr || count == 1 || count > 4) {
320319
err_throw(ERR_PARAM);
321320
} else {
322321
// 0, 2, 3, 4 arguments accepted
323322
if (count >= 2) {
324-
image._x = x;
325-
image._y = y;
323+
image->_x = x;
324+
image->_y = y;
326325
map_set_int(self, IMG_X, x);
327326
map_set_int(self, IMG_Y, y);
328327
} else {
329-
image._x = map_get_int(self, IMG_X, -1);
330-
image._y = map_get_int(self, IMG_Y, -1);
328+
image->_x = map_get_int(self, IMG_X, -1);
329+
image->_y = map_get_int(self, IMG_Y, -1);
331330
}
332331
if (count >= 3) {
333-
image._zIndex = z;
332+
image->_zIndex = z;
334333
map_set_int(self, IMG_ZINDEX, z);
335334
} else {
336-
image._zIndex = map_get_int(self, IMG_ZINDEX, -1);
335+
image->_zIndex = map_get_int(self, IMG_ZINDEX, -1);
337336
}
338337
if (count == 4) {
339-
image._opacity = op;
338+
image->_opacity = op;
340339
map_set_int(self, IMG_OPACITY, op);
341340
} else {
342-
image._opacity = map_get_int(self, IMG_OPACITY, -1);
341+
image->_opacity = map_get_int(self, IMG_OPACITY, -1);
343342
}
344343

345-
image._offsetLeft = map_get_int(self, IMG_OFFSET_LEFT, -1);
346-
image._offsetTop = map_get_int(self, IMG_OFFSET_TOP, -1);
347-
image._width = map_get_int(self, IMG_WIDTH, -1);
348-
image._height = map_get_int(self, IMG_HEIGHT, -1);
349-
image._id = map_get_int(self, IMG_ID, -1);
344+
image->_offsetLeft = map_get_int(self, IMG_OFFSET_LEFT, -1);
345+
image->_offsetTop = map_get_int(self, IMG_OFFSET_TOP, -1);
346+
image->_width = map_get_int(self, IMG_WIDTH, -1);
347+
image->_height = map_get_int(self, IMG_HEIGHT, -1);
348+
image->_id = map_get_int(self, IMG_ID, -1);
349+
}
350+
}
351+
352+
void cmd_image_show(var_s *self, var_s *) {
353+
ImageDisplay image;
354+
get_image_display(self, &image);
355+
if (!prog_error) {
350356
g_system->getOutput()->addImage(image);
351357
}
352358
}
353359

360+
void cmd_image_draw(var_s *self, var_s *) {
361+
ImageDisplay image;
362+
get_image_display(self, &image);
363+
if (!prog_error) {
364+
g_system->getOutput()->drawImage(image);
365+
}
366+
}
367+
354368
void cmd_image_hide(var_s *self, var_s *) {
355369
int id = map_get_int(self, IMG_ID, -1);
356370
g_system->getOutput()->removeImage(id);
@@ -397,9 +411,18 @@ void cmd_image_save(var_s *self, var_s *) {
397411
int yoffs = (4 * y * w);
398412
for (int x = 0; x < w; x++) {
399413
int offs = yoffs + (4 * x);
400-
uint8_t r = image->_image[offs + 0];
401-
uint8_t g = image->_image[offs + 1];
402-
uint8_t b = image->_image[offs + 2];
414+
#if defined(PIXELFORMAT_RGBA8888)
415+
int r_offs = offs + 2;
416+
int g_offs = offs + 1;
417+
int b_offs = offs + 0;
418+
#else
419+
int r_offs = offs + 0;
420+
int g_offs = offs + 1;
421+
int b_offs = offs + 2;
422+
#endif
423+
uint8_t r = image->_image[r_offs];
424+
uint8_t g = image->_image[g_offs];
425+
uint8_t b = image->_image[b_offs];
403426
pixel_t px = SET_RGB(r, g, b);
404427
int pos = y * w + x;
405428
var_t *elem = v_elem(array, pos);
@@ -427,9 +450,10 @@ void create_image(var_p_t var, ImageBuffer *image) {
427450
map_add_var(var, IMG_WIDTH, image->_width);
428451
map_add_var(var, IMG_HEIGHT, image->_height);
429452
map_add_var(var, IMG_BID, image->_bid);
430-
v_create_func(var, "show", cmd_image_show);
453+
v_create_func(var, "draw", cmd_image_draw);
431454
v_create_func(var, "hide", cmd_image_hide);
432455
v_create_func(var, "save", cmd_image_save);
456+
v_create_func(var, "show", cmd_image_show);
433457
}
434458

435459
// loads an image for the form image input type

src/ui/rgb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ inline void GET_ARGB(pixel_t c, uint8_t &a, uint8_t &r, uint8_t &g, uint8_t &b)
5151
#define SET_RGB(r, g, b) ((r << 16) | (g << 8) | (b))
5252
#define GET_RGB RGB888_to_RGB
5353
#define GET_RGB2 RGB888_to_RGB
54-
#define GET_FROM_RGB888(c) (c)
54+
#define GET_FROM_RGB888(c) (c)
5555
#endif
5656

5757
#endif

src/ui/screen.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,11 @@ void GraphicScreen::drawEllipse(int xc, int yc, int rx, int ry, int fill) {
555555
maEllipse(xc, yc, rx, ry, fill);
556556
}
557557

558+
void GraphicScreen::drawImage(ImageDisplay &image) {
559+
drawInto();
560+
image.draw(image._x, image._y, image._width, image._height, 0);
561+
}
562+
558563
void GraphicScreen::drawInto(bool background) {
559564
maSetDrawTarget(_image);
560565
Screen::drawInto(background);

src/ui/screen.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct Screen : public Shape {
3434
virtual void drawArc(int xc, int yc, double r, double start, double end, double aspect) = 0;
3535
virtual void drawBase(bool vscroll, bool update=true) = 0;
3636
virtual void drawEllipse(int xc, int yc, int rx, int ry, int fill) = 0;
37+
virtual void drawImage(ImageDisplay &image) = 0;
3738
virtual void drawInto(bool background=false);
3839
virtual void drawLine(int x1, int y1, int x2, int y2) = 0;
3940
virtual void drawRect(int x1, int y1, int x2, int y2) = 0;
@@ -107,6 +108,7 @@ struct GraphicScreen : public Screen {
107108
void drawArc(int xc, int yc, double r, double start, double end, double aspect);
108109
void drawBase(bool vscroll, bool update=true);
109110
void drawEllipse(int xc, int yc, int rx, int ry, int fill);
111+
void drawImage(ImageDisplay &image);
110112
void drawInto(bool background=false);
111113
void drawLine(int x1, int y1, int x2, int y2);
112114
void drawRect(int x1, int y1, int x2, int y2);
@@ -326,6 +328,7 @@ struct TextScreen : public Screen {
326328
void clear();
327329
void drawArc(int xc, int yc, double r, double start, double end, double aspect) {}
328330
void drawBase(bool vscroll, bool update=true);
331+
void drawImage(ImageDisplay &image) {}
329332
void drawEllipse(int xc, int yc, int rx, int ry, int fill) {}
330333
void drawLine(int x1, int y1, int x2, int y2);
331334
void drawText(const char *text, int len, int x, int lineHeight);

0 commit comments

Comments
 (0)