Deleted Added
full compact
splash_bmp.c (47945) splash_bmp.c (48104)
1/*-
2 * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@freebsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
1/*-
2 * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@freebsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $Id: splash_bmp.c,v 1.7 1999/03/29 15:13:53 yokota Exp $
27 * $Id: splash_bmp.c,v 1.8 1999/06/16 14:04:45 yokota Exp $
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/linker.h>
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/linker.h>
34#include <sys/fbio.h>
34
35
35#include <machine/console.h>
36
37#include <dev/fb/fbreg.h>
38#include <dev/fb/splashreg.h>
39
40#define FADE_TIMEOUT 15 /* sec */
41#define FADE_LEVELS 10
42
43static int splash_mode = -1;
44static int splash_on = FALSE;
45
46static int bmp_start(video_adapter_t *adp);
47static int bmp_end(video_adapter_t *adp);
48static int bmp_splash(video_adapter_t *adp, int on);
49static int bmp_Init(const char *data, int swidth, int sheight, int sdepth);
50static int bmp_Draw(video_adapter_t *adp);
51
52static splash_decoder_t bmp_decoder = {
53 "splash_bmp", bmp_start, bmp_end, bmp_splash, SPLASH_IMAGE,
54};
55
56SPLASH_DECODER(splash_bmp, bmp_decoder);
57
58static int
59bmp_start(video_adapter_t *adp)
60{
61 /* currently only 256-color modes are supported XXX */
62 static int modes[] = {
63 M_VESA_CG640x480,
64 M_VESA_CG800x600,
65 M_VESA_CG1024x768,
66 /*
67 * As 320x200 doesn't generally look great,
68 * it's least preferred here.
69 */
70 M_VGA_CG320,
71 -1,
72 };
73 video_info_t info;
74 int i;
75
76 if ((bmp_decoder.data == NULL) || (bmp_decoder.data_size <= 0)) {
77 printf("splash_bmp: No bitmap file found\n");
78 return ENODEV;
79 }
80 for (i = 0; modes[i] >= 0; ++i) {
81 if (((*vidsw[adp->va_index]->get_info)(adp, modes[i], &info) == 0)
82 && (bmp_Init((u_char *)bmp_decoder.data,
83 info.vi_width, info.vi_height, info.vi_depth) == 0))
84 break;
85 }
86 splash_mode = modes[i];
87 if (splash_mode < 0)
88 printf("splash_bmp: No appropriate video mode found\n");
89 if (bootverbose)
90 printf("bmp_start(): splash_mode:%d\n", splash_mode);
91 return ((splash_mode < 0) ? ENODEV : 0);
92}
93
94static int
95bmp_end(video_adapter_t *adp)
96{
97 /* nothing to do */
98 return 0;
99}
100
101static int
102bmp_splash(video_adapter_t *adp, int on)
103{
104 static u_char pal[256*3];
105 static long time_stamp;
106 u_char tpal[256*3];
107 static int fading = TRUE, brightness = FADE_LEVELS;
108 struct timeval tv;
109 int i;
110
111 if (on) {
112 if (!splash_on) {
113 /* set up the video mode and draw something */
114 if ((*vidsw[adp->va_index]->set_mode)(adp, splash_mode))
115 return 1;
116 if (bmp_Draw(adp))
117 return 1;
118 (*vidsw[adp->va_index]->save_palette)(adp, pal);
119 time_stamp = 0;
120 splash_on = TRUE;
121 }
122 /*
123 * This is a kludge to fade the image away. This section of the
124 * code takes effect only after the system is completely up.
125 * FADE_TIMEOUT should be configurable.
126 */
127 if (!cold) {
128 getmicrotime(&tv);
129 if (time_stamp == 0)
130 time_stamp = tv.tv_sec;
131 if (tv.tv_sec > time_stamp + FADE_TIMEOUT) {
132 if (fading)
133 if (brightness == 0) {
134 fading = FALSE;
135 brightness++;
136 }
137 else brightness--;
138 else
139 if (brightness == FADE_LEVELS) {
140 fading = TRUE;
141 brightness--;
142 }
143 else brightness++;
144 for (i = 0; i < sizeof(pal); ++i) {
145 tpal[i] = pal[i] * brightness / FADE_LEVELS;
146 }
147 (*vidsw[adp->va_index]->load_palette)(adp, tpal);
148 time_stamp = tv.tv_sec;
149 }
150 }
151 return 0;
152 } else {
153 /* the video mode will be restored by the caller */
154 splash_on = FALSE;
155 return 0;
156 }
157}
158
159/*
160** Code to handle Microsoft DIB (".BMP") format images.
161**
162** Blame me (msmith@freebsd.org) if this is broken, not Soren.
163*/
164
165typedef struct tagBITMAPFILEHEADER { /* bmfh */
166 u_short bfType __attribute__ ((packed));
167 int bfSize __attribute__ ((packed));
168 u_short bfReserved1 __attribute__ ((packed));
169 u_short bfReserved2 __attribute__ ((packed));
170 int bfOffBits __attribute__ ((packed));
171} BITMAPFILEHEADER;
172
173typedef struct tagBITMAPINFOHEADER { /* bmih */
174 int biSize __attribute__ ((packed));
175 int biWidth __attribute__ ((packed));
176 int biHeight __attribute__ ((packed));
177 short biPlanes __attribute__ ((packed));
178 short biBitCount __attribute__ ((packed));
179 int biCompression __attribute__ ((packed));
180 int biSizeImage __attribute__ ((packed));
181 int biXPelsPerMeter __attribute__ ((packed));
182 int biYPelsPerMeter __attribute__ ((packed));
183 int biClrUsed __attribute__ ((packed));
184 int biClrImportant __attribute__ ((packed));
185} BITMAPINFOHEADER;
186
187typedef struct tagRGBQUAD { /* rgbq */
188 u_char rgbBlue __attribute__ ((packed));
189 u_char rgbGreen __attribute__ ((packed));
190 u_char rgbRed __attribute__ ((packed));
191 u_char rgbReserved __attribute__ ((packed));
192} RGBQUAD;
193
194typedef struct tagBITMAPINFO { /* bmi */
195 BITMAPINFOHEADER bmiHeader __attribute__ ((packed));
196 RGBQUAD bmiColors[256] __attribute__ ((packed));
197} BITMAPINFO;
198
199typedef struct tagBITMAPF
200{
201 BITMAPFILEHEADER bmfh __attribute__ ((packed));
202 BITMAPINFO bmfi __attribute__ ((packed));
203} BITMAPF;
204
205#define BI_RGB 0
206#define BI_RLE8 1
207#define BI_RLE4 2
208
209/*
210** all we actually care about the image
211*/
212typedef struct
213{
214 int width,height; /* image dimensions */
215 int swidth,sheight; /* screen dimensions for the current mode */
216 u_char sdepth; /* screen depth (1, 4, 8 bpp) */
217 int ncols; /* number of colours */
218 u_char palette[256][3]; /* raw palette data */
219 u_char format; /* one of the BI_* constants above */
220 u_char *data; /* pointer to the raw data */
221 u_char *index; /* running pointer to the data while drawing */
222 u_char *vidmem; /* video memory allocated for drawing */
223 video_adapter_t *adp;
224 int bank;
225} BMP_INFO;
226
227static BMP_INFO bmp_info;
228
229static void
230fill(BMP_INFO *info, int x, int y, int xsize, int ysize)
231{
232 u_char *window;
233 int banksize;
234 int bank;
235 int p;
236
237 banksize = info->adp->va_window_size;
238 bank = (info->adp->va_line_width*y + x)/banksize;
239 window = (u_char *)info->adp->va_window;
240 (*vidsw[info->adp->va_index]->set_win_org)(info->adp, bank*banksize);
241 while (ysize > 0) {
242 p = (info->adp->va_line_width*y + x)%banksize;
243 for (; (p + xsize <= banksize) && ysize > 0; --ysize, ++y) {
244 generic_bzero(window + p, xsize);
245 p += info->adp->va_line_width;
246 }
247 if (ysize <= 0)
248 break;
249 if (p < banksize) {
250 /* the last line crosses the window boundary */
251 generic_bzero(window + p, banksize - p);
252 }
253 ++bank; /* next bank */
254 (*vidsw[info->adp->va_index]->set_win_org)(info->adp, bank*banksize);
255 if (p < banksize) {
256 /* the remaining part of the last line */
257 generic_bzero(window, p + xsize - banksize);
258 ++y;
259 --ysize;
260 }
261 }
262 info->bank = bank;
263}
264
265/*
266** bmp_SetPix
267**
268** Given (info), set the pixel at (x),(y) to (val)
269**
270*/
271static void
272bmp_SetPix(BMP_INFO *info, int x, int y, u_char val)
273{
274 int sofs, bofs;
275 u_char tpv, mask;
276 int newbank;
277
278 /*
279 * range check to avoid explosions
280 */
281 if ((x < 0) || (x >= info->swidth) || (y < 0) || (y >= info->sheight))
282 return;
283
284 /*
285 * calculate offset into video memory;
286 * because 0,0 is bottom-left for DIB, we have to convert.
287 */
288 sofs = ((info->height - (y+1) + (info->sheight - info->height) / 2)
289 * info->adp->va_line_width);
290
291 switch(info->sdepth) {
292 case 1:
293 sofs += ((x + (info->swidth - info->width) / 2) >> 3);
294 bofs = x & 0x7; /* offset within byte */
295
296 val &= 1; /* mask pixel value */
297 mask = ~(0x80 >> bofs); /* calculate bit mask */
298 tpv = *(info->vidmem+sofs) & mask; /* get screen contents, excluding masked bit */
299 *(info->vidmem+sofs) = tpv | (val << (8-bofs)); /* write new bit */
300 break;
301
302 /* XXX only correct for non-interleaved modes */
303 case 4:
304 sofs += ((x + (info->swidth - info->width) / 2) >> 1);
305 bofs = x & 0x1; /* offset within byte */
306
307 val &= 0xf; /* mask pixel value */
308 mask = bofs ? 0x0f : 0xf0; /* calculate bit mask */
309 tpv = *(info->vidmem+sofs) & mask; /* get screen contents, excluding masked bits */
310 *(info->vidmem+sofs) = tpv | (val << (bofs ? 0 : 4)); /* write new bits */
311 break;
312
313 case 8:
314 sofs += x + (info->swidth - info->width) / 2;
315 newbank = sofs/info->adp->va_window_size;
316 if (info->bank != newbank) {
317 (*vidsw[info->adp->va_index]->set_win_org)(info->adp, newbank*info->adp->va_window_size);
318 info->bank = newbank;
319 }
320 sofs %= info->adp->va_window_size;
321 *(info->vidmem+sofs) = val;
322 break;
323 }
324}
325
326/*
327** bmp_DecodeRLE4
328**
329** Given (data) pointing to a line of RLE4-format data and (line) being the starting
330** line onscreen, decode the line.
331*/
332static void
333bmp_DecodeRLE4(BMP_INFO *info, int line)
334{
335 int count; /* run count */
336 u_char val;
337 int x,y; /* screen position */
338
339 x = 0; /* starting position */
340 y = line;
341
342 /* loop reading data */
343 for (;;) {
344 /*
345 * encoded mode starts with a run length, and then a byte with
346 * two colour indexes to alternate between for the run
347 */
348 if (*info->index) {
349 for (count = 0; count < *info->index; count++, x++) {
350 if (count & 1) { /* odd count, low nybble */
351 bmp_SetPix(info, x, y, *(info->index+1) & 0x0f);
352 } else { /* even count, high nybble */
353 bmp_SetPix(info, x, y, (*(info->index+1) >>4) & 0x0f);
354 }
355 }
356 info->index += 2;
357 /*
358 * A leading zero is an escape; it may signal the end of the
359 * bitmap, a cursor move, or some absolute data.
360 */
361 } else { /* zero tag may be absolute mode or an escape */
362 switch (*(info->index+1)) {
363 case 0: /* end of line */
364 info->index += 2;
365 return;
366 case 1: /* end of bitmap */
367 info->index = NULL;
368 return;
369 case 2: /* move */
370 x += *(info->index + 2); /* new coords */
371 y += *(info->index + 3);
372 info->index += 4;
373 break;
374 default: /* literal bitmap data */
375 for (count = 0; count < *(info->index + 1); count++, x++) {
376 val = *(info->index + 2 + (count / 2)); /* byte with nybbles */
377 if (count & 1) {
378 val &= 0xf; /* get low nybble */
379 } else {
380 val = (val >> 4); /* get high nybble */
381 }
382 bmp_SetPix(info, x, y, val);
383 }
384 /* warning, this depends on integer truncation, do not hand-optimise! */
385 info->index += 2 + ((count + 3) / 4) * 2;
386 break;
387 }
388 }
389 }
390}
391
392/*
393** bmp_DecodeRLE8
394** Given (data) pointing to a line of RLE4-format data and (line) being the starting
395** line onscreen, decode the line.
396*/
397static void
398bmp_DecodeRLE8(BMP_INFO *info, int line)
399{
400 int count; /* run count */
401 int x,y; /* screen position */
402
403 x = 0; /* starting position */
404 y = line;
405
406 /* loop reading data */
407 for(;;) {
408 /*
409 * encoded mode starts with a run length, and then a byte with
410 * two colour indexes to alternate between for the run
411 */
412 if (*info->index) {
413 for (count = 0; count < *info->index; count++, x++)
414 bmp_SetPix(info, x, y, *(info->index+1));
415 info->index += 2;
416 /*
417 * A leading zero is an escape; it may signal the end of the
418 * bitmap, a cursor move, or some absolute data.
419 */
420 } else { /* zero tag may be absolute mode or an escape */
421 switch(*(info->index+1)) {
422 case 0: /* end of line */
423 info->index += 2;
424 return;
425 case 1: /* end of bitmap */
426 info->index = NULL;
427 return;
428 case 2: /* move */
429 x += *(info->index + 2); /* new coords */
430 y += *(info->index + 3);
431 info->index += 4;
432 break;
433 default: /* literal bitmap data */
434 for (count = 0; count < *(info->index + 1); count++, x++)
435 bmp_SetPix(info, x, y, *(info->index + 2 + count));
436 /* must be an even count */
437 info->index += 2 + count + (count & 1);
438 break;
439 }
440 }
441 }
442}
443
444/*
445** bmp_DecodeLine
446**
447** Given (info) pointing to an image being decoded, (line) being the line currently
448** being displayed, decode a line of data.
449*/
450static void
451bmp_DecodeLine(BMP_INFO *info, int line)
452{
453 int x;
454
455 switch(info->format) {
456 case BI_RGB:
457 for (x = 0; x < info->width; x++, info->index++)
458 bmp_SetPix(info, x, line, *info->index);
459 info->index += 3 - (--x % 4);
460 break;
461 case BI_RLE4:
462 bmp_DecodeRLE4(info, line);
463 break;
464 case BI_RLE8:
465 bmp_DecodeRLE8(info, line);
466 break;
467 }
468}
469
470/*
471** bmp_Init
472**
473** Given a pointer (data) to the image of a BMP file, fill in bmp_info with what
474** can be learnt from it. Return nonzero if the file isn't usable.
475**
476** Take screen dimensions (swidth), (sheight) and (sdepth) and make sure we
477** can work with these.
478*/
479static int
480bmp_Init(const char *data, int swidth, int sheight, int sdepth)
481{
482 BITMAPF *bmf = (BITMAPF *)data;
483 int pind;
484
485 bmp_info.data = NULL; /* assume setup failed */
486
487 /* check file ID */
488 if (bmf->bmfh.bfType != 0x4d42) {
489 printf("splash_bmp: not a BMP file\n");
490 return(1); /* XXX check word ordering for big-endian ports? */
491 }
492
493 /* do we understand this bitmap format? */
494 if (bmf->bmfi.bmiHeader.biSize > sizeof(bmf->bmfi.bmiHeader)) {
495 printf("splash_bmp: unsupported BMP format (size=%d)\n",
496 bmf->bmfi.bmiHeader.biSize);
497 return(1);
498 }
499
500 /* save what we know about the screen */
501 bmp_info.swidth = swidth;
502 bmp_info.sheight = sheight;
503 bmp_info.sdepth = sdepth;
504
505 /* where's the data? */
506 bmp_info.data = (u_char *)data + bmf->bmfh.bfOffBits;
507
508 /* image parameters */
509 bmp_info.width = bmf->bmfi.bmiHeader.biWidth;
510 bmp_info.height = bmf->bmfi.bmiHeader.biHeight;
511 bmp_info.format = bmf->bmfi.bmiHeader.biCompression;
512
513 switch(bmp_info.format) { /* check compression format */
514 case BI_RGB:
515 case BI_RLE4:
516 case BI_RLE8:
517 break;
518 default:
519 printf("splash_bmp: unsupported compression format\n");
520 return(1); /* unsupported compression format */
521 }
522
523 /* palette details */
524 bmp_info.ncols = (bmf->bmfi.bmiHeader.biClrUsed);
525 bzero(bmp_info.palette,sizeof(bmp_info.palette));
526 if (bmp_info.ncols == 0) { /* uses all of them */
527 bmp_info.ncols = 1 << bmf->bmfi.bmiHeader.biBitCount;
528 }
529 if ((bmf->bmfi.bmiHeader.biBitCount != sdepth)
530 || (bmp_info.ncols > (1 << sdepth))) {
531 printf("splash_bmp: unsupported color depth (%d bits, %d colors)\n",
532 bmf->bmfi.bmiHeader.biBitCount, bmp_info.ncols);
533 return(1);
534 }
535 if ((bmp_info.height > bmp_info.sheight) ||
536 (bmp_info.width > bmp_info.swidth) ||
537 (bmp_info.ncols > (1 << sdepth))) {
538 return(1); /* beyond screen capacity */
539 }
540
541 /* read palette */
542 for (pind = 0; pind < bmp_info.ncols; pind++) {
543 bmp_info.palette[pind][0] = bmf->bmfi.bmiColors[pind].rgbRed;
544 bmp_info.palette[pind][1] = bmf->bmfi.bmiColors[pind].rgbGreen;
545 bmp_info.palette[pind][2] = bmf->bmfi.bmiColors[pind].rgbBlue;
546 }
547 return(0);
548}
549
550/*
551** bmp_Draw
552**
553** Render the image. Return nonzero if that's not possible.
554**
555*/
556static int
557bmp_Draw(video_adapter_t *adp)
558{
559 int line;
560
561 if (bmp_info.data == NULL) { /* init failed, do nothing */
562 return(1);
563 }
564
565 /* clear the screen */
566 bmp_info.vidmem = (u_char *)adp->va_window;
567 bmp_info.adp = adp;
568 /* XXX; the following line is correct only for 8bpp modes */
569 fill(&bmp_info, 0, 0, bmp_info.swidth, bmp_info.sheight);
570 (*vidsw[adp->va_index]->set_win_org)(adp, 0);
571 bmp_info.bank = 0;
572
573 /* initialise the info structure for drawing */
574 bmp_info.index = bmp_info.data;
575
576 /* set the palette for our image */
577 (*vidsw[adp->va_index]->load_palette)(adp, (u_char *)&bmp_info.palette);
578
579 for (line = 0; (line < bmp_info.height) && bmp_info.index; line++) {
580 bmp_DecodeLine(&bmp_info, line);
581 }
582 return(0);
583}
36#include <dev/fb/fbreg.h>
37#include <dev/fb/splashreg.h>
38
39#define FADE_TIMEOUT 15 /* sec */
40#define FADE_LEVELS 10
41
42static int splash_mode = -1;
43static int splash_on = FALSE;
44
45static int bmp_start(video_adapter_t *adp);
46static int bmp_end(video_adapter_t *adp);
47static int bmp_splash(video_adapter_t *adp, int on);
48static int bmp_Init(const char *data, int swidth, int sheight, int sdepth);
49static int bmp_Draw(video_adapter_t *adp);
50
51static splash_decoder_t bmp_decoder = {
52 "splash_bmp", bmp_start, bmp_end, bmp_splash, SPLASH_IMAGE,
53};
54
55SPLASH_DECODER(splash_bmp, bmp_decoder);
56
57static int
58bmp_start(video_adapter_t *adp)
59{
60 /* currently only 256-color modes are supported XXX */
61 static int modes[] = {
62 M_VESA_CG640x480,
63 M_VESA_CG800x600,
64 M_VESA_CG1024x768,
65 /*
66 * As 320x200 doesn't generally look great,
67 * it's least preferred here.
68 */
69 M_VGA_CG320,
70 -1,
71 };
72 video_info_t info;
73 int i;
74
75 if ((bmp_decoder.data == NULL) || (bmp_decoder.data_size <= 0)) {
76 printf("splash_bmp: No bitmap file found\n");
77 return ENODEV;
78 }
79 for (i = 0; modes[i] >= 0; ++i) {
80 if (((*vidsw[adp->va_index]->get_info)(adp, modes[i], &info) == 0)
81 && (bmp_Init((u_char *)bmp_decoder.data,
82 info.vi_width, info.vi_height, info.vi_depth) == 0))
83 break;
84 }
85 splash_mode = modes[i];
86 if (splash_mode < 0)
87 printf("splash_bmp: No appropriate video mode found\n");
88 if (bootverbose)
89 printf("bmp_start(): splash_mode:%d\n", splash_mode);
90 return ((splash_mode < 0) ? ENODEV : 0);
91}
92
93static int
94bmp_end(video_adapter_t *adp)
95{
96 /* nothing to do */
97 return 0;
98}
99
100static int
101bmp_splash(video_adapter_t *adp, int on)
102{
103 static u_char pal[256*3];
104 static long time_stamp;
105 u_char tpal[256*3];
106 static int fading = TRUE, brightness = FADE_LEVELS;
107 struct timeval tv;
108 int i;
109
110 if (on) {
111 if (!splash_on) {
112 /* set up the video mode and draw something */
113 if ((*vidsw[adp->va_index]->set_mode)(adp, splash_mode))
114 return 1;
115 if (bmp_Draw(adp))
116 return 1;
117 (*vidsw[adp->va_index]->save_palette)(adp, pal);
118 time_stamp = 0;
119 splash_on = TRUE;
120 }
121 /*
122 * This is a kludge to fade the image away. This section of the
123 * code takes effect only after the system is completely up.
124 * FADE_TIMEOUT should be configurable.
125 */
126 if (!cold) {
127 getmicrotime(&tv);
128 if (time_stamp == 0)
129 time_stamp = tv.tv_sec;
130 if (tv.tv_sec > time_stamp + FADE_TIMEOUT) {
131 if (fading)
132 if (brightness == 0) {
133 fading = FALSE;
134 brightness++;
135 }
136 else brightness--;
137 else
138 if (brightness == FADE_LEVELS) {
139 fading = TRUE;
140 brightness--;
141 }
142 else brightness++;
143 for (i = 0; i < sizeof(pal); ++i) {
144 tpal[i] = pal[i] * brightness / FADE_LEVELS;
145 }
146 (*vidsw[adp->va_index]->load_palette)(adp, tpal);
147 time_stamp = tv.tv_sec;
148 }
149 }
150 return 0;
151 } else {
152 /* the video mode will be restored by the caller */
153 splash_on = FALSE;
154 return 0;
155 }
156}
157
158/*
159** Code to handle Microsoft DIB (".BMP") format images.
160**
161** Blame me (msmith@freebsd.org) if this is broken, not Soren.
162*/
163
164typedef struct tagBITMAPFILEHEADER { /* bmfh */
165 u_short bfType __attribute__ ((packed));
166 int bfSize __attribute__ ((packed));
167 u_short bfReserved1 __attribute__ ((packed));
168 u_short bfReserved2 __attribute__ ((packed));
169 int bfOffBits __attribute__ ((packed));
170} BITMAPFILEHEADER;
171
172typedef struct tagBITMAPINFOHEADER { /* bmih */
173 int biSize __attribute__ ((packed));
174 int biWidth __attribute__ ((packed));
175 int biHeight __attribute__ ((packed));
176 short biPlanes __attribute__ ((packed));
177 short biBitCount __attribute__ ((packed));
178 int biCompression __attribute__ ((packed));
179 int biSizeImage __attribute__ ((packed));
180 int biXPelsPerMeter __attribute__ ((packed));
181 int biYPelsPerMeter __attribute__ ((packed));
182 int biClrUsed __attribute__ ((packed));
183 int biClrImportant __attribute__ ((packed));
184} BITMAPINFOHEADER;
185
186typedef struct tagRGBQUAD { /* rgbq */
187 u_char rgbBlue __attribute__ ((packed));
188 u_char rgbGreen __attribute__ ((packed));
189 u_char rgbRed __attribute__ ((packed));
190 u_char rgbReserved __attribute__ ((packed));
191} RGBQUAD;
192
193typedef struct tagBITMAPINFO { /* bmi */
194 BITMAPINFOHEADER bmiHeader __attribute__ ((packed));
195 RGBQUAD bmiColors[256] __attribute__ ((packed));
196} BITMAPINFO;
197
198typedef struct tagBITMAPF
199{
200 BITMAPFILEHEADER bmfh __attribute__ ((packed));
201 BITMAPINFO bmfi __attribute__ ((packed));
202} BITMAPF;
203
204#define BI_RGB 0
205#define BI_RLE8 1
206#define BI_RLE4 2
207
208/*
209** all we actually care about the image
210*/
211typedef struct
212{
213 int width,height; /* image dimensions */
214 int swidth,sheight; /* screen dimensions for the current mode */
215 u_char sdepth; /* screen depth (1, 4, 8 bpp) */
216 int ncols; /* number of colours */
217 u_char palette[256][3]; /* raw palette data */
218 u_char format; /* one of the BI_* constants above */
219 u_char *data; /* pointer to the raw data */
220 u_char *index; /* running pointer to the data while drawing */
221 u_char *vidmem; /* video memory allocated for drawing */
222 video_adapter_t *adp;
223 int bank;
224} BMP_INFO;
225
226static BMP_INFO bmp_info;
227
228static void
229fill(BMP_INFO *info, int x, int y, int xsize, int ysize)
230{
231 u_char *window;
232 int banksize;
233 int bank;
234 int p;
235
236 banksize = info->adp->va_window_size;
237 bank = (info->adp->va_line_width*y + x)/banksize;
238 window = (u_char *)info->adp->va_window;
239 (*vidsw[info->adp->va_index]->set_win_org)(info->adp, bank*banksize);
240 while (ysize > 0) {
241 p = (info->adp->va_line_width*y + x)%banksize;
242 for (; (p + xsize <= banksize) && ysize > 0; --ysize, ++y) {
243 generic_bzero(window + p, xsize);
244 p += info->adp->va_line_width;
245 }
246 if (ysize <= 0)
247 break;
248 if (p < banksize) {
249 /* the last line crosses the window boundary */
250 generic_bzero(window + p, banksize - p);
251 }
252 ++bank; /* next bank */
253 (*vidsw[info->adp->va_index]->set_win_org)(info->adp, bank*banksize);
254 if (p < banksize) {
255 /* the remaining part of the last line */
256 generic_bzero(window, p + xsize - banksize);
257 ++y;
258 --ysize;
259 }
260 }
261 info->bank = bank;
262}
263
264/*
265** bmp_SetPix
266**
267** Given (info), set the pixel at (x),(y) to (val)
268**
269*/
270static void
271bmp_SetPix(BMP_INFO *info, int x, int y, u_char val)
272{
273 int sofs, bofs;
274 u_char tpv, mask;
275 int newbank;
276
277 /*
278 * range check to avoid explosions
279 */
280 if ((x < 0) || (x >= info->swidth) || (y < 0) || (y >= info->sheight))
281 return;
282
283 /*
284 * calculate offset into video memory;
285 * because 0,0 is bottom-left for DIB, we have to convert.
286 */
287 sofs = ((info->height - (y+1) + (info->sheight - info->height) / 2)
288 * info->adp->va_line_width);
289
290 switch(info->sdepth) {
291 case 1:
292 sofs += ((x + (info->swidth - info->width) / 2) >> 3);
293 bofs = x & 0x7; /* offset within byte */
294
295 val &= 1; /* mask pixel value */
296 mask = ~(0x80 >> bofs); /* calculate bit mask */
297 tpv = *(info->vidmem+sofs) & mask; /* get screen contents, excluding masked bit */
298 *(info->vidmem+sofs) = tpv | (val << (8-bofs)); /* write new bit */
299 break;
300
301 /* XXX only correct for non-interleaved modes */
302 case 4:
303 sofs += ((x + (info->swidth - info->width) / 2) >> 1);
304 bofs = x & 0x1; /* offset within byte */
305
306 val &= 0xf; /* mask pixel value */
307 mask = bofs ? 0x0f : 0xf0; /* calculate bit mask */
308 tpv = *(info->vidmem+sofs) & mask; /* get screen contents, excluding masked bits */
309 *(info->vidmem+sofs) = tpv | (val << (bofs ? 0 : 4)); /* write new bits */
310 break;
311
312 case 8:
313 sofs += x + (info->swidth - info->width) / 2;
314 newbank = sofs/info->adp->va_window_size;
315 if (info->bank != newbank) {
316 (*vidsw[info->adp->va_index]->set_win_org)(info->adp, newbank*info->adp->va_window_size);
317 info->bank = newbank;
318 }
319 sofs %= info->adp->va_window_size;
320 *(info->vidmem+sofs) = val;
321 break;
322 }
323}
324
325/*
326** bmp_DecodeRLE4
327**
328** Given (data) pointing to a line of RLE4-format data and (line) being the starting
329** line onscreen, decode the line.
330*/
331static void
332bmp_DecodeRLE4(BMP_INFO *info, int line)
333{
334 int count; /* run count */
335 u_char val;
336 int x,y; /* screen position */
337
338 x = 0; /* starting position */
339 y = line;
340
341 /* loop reading data */
342 for (;;) {
343 /*
344 * encoded mode starts with a run length, and then a byte with
345 * two colour indexes to alternate between for the run
346 */
347 if (*info->index) {
348 for (count = 0; count < *info->index; count++, x++) {
349 if (count & 1) { /* odd count, low nybble */
350 bmp_SetPix(info, x, y, *(info->index+1) & 0x0f);
351 } else { /* even count, high nybble */
352 bmp_SetPix(info, x, y, (*(info->index+1) >>4) & 0x0f);
353 }
354 }
355 info->index += 2;
356 /*
357 * A leading zero is an escape; it may signal the end of the
358 * bitmap, a cursor move, or some absolute data.
359 */
360 } else { /* zero tag may be absolute mode or an escape */
361 switch (*(info->index+1)) {
362 case 0: /* end of line */
363 info->index += 2;
364 return;
365 case 1: /* end of bitmap */
366 info->index = NULL;
367 return;
368 case 2: /* move */
369 x += *(info->index + 2); /* new coords */
370 y += *(info->index + 3);
371 info->index += 4;
372 break;
373 default: /* literal bitmap data */
374 for (count = 0; count < *(info->index + 1); count++, x++) {
375 val = *(info->index + 2 + (count / 2)); /* byte with nybbles */
376 if (count & 1) {
377 val &= 0xf; /* get low nybble */
378 } else {
379 val = (val >> 4); /* get high nybble */
380 }
381 bmp_SetPix(info, x, y, val);
382 }
383 /* warning, this depends on integer truncation, do not hand-optimise! */
384 info->index += 2 + ((count + 3) / 4) * 2;
385 break;
386 }
387 }
388 }
389}
390
391/*
392** bmp_DecodeRLE8
393** Given (data) pointing to a line of RLE4-format data and (line) being the starting
394** line onscreen, decode the line.
395*/
396static void
397bmp_DecodeRLE8(BMP_INFO *info, int line)
398{
399 int count; /* run count */
400 int x,y; /* screen position */
401
402 x = 0; /* starting position */
403 y = line;
404
405 /* loop reading data */
406 for(;;) {
407 /*
408 * encoded mode starts with a run length, and then a byte with
409 * two colour indexes to alternate between for the run
410 */
411 if (*info->index) {
412 for (count = 0; count < *info->index; count++, x++)
413 bmp_SetPix(info, x, y, *(info->index+1));
414 info->index += 2;
415 /*
416 * A leading zero is an escape; it may signal the end of the
417 * bitmap, a cursor move, or some absolute data.
418 */
419 } else { /* zero tag may be absolute mode or an escape */
420 switch(*(info->index+1)) {
421 case 0: /* end of line */
422 info->index += 2;
423 return;
424 case 1: /* end of bitmap */
425 info->index = NULL;
426 return;
427 case 2: /* move */
428 x += *(info->index + 2); /* new coords */
429 y += *(info->index + 3);
430 info->index += 4;
431 break;
432 default: /* literal bitmap data */
433 for (count = 0; count < *(info->index + 1); count++, x++)
434 bmp_SetPix(info, x, y, *(info->index + 2 + count));
435 /* must be an even count */
436 info->index += 2 + count + (count & 1);
437 break;
438 }
439 }
440 }
441}
442
443/*
444** bmp_DecodeLine
445**
446** Given (info) pointing to an image being decoded, (line) being the line currently
447** being displayed, decode a line of data.
448*/
449static void
450bmp_DecodeLine(BMP_INFO *info, int line)
451{
452 int x;
453
454 switch(info->format) {
455 case BI_RGB:
456 for (x = 0; x < info->width; x++, info->index++)
457 bmp_SetPix(info, x, line, *info->index);
458 info->index += 3 - (--x % 4);
459 break;
460 case BI_RLE4:
461 bmp_DecodeRLE4(info, line);
462 break;
463 case BI_RLE8:
464 bmp_DecodeRLE8(info, line);
465 break;
466 }
467}
468
469/*
470** bmp_Init
471**
472** Given a pointer (data) to the image of a BMP file, fill in bmp_info with what
473** can be learnt from it. Return nonzero if the file isn't usable.
474**
475** Take screen dimensions (swidth), (sheight) and (sdepth) and make sure we
476** can work with these.
477*/
478static int
479bmp_Init(const char *data, int swidth, int sheight, int sdepth)
480{
481 BITMAPF *bmf = (BITMAPF *)data;
482 int pind;
483
484 bmp_info.data = NULL; /* assume setup failed */
485
486 /* check file ID */
487 if (bmf->bmfh.bfType != 0x4d42) {
488 printf("splash_bmp: not a BMP file\n");
489 return(1); /* XXX check word ordering for big-endian ports? */
490 }
491
492 /* do we understand this bitmap format? */
493 if (bmf->bmfi.bmiHeader.biSize > sizeof(bmf->bmfi.bmiHeader)) {
494 printf("splash_bmp: unsupported BMP format (size=%d)\n",
495 bmf->bmfi.bmiHeader.biSize);
496 return(1);
497 }
498
499 /* save what we know about the screen */
500 bmp_info.swidth = swidth;
501 bmp_info.sheight = sheight;
502 bmp_info.sdepth = sdepth;
503
504 /* where's the data? */
505 bmp_info.data = (u_char *)data + bmf->bmfh.bfOffBits;
506
507 /* image parameters */
508 bmp_info.width = bmf->bmfi.bmiHeader.biWidth;
509 bmp_info.height = bmf->bmfi.bmiHeader.biHeight;
510 bmp_info.format = bmf->bmfi.bmiHeader.biCompression;
511
512 switch(bmp_info.format) { /* check compression format */
513 case BI_RGB:
514 case BI_RLE4:
515 case BI_RLE8:
516 break;
517 default:
518 printf("splash_bmp: unsupported compression format\n");
519 return(1); /* unsupported compression format */
520 }
521
522 /* palette details */
523 bmp_info.ncols = (bmf->bmfi.bmiHeader.biClrUsed);
524 bzero(bmp_info.palette,sizeof(bmp_info.palette));
525 if (bmp_info.ncols == 0) { /* uses all of them */
526 bmp_info.ncols = 1 << bmf->bmfi.bmiHeader.biBitCount;
527 }
528 if ((bmf->bmfi.bmiHeader.biBitCount != sdepth)
529 || (bmp_info.ncols > (1 << sdepth))) {
530 printf("splash_bmp: unsupported color depth (%d bits, %d colors)\n",
531 bmf->bmfi.bmiHeader.biBitCount, bmp_info.ncols);
532 return(1);
533 }
534 if ((bmp_info.height > bmp_info.sheight) ||
535 (bmp_info.width > bmp_info.swidth) ||
536 (bmp_info.ncols > (1 << sdepth))) {
537 return(1); /* beyond screen capacity */
538 }
539
540 /* read palette */
541 for (pind = 0; pind < bmp_info.ncols; pind++) {
542 bmp_info.palette[pind][0] = bmf->bmfi.bmiColors[pind].rgbRed;
543 bmp_info.palette[pind][1] = bmf->bmfi.bmiColors[pind].rgbGreen;
544 bmp_info.palette[pind][2] = bmf->bmfi.bmiColors[pind].rgbBlue;
545 }
546 return(0);
547}
548
549/*
550** bmp_Draw
551**
552** Render the image. Return nonzero if that's not possible.
553**
554*/
555static int
556bmp_Draw(video_adapter_t *adp)
557{
558 int line;
559
560 if (bmp_info.data == NULL) { /* init failed, do nothing */
561 return(1);
562 }
563
564 /* clear the screen */
565 bmp_info.vidmem = (u_char *)adp->va_window;
566 bmp_info.adp = adp;
567 /* XXX; the following line is correct only for 8bpp modes */
568 fill(&bmp_info, 0, 0, bmp_info.swidth, bmp_info.sheight);
569 (*vidsw[adp->va_index]->set_win_org)(adp, 0);
570 bmp_info.bank = 0;
571
572 /* initialise the info structure for drawing */
573 bmp_info.index = bmp_info.data;
574
575 /* set the palette for our image */
576 (*vidsw[adp->va_index]->load_palette)(adp, (u_char *)&bmp_info.palette);
577
578 for (line = 0; (line < bmp_info.height) && bmp_info.index; line++) {
579 bmp_DecodeLine(&bmp_info, line);
580 }
581 return(0);
582}