Skip to content

Commit 871a649

Browse files
committed
FLTK: Implemented image opacity handling
1 parent 79a8106 commit 871a649

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

src/platform/fltk/display.cxx

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
#include "config.h"
1010
#include <math.h>
11+
#include <stdint.h>
1112
#include "ui/utils.h"
13+
#include "ui/rgb.h"
1214
#include "platform/fltk/display.h"
1315

1416
GraphicsWidget *graphics;
@@ -112,17 +114,44 @@ void Canvas::drawPixel(int posX, int posY) {
112114
fl_end_offscreen();
113115
}
114116

115-
// x, y, w are position and width of scan line in image. copy w
116-
// pixels from scanline y, starting at pixel x to this buffer.
117+
struct DrawData {
118+
uint8_t *_image;
119+
uint8_t *_screen;
120+
int _opacity;
121+
};
122+
123+
// x, y, w are position and width of scan line in image.
124+
// copy w pixels from scanline y, starting at pixel x to this buffer.
117125
void drawImage(void *data, int x, int y, int w, uchar *out) {
118-
uint8_t *image = (uint8_t *)data;
126+
DrawData *drawData = (DrawData *)data;
127+
uint8_t *image = drawData->_image;
128+
uint8_t *screen = drawData->_screen;
129+
int opacity = drawData->_opacity;
119130
int scanLine = w * 3;
120131
int offs = y * w * 4;
132+
float op = opacity / 100.0f;
133+
fprintf(stderr, "op=%f\n", op);
121134

122135
for (int sx = 0; sx < scanLine; sx += 3, offs += 4) {
123-
out[sx + 0] = image[offs + 0];
124-
out[sx + 1] = image[offs + 1];
125-
out[sx + 2] = image[offs + 2];
136+
uint8_t a = image[offs + 3];
137+
uint8_t r = image[offs + 2];
138+
uint8_t g = image[offs + 1];
139+
uint8_t b = image[offs + 0];
140+
uint8_t dR = screen[offs + 2];
141+
uint8_t dG = screen[offs + 1];
142+
uint8_t dB = screen[offs + 0];
143+
if (opacity > 0 && opacity < 100 && a > 64) {
144+
dR = (op * r) + ((1 - op) * dR);
145+
dG = (op * g) + ((1 - op) * dG);
146+
dB = (op * b) + ((1 - op) * dB);
147+
} else {
148+
dR = dR + ((r - dR) * a / 255);
149+
dG = dG + ((g - dG) * a / 255);
150+
dB = dB + ((b - dB) * a / 255);
151+
}
152+
out[sx + 2] = dR;
153+
out[sx + 1] = dG;
154+
out[sx + 0] = dB;
126155
}
127156
}
128157

@@ -131,9 +160,16 @@ void Canvas::drawRGB(const MAPoint2d *dstPoint, const void *src, const MARect *s
131160
int y = dstPoint->y;
132161
int w = srcRect->width;
133162
int h = srcRect->height;
163+
DrawData data;
164+
data._image = (uint8_t *)src;
165+
data._opacity = opacity;
166+
data._screen = (uint8_t *)calloc(w * h * 4, 1);
167+
134168
fl_begin_offscreen(_offscreen);
135-
fl_draw_image(drawImage, (void *)src, x, y, w, h);
169+
fl_read_image(data._screen, x, y, w, h, 1);
170+
fl_draw_image(drawImage, (void *)&data, x, y, w, h, 3);
136171
fl_end_offscreen();
172+
free(data._screen);
137173
}
138174

139175
void Canvas::drawRegion(Canvas *src, const MARect *srcRect, int destX, int destY) {

src/platform/fltk/display.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "lib/maapi.h"
1616

1717
struct Font {
18-
Font(Fl_Font font, Fl_Fontsize size) : _font(font), _size(size) {}
18+
Font(Fl_Font font, Fl_Fontsize size) : _font(font), _size(size) {}
1919
virtual ~Font() {}
2020
void setCurrent() { fl_font(_font, _size); }
2121

src/ui/graphics.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,8 @@ void Graphics::drawRGB(const MAPoint2d *dstPoint, const void *src,
281281
uint8_t *image = (uint8_t *)src;
282282
size_t scale = 1;
283283
int w = bytesPerLine;
284+
float op = opacity / 100.0f;
284285

285-
if (opacity > 0 && opacity < 100) {
286-
// higher opacity values should make the image less transparent
287-
opacity = 100 - opacity;
288-
}
289286
for (int y = srcRect->top; y < srcRect->height; y += scale) {
290287
int dY = dstPoint->y + y;
291288
if (dY >= _drawTarget->y() &&
@@ -301,10 +298,9 @@ void Graphics::drawRGB(const MAPoint2d *dstPoint, const void *src,
301298
uint8_t dR, dG, dB;
302299
GET_RGB(line[dX], dR, dG, dB);
303300
if (opacity > 0 && opacity < 100 && a > 64) {
304-
float op = opacity / 100.0f;
305-
dR = ((1-op) * r) + (op * dR);
306-
dG = ((1-op) * g) + (op * dG);
307-
dB = ((1-op) * b) + (op * dB);
301+
dR = (op * r) + ((1 - op) * dR);
302+
dG = (op * g) + ((1 - op) * dG);
303+
dB = (op * b) + ((1 - op) * dB);
308304
} else {
309305
dR = dR + ((r - dR) * a / 255);
310306
dG = dG + ((g - dG) * a / 255);

0 commit comments

Comments
 (0)