]> Dogcows Code - chaz/openbox/blob - render/image.c
finally fix broken gimp window icons
[chaz/openbox] / 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 = (gint)(dw * ((gdouble)sh / sw));
137 if (dh > area->height) {
138 dh = area->height;
139 dw = (gint)(dh * ((gdouble)sw / sh));
140 }
141
142 if (!(dw && dh))
143 return; /* XXX sanity check */
144
145 if (sw != dw || sh != dh) {
146 /*if (!(rgba->cache && dw == rgba->cwidth && dh == rgba->cheight))*/ {
147 g_free(rgba->cache);
148 rgba->cache = scale_rect(rgba->data, sw, sh, dw, dh);
149 rgba->cwidth = dw;
150 rgba->cheight = dh;
151 }
152 source = rgba->cache;
153 } else {
154 source = rgba->data;
155 }
156
157 /* copy source -> dest, and apply the alpha channel */
158 col = 0;
159 num_pixels = dw * dh;
160 dest = target + area->x + target_w * area->y;
161 while (num_pixels-- > 0) {
162 guchar alpha, r, g, b, bgr, bgg, bgb;
163
164 alpha = *source >> RrDefaultAlphaOffset;
165 r = *source >> RrDefaultRedOffset;
166 g = *source >> RrDefaultGreenOffset;
167 b = *source >> RrDefaultBlueOffset;
168
169 /* background color */
170 bgr = *dest >> RrDefaultRedOffset;
171 bgg = *dest >> RrDefaultGreenOffset;
172 bgb = *dest >> RrDefaultBlueOffset;
173
174 r = bgr + (((r - bgr) * alpha) >> 8);
175 g = bgg + (((g - bgg) * alpha) >> 8);
176 b = bgb + (((b - bgb) * alpha) >> 8);
177
178 *dest = ((r << RrDefaultRedOffset) |
179 (g << RrDefaultGreenOffset) |
180 (b << RrDefaultBlueOffset));
181
182 dest++;
183 source++;
184
185 if (++col >= dw) {
186 col = 0;
187 dest += target_w - dw;
188 }
189 }
190 }
This page took 0.03888 seconds and 4 git commands to generate.