1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
diff --git a/apps/buffering.c b/apps/buffering.c
index c47564b..19bb111 100644
--- a/apps/buffering.c
+++ b/apps/buffering.c
@@ -874,6 +874,7 @@ static int load_image(int fd, const char *path,
     bmp->width = dim->width, bmp->height = dim->height;
     /* FIXME: alignment may be needed for the data buffer. */
     bmp->data = &buffer[buf_widx + sizeof(struct bitmap)];
+    bmp->alpha = NULL;
 #ifndef HAVE_JPEG
     (void) path;
 #endif
diff --git a/apps/gui/backdrop.c b/apps/gui/backdrop.c
index fb35fe1..b42bd2a 100644
--- a/apps/gui/backdrop.c
+++ b/apps/gui/backdrop.c
@@ -34,6 +34,7 @@ bool backdrop_load(const char* filename, char *backdrop_buffer)
 
     /* load the image */
     bm.data = backdrop_buffer;
+    bm.alpha = NULL;
     ret = read_bmp_file(filename, &bm, LCD_BACKDROP_BYTES,
                         FORMAT_NATIVE | FORMAT_DITHER, NULL);
 
@@ -59,6 +60,7 @@ bool remote_backdrop_load(const char *filename, char* backdrop_buffer)
 
     /* load the image */
     bm.data = backdrop_buffer;
+    bmp->alpha = NULL;
     ret = read_bmp_file(filename, &bm, REMOTE_LCD_BACKDROP_BYTES,
                         FORMAT_NATIVE | FORMAT_DITHER | FORMAT_REMOTE, NULL);
     return ((ret > 0)
diff --git a/apps/gui/icon.c b/apps/gui/icon.c
index 87ea071..ac26997 100644
--- a/apps/gui/icon.c
+++ b/apps/gui/icon.c
@@ -225,6 +225,8 @@ static void load_icons(const char* filename, enum Iconset iconset)
     }
     
     *loaded_ok = false;
+    bmp->alpha = NULL;
+
     if (filename[0] && filename[0] != '-')
     {
         char path[MAX_PATH];
diff --git a/apps/gui/skin_engine/skin_parser.c b/apps/gui/skin_engine/skin_parser.c
index 1557783..0fd5741 100644
--- a/apps/gui/skin_engine/skin_parser.c
+++ b/apps/gui/skin_engine/skin_parser.c
@@ -1568,6 +1568,7 @@ static int load_skin_bmp(struct wps_data *wps_data, struct bitmap *bitmap, char*
     lseek(fd, 0, SEEK_SET);
     lock_handle(handle);
     bitmap->data = core_get_data(handle);
+    bitmap->alpha = NULL;
     int ret = read_bmp_fd(fd, bitmap, buf_size, format, NULL);
     bitmap->data = NULL; /* do this to force a crash later if the 
                             caller doesnt call core_get_data() */
diff --git a/apps/plugin.c b/apps/plugin.c
index b9c2e7c..d8b54f3 100644
--- a/apps/plugin.c
+++ b/apps/plugin.c
@@ -805,6 +805,7 @@ static const struct plugin_api rockbox_api = {
 
     /* new stuff at the end, sort into place next time
        the API gets incompatible */
+    lcd_bmp_part,
 };
 
 int plugin_load(const char* plugin, const void* parameter)
diff --git a/apps/plugin.h b/apps/plugin.h
index fa1259c..17ec2b1 100644
--- a/apps/plugin.h
+++ b/apps/plugin.h
@@ -947,7 +947,9 @@ struct plugin_api {
 
     /* new stuff at the end, sort into place next time
        the API gets incompatible */
-    
+    void (*lcd_bmp_part)(const struct bitmap* bm, int src_x, int src_y,
+                                int x, int y, int width, int height);
+    void (*lcd_bmp)(const struct bitmap* bm, int x, int y);
 };
 
 /* plugin header */
diff --git a/apps/plugins/CATEGORIES b/apps/plugins/CATEGORIES
index 0ac5bdb..d19d7c0 100644
--- a/apps/plugins/CATEGORIES
+++ b/apps/plugins/CATEGORIES
@@ -116,6 +116,7 @@ test_sampr,apps
 test_scanrate,apps
 test_touchscreen,apps
 test_viewports,apps
+test_transparency,apps
 test_greylib_bitmap_scale,viewers
 text_editor,apps
 text_viewer,viewers
diff --git a/apps/plugins/SOURCES b/apps/plugins/SOURCES
index 387f145..fad6e4e 100644
--- a/apps/plugins/SOURCES
+++ b/apps/plugins/SOURCES
@@ -258,3 +258,4 @@ test_touchscreen.c
 #endif
 test_viewports.c
 #endif /* HAVE_TEST_PLUGINS */
+test_transparency.c
diff --git a/apps/recorder/bmp.c b/apps/recorder/bmp.c
index d6f61d1..f1d8138 100644
--- a/apps/recorder/bmp.c
+++ b/apps/recorder/bmp.c
@@ -173,6 +173,7 @@ static inline void set_rgb_union(struct uint8_rgb *dst, union rgb_union src)
     dst->red = src.red;
     dst->green = src.green;
     dst->blue = src.blue;
+    dst->alpha = 0xff;
 }
 
 struct bmp_args {
@@ -274,6 +275,7 @@ static unsigned int read_part_line(struct bmp_args *ba)
             component = data & 0xf8;
             component |= component >> 5;
             buf->red = component;
+            buf->alpha = 0xff;
             buf++;
             ibuf += 2;
             break;
@@ -282,8 +284,7 @@ static unsigned int read_part_line(struct bmp_args *ba)
             buf->blue = *ibuf++;
             buf->green = *ibuf++;
             buf->red = *ibuf++;
-            if (depth == 32)
-                ibuf++;
+            buf->alpha = (depth == 32) ? *ibuf++ : 0xff;
             buf++;
             break;
         }
@@ -445,16 +446,23 @@ void output_row_8_native(uint32_t row, void * row_in,
                 fb_data *dest = (fb_data *)ctx->bm->data + fb_width * row;
                 int delta = 127;
                 unsigned r, g, b;
+                unsigned char *bm_alpha = ctx->bm->alpha;
+                if (bm_alpha) bm_alpha += ctx->bm->width*row;
                 for (col = 0; col < ctx->bm->width; col++) {
                     if (ctx->dither)
                         delta = DITHERXDY(col,dy);
                     r = qp->red;
                     g = qp->green;
-                    b = (qp++)->blue;
+                    b = qp->blue;
                     r = (31 * r + (r >> 3) + delta) >> 8;
                     g = (63 * g + (g >> 2) + delta) >> 8;
                     b = (31 * b + (b >> 3) + delta) >> 8;
                     *dest++ = LCD_RGBPACK_LCD(r, g, b);
+                    if (bm_alpha) {   /* pack alpha channel for 2 pixels into 1 byte */
+                        DEBUGF("alpha: %d\n", qp->alpha);
+                        *bm_alpha++ = 255-qp->alpha;
+                    }
+                    qp++;
                 }
 #endif
 #endif /* LCD_DEPTH */
diff --git a/apps/recorder/bmp.h b/apps/recorder/bmp.h
index 31bd073..3ddc3b6 100644
--- a/apps/recorder/bmp.h
+++ b/apps/recorder/bmp.h
@@ -39,6 +39,7 @@ struct uint8_rgb {
     uint8_t blue;
     uint8_t green;
     uint8_t red;
+    uint8_t alpha;
 };
 
 struct dim {
diff --git a/firmware/drivers/lcd-16bit-common.c b/firmware/drivers/lcd-16bit-common.c
index 1e1548e..f36bf07 100644
--- a/firmware/drivers/lcd-16bit-common.c
+++ b/firmware/drivers/lcd-16bit-common.c
@@ -515,3 +515,95 @@ void ICODE_ATTR lcd_alpha_bitmap_part(const unsigned char *src, int src_x,
 #endif
     } while (--row);
 }
+
+/* Draw a partial native bitmap including alpha channel */
+void ICODE_ATTR lcd_bmp_part(const struct bitmap* bm, int src_x, int src_y,
+                                int x, int y, int width, int height)
+{
+    fb_data *dst, *src = (fb_data*)bm->data, *bg = lcd_backdrop;
+    unsigned char* alpha = bm->alpha;
+    int stride = bm->width;
+
+    if (!alpha)
+        lcd_bitmap_part(src, src_x, src_y, stride, x, y, width, height);
+
+    /******************** Image in viewport clipping **********************/
+    /* nothing to draw? */
+    if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
+        (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
+        return;
+        
+    if (x < 0)
+    {
+        width += x;
+        src_x -= x;
+        x = 0;
+    }
+    if (y < 0)
+    {
+        height += y;
+        src_y -= y;
+        y = 0;
+    }
+    
+    if (x + width > current_vp->width)
+        width = current_vp->width - x;
+    if (y + height > current_vp->height)
+        height = current_vp->height - y;    
+    
+    /* adjust for viewport */
+    x += current_vp->x;
+    y += current_vp->y;
+    
+#if defined(HAVE_VIEWPORT_CLIP)
+    /********************* Viewport on screen clipping ********************/
+    /* nothing to draw? */
+    if ((x >= LCD_WIDTH) || (y >= LCD_HEIGHT) 
+        || (x + width <= 0) || (y + height <= 0))
+        return;
+    
+    /* clip image in viewport in screen */
+    if (x < 0)
+    {
+        width += x;
+        src_x -= x;
+        x = 0;
+    }
+    if (y < 0)
+    {
+        height += y;
+        src_y -= y;
+        y = 0;
+    }
+    if (x + width > LCD_WIDTH)
+        width = LCD_WIDTH - x;
+    if (y + height > LCD_HEIGHT)
+        height = LCD_HEIGHT - y;
+#endif
+    
+    src += stride * src_y + src_x; /* move starting point */
+    alpha += stride * src_y + src_x;
+    dst = LCDADDR(x, y);
+    
+    unsigned bg_pattern = current_vp->bg_pattern;
+    long bo = lcd_backdrop_offset;
+
+    do
+    {
+        for(int col = 0; col < width; col++)
+        {
+            unsigned c1 = lcd_backdrop ? ((fb_data *)((long)dst + bo))[col] : bg_pattern;
+            dst[col] = blend_two_colors(c1, src[col], alpha[col] >> 4);
+        }
+        src += stride;
+        alpha += stride;
+        dst += LCD_WIDTH;
+    }
+    while (--height > 0);
+}
+
+/* Draw a native bitmap with alpha channel */
+void ICODE_ATTR lcd_bmp(const struct bitmap *bmp, int x, int y)
+{
+    lcd_bmp_part(bmp, 0, 0, x, y, bmp->width, bmp->height);
+}
diff --git a/firmware/export/lcd.h b/firmware/export/lcd.h
index f433623..c080832 100644
--- a/firmware/export/lcd.h
+++ b/firmware/export/lcd.h
@@ -467,6 +467,7 @@ struct bitmap {
 #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
     int format;
     unsigned char *maskdata;
+    unsigned char *alpha;
 #endif
     unsigned char *data;
 };
@@ -514,6 +515,9 @@ extern void lcd_bitmap_part(const fb_data *src, int src_x, int src_y,
                             int stride, int x, int y, int width, int height);
 extern void lcd_bitmap(const fb_data *src, int x, int y, int width,
                        int height);
+extern void lcd_bmp_part(const struct bitmap* bm, int src_x, int src_y,
+                            int x, int y, int width, int height);
+extern void lcd_bmp(const struct bitmap* bm, int x, int y);
 
 extern void lcd_scroll_step(int pixels);