2 #include <kms++/kms++.h>
3 #include <kms++util/kms++util.h>
5 using namespace std;
7 namespace kms
8 {
9 void draw_rgb_pixel(IMappedFramebuffer& buf, unsigned x, unsigned y, RGB color)
10 {
11 switch (buf.format()) {
12 case PixelFormat::XRGB8888:
13 case PixelFormat::ARGB8888:
14 {
15 uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
16 *p = color.argb8888();
17 break;
18 }
19 case PixelFormat::XBGR8888:
20 case PixelFormat::ABGR8888:
21 {
22 uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
23 *p = color.abgr8888();
24 break;
25 }
26 case PixelFormat::RGB888:
27 {
28 uint8_t *p = buf.map(0) + buf.stride(0) * y + x * 3;
29 p[0] = color.r;
30 p[1] = color.g;
31 p[2] = color.b;
32 break;
33 }
34 case PixelFormat::BGR888:
35 {
36 uint8_t *p = buf.map(0) + buf.stride(0) * y + x * 3;
37 p[0] = color.b;
38 p[1] = color.g;
39 p[2] = color.r;
40 break;
41 }
42 case PixelFormat::RGB565:
43 {
44 uint16_t *p = (uint16_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
45 *p = color.rgb565();
46 break;
47 }
48 case PixelFormat::BGR565:
49 {
50 uint16_t *p = (uint16_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
51 *p = color.bgr565();
52 break;
53 }
54 default:
55 throw std::invalid_argument("invalid pixelformat");
56 }
57 }
59 void draw_yuv422_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y, YUV yuv1, YUV yuv2)
60 {
61 ASSERT((x & 1) == 0);
63 uint8_t *p = (uint8_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
65 uint8_t y0 = yuv1.y;
66 uint8_t y1 = yuv2.y;
67 uint8_t u = (yuv1.u + yuv2.u) / 2;
68 uint8_t v = (yuv1.v + yuv2.v) / 2;
70 switch (buf.format()) {
71 case PixelFormat::UYVY:
72 p[0] = u;
73 p[1] = y0;
74 p[2] = v;
75 p[3] = y1;
76 break;
78 case PixelFormat::YUYV:
79 p[0] = y0;
80 p[1] = u;
81 p[2] = y1;
82 p[3] = v;
83 break;
85 case PixelFormat::YVYU:
86 p[0] = y0;
87 p[1] = v;
88 p[2] = y1;
89 p[3] = u;
90 break;
92 case PixelFormat::VYUY:
93 p[0] = v;
94 p[1] = y0;
95 p[2] = u;
96 p[3] = y1;
97 break;
99 default:
100 throw std::invalid_argument("invalid pixelformat");
101 }
102 }
104 void draw_yuv420_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y,
105 YUV yuv1, YUV yuv2, YUV yuv3, YUV yuv4)
106 {
107 ASSERT((x & 1) == 0);
108 ASSERT((y & 1) == 0);
110 uint8_t *py1 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 0) + x);
111 uint8_t *py2 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 1) + x);
113 uint8_t *puv = (uint8_t*)(buf.map(1) + buf.stride(1) * (y / 2) + x);
115 uint8_t y0 = yuv1.y;
116 uint8_t y1 = yuv2.y;
117 uint8_t y2 = yuv3.y;
118 uint8_t y3 = yuv4.y;
119 uint8_t u = (yuv1.u + yuv2.u + yuv3.u + yuv4.u) / 4;
120 uint8_t v = (yuv1.v + yuv2.v + yuv3.v + yuv4.v) / 4;
122 switch (buf.format()) {
123 case PixelFormat::NV12:
124 py1[0] = y0;
125 py1[1] = y1;
126 py2[0] = y2;
127 py2[1] = y3;
128 puv[0] = u;
129 puv[1] = v;
130 break;
132 case PixelFormat::NV21:
133 py1[0] = y0;
134 py1[1] = y1;
135 py2[0] = y2;
136 py2[1] = y3;
137 puv[0] = v;
138 puv[1] = u;
139 break;
141 default:
142 throw std::invalid_argument("invalid pixelformat");
143 }
144 }
146 void draw_rect(IMappedFramebuffer &fb, uint32_t x, uint32_t y, uint32_t w, uint32_t h, RGB color)
147 {
148 for (unsigned i = x; i < x + w; ++i) {
149 for (unsigned j = y; j < y + h; ++j) {
150 draw_rgb_pixel(fb, i, j, color);
151 }
152 }
153 }
155 static bool get_char_pixel(char c, uint32_t x, uint32_t y)
156 {
157 #include "font_8x8.h"
159 uint8_t bits = fontdata_8x8[8 * c + y];
160 bool bit = (bits >> (7 - x)) & 1;
162 return bit;
163 }
165 static void draw_char(IMappedFramebuffer& buf, uint32_t xpos, uint32_t ypos, char c, RGB color)
166 {
167 unsigned x, y;
168 YUV yuvcolor = color.yuv();
170 switch (buf.format()) {
171 case PixelFormat::XRGB8888:
172 case PixelFormat::XBGR8888:
173 case PixelFormat::ARGB8888:
174 case PixelFormat::ABGR8888:
175 case PixelFormat::RGB888:
176 case PixelFormat::BGR888:
177 case PixelFormat::RGB565:
178 case PixelFormat::BGR565:
179 for (y = 0; y < 8; y++) {
180 for (x = 0; x < 8; x++) {
181 bool b = get_char_pixel(c, x, y);
183 draw_rgb_pixel(buf, xpos + x, ypos + y, b ? color : RGB());
184 }
185 }
186 break;
188 case PixelFormat::UYVY:
189 case PixelFormat::YUYV:
190 case PixelFormat::YVYU:
191 case PixelFormat::VYUY:
192 for (y = 0; y < 8; y++) {
193 for (x = 0; x < 8; x += 2) {
194 bool b0 = get_char_pixel(c, x, y);
195 bool b1 = get_char_pixel(c, x + 1, y);
197 draw_yuv422_macropixel(buf, xpos + x, ypos + y,
198 b0 ? yuvcolor : YUV(), b1 ? yuvcolor : YUV());
199 }
200 }
201 break;
203 case PixelFormat::NV12:
204 case PixelFormat::NV21:
205 for (y = 0; y < 8; y += 2) {
206 for (x = 0; x < 8; x += 2) {
207 bool b00 = get_char_pixel(c, x, y);
208 bool b10 = get_char_pixel(c, x + 1, y);
209 bool b01 = get_char_pixel(c, x, y + 1);
210 bool b11 = get_char_pixel(c, x + 1, y + 1);
212 draw_yuv420_macropixel(buf, xpos + x, ypos + y,
213 b00 ? yuvcolor : YUV(), b10 ? yuvcolor : YUV(),
214 b01 ? yuvcolor : YUV(), b11 ? yuvcolor : YUV());
215 }
216 }
217 break;
218 default:
219 throw std::invalid_argument("unknown pixelformat");
220 }
221 }
223 void draw_text(IMappedFramebuffer& buf, uint32_t x, uint32_t y, const string& str, RGB color)
224 {
225 for(unsigned i = 0; i < str.size(); i++)
226 draw_char(buf, (x + 8 * i), y, str[i], color);
227 }
229 }