Skip to content

Commit f1336f3

Browse files
committed
Correction error handling
1 parent 90f4139 commit f1336f3

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/ui/image.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ ImageBuffer *get_image(unsigned bid) {
215215
}
216216

217217
void scaleImage(ImageBuffer* image, var_num_t scaling) {
218-
if(scaling == 1.0 || scaling <= 0.0) {
218+
if (scaling == 1.0 || scaling <= 0.0) {
219219
return;
220220
}
221221

@@ -226,15 +226,16 @@ void scaleImage(ImageBuffer* image, var_num_t scaling) {
226226
h = round((var_num_t)image->_height * scaling);
227227

228228
uint8_t* scaledImage = (uint8_t *)malloc(w * h * 4);
229-
if(!scaledImage) {
229+
if (!scaledImage) {
230230
err_throw(ERR_IMAGE_LOAD, "Failed to allocate RAM");
231+
return;
231232
}
232233

233234
uint32_t* image32bit = (uint32_t*)image->_image;
234235
uint32_t* scaledImage32bit = (uint32_t*)scaledImage;
235236

236-
for(yy = 0; yy < h; yy++) {
237-
for(xx = 0; xx < w; xx++) {
237+
for (yy = 0; yy < h; yy++) {
238+
for (xx = 0; xx < w; xx++) {
238239
offsetScaledImage = yy * w + xx;
239240
offsetImage = floor((var_num_t)yy / scaling) * image->_width + floor((var_num_t)xx / scaling);
240241
scaledImage32bit[offsetScaledImage] = image32bit[offsetImage];
@@ -300,14 +301,15 @@ ImageBuffer *load_image(var_t *var) {
300301
if (var->type == V_MAP) {
301302
int bid = map_get_int(var, IMG_BID, -1);
302303
if (bid != -1) {
303-
if(scaling == 1.0 || scaling <= 0.0) {
304+
if (scaling == 1.0 || scaling <= 0.0) {
304305
result = get_image((unsigned)bid);
305306
} else {
306307
ImageBuffer *inputImage = nullptr;
307308
inputImage = get_image((unsigned)bid);
308309
uint8_t* imageData = (uint8_t *)malloc(inputImage->_width * inputImage->_height * 4);
309-
if(!imageData) {
310+
if (!imageData) {
310311
err_throw(ERR_IMAGE_LOAD, "Failed to allocate RAM");
312+
return result;
311313
}
312314
result = new ImageBuffer;
313315
result->_bid = ++nextId;
@@ -325,8 +327,9 @@ ImageBuffer *load_image(var_t *var) {
325327
int w = ABS(v_ubound(var, 1) - v_lbound(var, 1)) + 1;
326328
int size = w * h * 4;
327329
auto imageData = (uint8_t *)malloc(size);
328-
if(!imageData) {
330+
if (!imageData) {
329331
err_throw(ERR_IMAGE_LOAD, "Failed to allocate RAM");
332+
return result;
330333
}
331334
for (int y = 0; y < h; y++) {
332335
int yoffs = (y * w * 4);
@@ -391,7 +394,7 @@ ImageBuffer *load_image(dev_file_t *filep) {
391394
scaling = par_getnum();
392395
}
393396

394-
if(scaling == 1.0 || scaling <= 0.0) {
397+
if (scaling == 1.0 || scaling <= 0.0) {
395398
List_each(ImageBuffer *, it, buffers) {
396399
ImageBuffer *next = (*it);
397400
if (next->_filename != nullptr && strcmp(next->_filename, filep->name) == 0) {
@@ -751,6 +754,16 @@ void screen_dump() {
751754
}
752755
}
753756

757+
/*
758+
* I = Image(file [,scale])
759+
* I = Image(image [,scale])
760+
* I = Image(x1,y1,x2,y2 [,scale])
761+
* I = Image(pixmap [,scale])
762+
* I = Image(array [,scale])
763+
* scale > 1: upscale
764+
* scale < 1: downscale
765+
* scale <=0: don't scale
766+
*/
754767
extern "C" void v_create_image(var_p_t var) {
755768
var_t arg;
756769
ImageBuffer *image = nullptr;

0 commit comments

Comments
 (0)