1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
7 #include <bmp_layout.h>
14 #include <asm/unaligned.h>
16 #define BMP_RLE8_ESCAPE 0
17 #define BMP_RLE8_EOL 0
18 #define BMP_RLE8_EOBMP 1
19 #define BMP_RLE8_DELTA 2
22 * get_bmp_col_16bpp() - Convert a colour-table entry into a 16bpp pixel value
24 * @return value to write to the 16bpp frame buffer for this palette entry
26 static uint get_bmp_col_16bpp(struct bmp_color_table_entry cte)
28 return ((cte.red << 8) & 0xf800) |
29 ((cte.green << 3) & 0x07e0) |
30 ((cte.blue >> 3) & 0x001f);
34 * write_pix8() - Write a pixel from a BMP image into the framebuffer
36 * This handles frame buffers with 8, 16, 24 or 32 bits per pixel
38 * @fb: Place in frame buffer to update
39 * @bpix: Frame buffer bits-per-pixel, which controls how many bytes are written
40 * @palette: BMP palette table
41 * @bmap: Pointer to BMP bitmap position to write. This contains a single byte
42 * which is either written directly (bpix == 8) or used to look up the
43 * palette to get a colour to write
45 static void write_pix8(u8 *fb, uint bpix, struct bmp_color_table_entry *palette,
50 } else if (bpix == 16) {
51 *(u16 *)fb = get_bmp_col_16bpp(palette[*bmap]);
53 /* Only support big endian */
54 struct bmp_color_table_entry *cte = &palette[*bmap];
69 #ifdef CONFIG_VIDEO_BMP_RLE8
70 static void draw_unencoded_bitmap(u8 **fbp, uint bpix, uchar *bmap,
71 struct bmp_color_table_entry *palette,
77 write_pix8(fb, bpix, palette, bmap++);
84 static void draw_encoded_bitmap(u8 **fbp, uint bpix,
85 struct bmp_color_table_entry *palette, u8 *bmap,
91 write_pix8(fb, bpix, palette, bmap);
98 static void video_display_rle8_bitmap(struct udevice *dev,
99 struct bmp_image *bmp, uint bpix,
100 struct bmp_color_table_entry *palette,
101 uchar *fb, int x_off, int y_off,
102 ulong width, ulong height)
104 struct video_priv *priv = dev_get_uclass_priv(dev);
109 uint bytes_per_pixel = bpix / 8;
111 debug("%s\n", __func__);
112 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
118 if (bmap[0] == BMP_RLE8_ESCAPE) {
125 fb -= width * bytes_per_pixel +
136 fb = (uchar *)(priv->fb +
137 (y + y_off - 1) * priv->line_length +
138 (x + x_off) * bytes_per_pixel);
147 if (x + runlen > width)
151 draw_unencoded_bitmap(
166 /* aggregate the same code */
167 while (bmap[0] == 0xff &&
168 bmap[2] != BMP_RLE8_ESCAPE &&
169 bmap[1] == bmap[3]) {
173 if (x + runlen > width)
177 draw_encoded_bitmap(&fb, bpix, palette,
189 * video_splash_align_axis() - Align a single coordinate
191 *- if a coordinate is 0x7fff then the image will be centred in
193 *- if a coordinate is -ve then it will be offset to the
194 * left/top of the centre by that many pixels
195 *- if a coordinate is positive it will be used unchnaged.
197 * @axis: Input and output coordinate
198 * @panel_size: Size of panel in pixels for that axis
199 * @picture_size: Size of bitmap in pixels for that axis
201 static void video_splash_align_axis(int *axis, unsigned long panel_size,
202 unsigned long picture_size)
204 long panel_picture_delta = panel_size - picture_size;
207 if (*axis == BMP_ALIGN_CENTER)
208 axis_alignment = panel_picture_delta / 2;
210 axis_alignment = panel_picture_delta + *axis + 1;
214 *axis = max(0, (int)axis_alignment);
217 int video_bmp_display(struct udevice *dev, ulong bmp_image, int x, int y,
220 struct video_priv *priv = dev_get_uclass_priv(dev);
223 struct bmp_image *bmp = map_sysmem(bmp_image, 0);
226 unsigned long width, height, byte_width;
227 unsigned long pwidth = priv->xsize;
228 unsigned colours, bpix, bmp_bpix;
229 struct bmp_color_table_entry *palette;
233 if (!bmp || !(bmp->header.signature[0] == 'B' &&
234 bmp->header.signature[1] == 'M')) {
235 printf("Error: no valid bmp image at %lx\n", bmp_image);
240 width = get_unaligned_le32(&bmp->header.width);
241 height = get_unaligned_le32(&bmp->header.height);
242 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
243 hdr_size = get_unaligned_le16(&bmp->header.size);
244 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
245 palette = (void *)bmp + 14 + hdr_size;
247 colours = 1 << bmp_bpix;
249 bpix = VNBITS(priv->bpix);
251 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
252 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
259 * We support displaying 8bpp and 24bpp BMPs on 16bpp LCDs
260 * and displaying 24bpp BMPs on 32bpp LCDs
262 if (bpix != bmp_bpix &&
263 !(bmp_bpix == 8 && bpix == 16) &&
264 !(bmp_bpix == 8 && bpix == 24) &&
265 !(bmp_bpix == 8 && bpix == 32) &&
266 !(bmp_bpix == 24 && bpix == 16) &&
267 !(bmp_bpix == 24 && bpix == 32)) {
268 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
269 bpix, get_unaligned_le16(&bmp->header.bit_count));
273 debug("Display-bmp: %d x %d with %d colours, display %d\n",
274 (int)width, (int)height, (int)colours, 1 << bpix);
276 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
279 video_splash_align_axis(&x, priv->xsize, width);
280 video_splash_align_axis(&y, priv->ysize, height);
283 if ((x + width) > pwidth)
285 if ((y + height) > priv->ysize)
286 height = priv->ysize - y;
288 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
289 start = (uchar *)(priv->fb +
290 (y + height) * priv->line_length + x * bpix / 8);
292 /* Move back to the final line to be drawn */
293 fb = start - priv->line_length;
298 #ifdef CONFIG_VIDEO_BMP_RLE8
299 u32 compression = get_unaligned_le32(&bmp->header.compression);
300 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
301 if (compression == BMP_BI_RLE8) {
302 video_display_rle8_bitmap(dev, bmp, bpix, palette, fb,
303 x, y, width, height);
307 byte_width = width * (bpix / 8);
311 for (i = 0; i < height; ++i) {
313 for (j = 0; j < width; j++) {
314 write_pix8(fb, bpix, palette, bmap);
318 bmap += (padded_width - width);
319 fb -= byte_width + priv->line_length;
323 #if defined(CONFIG_BMP_16BPP)
325 for (i = 0; i < height; ++i) {
327 for (j = 0; j < width; j++) {
331 bmap += (padded_width - width);
332 fb -= width * 2 + priv->line_length;
335 #endif /* CONFIG_BMP_16BPP */
336 #if defined(CONFIG_BMP_24BPP)
338 for (i = 0; i < height; ++i) {
339 for (j = 0; j < width; j++) {
341 /* 16bit 555RGB format */
342 *(u16 *)fb = ((bmap[2] >> 3) << 10) |
343 ((bmap[1] >> 3) << 5) |
354 fb -= priv->line_length + width * (bpix / 8);
355 bmap += (padded_width - width);
358 #endif /* CONFIG_BMP_24BPP */
359 #if defined(CONFIG_BMP_32BPP)
361 for (i = 0; i < height; ++i) {
362 for (j = 0; j < width; j++) {
368 fb -= priv->line_length + width * (bpix / 8);
371 #endif /* CONFIG_BMP_32BPP */
376 /* Find the position of the top left of the image in the framebuffer */
377 fb = (uchar *)(priv->fb + y * priv->line_length + x * bpix / 8);
378 ret = video_sync_copy(dev, start, fb);
382 return video_sync(dev, false);