no tabs
[mikachu/openbox.git] / render / image.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    image.c for the Openbox window manager
4    Copyright (c) 2003        Ben Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "geom.h"
20 #include "image.h"
21 #include "color.h"
22
23 #include <glib.h>
24
25 #define AVERAGE(a, b)   ( ((((a) ^ (b)) & 0xfefefefeL) >> 1) + ((a) & (b)) )
26
27 static void scale_line(RrPixel32 *dest, RrPixel32 *source, gint w, gint dw)
28 {
29     gint num_pixels = dw;
30     gint int_part = w / dw;
31     gint fract_part = w % dw;
32     gint err = 0;
33
34     while (num_pixels-- > 0) {
35         *dest++ = *source;
36         source += int_part;
37         err += fract_part;
38         if (err >= dw) {
39             err -= dw;
40             source++;
41         }
42     }
43 }
44
45 static RrPixel32* scale_half(RrPixel32 *source, gint w, gint h)
46 {
47     RrPixel32 *out, *dest, *sourceline, *sourceline2;
48     gint dw, dh, x, y;
49
50     sourceline = source;
51     sourceline2 = source + w;
52
53     dw = w >> 1;
54     dh = h >> 1;
55
56     out = dest = g_new(RrPixel32, dw * dh);
57
58     for (y = 0; y < dh; ++y) {
59         RrPixel32 *s, *s2;
60
61         s = sourceline;
62         s2 = sourceline2;
63
64         for (x = 0; x < dw; ++x) {
65             *dest++ = AVERAGE(AVERAGE(*s, *(s+1)),
66                               AVERAGE(*s2, *(s2+1)));
67             s += 2;
68             s2 += 2;
69         }
70         sourceline += w << 1;
71         sourceline2 += w << 1;
72     }
73     return out;
74 }
75
76 static RrPixel32* scale_rect(RrPixel32 *fullsource,
77                              gint w, gint h, gint dw, gint dh)
78 {
79     RrPixel32 *out, *dest;
80     RrPixel32 *source = fullsource;
81     RrPixel32 *oldsource = NULL;
82     RrPixel32 *prev_source = NULL;
83     gint num_pixels;
84     gint int_part;
85     gint fract_part;
86     gint err = 0;
87
88     while (dw <= (w >> 1) && dh <= (h >> 1)) {
89         source = scale_half(source, w, h);
90         w >>= 1; h >>= 1;
91         g_free(oldsource);
92         oldsource = source;
93     }
94
95     num_pixels = dh;
96     int_part = (h / dh) * w;
97     fract_part = h % dh;
98
99     out = dest = g_new(RrPixel32, dw * dh);
100
101     while (num_pixels-- > 0) {
102         if (source == prev_source) {
103             memcpy(dest, dest - dw, dw * sizeof(RrPixel32));
104         } else {
105             scale_line(dest, source, w, dw);
106             prev_source = source;
107         }
108         dest += dw;
109         source += int_part;
110         err += fract_part;
111         if (err >= dh) {
112             err -= dh;
113             source += w;
114         }
115     }
116
117     g_free(oldsource);
118
119     return out;
120 }
121
122 void RrImageDraw(RrPixel32 *target, RrTextureRGBA *rgba,
123                  gint target_w, gint target_h,
124                  RrRect *area)
125 {
126     RrPixel32 *dest;
127     RrPixel32 *source;
128     gint sw, sh, dw, dh;
129     gint col, num_pixels;
130
131     sw = rgba->width;
132     sh = rgba->height;
133
134     /* keep the ratio */
135     dw = area->width;
136     dh = (int)(dw * ((double)sh / sw));
137     if (dh > area->height) {
138         dh = area->height;
139         dw = (int)(dh * ((double)sw / sh));
140     }
141
142     if (sw != dw || sh != dh) {
143         /*if (!(rgba->cache && dw == rgba->cwidth && dh == rgba->cheight))*/ {
144             g_free(rgba->cache);
145             rgba->cache = scale_rect(rgba->data, sw, sh, dw, dh);
146             rgba->cwidth = dw;
147             rgba->cheight = dh;
148         }
149         source = rgba->cache;
150     } else {
151         source = rgba->data;
152     }
153
154     /* copy source -> dest, and apply the alpha channel */
155     col = 0;
156     num_pixels = dw * dh;
157     dest = target + area->x + target_w * area->y;
158     while (num_pixels-- > 0) {
159         guchar alpha, r, g, b, bgr, bgg, bgb;
160
161         alpha = *source >> RrDefaultAlphaOffset;
162         r = *source >> RrDefaultRedOffset;
163         g = *source >> RrDefaultGreenOffset;
164         b = *source >> RrDefaultBlueOffset;
165         
166         /* background color */
167         bgr = *dest >> RrDefaultRedOffset;
168         bgg = *dest >> RrDefaultGreenOffset;
169         bgb = *dest >> RrDefaultBlueOffset;
170
171         r = bgr + (((r - bgr) * alpha) >> 8);
172         g = bgg + (((g - bgg) * alpha) >> 8);
173         b = bgb + (((b - bgb) * alpha) >> 8);
174
175         *dest = ((r << RrDefaultRedOffset) |
176                  (g << RrDefaultGreenOffset) |
177                  (b << RrDefaultBlueOffset));
178
179         dest++;
180         source++;
181
182         if (col++ >= dw) {
183             col = 0;
184             dest += target_w - dw;
185         }
186     }
187 }