]> Dogcows Code - chaz/openbox/blob - render/image.c
bdc51eeda04562f0856c58723255dea9902566a6
[chaz/openbox] / render / image.c
1 #include <glib.h>
2 #include "../kernel/geom.h"
3 #include "image.h"
4
5 void image_draw(pixel32 *target, TextureRGBA *rgba, Rect *position,
6 Rect *surarea)
7 {
8 unsigned long *draw = rgba->data;
9 int c, sfw, sfh;
10 unsigned int i, e;
11 sfw = position->width;
12 sfh = position->height;
13
14 /* it would be nice if this worked, but this function is well broken in these
15 cercumstances. */
16 g_assert(position->width == surarea->width &&
17 position->height == surarea->height);
18
19 g_assert(rgba->data != NULL);
20
21 if ((rgba->width != sfw || rgba->height != sfh) &&
22 (rgba->width != rgba->cwidth || rgba->height != rgba->cheight)) {
23 double dx = rgba->width / (double)sfw;
24 double dy = rgba->height / (double)sfh;
25 double px = 0.0;
26 double py = 0.0;
27 int iy = 0;
28
29 /* scale it and cache it */
30 if (rgba->cache != NULL)
31 g_free(rgba->cache);
32 rgba->cache = g_new(unsigned long, sfw * sfh);
33 rgba->cwidth = sfw;
34 rgba->cheight = sfh;
35 for (i = 0, c = 0, e = sfw*sfh; i < e; ++i) {
36 rgba->cache[i] = rgba->data[(int)px + iy];
37 if (++c >= sfw) {
38 c = 0;
39 px = 0;
40 py += dy;
41 iy = (int)py * rgba->width;
42 } else
43 px += dx;
44 }
45
46 /* do we use the cache we may have just created, or the original? */
47 if (rgba->width != sfw || rgba->height != sfh)
48 draw = rgba->cache;
49
50 /* apply the alpha channel */
51 for (i = 0, c = 0, e = sfw*sfh; i < e; ++i) {
52 unsigned char alpha = draw[i] >> 24;
53 unsigned char r = draw[i] >> 16;
54 unsigned char g = draw[i] >> 8;
55 unsigned char b = draw[i];
56
57 /* background color */
58 unsigned char bgr = target[i] >> default_red_shift;
59 unsigned char bgg = target[i] >> default_green_shift;
60 unsigned char bgb = target[i] >> default_blue_shift;
61
62 r = bgr + (((r - bgr) * alpha) >> 8);
63 g = bgg + (((g - bgg) * alpha) >> 8);
64 b = bgb + (((b - bgb) * alpha) >> 8);
65
66 target[i] = (r << default_red_shift) | (g << default_green_shift) |
67 (b << default_blue_shift);
68 }
69 }
70 }
This page took 0.035964 seconds and 3 git commands to generate.