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
3129struct ImageBuffer {
3230 ImageBuffer ();
@@ -49,8 +47,8 @@ void reset_image_cache() {
4947
5048ImageBuffer::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) :
6664ImageBuffer::~ImageBuffer () {
6765 free (_filename);
6866 free (_image);
69- _filename = NULL ;
70- _image = NULL ;
67+ _filename = nullptr ;
68+ _image = nullptr ;
7169}
7270
7371dev_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
8583ImageBuffer *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
109107ImageBuffer *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
153145ImageBuffer *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//
177169ImageBuffer *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//
301293void 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//
387373void 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//
446432extern " 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);
0 commit comments