1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
7 #include <bmp_layout.h>
12 #include <asm/unaligned.h>
14 #ifdef CONFIG_VIDEO_BMP_RLE8
15 #define BMP_RLE8_ESCAPE 0
16 #define BMP_RLE8_EOL 0
17 #define BMP_RLE8_EOBMP 1
18 #define BMP_RLE8_DELTA 2
20 static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
24 *(*fbp)++ = cmap[*bmap++];
29 static void draw_encoded_bitmap(ushort **fbp, ushort col, int cnt)
40 static void video_display_rle8_bitmap(struct udevice *dev,
41 struct bmp_image *bmp, ushort *cmap,
42 uchar *fb, int x_off, int y_off)
44 struct video_priv *priv = dev_get_uclass_priv(dev);
51 debug("%s\n", __func__);
52 width = get_unaligned_le32(&bmp->header.width);
53 height = get_unaligned_le32(&bmp->header.height);
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 */
143 #define BMP_ALIGN_CENTER 0x7fff
146 * video_splash_align_axis() - Align a single coordinate
148 *- if a coordinate is 0x7fff then the image will be centred in
150 *- if a coordinate is -ve then it will be offset to the
151 * left/top of the centre by that many pixels
152 *- if a coordinate is positive it will be used unchnaged.
154 * @axis: Input and output coordinate
155 * @panel_size: Size of panel in pixels for that axis
156 * @picture_size: Size of bitmap in pixels for that axis
158 static void video_splash_align_axis(int *axis, unsigned long panel_size,
159 unsigned long picture_size)
161 unsigned long panel_picture_delta = panel_size - picture_size;
162 unsigned long axis_alignment;
164 if (*axis == BMP_ALIGN_CENTER)
165 axis_alignment = panel_picture_delta / 2;
167 axis_alignment = panel_picture_delta + *axis + 1;
171 *axis = max(0, (int)axis_alignment);
174 static void video_set_cmap(struct udevice *dev,
175 struct bmp_color_table_entry *cte, unsigned colours)
177 struct video_priv *priv = dev_get_uclass_priv(dev);
179 ushort *cmap = priv->cmap;
181 debug("%s: colours=%d\n", __func__, colours);
182 for (i = 0; i < colours; ++i) {
183 *cmap = ((cte->red << 8) & 0xf800) |
184 ((cte->green << 3) & 0x07e0) |
185 ((cte->blue >> 3) & 0x001f);
191 int video_bmp_display(struct udevice *dev, ulong bmp_image, int x, int y,
194 struct video_priv *priv = dev_get_uclass_priv(dev);
195 ushort *cmap_base = NULL;
198 struct bmp_image *bmp = map_sysmem(bmp_image, 0);
201 unsigned long width, height, byte_width;
202 unsigned long pwidth = priv->xsize;
203 unsigned colours, bpix, bmp_bpix;
204 struct bmp_color_table_entry *palette;
207 if (!bmp || !(bmp->header.signature[0] == 'B' &&
208 bmp->header.signature[1] == 'M')) {
209 printf("Error: no valid bmp image at %lx\n", bmp_image);
214 width = get_unaligned_le32(&bmp->header.width);
215 height = get_unaligned_le32(&bmp->header.height);
216 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
217 hdr_size = get_unaligned_le16(&bmp->header.size);
218 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
219 palette = (void *)bmp + 14 + hdr_size;
221 colours = 1 << bmp_bpix;
223 bpix = VNBITS(priv->bpix);
225 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
226 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
233 * We support displaying 8bpp BMPs on 16bpp LCDs
234 * and displaying 24bpp BMPs on 32bpp LCDs
236 if (bpix != bmp_bpix &&
237 !(bmp_bpix == 8 && bpix == 16) &&
238 !(bmp_bpix == 24 && bpix == 32)) {
239 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
240 bpix, get_unaligned_le16(&bmp->header.bit_count));
244 debug("Display-bmp: %d x %d with %d colours, display %d\n",
245 (int)width, (int)height, (int)colours, 1 << bpix);
248 video_set_cmap(dev, palette, colours);
250 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
253 video_splash_align_axis(&x, priv->xsize, width);
254 video_splash_align_axis(&y, priv->ysize, height);
257 if ((x + width) > pwidth)
259 if ((y + height) > priv->ysize)
260 height = priv->ysize - y;
262 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
263 fb = (uchar *)(priv->fb +
264 (y + height - 1) * priv->line_length + x * bpix / 8);
269 cmap_base = priv->cmap;
270 #ifdef CONFIG_VIDEO_BMP_RLE8
271 u32 compression = get_unaligned_le32(&bmp->header.compression);
272 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
273 if (compression == BMP_BI_RLE8) {
275 /* TODO implement render code for bpix != 16 */
276 printf("Error: only support 16 bpix");
277 return -EPROTONOSUPPORT;
279 video_display_rle8_bitmap(dev, bmp, cmap_base, fb, x,
288 byte_width = width * 2;
290 for (i = 0; i < height; ++i) {
292 for (j = 0; j < width; j++) {
294 fb_put_byte(&fb, &bmap);
296 *(uint16_t *)fb = cmap_base[*bmap];
298 fb += sizeof(uint16_t) / sizeof(*fb);
301 bmap += (padded_width - width);
302 fb -= byte_width + priv->line_length;
306 #if defined(CONFIG_BMP_16BPP)
308 for (i = 0; i < height; ++i) {
310 for (j = 0; j < width; j++)
311 fb_put_word(&fb, &bmap);
313 bmap += (padded_width - width) * 2;
314 fb -= width * 2 + priv->line_length;
317 #endif /* CONFIG_BMP_16BPP */
318 #if defined(CONFIG_BMP_24BPP)
320 for (i = 0; i < height; ++i) {
321 for (j = 0; j < width; j++) {
327 fb -= priv->line_length + width * (bpix / 8);
330 #endif /* CONFIG_BMP_24BPP */
331 #if defined(CONFIG_BMP_32BPP)
333 for (i = 0; i < height; ++i) {
334 for (j = 0; j < width; j++) {
340 fb -= priv->line_length + width * (bpix / 8);
343 #endif /* CONFIG_BMP_32BPP */
348 video_sync(dev, false);