@@ -191,6 +191,19 @@ uint8_t *get_image_data(int x, int y, int w, int h) {
191191 return result;
192192}
193193
194+
195+ ImageBuffer *get_image (unsigned bid) {
196+ ImageBuffer *result = nullptr ;
197+ List_each (ImageBuffer *, it, buffers) {
198+ ImageBuffer *next = (*it);
199+ if (next->_bid == (unsigned )bid) {
200+ result = next;
201+ break ;
202+ }
203+ }
204+ return result;
205+ }
206+
194207ImageBuffer *load_image (var_int_t x) {
195208 var_int_t y, w, h;
196209 int count = par_massget (" iii" , &y, &w, &h);
@@ -230,13 +243,7 @@ ImageBuffer *load_image(var_t *var) {
230243 if (var->type == V_MAP) {
231244 int bid = map_get_int (var, IMG_BID, -1 );
232245 if (bid != -1 ) {
233- List_each (ImageBuffer *, it, buffers) {
234- ImageBuffer *next = (*it);
235- if (next->_bid == (unsigned )bid) {
236- result = next;
237- break ;
238- }
239- }
246+ result = get_image ((unsigned )bid);
240247 }
241248 } else if (var->type == V_ARRAY && v_maxdim (var) == 2 ) {
242249 int w = ABS (v_lbound (var, 0 ) - v_ubound (var, 0 )) + 1 ;
@@ -406,6 +413,9 @@ void get_image_display(var_s *self, ImageDisplay *image) {
406413 }
407414}
408415
416+ //
417+ // png.show(x, y, zindex, opacity)
418+ //
409419void cmd_image_show (var_s *self, var_s *) {
410420 ImageDisplay image;
411421 get_image_display (self, &image);
@@ -414,6 +424,9 @@ void cmd_image_show(var_s *self, var_s *) {
414424 }
415425}
416426
427+ //
428+ // png.draw(x, y, opacity)
429+ //
417430void cmd_image_draw (var_s *self, var_s *) {
418431 ImageDisplay image;
419432 get_image_display (self, &image);
@@ -423,22 +436,23 @@ void cmd_image_draw(var_s *self, var_s *) {
423436 }
424437}
425438
439+ //
440+ // png.hide()
441+ //
426442void cmd_image_hide (var_s *self, var_s *) {
427443 int id = map_get_int (self, IMG_ID, -1 );
428444 g_system->getOutput ()->removeImage (id);
429445}
430446
447+ //
448+ // Output the image to a PNG file
449+ //
450+ // png.save("horse1.png")
451+ // png.save(#1)
452+ //
431453void cmd_image_save (var_s *self, var_s *) {
432454 unsigned id = map_get_int (self, IMG_BID, -1 );
433- ImageBuffer *image = nullptr ;
434- List_each (ImageBuffer *, it, buffers) {
435- ImageBuffer *next = (*it);
436- if (next->_bid == id) {
437- image = next;
438- break ;
439- }
440- }
441-
455+ ImageBuffer *image = get_image (id);
442456 var_t *array = nullptr ;
443457 dev_file_t *file = nullptr ;
444458 if (code_peek () == kwTYPE_SEP) {
@@ -481,13 +495,42 @@ void cmd_image_save(var_s *self, var_s *) {
481495 }
482496}
483497
498+
499+ //
500+ // Reduces the size of the image
501+ // arguments: left, top, right, bottom
502+ //
503+ // png.clip(10, 10, 10, 10)
504+ //
505+ void cmd_image_clip (var_s *self, var_s *) {
506+ bool updated = false ;
507+ if (self->type == V_MAP) {
508+ int bid = map_get_int (self, IMG_BID, -1 );
509+ if (bid != -1 ) {
510+ ImageBuffer *image = get_image ((unsigned )bid);
511+ var_int_t left, top, right, bottom;
512+ if (image != nullptr && par_massget (" iiii" , &left, &top, &right, &bottom) == 4 &&
513+ left >= 0 && left < image->_width && top >= 0 && top < image->_height ) {
514+ map_set_int (self, IMG_OFFSET_LEFT, left);
515+ map_set_int (self, IMG_OFFSET_TOP, top);
516+ map_set_int (self, IMG_WIDTH, right);
517+ map_set_int (self, IMG_HEIGHT, bottom);
518+ updated = true ;
519+ }
520+ }
521+ }
522+ if (!updated) {
523+ err_throw (ERR_PARAM);
524+ }
525+ }
526+
484527void create_image (var_p_t var, ImageBuffer *image) {
485528 map_init (var);
486529 map_add_var (var, IMG_X, 0 );
487530 map_add_var (var, IMG_Y, 0 );
488531 map_add_var (var, IMG_OFFSET_TOP, 0 );
489532 map_add_var (var, IMG_OFFSET_LEFT, 0 );
490- map_add_var (var, IMG_ZINDEX, 1 );
533+ map_add_var (var, IMG_ZINDEX, 100 );
491534 map_add_var (var, IMG_OPACITY, 0 );
492535 map_add_var (var, IMG_ID, ++nextId);
493536 map_add_var (var, IMG_WIDTH, image->_width );
@@ -497,6 +540,7 @@ void create_image(var_p_t var, ImageBuffer *image) {
497540 v_create_func (var, " hide" , cmd_image_hide);
498541 v_create_func (var, " save" , cmd_image_save);
499542 v_create_func (var, " show" , cmd_image_show);
543+ v_create_func (var, " clip" , cmd_image_clip);
500544}
501545
502546// loads an image for the form image input type
0 commit comments