1616#include " common/var.h"
1717#include " common/var_map.h"
1818#include " lib/maapi.h"
19+ #include " ui/image.h"
1920#include " ui/system.h"
21+ #include " ui/graphics.h"
2022
2123#if !defined(LODEPNG_NO_COMPILE_CPP)
2224 #define LODEPNG_NO_COMPILE_CPP
@@ -40,8 +42,6 @@ extern System *g_system;
4042unsigned nextId = 0 ;
4143strlib::List<ImageBuffer *> cache;
4244
43- void create_func (var_p_t form, const char *name, method cb);
44-
4545void reset_image_cache () {
4646 cache.removeAll ();
4747}
@@ -129,7 +129,7 @@ uint8_t *get_image_data(int x, int y, int w, int h) {
129129 rc.top = y;
130130 rc.width = w;
131131 rc.height = h;
132- int size = w * 4 * h * 4 ;
132+ int size = w * h * 4 ;
133133 uint8_t *result = (uint8_t *)malloc (size);
134134 if (result != NULL ) {
135135 g_system->getOutput ()->redraw ();
@@ -188,13 +188,21 @@ ImageBuffer *load_image(var_t *var) {
188188 } else if (var->type == V_ARRAY && var->v .a .maxdim == 2 ) {
189189 int w = ABS (var->v .a .lbound [0 ] - var->v .a .ubound [0 ]) + 1 ;
190190 int h = ABS (var->v .a .lbound [1 ] - var->v .a .ubound [1 ]) + 1 ;
191- unsigned char *image = (unsigned char *)malloc (w * h);
191+ int size = w * h * 4 ;
192+ unsigned char *image = (unsigned char *)malloc (size);
192193 for (int y = 0 ; y < h; y++) {
194+ int yoffs = (4 * y * w);
193195 for (int x = 0 ; x < w; x++) {
194196 int pos = y * w + x;
195197 var_t *elem = (var_t *) (var->v .a .ptr + (sizeof (var_t ) * pos));
196- // TODO combine RGBA from next four bytes of image[pos]
197- // image[pos] = v_getint(elem);
198+ pixel_t px = v_getint (elem);
199+ uint8_t r, g, b;
200+ GET_RGB2 (px, r, g, b);
201+ int offs = yoffs + (4 * x);
202+ image[offs + 0 ] = r;
203+ image[offs + 1 ] = g;
204+ image[offs + 2 ] = b;
205+ image[offs + 3 ] = 255 ;
198206 }
199207 }
200208 result = new ImageBuffer ();
@@ -385,11 +393,16 @@ void cmd_image_save(var_s *self) {
385393 } else if (array != NULL ) {
386394 v_tomatrix (array, w, h);
387395 for (int y = 0 ; y < h; y++) {
396+ int yoffs = (4 * y * w);
388397 for (int x = 0 ; x < w; x++) {
398+ int offs = yoffs + (4 * x);
399+ uint8_t r = image->_image [offs + 0 ];
400+ uint8_t g = image->_image [offs + 1 ];
401+ uint8_t b = image->_image [offs + 2 ];
402+ pixel_t px = SET_RGB (r, g, b);
389403 int pos = y * w + x;
390404 var_t *elem = (var_t *) (array->v .a .ptr + (sizeof (var_t ) * pos));
391- // TODO combine RGBA from next four bytes of image[pos]
392- // v_setint(elem, image->_image[pos]);
405+ v_setint (elem, px);
393406 }
394407 }
395408 saved = true ;
0 commit comments