Skip to content

Commit 724bc30

Browse files
committed
UI: update IMAGE handling
1 parent 67ecb69 commit 724bc30

File tree

6 files changed

+38
-23
lines changed

6 files changed

+38
-23
lines changed

src/ui/form.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,6 @@ FormInput *create_input(var_p_t v_field) {
236236
return widget;
237237
}
238238

239-
void create_func(var_p_t form, const char *name, method cb) {
240-
var_p_t v_func = map_add_var(form, name, 0);
241-
v_func->type = V_FUNC;
242-
v_func->v.fn.self = form;
243-
v_func->v.fn.cb = cb;
244-
}
245-
246239
// creates a new form using the given map
247240
extern "C" void v_create_form(var_p_t var) {
248241
bool hasInputs = false;

src/ui/graphics.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,20 @@ void Graphics::drawText(int left, int top, const char *str, int len) {
283283
void Graphics::getImageData(Canvas *canvas, uint8_t *image,
284284
const MARect *srcRect, int bytesPerLine) {
285285
size_t scale = 1;
286-
int w = bytesPerLine;
286+
int x_end = srcRect->left + srcRect->width;
287+
int y_end = srcRect->top + srcRect->height;
287288
if (canvas == HANDLE_SCREEN) {
288289
canvas = _screen;
289290
}
290-
for (int dy = 0, y = srcRect->top; y < srcRect->height; y += scale, dy++) {
291-
if (y >= canvas->y() && y < canvas->h()) {
291+
for (int dy = 0, y = srcRect->top; y < y_end; y += scale, dy++) {
292+
if (y >= canvas->y() && y < canvas->h()) {
292293
pixel_t *line = canvas->getLine(y);
293-
for (int dx = 0, x = srcRect->left; x < srcRect->width; x += scale, dx++) {
294+
int yoffs = (dy * bytesPerLine * 4);
295+
for (int dx = 0, x = srcRect->left; x < x_end; x += scale, dx++) {
294296
if (x >= canvas->x() && x < canvas->w()) {
295-
uint8_t r,g,b;
297+
uint8_t r, g, b;
296298
GET_RGB2(line[x], r, g, b);
297-
int offs = (4 * dy * w) + (4 * dx);
299+
int offs = yoffs + (dx * 4);
298300
image[offs + 0] = r;
299301
image[offs + 1] = g;
300302
image[offs + 2] = b;

src/ui/image.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
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;
4042
unsigned nextId = 0;
4143
strlib::List<ImageBuffer *> cache;
4244

43-
void create_func(var_p_t form, const char *name, method cb);
44-
4545
void 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;

src/ui/system.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "common/keymap.h"
2020
#include "ui/system.h"
2121
#include "ui/inputs.h"
22-
#include "ui/image.h"
2322

2423
#define MENU_CONSOLE 0
2524
#define MENU_SOURCE 1
@@ -1173,3 +1172,9 @@ int maGetMilliSecondCount(void) {
11731172
return dev_get_millisecond_count();
11741173
}
11751174

1175+
void create_func(var_p_t form, const char *name, method cb) {
1176+
var_p_t v_func = map_add_var(form, name, 0);
1177+
v_func->type = V_FUNC;
1178+
v_func->v.fn.self = form;
1179+
v_func->v.fn.cb = cb;
1180+
}

src/ui/system.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#include "platform/fltk/system.h"
1919
#else
2020

21+
void create_func(var_p_t form, const char *name, method cb);
22+
void reset_image_cache();
23+
2124
struct Cache : public strlib::Properties {
2225
Cache(int size) : Properties(size * 2), _index(0) {}
2326
void add(const char *key, const char *value);

src/ui/window.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "ui/system.h"
1717

1818
extern System *g_system;
19-
void create_func(var_p_t form, const char *name, method cb);
2019

2120
#define WINDOW_SCREEN1 "graphicsScreen1"
2221
#define WINDOW_SCREEN2 "graphicsScreen2"

0 commit comments

Comments
 (0)