@@ -114,12 +114,13 @@ void ImageDisplay::draw(int x, int y, int w, int h, int cw) {
114114 }
115115}
116116
117- void argb_to_abgr (unsigned char *image, unsigned w, unsigned h) {
118- #if !defined(PIXELFORMAT_ARGB8888)
117+ void to_argb (unsigned char *image, unsigned w, unsigned h) {
118+ #if defined(_SDL)
119+ // convert from LCT_RGBA to ARGB
119120 for (unsigned y = 0 ; y < h; y++) {
120- unsigned yoffs = (4 * y * w );
121+ unsigned yoffs = (y * w * 4 );
121122 for (unsigned x = 0 ; x < w; x++) {
122- unsigned offs = yoffs + (4 * x );
123+ unsigned offs = yoffs + (x * 4 );
123124 uint8_t r = image[offs + 2 ];
124125 uint8_t b = image[offs + 0 ];
125126 image[offs + 2 ] = b;
@@ -132,43 +133,46 @@ void argb_to_abgr(unsigned char *image, unsigned w, unsigned h) {
132133unsigned decode_png (unsigned char **image, unsigned *w, unsigned *h, const unsigned char *buffer, size_t size) {
133134 unsigned error = lodepng_decode32 (image, w, h, buffer, size);
134135 if (!error) {
135- argb_to_abgr (*image, *w, *h);
136+ to_argb (*image, *w, *h);
136137 }
137138 return error;
138139}
139140
140141unsigned decode_png_file (unsigned char **image, unsigned *w, unsigned *h, const char *filename) {
141142 unsigned error = lodepng_decode32_file (image, w, h, filename);
142143 if (!error) {
143- argb_to_abgr (*image, *w, *h);
144+ to_argb (*image, *w, *h);
144145 }
145146 return error;
146147}
147148
148149unsigned encode_png_file (const char *filename, const unsigned char *image, unsigned w, unsigned h) {
149150 unsigned result;
150- #if defined(PIXELFORMAT_ARGB8888)
151- result = lodepng_encode32_file (filename, image, w, h);
152- #else
151+ #if defined(_SDL)
153152 unsigned size = w * h * 4 ;
154153 auto imageCopy = (uint8_t *)malloc (size);
155154 if (!imageCopy) {
155+ // lodepng memory error code
156156 result = 83 ;
157157 } else {
158+ // convert from ARGB to LCT_RGBA
158159 for (unsigned y = 0 ; y < h; y++) {
159- unsigned yoffs = (4 * y * w );
160+ unsigned yoffs = (y * w * 4 );
160161 for (unsigned x = 0 ; x < w; x++) {
161162 int offs = yoffs + (x * 4 );
162- uint8_t a = image[offs + 3 ];
163- uint8_t r = image[offs + 2 ];
164- uint8_t g = image[offs + 1 ];
165- uint8_t b = image[offs + 0 ];
166- SET_IMAGE_ARGB (imageCopy, offs, a, r, g, b);
163+ uint8_t a, r, g, b;
164+ GET_IMAGE_ARGB (image, offs, a, r, g, b);
165+ imageCopy[offs + 3 ] = a;
166+ imageCopy[offs + 2 ] = b;
167+ imageCopy[offs + 1 ] = g;
168+ imageCopy[offs + 0 ] = r;
167169 }
168170 }
169171 result = lodepng_encode32_file (filename, imageCopy, w, h);
170172 free (imageCopy);
171173 }
174+ #else
175+ result = lodepng_encode32_file (filename, image, w, h);
172176#endif
173177 return result;
174178}
@@ -200,7 +204,6 @@ uint8_t *get_image_data(int x, int y, int w, int h) {
200204 return result;
201205}
202206
203-
204207ImageBuffer *get_image (unsigned bid) {
205208 ImageBuffer *result = nullptr ;
206209 List_each (ImageBuffer *, it, buffers) {
@@ -260,11 +263,11 @@ ImageBuffer *load_image(var_t *var) {
260263 int size = w * h * 4 ;
261264 auto image = (uint8_t *)malloc (size);
262265 for (int y = 0 ; y < h; y++) {
263- int yoffs = (4 * y * w );
266+ int yoffs = (y * w * 4 );
264267 for (int x = 0 ; x < w; x++) {
265268 int pos = y * w + x;
266269 uint8_t a, r, g, b;
267- v_get_argb (- v_getint (v_elem (var, pos)), a, r, g, b);
270+ v_get_argb (v_getint (v_elem (var, pos)), a, r, g, b);
268271 SET_IMAGE_ARGB (image, yoffs + (x * 4 ), a, r, g, b);
269272 }
270273 }
@@ -483,14 +486,13 @@ void cmd_image_save(var_s *self, var_s *) {
483486 // y1 rgba rgba rgba ypos=12
484487 //
485488 for (unsigned y = 0 ; y < h; y++) {
486- unsigned yoffs = (4 * y * w );
489+ unsigned yoffs = (y * w * 4 );
487490 for (unsigned x = 0 ; x < w; x++) {
488491 uint8_t a, r, g, b;
489492 GET_IMAGE_ARGB (image->_image , yoffs + (x * 4 ), a, r, g, b);
490493 pixel_t px = v_get_argb_px (a, r, g, b);
491494 unsigned pos = y * w + x;
492- var_t *elem = v_elem (array, pos);
493- v_setint (elem, -px);
495+ v_setint (v_elem (array, pos), px);
494496 }
495497 }
496498 saved = true ;
0 commit comments