Skip to content

Commit 820e347

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

File tree

3 files changed

+45
-42
lines changed

3 files changed

+45
-42
lines changed

src/platform/fltk/display.cxx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ void drawImage(void *data, int x, int y, int w, uchar *out) {
135135

136136
for (int sx = 0; sx < scanLine; sx += 4, i_offs += 4, s_offs += 4) {
137137
uint8_t a = image[i_offs + 3];
138-
uint8_t r = image[i_offs + 2];
138+
uint8_t b = image[i_offs + 2];
139139
uint8_t g = image[i_offs + 1];
140-
uint8_t b = image[i_offs + 0];
141-
uint8_t sR = screen[s_offs + 2];
140+
uint8_t r = image[i_offs + 0];
141+
uint8_t sB = screen[s_offs + 2];
142142
uint8_t sG = screen[s_offs + 1];
143-
uint8_t sB = screen[s_offs + 0];
143+
uint8_t sR = screen[s_offs + 0];
144144
if (opacity > 0 && opacity < 100 && a > 64) {
145145
sR = (op * r) + ((1 - op) * sR);
146146
sG = (op * g) + ((1 - op) * sG);
@@ -151,9 +151,9 @@ void drawImage(void *data, int x, int y, int w, uchar *out) {
151151
sB = sB + ((b - sB) * a / 255);
152152
}
153153
out[sx + 3] = a;
154-
out[sx + 2] = sR;
154+
out[sx + 2] = sB;
155155
out[sx + 1] = sG;
156-
out[sx + 0] = sB;
156+
out[sx + 0] = sR;
157157
}
158158
}
159159

src/ui/image.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
132133
unsigned 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

140141
unsigned 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

148149
unsigned 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-
204207
ImageBuffer *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;

src/ui/rgb.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,25 @@ inline void v_get_argb(pixel_t c, uint8_t &a, uint8_t &r, uint8_t &g, uint8_t &b
2626

2727
#define v_get_argb_px(a, r, g, b) (a << 24 | (r << 16) | (g << 8) | (b))
2828

29-
#if defined(PIXELFORMAT_ARGB8888)
29+
#if defined(_SDL)
30+
// SDL_PACKEDORDER_XRGB
31+
// A = byte 3
32+
// R = byte 2
33+
// G = byte 1
34+
// B = byte 0
3035

3136
#define GET_RGB_PX(r, g, b) ((0xff000000) | (r << 16) | (g << 8) | (b))
3237

33-
inline pixel_t GET_FROM_RGB888(unsigned c) {
34-
uint8_t r = (c & 0xff0000) >> 16;
35-
uint8_t g = (c & 0xff00) >> 8;
36-
uint8_t b = (c & 0xff);
37-
return ((0xff000000) | (b << 16) | (g << 8) | (r));
38-
}
38+
// same as internal format
39+
#define GET_FROM_RGB888(c) (c)
3940

4041
inline void GET_RGB(pixel_t c, uint8_t &r, uint8_t &g, uint8_t &b) {
4142
r = (c & 0xff0000) >> 16;
4243
g = (c & 0xff00) >> 8;
4344
b = (c & 0xff);
4445
}
4546

46-
inline void GET_IMAGE_ARGB(uint8_t *image, unsigned offs, uint8_t &a, uint8_t &r, uint8_t &g, uint8_t &b) {
47+
inline void GET_IMAGE_ARGB(const uint8_t *image, unsigned offs, uint8_t &a, uint8_t &r, uint8_t &g, uint8_t &b) {
4748
a = image[offs + 3];
4849
r = image[offs + 2];
4950
g = image[offs + 1];
@@ -59,22 +60,22 @@ inline void SET_IMAGE_ARGB(uint8_t *image, unsigned offs, uint8_t a, uint8_t r,
5960

6061
#else
6162

62-
// A = byte 3
63-
// B = byte 2
64-
// G = byte 1
65-
// R = byte 0
66-
6763
#define GET_RGB_PX(r, g, b) ((0xff000000) | (b << 16) | (g << 8) | (r))
6864

69-
#define GET_FROM_RGB888(c) (c)
65+
inline pixel_t GET_FROM_RGB888(unsigned c) {
66+
uint8_t r = (c & 0xff0000) >> 16;
67+
uint8_t g = (c & 0xff00) >> 8;
68+
uint8_t b = (c & 0xff);
69+
return ((0xff000000) | (b << 16) | (g << 8) | (r));
70+
}
7071

7172
inline void GET_RGB(pixel_t c, uint8_t &r, uint8_t &g, uint8_t &b) {
7273
b = (c & 0xff0000) >> 16;
7374
g = (c & 0xff00) >> 8;
7475
r = (c & 0xff);
7576
}
7677

77-
inline void GET_IMAGE_ARGB(uint8_t *image, unsigned offs, uint8_t &a, uint8_t &r, uint8_t &g, uint8_t &b) {
78+
inline void GET_IMAGE_ARGB(const uint8_t *image, unsigned offs, uint8_t &a, uint8_t &r, uint8_t &g, uint8_t &b) {
7879
a = image[offs + 3];
7980
b = image[offs + 2];
8081
g = image[offs + 1];

0 commit comments

Comments
 (0)