4 * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
5 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/sys/modules/splash/bmp/splash_bmp.c,v 1.10.2.3 2000/10/31 08:00:06 nyan Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/linker.h>
37 #include <sys/thread.h>
39 #include <dev/video/fb/fbreg.h>
40 #include <dev/video/fb/splashreg.h>
41 #include <dev/video/fb/vgareg.h>
43 #include <bus/isa/isareg.h>
45 #define FADE_TIMEOUT 15 /* sec */
46 #define FADE_LEVELS 10
48 static int splash_mode = -1;
49 static int splash_on = FALSE;
51 static int bmp_start(video_adapter_t *);
52 static int bmp_end(video_adapter_t *);
53 static int bmp_splash(video_adapter_t *, int);
54 static int bmp_Init(const char *, int, int, int);
55 static int bmp_Draw(video_adapter_t *);
57 static splash_decoder_t bmp_decoder = {
58 "splash_bmp", bmp_start, bmp_end, bmp_splash, SPLASH_IMAGE,
61 SPLASH_DECODER(splash_bmp, bmp_decoder);
64 bmp_start(video_adapter_t *adp)
66 /* currently only 256-color modes are supported XXX */
67 static int modes[] = {
70 * As 320x200 doesn't generally look great,
71 * it's least preferred here.
79 lwkt_gettoken(&vga_token);
80 if ((bmp_decoder.data == NULL) || (bmp_decoder.data_size <= 0)) {
81 kprintf("splash_bmp: No bitmap file found\n");
82 lwkt_reltoken(&vga_token);
85 for (i = 0; modes[i] >= 0; ++i) {
86 if (((*vidsw[adp->va_index]->get_info)(adp, modes[i], &info) == 0)
87 && (bmp_Init((u_char *)bmp_decoder.data,
88 info.vi_width, info.vi_height, info.vi_depth) == 0))
91 splash_mode = modes[i];
93 kprintf("splash_bmp: No appropriate video mode found\n");
95 kprintf("bmp_start(): splash_mode:%d\n", splash_mode);
96 lwkt_reltoken(&vga_token);
97 return ((splash_mode < 0) ? ENODEV : 0);
101 bmp_end(video_adapter_t *adp)
108 bmp_splash(video_adapter_t *adp, int on)
110 static u_char pal[256*3];
111 static long time_stamp;
113 static int fading = TRUE, brightness = FADE_LEVELS;
117 lwkt_gettoken(&vga_token);
120 /* set up the video mode and draw something */
121 if ((*vidsw[adp->va_index]->set_mode)(adp, splash_mode)) {
122 lwkt_reltoken(&vga_token);
126 lwkt_reltoken(&vga_token);
129 (*vidsw[adp->va_index]->save_palette)(adp, pal);
134 * This is a kludge to fade the image away. This section of the
135 * code takes effect only after the system is completely up.
136 * FADE_TIMEOUT should be configurable.
141 time_stamp = tv.tv_sec;
142 if (tv.tv_sec > time_stamp + FADE_TIMEOUT) {
144 if (brightness == 0) {
150 if (brightness == FADE_LEVELS) {
155 for (i = 0; i < sizeof(pal); ++i) {
156 tpal[i] = pal[i] * brightness / FADE_LEVELS;
158 (*vidsw[adp->va_index]->load_palette)(adp, tpal);
159 time_stamp = tv.tv_sec;
162 lwkt_reltoken(&vga_token);
165 /* the video mode will be restored by the caller */
167 lwkt_reltoken(&vga_token);
173 ** Code to handle Microsoft DIB (".BMP") format images.
175 ** Blame me (msmith@freebsd.org) if this is broken, not Soren.
178 typedef struct tagBITMAPFILEHEADER { /* bmfh */
184 } __packed BITMAPFILEHEADER;
186 typedef struct tagBITMAPINFOHEADER { /* bmih */
198 } __packed BITMAPINFOHEADER;
200 typedef struct tagRGBQUAD { /* rgbq */
207 typedef struct tagBITMAPINFO { /* bmi */
208 BITMAPINFOHEADER bmiHeader;
209 RGBQUAD bmiColors[256];
210 } __packed BITMAPINFO;
212 typedef struct tagBITMAPF
214 BITMAPFILEHEADER bmfh;
223 ** all we actually care about the image
227 int width,height; /* image dimensions */
228 int swidth,sheight; /* screen dimensions for the current mode */
229 u_char depth; /* image depth (1, 4, 8, 24 bits) */
230 u_char sdepth; /* screen depth (1, 4, 8 bpp) */
231 int ncols; /* number of colours */
232 u_char palette[256][3]; /* raw palette data */
233 u_char format; /* one of the BI_* constants above */
234 const u_char *data; /* pointer to the raw data */
235 const u_char *index; /* running pointer to the data while drawing */
236 u_char *vidmem; /* video memory allocated for drawing */
237 video_adapter_t *adp;
241 static BMP_INFO bmp_info;
246 ** Given (info), set the pixel at (x),(y) to (val)
250 bmp_SetPix(BMP_INFO *info, int x, int y, u_char val)
256 * range check to avoid explosions
258 if ((x < 0) || (x >= info->swidth) || (y < 0) || (y >= info->sheight))
261 lwkt_gettoken(&vga_token);
263 * calculate offset into video memory;
264 * because 0,0 is bottom-left for DIB, we have to convert.
266 sofs = ((info->height - (y+1) + (info->sheight - info->height) / 2)
267 * info->adp->va_line_width);
268 x += (info->swidth - info->width) / 2;
270 switch(info->sdepth) {
273 /* EGA/VGA planar modes */
275 newbank = sofs/info->adp->va_window_size;
276 if (info->bank != newbank) {
277 (*vidsw[info->adp->va_index]->set_win_org)(info->adp, newbank*info->adp->va_window_size);
278 info->bank = newbank;
280 sofs %= info->adp->va_window_size;
281 bofs = x & 0x7; /* offset within byte */
282 outw(GDCIDX, (0x8000 >> bofs) | 0x08); /* bit mask */
283 outw(GDCIDX, (val << 8) | 0x00); /* set/reset */
284 *(info->vidmem + sofs) ^= 0xff; /* read-modify-write */
289 newbank = sofs/info->adp->va_window_size;
290 if (info->bank != newbank) {
291 (*vidsw[info->adp->va_index]->set_win_org)(info->adp, newbank*info->adp->va_window_size);
292 info->bank = newbank;
294 sofs %= info->adp->va_window_size;
295 *(info->vidmem+sofs) = val;
298 lwkt_reltoken(&vga_token);
304 ** Given (data) pointing to a line of RLE4-format data and (line) being the starting
305 ** line onscreen, decode the line.
308 bmp_DecodeRLE4(BMP_INFO *info, int line)
310 int count; /* run count */
312 int x,y; /* screen position */
314 x = 0; /* starting position */
317 /* loop reading data */
320 * encoded mode starts with a run length, and then a byte with
321 * two colour indexes to alternate between for the run
324 for (count = 0; count < *info->index; count++, x++) {
325 if (count & 1) { /* odd count, low nybble */
326 bmp_SetPix(info, x, y, *(info->index+1) & 0x0f);
327 } else { /* even count, high nybble */
328 bmp_SetPix(info, x, y, (*(info->index+1) >>4) & 0x0f);
333 * A leading zero is an escape; it may signal the end of the
334 * bitmap, a cursor move, or some absolute data.
336 } else { /* zero tag may be absolute mode or an escape */
337 switch (*(info->index+1)) {
338 case 0: /* end of line */
341 case 1: /* end of bitmap */
345 x += *(info->index + 2); /* new coords */
346 y += *(info->index + 3);
349 default: /* literal bitmap data */
350 for (count = 0; count < *(info->index + 1); count++, x++) {
351 val = *(info->index + 2 + (count / 2)); /* byte with nybbles */
353 val &= 0xf; /* get low nybble */
355 val = (val >> 4); /* get high nybble */
357 bmp_SetPix(info, x, y, val);
359 /* warning, this depends on integer truncation, do not hand-optimise! */
360 info->index += 2 + ((count + 3) / 4) * 2;
369 ** Given (data) pointing to a line of RLE8-format data and (line) being the starting
370 ** line onscreen, decode the line.
373 bmp_DecodeRLE8(BMP_INFO *info, int line)
375 int count; /* run count */
376 int x,y; /* screen position */
378 x = 0; /* starting position */
381 /* loop reading data */
384 * encoded mode starts with a run length, and then a byte with
385 * two colour indexes to alternate between for the run
388 for (count = 0; count < *info->index; count++, x++)
389 bmp_SetPix(info, x, y, *(info->index+1));
392 * A leading zero is an escape; it may signal the end of the
393 * bitmap, a cursor move, or some absolute data.
395 } else { /* zero tag may be absolute mode or an escape */
396 switch(*(info->index+1)) {
397 case 0: /* end of line */
400 case 1: /* end of bitmap */
404 x += *(info->index + 2); /* new coords */
405 y += *(info->index + 3);
408 default: /* literal bitmap data */
409 for (count = 0; count < *(info->index + 1); count++, x++)
410 bmp_SetPix(info, x, y, *(info->index + 2 + count));
411 /* must be an even count */
412 info->index += 2 + count + (count & 1);
422 ** Given (info) pointing to an image being decoded, (line) being the line currently
423 ** being displayed, decode a line of data.
426 bmp_DecodeLine(BMP_INFO *info, int line)
432 switch(info->format) {
434 switch(info->depth) {
436 for (x = 0; x < info->width; x++, info->index++)
437 bmp_SetPix(info, x, line, *info->index);
438 info->index += 3 - (--x % 4);
442 for (x = 0; x < info->width; x++) {
444 val = *p & 0xf; /* get low nybble */
447 val = *p >> 4; /* get high nybble */
449 bmp_SetPix(info, x, line, val);
451 /* warning, this depends on integer truncation, do not hand-optimise! */
452 info->index += ((x + 7) / 8) * 4;
457 for (x = 0; x < info->width; x++) {
458 val = (*p & mask) ? 1 : 0;
464 bmp_SetPix(info, x, line, val);
466 /* warning, this depends on integer truncation, do not hand-optimise! */
467 info->index += ((x + 31) / 32) * 4;
472 bmp_DecodeRLE4(info, line);
475 bmp_DecodeRLE8(info, line);
483 ** Given a pointer (data) to the image of a BMP file, fill in bmp_info with what
484 ** can be learnt from it. Return nonzero if the file isn't usable.
486 ** Take screen dimensions (swidth), (sheight) and (sdepth) and make sure we
487 ** can work with these.
490 bmp_Init(const char *data, int swidth, int sheight, int sdepth)
492 const BITMAPF *bmf = (const BITMAPF *)data;
495 bmp_info.data = NULL; /* assume setup failed */
498 if (bmf->bmfh.bfType != 0x4d42) {
499 kprintf("splash_bmp: not a BMP file\n");
500 return(1); /* XXX check word ordering for big-endian ports? */
503 /* do we understand this bitmap format? */
504 if (bmf->bmfi.bmiHeader.biSize > sizeof(bmf->bmfi.bmiHeader)) {
505 kprintf("splash_bmp: unsupported BMP format (size=%d)\n",
506 bmf->bmfi.bmiHeader.biSize);
510 /* save what we know about the screen */
511 bmp_info.swidth = swidth;
512 bmp_info.sheight = sheight;
513 bmp_info.sdepth = sdepth;
515 /* where's the data? */
516 bmp_info.data = (const u_char *)data + bmf->bmfh.bfOffBits;
518 /* image parameters */
519 bmp_info.width = bmf->bmfi.bmiHeader.biWidth;
520 bmp_info.height = bmf->bmfi.bmiHeader.biHeight;
521 bmp_info.depth = bmf->bmfi.bmiHeader.biBitCount;
522 bmp_info.format = bmf->bmfi.bmiHeader.biCompression;
524 switch(bmp_info.format) { /* check compression format */
530 kprintf("splash_bmp: unsupported compression format\n");
531 return(1); /* unsupported compression format */
534 /* palette details */
535 bmp_info.ncols = (bmf->bmfi.bmiHeader.biClrUsed);
536 bzero(bmp_info.palette,sizeof(bmp_info.palette));
537 if (bmp_info.ncols == 0) { /* uses all of them */
538 bmp_info.ncols = 1 << bmf->bmfi.bmiHeader.biBitCount;
540 if ((bmp_info.height > bmp_info.sheight) ||
541 (bmp_info.width > bmp_info.swidth) ||
542 (bmp_info.ncols > (1 << sdepth))) {
544 kprintf("splash_bmp: beyond screen capacity (%dx%d, %d colors)\n",
545 bmp_info.width, bmp_info.height, bmp_info.ncols);
550 for (pind = 0; pind < bmp_info.ncols; pind++) {
551 bmp_info.palette[pind][0] = bmf->bmfi.bmiColors[pind].rgbRed;
552 bmp_info.palette[pind][1] = bmf->bmfi.bmiColors[pind].rgbGreen;
553 bmp_info.palette[pind][2] = bmf->bmfi.bmiColors[pind].rgbBlue;
561 ** Render the image. Return nonzero if that's not possible.
565 bmp_Draw(video_adapter_t *adp)
572 if (bmp_info.data == NULL) { /* init failed, do nothing */
576 lwkt_gettoken(&vga_token);
577 /* clear the screen */
578 bmp_info.vidmem = (u_char *)adp->va_window;
580 (*vidsw[adp->va_index]->clear)(adp);
581 (*vidsw[adp->va_index]->set_win_org)(adp, 0);
584 /* initialise the info structure for drawing */
585 bmp_info.index = bmp_info.data;
587 /* set the palette for our image */
588 (*vidsw[adp->va_index]->load_palette)(adp, (u_char *)&bmp_info.palette);
591 /* XXX: this is ugly, but necessary for EGA/VGA 1bpp/4bpp modes */
592 if ((adp->va_type == KD_EGA) || (adp->va_type == KD_VGA)) {
593 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
596 for (i = 0; i < 16; ++i) {
600 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
601 outb(ATC, 0x20); /* enable palette */
603 outw(GDCIDX, 0x0f01); /* set/reset enable */
605 if (bmp_info.sdepth == 1)
606 outw(TSIDX, 0x0102); /* unmask plane #0 */
610 for (line = 0; (line < bmp_info.height) && bmp_info.index; line++) {
611 bmp_DecodeLine(&bmp_info, line);
613 lwkt_reltoken(&vga_token);