Skip to content

Commit 5dc4cca

Browse files
committed
UI: make image save and load compatible.
1 parent 946c639 commit 5dc4cca

File tree

6 files changed

+65
-81
lines changed

6 files changed

+65
-81
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2021-07-02 (12.22)
2+
UI: Fix to make image save and load compatible
3+
14
2021-06-25 (12.22)
25
ANDROID: Unified file listing includes bas files from sub-folders
36

src/lib/xpm.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@
2929

3030
void set_pixels(uint8_t *image, int w, int x, int y, uint32_t c) {
3131
int offs = y * w * 4 + x * 4;
32-
image[offs + 0] = (c >> 24) & 0xff;
32+
image[offs + 3] = (c) & 0xff;
33+
#if defined(_SDL)
34+
image[offs + 2] = (c >> 24) & 0xff;
3335
image[offs + 1] = (c >> 16) & 0xff;
36+
image[offs + 0] = (c >> 8) & 0xff;
37+
#else
3438
image[offs + 2] = (c >> 8) & 0xff;
35-
image[offs + 3] = (c) & 0xff;
39+
image[offs + 1] = (c >> 16) & 0xff;
40+
image[offs + 0] = (c >> 24) & 0xff;
41+
#endif
3642
}
3743

3844
int xpm_decode32(uint8_t **image, unsigned *width, unsigned *height,

src/platform/console/image.cpp

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
#include "ui/rgb.h"
2525
#include "lib/lodepng/lodepng.h"
2626

27-
extern "C" {
28-
int xpm_decode32(uint8_t **image, unsigned *width, unsigned *height, const char *const *xpm);
29-
}
27+
extern "C" int xpm_decode32(uint8_t **image, unsigned *width, unsigned *height, const char *const *xpm);
3028

3129
struct ImageBuffer {
3230
ImageBuffer();
@@ -49,8 +47,8 @@ void reset_image_cache() {
4947

5048
ImageBuffer::ImageBuffer() :
5149
_bid(0),
52-
_filename(NULL),
53-
_image(NULL),
50+
_filename(nullptr),
51+
_image(nullptr),
5452
_width(0),
5553
_height(0) {
5654
}
@@ -66,12 +64,12 @@ ImageBuffer::ImageBuffer(ImageBuffer &o) :
6664
ImageBuffer::~ImageBuffer() {
6765
free(_filename);
6866
free(_image);
69-
_filename = NULL;
70-
_image = NULL;
67+
_filename = nullptr;
68+
_image = nullptr;
7169
}
7270

7371
dev_file_t *get_file() {
74-
dev_file_t *result = NULL;
72+
dev_file_t *result = nullptr;
7573
code_skipnext();
7674
if (code_getnext() == '#') {
7775
int handle = par_getint();
@@ -83,7 +81,7 @@ dev_file_t *get_file() {
8381
}
8482

8583
ImageBuffer *load_image(int w) {
86-
ImageBuffer *result = NULL;
84+
ImageBuffer *result = nullptr;
8785
if (!par_getsep()) {
8886
err_throw(ERR_PARAM);
8987
} else {
@@ -97,7 +95,7 @@ ImageBuffer *load_image(int w) {
9795
result->_bid = ++nextId;
9896
result->_width = w;
9997
result->_height = h;
100-
result->_filename = NULL;
98+
result->_filename = nullptr;
10199
result->_image = image;
102100
cache.add(result);
103101
}
@@ -107,7 +105,7 @@ ImageBuffer *load_image(int w) {
107105

108106
// share image buffer from another image variable or array
109107
ImageBuffer *load_image(var_t *var) {
110-
ImageBuffer *result = NULL;
108+
ImageBuffer *result = nullptr;
111109
if (var->type == V_MAP) {
112110
int bid = map_get_int(var, IMG_BID, -1);
113111
if (bid != -1) {
@@ -120,38 +118,32 @@ ImageBuffer *load_image(var_t *var) {
120118
}
121119
}
122120
} else if (var->type == V_ARRAY && v_maxdim(var) == 2) {
123-
int w = ABS(v_lbound(var, 0) - v_ubound(var, 0)) + 1;
124-
int h = ABS(v_lbound(var, 1) - v_ubound(var, 1)) + 1;
121+
int h = ABS(v_ubound(var, 0) - v_lbound(var, 0)) + 1;
122+
int w = ABS(v_ubound(var, 1) - v_lbound(var, 1)) + 1;
125123
int size = w * h * 4;
126-
uint8_t *image = (uint8_t *)malloc(size);
124+
auto image = (uint8_t *)malloc(size);
127125
for (int y = 0; y < h; y++) {
128126
int yoffs = (4 * y * w);
129127
for (int x = 0; x < w; x++) {
130128
int pos = y * w + x;
131-
var_t *elem = v_elem(var, pos);
132-
pixel_t px = v_getint(elem);
133-
uint8_t r, g, b, a;
134-
GET_ARGB(px, a, r, g, b);
135-
int offs = yoffs + (4 * x);
136-
image[offs + 0] = r;
137-
image[offs + 1] = g;
138-
image[offs + 2] = b;
139-
image[offs + 3] = a;
129+
uint8_t a, r, g, b;
130+
v_get_argb(-v_getint(v_elem(var, pos)), a, r, g, b);
131+
SET_IMAGE_ARGB(image, yoffs + (x * 4), a, r, g, b);
140132
}
141133
}
142134
result = new ImageBuffer();
143135
result->_bid = ++nextId;
144136
result->_width = w;
145137
result->_height = h;
146-
result->_filename = NULL;
138+
result->_filename = nullptr;
147139
result->_image = image;
148140
cache.add(result);
149141
}
150142
return result;
151143
}
152144

153145
ImageBuffer *load_image(const uint8_t* buffer, int32_t size) {
154-
ImageBuffer *result = NULL;
146+
ImageBuffer *result = nullptr;
155147
unsigned w, h;
156148
uint8_t *image;
157149
unsigned error = 0;
@@ -162,7 +154,7 @@ ImageBuffer *load_image(const uint8_t* buffer, int32_t size) {
162154
result->_bid = ++nextId;
163155
result->_width = w;
164156
result->_height = h;
165-
result->_filename = NULL;
157+
result->_filename = nullptr;
166158
result->_image = image;
167159
cache.add(result);
168160
} else {
@@ -175,16 +167,16 @@ ImageBuffer *load_image(const uint8_t* buffer, int32_t size) {
175167
// png = image(#1)
176168
//
177169
ImageBuffer *load_image(dev_file_t *filep) {
178-
ImageBuffer *result = NULL;
170+
ImageBuffer *result = nullptr;
179171
List_each(ImageBuffer *, it, cache) {
180172
ImageBuffer *next = (*it);
181-
if (next->_filename != NULL && strcmp(next->_filename, filep->name) == 0) {
173+
if (next->_filename != nullptr && strcmp(next->_filename, filep->name) == 0) {
182174
result = next;
183175
break;
184176
}
185177
}
186178

187-
if (result == NULL) {
179+
if (result == nullptr) {
188180
unsigned w, h;
189181
uint8_t *image;
190182
unsigned error = 0;
@@ -232,13 +224,13 @@ ImageBuffer *load_xpm_image(char **data) {
232224
unsigned w, h;
233225
uint8_t *image;
234226
unsigned error = xpm_decode32(&image, &w, &h, data);
235-
ImageBuffer *result = NULL;
227+
ImageBuffer *result = nullptr;
236228
if (!error) {
237229
result = new ImageBuffer();
238230
result->_bid = ++nextId;
239231
result->_width = w;
240232
result->_height = h;
241-
result->_filename = NULL;
233+
result->_filename = nullptr;
242234
result->_image = image;
243235
cache.add(result);
244236
} else {
@@ -300,32 +292,26 @@ void cmd_image_clip(var_s *self, var_s *) {
300292
//
301293
void cmd_image_filter(var_s *self, var_s *) {
302294
ImageBuffer *image_buffer = load_image(self);
303-
if (code_peek() == kwUSE && image_buffer != NULL) {
295+
if (code_peek() == kwUSE && image_buffer != nullptr) {
304296
code_skipnext();
305297
bcip_t use_ip = code_getaddr();
306298
bcip_t exit_ip = code_getaddr();
307299
int w = image_buffer->_width;
308300
int h = image_buffer->_height;
309-
uint8_t *image = image_buffer->_image;
301+
auto image = image_buffer->_image;
310302
var_t var;
311303
v_init(&var);
312304
for (int y = 0; y < h; y++) {
313305
int yoffs = (4 * y * w);
314306
for (int x = 0; x < w; x++) {
315307
int offs = yoffs + (4 * x);
316-
uint8_t r = image[offs + 0];
317-
uint8_t g = image[offs + 1];
318-
uint8_t b = image[offs + 2];
319-
uint8_t a = image[offs + 3];
320-
pixel_t px = SET_ARGB(a, r, g, b);
308+
uint8_t a, r, g, b;
309+
GET_IMAGE_ARGB(image, offs, a, r, g, b);
310+
pixel_t px = v_get_argb_px(a, r, g, b);
321311
v_setint(&var, px);
322312
exec_usefunc(&var, use_ip);
323-
px = v_getint(&var);
324-
GET_ARGB(px, a, r, g, b);
325-
image[offs + 0] = r;
326-
image[offs + 1] = g;
327-
image[offs + 2] = b;
328-
image[offs + 3] = a;
313+
v_get_argb(v_getint(&var), a, r, g, b);
314+
SET_IMAGE_ARGB(image, offs, a, r, g, b);
329315
}
330316
}
331317
code_jump(exit_ip);
@@ -345,9 +331,9 @@ void cmd_image_paste(var_s *self, var_s *) {
345331
var_t *var;
346332
ImageBuffer *image = load_image(self);
347333
int count = par_massget("Piiii", &var, &x, &y);
348-
if (image != NULL && (count == 1 || count == 3)) {
334+
if (image != nullptr && (count == 1 || count == 3)) {
349335
ImageBuffer *srcImage = load_image(var);
350-
if (srcImage == NULL) {
336+
if (srcImage == nullptr) {
351337
err_throw(ERR_PARAM);
352338
} else {
353339
if (count == 1) {
@@ -386,18 +372,18 @@ void cmd_image_paste(var_s *self, var_s *) {
386372
//
387373
void cmd_image_save(var_s *self, var_s *) {
388374
ImageBuffer *image = load_image(self);
389-
dev_file_t *filep = NULL;
375+
dev_file_t *filep = nullptr;
390376
byte code = code_peek();
391377
int error = -1;
392378
int w = image->_width;
393379
int h = image->_height;
394380
var_t var;
395381

396-
if (!prog_error && image != NULL) {
382+
if (!prog_error && image != nullptr) {
397383
switch (code) {
398384
case kwTYPE_SEP:
399385
filep = get_file();
400-
if (filep != NULL && filep->open_flags == DEV_FILE_OUTPUT) {
386+
if (filep != nullptr && filep->open_flags == DEV_FILE_OUTPUT) {
401387
error = lodepng_encode32_file(filep->name, image->_image, w, h);
402388
}
403389
break;
@@ -424,7 +410,7 @@ void create_image(var_p_t var, ImageBuffer *image) {
424410
map_add_var(var, IMG_WIDTH, image->_width);
425411
map_add_var(var, IMG_HEIGHT, image->_height);
426412
map_add_var(var, IMG_BID, image->_bid);
427-
if (image->_filename != NULL) {
413+
if (image->_filename != nullptr) {
428414
var_p_t value = map_add_var(var, IMG_NAME, 0);
429415
v_setstr(value, image->_filename);
430416
}
@@ -445,14 +431,14 @@ void create_image(var_p_t var, ImageBuffer *image) {
445431
//
446432
extern "C" void v_create_image(var_p_t var) {
447433
var_t arg;
448-
ImageBuffer *image = NULL;
449-
dev_file_t *filep = NULL;
434+
ImageBuffer *image = nullptr;
435+
dev_file_t *filep = nullptr;
450436

451437
byte code = code_peek();
452438
switch (code) {
453439
case kwTYPE_SEP:
454440
filep = get_file();
455-
if (filep != NULL) {
441+
if (filep != nullptr) {
456442
image = load_image(filep);
457443
}
458444
break;
@@ -500,7 +486,7 @@ extern "C" void v_create_image(var_p_t var) {
500486
break;
501487
};
502488

503-
if (image != NULL) {
489+
if (image != nullptr) {
504490
create_image(var, image);
505491
} else {
506492
err_throw(ERR_BAD_FILE_HANDLE);

src/ui/image.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ extern System *g_system;
3232
unsigned nextId = 0;
3333
strlib::List<ImageBuffer *> buffers;
3434

35+
extern "C" int xpm_decode32(uint8_t **image, unsigned *width, unsigned *height, const char *const *xpm);
36+
3537
void reset_image_cache() {
3638
buffers.removeAll();
3739
}
@@ -256,15 +258,13 @@ ImageBuffer *load_image(var_t *var) {
256258
int h = ABS(v_ubound(var, 0) - v_lbound(var, 0)) + 1;
257259
int w = ABS(v_ubound(var, 1) - v_lbound(var, 1)) + 1;
258260
int size = w * h * 4;
259-
auto image = (unsigned char *)malloc(size);
261+
auto image = (uint8_t *)malloc(size);
260262
for (int y = 0; y < h; y++) {
261263
int yoffs = (4 * y * w);
262264
for (int x = 0; x < w; x++) {
263265
int pos = y * w + x;
264-
var_t *elem = v_elem(var, pos);
265-
pixel_t px = -v_getint(elem);
266266
uint8_t a, r, g, b;
267-
GET_ARGB(px, a, r, g, b);
267+
v_get_argb(-v_getint(v_elem(var, pos)), a, r, g, b);
268268
SET_IMAGE_ARGB(image, yoffs + (x * 4), a, r, g, b);
269269
}
270270
}
@@ -487,7 +487,7 @@ void cmd_image_save(var_s *self, var_s *) {
487487
for (unsigned x = 0; x < w; x++) {
488488
uint8_t a, r, g, b;
489489
GET_IMAGE_ARGB(image->_image, yoffs + (x * 4), a, r, g, b);
490-
pixel_t px = GET_ARGB_PX(a, r, g, b);
490+
pixel_t px = v_get_argb_px(a, r, g, b);
491491
unsigned pos = y * w + x;
492492
var_t *elem = v_elem(array, pos);
493493
v_setint(elem, -px);

src/ui/image.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,5 @@ struct ImageDisplay : public Shape {
4444
ImageDisplay *create_display_image(var_p_t var, const char *name);
4545
void reset_image_cache();
4646
void screen_dump();
47-
extern "C" int xpm_decode32(uint8_t **image, unsigned *width, unsigned *height,
48-
const char *const *xpm);
4947

5048
#endif

src/ui/rgb.h

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@ typedef uint32_t pixel_t;
1717
#define PIXELFORMAT AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM
1818
#endif
1919

20-
#if defined(PIXELFORMAT_ARGB8888)
20+
inline void v_get_argb(pixel_t c, uint8_t &a, uint8_t &r, uint8_t &g, uint8_t &b) {
21+
a = (c & 0xff000000) >> 24;
22+
r = (c & 0xff0000) >> 16;
23+
g = (c & 0xff00) >> 8;
24+
b = (c & 0xff);
25+
}
2126

22-
#define GET_ARGB_PX(a, r, g, b) (a << 24 | (r << 16) | (g << 8) | (b))
27+
#define v_get_argb_px(a, r, g, b) (a << 24 | (r << 16) | (g << 8) | (b))
28+
29+
#if defined(PIXELFORMAT_ARGB8888)
2330

2431
#define GET_RGB_PX(r, g, b) ((0xff000000) | (r << 16) | (g << 8) | (b))
2532

@@ -36,13 +43,6 @@ inline void GET_RGB(pixel_t c, uint8_t &r, uint8_t &g, uint8_t &b) {
3643
b = (c & 0xff);
3744
}
3845

39-
inline void GET_ARGB(pixel_t c, uint8_t &a, uint8_t &r, uint8_t &g, uint8_t &b) {
40-
a = (c & 0xff000000) >> 24;
41-
r = (c & 0xff0000) >> 16;
42-
g = (c & 0xff00) >> 8;
43-
b = (c & 0xff);
44-
}
45-
4646
inline void GET_IMAGE_ARGB(uint8_t *image, unsigned offs, uint8_t &a, uint8_t &r, uint8_t &g, uint8_t &b) {
4747
a = image[offs + 3];
4848
r = image[offs + 2];
@@ -64,8 +64,6 @@ inline void SET_IMAGE_ARGB(uint8_t *image, unsigned offs, uint8_t a, uint8_t r,
6464
// G = byte 1
6565
// R = byte 0
6666

67-
#define GET_ARGB_PX(a, r, g, b) (a << 24 | (b << 16) | (g << 8) | (r))
68-
6967
#define GET_RGB_PX(r, g, b) ((0xff000000) | (b << 16) | (g << 8) | (r))
7068

7169
#define GET_FROM_RGB888(c) (c)
@@ -76,13 +74,6 @@ inline void GET_RGB(pixel_t c, uint8_t &r, uint8_t &g, uint8_t &b) {
7674
r = (c & 0xff);
7775
}
7876

79-
inline void GET_ARGB(pixel_t c, uint8_t &a, uint8_t &r, uint8_t &g, uint8_t &b) {
80-
a = (c & 0xff000000) >> 24;
81-
b = (c & 0xff0000) >> 16;
82-
g = (c & 0xff00) >> 8;
83-
r = (c & 0xff);
84-
}
85-
8677
inline void GET_IMAGE_ARGB(uint8_t *image, unsigned offs, uint8_t &a, uint8_t &r, uint8_t &g, uint8_t &b) {
8778
a = image[offs + 3];
8879
b = image[offs + 2];

0 commit comments

Comments
 (0)