Fix typo in the Gentoo initscript.
[fbsplash.git] / core / src / effects.c
bloba9fcef716fd3afb5f7ccd428718f4f79c0ccf401
1 /*
2 * effects.c - miscellaneous graphical effects for splashutils
4 * Copyright (C) 2004-2005, Michal Januszewski <spock@gentoo.org>
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License v2. See the file COPYING in the main directory of this archive for
8 * more details.
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <sys/ioctl.h>
17 #include "common.h"
18 #include "render.h"
20 #define FADEIN_STEPS 64
21 #define FADEIN_STEPS_DC 256
24 * Copy the data from the background buffer to the framebuffer.
25 * The bg buffer dimensions need not match those of the current
26 * video mode.
28 void put_img(stheme_t *theme, u8 *dst, u8 *src)
30 int y, i;
31 u8 *to = dst;
33 to += theme->xmarg * fbd.bytespp + theme->ymarg * fbd.fix.line_length;
34 i = theme->xres * fbd.bytespp;
36 for (y = 0; y < theme->yres; y++) {
37 memcpy(to, src + i*y, i);
38 to += fbd.fix.line_length;
42 void paint_rect(stheme_t *theme, u8 *dst, u8 *src, int x1, int y1, int x2, int y2)
44 u8 *to;
45 int y, j;
47 j = (x2 - x1 + 1) * fbd.bytespp;
48 for (y = y1; y <= y2; y++) {
49 to = dst + (y + theme->ymarg) * fbd.fix.line_length + (x1 + theme->xmarg) * fbd.bytespp;
50 memcpy(to, src + (y * theme->xres + x1) * fbd.bytespp, j);
54 void paint_img(stheme_t *theme, u8 *dst, u8 *src)
56 item *i, *j;
58 for (i = theme->blit.head; i != NULL;) {
59 rect *re = i->p;
60 paint_rect(theme, dst, src, re->x1, re->y1, re->x2, re->y2);
62 j = i->next;
63 free(i);
64 free(re);
65 i = j;
68 list_init(theme->blit);
72 * @type = 0 (fadein) or 1 (fadeout)
74 void fade_directcolor(stheme_t *theme, u8 *dst, u8 *image, int fd, char type)
76 int len, i, step;
77 struct fb_cmap cmap;
79 len = min(min(fbd.var.red.length, fbd.var.green.length), fbd.var.blue.length);
81 cmap.start = 0;
82 cmap.len = (1 << len);
83 cmap.transp = NULL;
84 cmap.red = malloc(2 * 256 * 3);
85 if (!cmap.red)
86 return;
88 cmap.green = cmap.red + 256;
89 cmap.blue = cmap.green + 256;
91 for (i = 0; i < cmap.len; i++) {
92 cmap.red[i] = cmap.green[i] = cmap.blue[i] = 0;
95 ioctl(fd, FBIOPUTCMAP, &cmap);
96 put_img(theme, dst, image);
98 for (step = 1; step < FADEIN_STEPS_DC+1; step++) {
99 for (i = 0; i < cmap.len; i++) {
100 if (type)
101 cmap.red[i] = cmap.green[i] = cmap.blue[i] = (0xffff * i * (FADEIN_STEPS_DC+1-step))/
102 ((cmap.len-1)*FADEIN_STEPS_DC);
103 else
104 cmap.red[i] = cmap.green[i] = cmap.blue[i] = (0xffff * i * step)/((cmap.len-1)*FADEIN_STEPS_DC);
106 ioctl(fd, FBIOPUTCMAP, &cmap);
107 usleep(7500);
112 * @type = 0 (fadein) or 1 (fadeout)
114 void fade_truecolor(stheme_t *theme, u8 *dst, u8 *image, char type)
116 int rlen, glen, blen;
117 int i, step, h, x, y;
118 u8 *t, *p, *pic;
119 int r, g, b, rt, gt, bt;
120 int rl8, gl8, bl8;
121 int clut[256][FADEIN_STEPS];
123 rlen = fbd.var.red.length;
124 glen = fbd.var.green.length;
125 blen = fbd.var.blue.length;
127 rl8 = 8 - rlen;
128 gl8 = 8 - glen;
129 bl8 = 8 - blen;
131 t = malloc(theme->xres * theme->yres * 3);
132 if (!t) {
133 put_img(theme, dst, image);
134 return;
137 pic = image;
139 /* Decode the image into a table where each color component
140 * takes exatly one byte */
141 for (i = 0; i < theme->xres * theme->yres; i++) {
143 if (fbd.bytespp == 2) {
144 h = *(u16*)pic;
145 } else if (fbd.bytespp == 3) {
146 h = *(u32*)pic & 0xffffff;
147 } else {
148 h = *(u32*)pic;
151 pic += fbd.bytespp;
153 r = ((h >> fbd.var.red.offset & ((1 << rlen)-1)) << rl8);
154 g = ((h >> fbd.var.green.offset & ((1 << glen)-1)) << gl8);
155 b = ((h >> fbd.var.blue.offset & ((1 << blen)-1)) << bl8);
157 t[i*3] = r;
158 t[i*3+1] = g;
159 t[i*3+2] = b;
162 /* Compute the color look-up table */
163 for (step = 0; step < FADEIN_STEPS; step++) {
164 for (i = 0; i < 256; i++) {
165 if (type)
166 clut[i][step] = (FADEIN_STEPS-1-step) * i / FADEIN_STEPS;
167 else
168 clut[i][step] = (step+1) * i / FADEIN_STEPS;
172 if (type == 0)
173 memset(dst, 0, fbd.var.yres * fbd.fix.line_length);
175 for (step = 0; step < FADEIN_STEPS; step++) {
177 pic = dst + fbd.fix.line_length * theme->ymarg + theme->xmarg * fbd.bytespp;
178 p = t;
180 for (y = 0; y < theme->yres; y++) {
182 for (x = 0; x < theme->xres; x++) {
184 r = *p; p++;
185 g = *p; p++;
186 b = *p; p++;
188 rt = clut[r][step];
189 gt = clut[g][step];
190 bt = clut[b][step];
192 if (fbd.bytespp == 2) {
193 rt >>= rl8;
194 gt >>= gl8;
195 bt >>= bl8;
198 h = (rt << fbd.var.red.offset) |
199 (gt << fbd.var.green.offset) |
200 (bt << fbd.var.blue.offset);
202 if (fbd.bytespp == 2) {
203 *(u16*)pic = h;
204 pic += 2;
205 } else if (fbd.bytespp == 3) {
206 if (endianess == little) {
207 *(u16*)pic = h & 0xffff;
208 pic[2] = (h >> 16) & 0xff;
209 } else {
210 *(u16*)pic = (h >> 8) & 0xffff;
211 pic[2] = h & 0xff;
213 pic += 3;
214 } else if (fbd.bytespp == 4) {
215 *(u32*)pic = h;
216 pic += 4;
220 pic += fbd.fix.line_length - theme->xres * fbd.bytespp;
224 free(t);
227 void fade(stheme_t *theme, u8 *dst, u8 *image, struct fb_cmap cmap, u8 bgnd, int fd, char type)
229 if (bgnd) {
230 if (fork())
231 return;
234 /* FIXME: We need to handle 8bpp modes */
235 if (cmap.red) {
236 put_img(theme, dst, image);
237 return;
240 if (fbd.fix.visual == FB_VISUAL_DIRECTCOLOR) {
241 fade_directcolor(theme, dst, image, fd, type);
242 } else {
243 fade_truecolor(theme, dst, image, type);
246 if (bgnd) {
247 exit(0);
250 return;