1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
7 #include <bmp_layout.h>
14 #include <asm/unaligned.h>
16 #ifdef CONFIG_VIDEO_BMP_RLE8
17 #define BMP_RLE8_ESCAPE 0
18 #define BMP_RLE8_EOL 0
19 #define BMP_RLE8_EOBMP 1
20 #define BMP_RLE8_DELTA 2
22 static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
26 *(*fbp)++ = cmap[*bmap++];
31 static void draw_encoded_bitmap(ushort **fbp, ushort col, int cnt)
42 static void video_display_rle8_bitmap(struct udevice *dev,
43 struct bmp_image *bmp, ushort *cmap,
44 uchar *fb, int x_off, int y_off,
45 ulong width, ulong height)
47 struct video_priv *priv = dev_get_uclass_priv(dev);
53 debug("%s\n", __func__);
54 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
60 if (bmap[0] == BMP_RLE8_ESCAPE) {
67 /* 16bpix, 2-byte per pixel, width should *2 */
68 fb -= (width * 2 + priv->line_length);
78 /* 16bpix, 2-byte per pixel, x should *2 */
79 fb = (uchar *)(priv->fb + (y + y_off - 1)
80 * priv->line_length + (x + x_off) * 2);
89 if (x + runlen > width)
93 draw_unencoded_bitmap(
108 /* aggregate the same code */
109 while (bmap[0] == 0xff &&
110 bmap[2] != BMP_RLE8_ESCAPE &&
111 bmap[1] == bmap[3]) {
115 if (x + runlen > width)
119 draw_encoded_bitmap((ushort **)&fb,
130 __weak void fb_put_byte(uchar **fb, uchar **from)
132 *(*fb)++ = *(*from)++;
135 #if defined(CONFIG_BMP_16BPP)
136 __weak void fb_put_word(uchar **fb, uchar **from)
138 *(*fb)++ = *(*from)++;
139 *(*fb)++ = *(*from)++;
141 #endif /* CONFIG_BMP_16BPP */
144 * video_splash_align_axis() - Align a single coordinate
146 *- if a coordinate is 0x7fff then the image will be centred in
148 *- if a coordinate is -ve then it will be offset to the
149 * left/top of the centre by that many pixels
150 *- if a coordinate is positive it will be used unchnaged.
152 * @axis: Input and output coordinate
153 * @panel_size: Size of panel in pixels for that axis
154 * @picture_size: Size of bitmap in pixels for that axis
156 static void video_splash_align_axis(int *axis, unsigned long panel_size,
157 unsigned long picture_size)
159 long panel_picture_delta = panel_size - picture_size;
162 if (*axis == BMP_ALIGN_CENTER)
163 axis_alignment = panel_picture_delta / 2;
165 axis_alignment = panel_picture_delta + *axis + 1;
169 *axis = max(0, (int)axis_alignment);
172 static void video_set_cmap(struct udevice *dev,
173 struct bmp_color_table_entry *cte, unsigned colours)
175 struct video_priv *priv = dev_get_uclass_priv(dev);
177 ushort *cmap = priv->cmap;
179 debug("%s: colours=%d\n", __func__, colours);
180 for (i = 0; i < colours; ++i) {
181 *cmap = ((cte->red << 8) & 0xf800) |
182 ((cte->green << 3) & 0x07e0) |
183 ((cte->blue >> 3) & 0x001f);
189 int video_bmp_display(struct udevice *dev, ulong bmp_image, int x, int y,
192 struct video_priv *priv = dev_get_uclass_priv(dev);
193 ushort *cmap_base = NULL;
196 struct bmp_image *bmp = map_sysmem(bmp_image, 0);
199 unsigned long width, height, byte_width;
200 unsigned long pwidth = priv->xsize;
201 unsigned colours, bpix, bmp_bpix;
202 struct bmp_color_table_entry *palette;
205 if (!bmp || !(bmp->header.signature[0] == 'B' &&
206 bmp->header.signature[1] == 'M')) {
207 printf("Error: no valid bmp image at %lx\n", bmp_image);
212 width = get_unaligned_le32(&bmp->header.width);
213 height = get_unaligned_le32(&bmp->header.height);
214 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
215 hdr_size = get_unaligned_le16(&bmp->header.size);
216 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
217 palette = (void *)bmp + 14 + hdr_size;
219 colours = 1 << bmp_bpix;
221 bpix = VNBITS(priv->bpix);
223 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
224 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
231 * We support displaying 8bpp and 24bpp BMPs on 16bpp LCDs
232 * and displaying 24bpp BMPs on 32bpp LCDs
234 if (bpix != bmp_bpix &&
235 !(bmp_bpix == 8 && bpix == 16) &&
236 !(bmp_bpix == 24 && bpix == 16) &&
237 !(bmp_bpix == 24 && bpix == 32)) {
238 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
239 bpix, get_unaligned_le16(&bmp->header.bit_count));
243 debug("Display-bmp: %d x %d with %d colours, display %d\n",
244 (int)width, (int)height, (int)colours, 1 << bpix);
247 video_set_cmap(dev, palette, colours);
249 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
252 video_splash_align_axis(&x, priv->xsize, width);
253 video_splash_align_axis(&y, priv->ysize, height);
256 if ((x + width) > pwidth)
258 if ((y + height) > priv->ysize)
259 height = priv->ysize - y;
261 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
262 fb = (uchar *)(priv->fb +
263 (y + height - 1) * priv->line_length + x * bpix / 8);
268 cmap_base = priv->cmap;
269 #ifdef CONFIG_VIDEO_BMP_RLE8
270 u32 compression = get_unaligned_le32(&bmp->header.compression);
271 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
272 if (compression == BMP_BI_RLE8) {
274 /* TODO implement render code for bpix != 16 */
275 printf("Error: only support 16 bpix");
276 return -EPROTONOSUPPORT;
278 video_display_rle8_bitmap(dev, bmp, cmap_base, fb, x,
287 byte_width = width * 2;
289 for (i = 0; i < height; ++i) {
291 for (j = 0; j < width; j++) {
293 fb_put_byte(&fb, &bmap);
295 *(uint16_t *)fb = cmap_base[*bmap];
297 fb += sizeof(uint16_t) / sizeof(*fb);
300 bmap += (padded_width - width);
301 fb -= byte_width + priv->line_length;
305 #if defined(CONFIG_BMP_16BPP)
307 for (i = 0; i < height; ++i) {
309 for (j = 0; j < width; j++)
310 fb_put_word(&fb, &bmap);
312 bmap += (padded_width - width) * 2;
313 fb -= width * 2 + priv->line_length;
316 #endif /* CONFIG_BMP_16BPP */
317 #if defined(CONFIG_BMP_24BPP)
319 for (i = 0; i < height; ++i) {
320 for (j = 0; j < width; j++) {
322 /* 16bit 555RGB format */
323 *(u16 *)fb = ((bmap[2] >> 3) << 10) |
324 ((bmap[1] >> 3) << 5) |
335 fb -= priv->line_length + width * (bpix / 8);
336 bmap += (padded_width - width) * 3;
339 #endif /* CONFIG_BMP_24BPP */
340 #if defined(CONFIG_BMP_32BPP)
342 for (i = 0; i < height; ++i) {
343 for (j = 0; j < width; j++) {
349 fb -= priv->line_length + width * (bpix / 8);
352 #endif /* CONFIG_BMP_32BPP */
357 video_sync(dev, false);