-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonochrome.h
288 lines (233 loc) · 7.92 KB
/
monochrome.h
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
/*
* This file is part of AtomGL.
*
* Copyright 2022-2024 Davide Bettio <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <math.h>
static int get_color(int x, int y, uint8_t r, uint8_t g, uint8_t b)
{
// dither
/*
* Original bayer matrix
* { 0, 8, 2, 10 },
* { 12, 4, 14, 6 },
* { 3, 11, 1, 9 },
* { 15, 7, 13, 5 }
*
* The following is calculated applying the following code element by element
* r = 255 / values / 4
* roundf(63.75 * ((float) m[x % 4][y % 4] * 0.0625 - 0.5));
*/
const int m[4][4] = {
{ -32, 0, -24, 8 },
{ 16, -16, 24, -8 },
{ -20, 12, -28, 4 },
{ 28, -4, 20, -12 }
};
int v = m[x % 4][y % 4];
int out_r = r + v;
int out_g = g + v;
int out_b = b + v;
// end of dither
// get closest
// float yval = 0.2126 * out_r + 0.7152 * out_g + 0.0722 * out_b;
// the following is a fast formula
int yval = ((out_r << 1) + out_r + (out_g << 2) + out_b) >> 3;
return yval >= 128;
}
static inline void draw_pixel_x(uint8_t *line_buf, int xpos, int color)
{
#if CHECK_OVERFLOW
if (xpos > DISPLAY_WIDTH) {
fprintf(stderr, "display buffer overflow: %i!\n", xpos);
return;
}
#endif
int bpos = (xpos % 8);
line_buf[xpos / 8] = (line_buf[xpos / 8] & ~(0x1 << bpos)) | (color << bpos);
}
static int draw_image_x(uint8_t *line_buf, int xpos, int ypos, int max_line_len, BaseDisplayItem *item)
{
int x = item->x;
int y = item->y;
int bgcolor_r;
int bgcolor_g;
int bgcolor_b;
bool visible_bg;
if (item->brcolor != 0) {
bgcolor_r = (item->brcolor >> 24) & 0xFF;
bgcolor_g = (item->brcolor >> 16) & 0xFF;
bgcolor_b = (item->brcolor >> 8) & 0xFF;
visible_bg = true;
} else {
bgcolor_r = 0;
bgcolor_g = 0;
bgcolor_b = 0;
visible_bg = false;
}
int width = item->width;
const char *data = item->data.image_data.pix;
int drawn_pixels = 0;
uint32_t *pixels = ((uint32_t *) data) + (ypos - y) * width + (xpos - x);
if (width > xpos - x + max_line_len) {
width = xpos - x + max_line_len;
}
for (int j = xpos - x; j < width; j++) {
uint32_t img_pixel = READ_32_UNALIGNED(pixels);
if ((*pixels >> 24) & 0xFF) {
uint8_t r = img_pixel >> 24;
uint8_t g = (img_pixel >> 16) & 0xFF;
uint8_t b = (img_pixel >> 8) & 0xFF;
uint8_t c = get_color(xpos + drawn_pixels, ypos, r, g, b);
draw_pixel_x(line_buf, xpos + drawn_pixels, c);
} else if (visible_bg) {
uint8_t c = get_color(xpos + drawn_pixels, ypos, bgcolor_r, bgcolor_g, bgcolor_b);
draw_pixel_x(line_buf, xpos + drawn_pixels, c);
} else {
return drawn_pixels;
}
drawn_pixels++;
pixels++;
}
return drawn_pixels;
}
static int draw_scaled_cropped_img_x(uint8_t *line_buf, int xpos, int ypos, int max_line_len, BaseDisplayItem *item)
{
int x = item->x;
int y = item->y;
int bgcolor_r;
int bgcolor_g;
int bgcolor_b;
bool visible_bg;
if (item->brcolor != 0) {
bgcolor_r = (item->brcolor >> 24) & 0xFF;
bgcolor_g = (item->brcolor >> 16) & 0xFF;
bgcolor_b = (item->brcolor >> 8) & 0xFF;
visible_bg = true;
} else {
bgcolor_r = 0;
bgcolor_g = 0;
bgcolor_b = 0;
visible_bg = false;
}
int width = item->width;
const char *data = item->data.image_data_with_size.pix;
int drawn_pixels = 0;
int y_scale = item->y_scale;
int x_scale = item->x_scale;
int img_width = item->data.image_data_with_size.width;
int source_x = item->source_x;
int source_y = item->source_y;
uint32_t *pixels = ((uint32_t *) data) + (source_y + ((ypos - y) / y_scale)) * img_width + source_x + ((xpos - x) / x_scale);
if (source_x + (width / x_scale) > img_width) {
width = (img_width - source_x) * x_scale;
}
if (width > xpos - x + max_line_len) {
width = xpos - x + max_line_len;
}
for (int j = xpos - x; j < width; j++) {
uint32_t img_pixel = READ_32_UNALIGNED(pixels);
if ((*pixels >> 24) & 0xFF) {
uint8_t r = img_pixel >> 24;
uint8_t g = (img_pixel >> 16) & 0xFF;
uint8_t b = (img_pixel >> 8) & 0xFF;
uint8_t c = get_color(xpos + drawn_pixels, ypos, r, g, b);
draw_pixel_x(line_buf, xpos + drawn_pixels, c);
} else if (visible_bg) {
uint8_t c = get_color(xpos + drawn_pixels, ypos, bgcolor_r, bgcolor_g, bgcolor_b);
draw_pixel_x(line_buf, xpos + drawn_pixels, c);
} else {
return drawn_pixels;
}
drawn_pixels++;
//TODO: optimize here
pixels = ((uint32_t *) data) + (source_y + ((ypos - y) / y_scale)) * img_width + source_x + (j / x_scale);
}
return drawn_pixels;
}
static int draw_rect_x(uint8_t *line_buf, int xpos, int ypos, int max_line_len, BaseDisplayItem *item)
{
int x = item->x;
int width = item->width;
uint8_t r = (item->brcolor >> 24) & 0xFF;
uint8_t g = (item->brcolor >> 16) & 0xFF;
uint8_t b = (item->brcolor >> 8) & 0xFF;
int drawn_pixels = 0;
if (width > xpos - x + max_line_len) {
width = xpos - x + max_line_len;
}
for (int j = xpos - x; j < width; j++) {
uint8_t c = get_color(xpos + drawn_pixels, ypos, r, g, b);
draw_pixel_x(line_buf, xpos + drawn_pixels, c);
drawn_pixels++;
}
return drawn_pixels;
}
static int draw_text_x(uint8_t *line_buf, int xpos, int ypos, int max_line_len, BaseDisplayItem *item)
{
int x = item->x;
int y = item->y;
bool visible_bg;
int fgcolor_r = (item->data.text_data.fgcolor >> 24) & 0xFF;
int fgcolor_g = (item->data.text_data.fgcolor >> 16) & 0xFF;
int fgcolor_b = (item->data.text_data.fgcolor >> 8) & 0xFF;
int bgcolor_r;
int bgcolor_g;
int bgcolor_b;
if (item->brcolor != 0) {
bgcolor_r = (item->brcolor >> 24) & 0xFF;
bgcolor_g = (item->brcolor >> 16) & 0xFF;
bgcolor_b = (item->brcolor >> 8) & 0xFF;
visible_bg = true;
} else {
bgcolor_r = 0;
bgcolor_g = 0;
bgcolor_b = 0;
visible_bg = false;
}
char *text = (char *) item->data.text_data.text;
int width = item->width;
int drawn_pixels = 0;
if (width > xpos - x + max_line_len) {
width = xpos - x + max_line_len;
}
for (int j = xpos - x; j < width; j++) {
int char_index = j / CHAR_WIDTH;
char c = text[char_index];
unsigned const char *glyph = fontdata + ((unsigned char) c) * 16;
unsigned char row = glyph[ypos - y];
bool opaque;
int k = j % CHAR_WIDTH;
if (row & (1 << (7 - k))) {
opaque = true;
} else {
opaque = false;
}
if (opaque) {
uint8_t c = get_color(xpos + drawn_pixels, ypos, fgcolor_r, fgcolor_g, fgcolor_b);
draw_pixel_x(line_buf, xpos + drawn_pixels, c);
} else if (visible_bg) {
uint8_t c = get_color(xpos + drawn_pixels, ypos, bgcolor_r, bgcolor_g, bgcolor_b);
draw_pixel_x(line_buf, xpos + drawn_pixels, c);
} else {
return drawn_pixels;
}
drawn_pixels++;
}
return drawn_pixels;
}