Skip to content

Commit dcb1187

Browse files
committed
COMMON: Fix image.save() to array destination
1 parent 22de0b9 commit dcb1187

File tree

2 files changed

+54
-46
lines changed

2 files changed

+54
-46
lines changed

ChangeLog

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
2020-05-07 (0.12.19)
2+
COMMON: Fix image.save() to array destination
3+
14
2020-05-04 (0.12.19)
2-
INPUT crash #99
3-
Fix compile warning with updated gcc
5+
COMMON: INPUT crash #99
6+
COMMON: Fix compile warning with updated gcc
47

58
2020-03-07 (0.12.19)
69
COMMON: implement DEFINEKEY undo #92

src/ui/image.cpp

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ void reset_image_cache() {
3838

3939
ImageBuffer::ImageBuffer() :
4040
_bid(0),
41-
_filename(NULL),
42-
_image(NULL),
41+
_filename(nullptr),
42+
_image(nullptr),
4343
_width(0),
4444
_height(0) {
4545
}
@@ -55,8 +55,8 @@ ImageBuffer::ImageBuffer(ImageBuffer &o) :
5555
ImageBuffer::~ImageBuffer() {
5656
free(_filename);
5757
free(_image);
58-
_filename = NULL;
59-
_image = NULL;
58+
_filename = nullptr;
59+
_image = nullptr;
6060
}
6161

6262
ImageDisplay::ImageDisplay() : Shape(0, 0, 0, 0),
@@ -66,7 +66,7 @@ ImageDisplay::ImageDisplay() : Shape(0, 0, 0, 0),
6666
_opacity(0),
6767
_id(0),
6868
_bid(0),
69-
_buffer(NULL) {
69+
_buffer(nullptr) {
7070
}
7171

7272
ImageDisplay::ImageDisplay(ImageDisplay &o) : Shape(o._x, o._y, o._width, o._height) {
@@ -102,7 +102,7 @@ void ImageDisplay::draw(int x, int y, int w, int h, int cw) {
102102
}
103103

104104
dev_file_t *eval_filep() {
105-
dev_file_t *result = NULL;
105+
dev_file_t *result = nullptr;
106106
code_skipnext();
107107
if (code_getnext() == '#') {
108108
int handle = par_getint();
@@ -121,7 +121,7 @@ uint8_t *get_image_data(int x, int y, int w, int h) {
121121
rc.height = h;
122122
int size = w * h * 4;
123123
uint8_t *result = (uint8_t *)malloc(size);
124-
if (result != NULL) {
124+
if (result != nullptr) {
125125
g_system->getOutput()->redraw();
126126
maGetImageData(HANDLE_SCREEN, result, &rc, w * 4);
127127
}
@@ -133,7 +133,7 @@ ImageBuffer *load_image(var_int_t x) {
133133
int count = par_massget("iii", &y, &w, &h);
134134
int width = g_system->getOutput()->getWidth();
135135
int height = g_system->getOutput()->getHeight();
136-
ImageBuffer *result = NULL;
136+
ImageBuffer *result = nullptr;
137137

138138
if (prog_error || count == 0 || count == 2) {
139139
err_throw(ERR_PARAM);
@@ -146,14 +146,14 @@ ImageBuffer *load_image(var_int_t x) {
146146
h = MIN(h, height);
147147
}
148148
uint8_t* image = get_image_data(x, y, w, h);
149-
if (image == NULL) {
149+
if (image == nullptr) {
150150
err_throw(ERR_IMAGE_LOAD, "Failed to load screen image");
151151
} else {
152152
result = new ImageBuffer();
153153
result->_bid = ++nextId;
154154
result->_width = w;
155155
result->_height = h;
156-
result->_filename = NULL;
156+
result->_filename = nullptr;
157157
result->_image = image;
158158
cache.add(result);
159159
}
@@ -163,7 +163,7 @@ ImageBuffer *load_image(var_int_t x) {
163163

164164
// share image buffer from another image variable
165165
ImageBuffer *load_image(var_t *var) {
166-
ImageBuffer *result = NULL;
166+
ImageBuffer *result = nullptr;
167167
if (var->type == V_MAP) {
168168
int bid = map_get_int(var, IMG_BID, -1);
169169
if (bid != -1) {
@@ -199,15 +199,15 @@ ImageBuffer *load_image(var_t *var) {
199199
result->_bid = ++nextId;
200200
result->_width = w;
201201
result->_height = h;
202-
result->_filename = NULL;
202+
result->_filename = nullptr;
203203
result->_image = image;
204204
cache.add(result);
205205
}
206206
return result;
207207
}
208208

209209
ImageBuffer *load_image(const unsigned char* buffer, int32_t size) {
210-
ImageBuffer *result = NULL;
210+
ImageBuffer *result = nullptr;
211211
unsigned w, h;
212212
unsigned char *image;
213213
unsigned error = 0;
@@ -218,7 +218,7 @@ ImageBuffer *load_image(const unsigned char* buffer, int32_t size) {
218218
result->_bid = ++nextId;
219219
result->_width = w;
220220
result->_height = h;
221-
result->_filename = NULL;
221+
result->_filename = nullptr;
222222
result->_image = image;
223223
cache.add(result);
224224
} else {
@@ -228,16 +228,16 @@ ImageBuffer *load_image(const unsigned char* buffer, int32_t size) {
228228
}
229229

230230
ImageBuffer *load_image(dev_file_t *filep) {
231-
ImageBuffer *result = NULL;
231+
ImageBuffer *result = nullptr;
232232
List_each(ImageBuffer *, it, cache) {
233233
ImageBuffer *next = (*it);
234-
if (next->_filename != NULL && strcmp(next->_filename, filep->name) == 0) {
234+
if (next->_filename != nullptr && strcmp(next->_filename, filep->name) == 0) {
235235
result = next;
236236
break;
237237
}
238238
}
239239

240-
if (result == NULL) {
240+
if (result == nullptr) {
241241
unsigned w, h;
242242
unsigned char *image;
243243
unsigned error = 0;
@@ -286,13 +286,13 @@ ImageBuffer *load_xpm_image(char **data) {
286286
unsigned w, h;
287287
unsigned char *image;
288288
unsigned error = xpm_decode32(&image, &w, &h, data);
289-
ImageBuffer *result = NULL;
289+
ImageBuffer *result = nullptr;
290290
if (!error) {
291291
result = new ImageBuffer();
292292
result->_bid = ++nextId;
293293
result->_width = w;
294294
result->_height = h;
295-
result->_filename = NULL;
295+
result->_filename = nullptr;
296296
result->_image = image;
297297
cache.add(result);
298298
} else {
@@ -316,7 +316,7 @@ void cmd_image_show(var_s *self) {
316316
var_int_t x, y, z, op;
317317
int count = par_massget("iiii", &x, &y, &z, &op);
318318

319-
if (prog_error || image._buffer == NULL || count == 1 || count > 4) {
319+
if (prog_error || image._buffer == nullptr || count == 1 || count > 4) {
320320
err_throw(ERR_PARAM);
321321
} else {
322322
// 0, 2, 3, 4 arguments accepted
@@ -358,7 +358,7 @@ void cmd_image_hide(var_s *self) {
358358

359359
void cmd_image_save(var_s *self) {
360360
unsigned id = map_get_int(self, IMG_BID, -1);
361-
ImageBuffer *image = NULL;
361+
ImageBuffer *image = nullptr;
362362
List_each(ImageBuffer *, it, cache) {
363363
ImageBuffer *next = (*it);
364364
if (next->_bid == id) {
@@ -367,8 +367,8 @@ void cmd_image_save(var_s *self) {
367367
}
368368
}
369369

370-
var_t *array = NULL;
371-
dev_file_t *filep = NULL;
370+
var_t *array = nullptr;
371+
dev_file_t *filep = nullptr;
372372
byte code = code_peek();
373373
switch (code) {
374374
case kwTYPE_SEP:
@@ -380,15 +380,19 @@ void cmd_image_save(var_s *self) {
380380
}
381381

382382
bool saved = false;
383-
if (!prog_error && image != NULL) {
383+
if (!prog_error && image != nullptr) {
384384
int w = image->_width;
385385
int h = image->_height;
386-
if (filep != NULL && filep->open_flags == DEV_FILE_OUTPUT) {
386+
if (filep != nullptr && filep->open_flags == DEV_FILE_OUTPUT) {
387387
if (!lodepng_encode32_file(filep->name, image->_image, w, h)) {
388388
saved = true;
389389
}
390-
} else if (array != NULL) {
391-
v_tomatrix(array, h, w);
390+
} else if (array != nullptr) {
391+
v_tomatrix(array, w, h);
392+
// x0 x1 x2 (w=3,h=2)
393+
// y0 rgba rgba rgba ypos=0
394+
// y1 rgba rgba rgba ypos=12
395+
//
392396
for (int y = 0; y < h; y++) {
393397
int yoffs = (4 * y * w);
394398
for (int x = 0; x < w; x++) {
@@ -430,13 +434,13 @@ void create_image(var_p_t var, ImageBuffer *image) {
430434

431435
// loads an image for the form image input type
432436
ImageDisplay *create_display_image(var_p_t var, const char *name) {
433-
ImageDisplay *result = NULL;
434-
if (name != NULL && var != NULL) {
437+
ImageDisplay *result = nullptr;
438+
if (name != nullptr && var != nullptr) {
435439
dev_file_t file;
436440
strlcpy(file.name, name, sizeof(file.name));
437441
file.type = ft_stream;
438442
ImageBuffer *buffer = load_image(&file);
439-
if (buffer != NULL) {
443+
if (buffer != nullptr) {
440444
result = new ImageDisplay();
441445
result->_buffer = buffer;
442446
result->_bid = buffer->_bid;
@@ -468,22 +472,23 @@ ImageDisplay *create_display_image(var_p_t var, const char *name) {
468472
void screen_dump() {
469473
int width = g_system->getOutput()->getWidth();
470474
int height = g_system->getOutput()->getHeight();
471-
uint8_t* image = get_image_data(0, 0, width, height);
472-
if (image != NULL) {
475+
auto image = get_image_data(0, 0, width, height);
476+
if (image != nullptr) {
473477
const char *path = gsb_bas_dir;
474478
#if defined(_ANDROID)
475479
path = getenv("EXTERNAL_STORAGE");
476480
#endif
477481
for (int i = 0; i < 1000; i++) {
478-
char file[OS_PATHNAME_SIZE];
479-
if (strstr(path, "://") != NULL) {
480-
sprintf(file, "sbasic_dump_%d.png", i);
481-
} else {
482-
sprintf(file, "%ssbasic_dump_%d.png", path, i);
482+
String file;
483+
if (strstr(path, "://") == nullptr) {
484+
file.append(path);
483485
}
484-
if (access(file, R_OK) != 0) {
485-
g_system->systemPrint("Saving screen to %s\n", file);
486-
unsigned error = lodepng_encode32_file(file, image, width, height);
486+
file.append("sbasic_dump_");
487+
file.append(i);
488+
file.append(".png");
489+
if (access(file.c_str(), R_OK) != 0) {
490+
g_system->systemPrint("Saving screen to %s\n", file.c_str());
491+
unsigned error = lodepng_encode32_file(file.c_str(), image, width, height);
487492
if (error) {
488493
g_system->systemPrint("Error: %s\n", lodepng_error_text(error));
489494
}
@@ -496,14 +501,14 @@ void screen_dump() {
496501

497502
extern "C" void v_create_image(var_p_t var) {
498503
var_t arg;
499-
ImageBuffer *image = NULL;
500-
dev_file_t *filep = NULL;
504+
ImageBuffer *image = nullptr;
505+
dev_file_t *filep = nullptr;
501506

502507
byte code = code_peek();
503508
switch (code) {
504509
case kwTYPE_SEP:
505510
filep = eval_filep();
506-
if (filep != NULL) {
511+
if (filep != nullptr) {
507512
image = load_image(filep);
508513
}
509514
break;
@@ -551,7 +556,7 @@ extern "C" void v_create_image(var_p_t var) {
551556
break;
552557
};
553558

554-
if (image != NULL) {
559+
if (image != nullptr) {
555560
create_image(var, image);
556561
} else {
557562
err_throw(ERR_BAD_FILE_HANDLE);

0 commit comments

Comments
 (0)