Skip to content

Commit 744723b

Browse files
committed
Improvements for image.save()
1 parent f69893e commit 744723b

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

src/ui/image.cpp

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -450,17 +450,19 @@ void cmd_image_hide(var_s *self, var_s *) {
450450
}
451451

452452
//
453-
// Output the image to a PNG file
453+
// Output the image to a PNG file or an array
454454
//
455455
// png.save("horse1.png")
456456
// png.save(#1)
457+
// png.save(byref var)
458+
// file = "abc.png" : png.save(file)
457459
//
458460
void cmd_image_save(var_s *self, var_s *) {
459461
unsigned id = map_get_int(self, IMG_BID, -1);
460462
ImageBuffer *image = get_image(id);
461463
dev_file_t *file;
462-
var_t *array;
463-
var_t var;
464+
var_t *var;
465+
var_t str;
464466
bool saved = false;
465467
if (!prog_error && image != nullptr) {
466468
unsigned w = image->_width;
@@ -473,37 +475,43 @@ void cmd_image_save(var_s *self, var_s *) {
473475
saved = true;
474476
}
475477
break;
476-
case kwTYPE_VAR:
477-
array = par_getvar_ptr();
478-
v_tomatrix(array, h, w);
479-
// x0 x1 x2 (w=3,h=2)
480-
// y0 rgba rgba rgba ypos=0
481-
// y1 rgba rgba rgba ypos=12
482-
//
483-
for (unsigned y = 0; y < h; y++) {
484-
unsigned yoffs = (y * w * 4);
485-
for (unsigned x = 0; x < w; x++) {
486-
uint8_t a, r, g, b;
487-
GET_IMAGE_ARGB(image->_image, yoffs + (x * 4), a, r, g, b);
488-
pixel_t px = v_get_argb_px(a, r, g, b);
489-
unsigned pos = y * w + x;
490-
v_setint(v_elem(array, pos), px);
491-
}
492-
}
493-
saved = true;
478+
case kwTYPE_STR:
479+
par_getstr(&str);
480+
if (!prog_error &&
481+
!encode_png_file(str.v.p.ptr, image->_image, w, h)) {
482+
saved = true;
483+
}
484+
v_free(&str);
494485
break;
495486
default:
496-
v_init(&var);
497-
eval(&var);
498-
if (var.type == V_STR && !prog_error &&
499-
!lodepng_encode32_file(var.v.p.ptr, image->_image, w, h)) {
487+
var = par_getvar_ptr();
488+
if (var->type == V_STR && !prog_error &&
489+
!encode_png_file(var->v.p.ptr, image->_image, w, h)) {
500490
saved = true;
501-
}
502-
v_free(&var);
503-
break;
491+
} else if (!prog_error) {
492+
u_int32_t offsetLeft = map_get_int(self, IMG_OFFSET_LEFT, -1);
493+
u_int32_t offsetTop = map_get_int(self, IMG_OFFSET_TOP, -1);
494+
u_int32_t wClip = map_get_int(self, IMG_WIDTH, -1);
495+
u_int32_t hClip = map_get_int(self, IMG_HEIGHT, -1);
496+
v_tomatrix(var, hClip, wClip);
497+
// x0 x1 x2 (w=3,h=2)
498+
// y0 rgba rgba rgba ypos=0
499+
// y1 rgba rgba rgba ypos=12
500+
//
501+
for (unsigned y = offsetTop; y < offsetTop + hClip; y++) {
502+
unsigned yoffs = (y * w * 4);
503+
for (unsigned x = offsetLeft; x < offsetLeft + wClip; x++) {
504+
uint8_t a, r, g, b;
505+
GET_IMAGE_ARGB(image->_image, yoffs + (x * 4), a, r, g, b);
506+
pixel_t px = v_get_argb_px(a, r, g, b);
507+
unsigned pos = (y - offsetTop ) * wClip + (x - offsetLeft);
508+
v_setint(v_elem(var, pos), px);
509+
}
510+
}
511+
saved = true;
512+
}
504513
}
505514
}
506-
507515
if (!saved) {
508516
err_throw(ERR_IMAGE_SAVE);
509517
}
@@ -512,7 +520,7 @@ void cmd_image_save(var_s *self, var_s *) {
512520

513521
//
514522
// Reduces the size of the image
515-
// arguments: left, top, right, bottom
523+
// arguments: left, top, width, height
516524
//
517525
// png.clip(10, 10, 10, 10)
518526
//
@@ -521,12 +529,12 @@ void cmd_image_clip(var_s *self, var_s *) {
521529
int bid = map_get_int(self, IMG_BID, -1);
522530
if (bid != -1) {
523531
ImageBuffer *image = get_image((unsigned)bid);
524-
var_int_t left, top, right, bottom;
525-
if (image != nullptr && par_massget("iiii", &left, &top, &right, &bottom)) {
532+
var_int_t left, top, width, heigth;
533+
if (image != nullptr && par_massget("iiii", &left, &top, &width, &heigth)) {
526534
map_set_int(self, IMG_OFFSET_LEFT, left);
527535
map_set_int(self, IMG_OFFSET_TOP, top);
528-
map_set_int(self, IMG_WIDTH, right);
529-
map_set_int(self, IMG_HEIGHT, bottom);
536+
map_set_int(self, IMG_WIDTH, width);
537+
map_set_int(self, IMG_HEIGHT, heigth);
530538
}
531539
}
532540
}

0 commit comments

Comments
 (0)