]> Dogcows Code - chaz/openbox/blob - render/image.c
try another scaling algo for the 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 FRACTION 12
26 #define FLOOR(i) ((i) & (~0UL << FRACTION))
27 #define AVERAGE(a, b) (((((a) ^ (b)) & 0xfefefefeL) >> 1) + ((a) & (b)))
28
29 static RrPixel32* scale_half(RrPixel32 *source, gint w, gint h)
30 {
31 RrPixel32 *out, *dest, *sourceline, *sourceline2;
32 gint dw, dh, x, y;
33
34 sourceline = source;
35 sourceline2 = source + w;
36
37 dw = w >> 1;
38 dh = h >> 1;
39
40 out = dest = g_new(RrPixel32, dw * dh);
41
42 for (y = 0; y < dh; ++y) {
43 RrPixel32 *s, *s2;
44
45 s = sourceline;
46 s2 = sourceline2;
47
48 for (x = 0; x < dw; ++x) {
49 *dest++ = AVERAGE(AVERAGE(*s, *(s+1)),
50 AVERAGE(*s2, *(s2+1)));
51 s += 2;
52 s2 += 2;
53 }
54 sourceline += w << 1;
55 sourceline2 += w << 1;
56 }
57 return out;
58 }
59
60 static void ImageCopyResampled(RrPixel32 *dst, RrPixel32 *src,
61 gulong dstW, gulong dstH,
62 gulong srcW, gulong srcH)
63 {
64 gulong dstX, dstY, srcX, srcY;
65 gulong srcX1, srcX2, srcY1, srcY2;
66 gulong ratioX, ratioY;
67
68 ratioX = (srcW << FRACTION) / dstW;
69 ratioY = (srcH << FRACTION) / dstH;
70
71 srcY2 = 0;
72 for (dstY = 0; dstY < dstH; dstY++) {
73 srcY1 = srcY2;
74 srcY2 += ratioY;
75
76 srcX2 = 0;
77 for (dstX = 0; dstX < dstW; dstX++) {
78 gulong red = 0, green = 0, blue = 0, alpha = 0;
79 gulong portionX, portionY, portionXY, sumXY = 0;
80 RrPixel32 pixel;
81
82 srcX1 = srcX2;
83 srcX2 += ratioX;
84
85 for (srcY = srcY1; srcY < srcY2; srcY += (1UL << FRACTION)) {
86 if (srcY == srcY1) {
87 srcY = FLOOR(srcY);
88 portionY = (1UL << FRACTION) - (srcY1 - srcY);
89 if (portionY > srcY2 - srcY1)
90 portionY = srcY2 - srcY1;
91 }
92 else if (srcY == FLOOR(srcY2))
93 portionY = srcY2 - srcY;
94 else
95 portionY = (1UL << FRACTION);
96
97 for (srcX = srcX1; srcX < srcX2; srcX += (1UL << FRACTION)) {
98 if (srcX == srcX1) {
99 srcX = FLOOR(srcX);
100 portionX = (1UL << FRACTION) - (srcX1 - srcX);
101 if (portionX > srcX2 - srcX1)
102 portionX = srcX2 - srcX1;
103 }
104 else if (srcX == FLOOR(srcX2))
105 portionX = srcX2 - srcX;
106 else
107 portionX = (1UL << FRACTION);
108
109 portionXY = (portionX * portionY) >> FRACTION;
110 sumXY += portionXY;
111
112 pixel = *(src + (srcY >> FRACTION) * srcW + (srcX >> FRACTION));
113 red += ((pixel >> RrDefaultRedOffset) & 0xFF) * portionXY;
114 green += ((pixel >> RrDefaultGreenOffset) & 0xFF) * portionXY;
115 blue += ((pixel >> RrDefaultBlueOffset) & 0xFF) * portionXY;
116 alpha += ((pixel >> RrDefaultAlphaOffset) & 0xFF) * portionXY;
117 }
118 }
119
120 g_assert(sumXY != 0);
121 red /= sumXY;
122 green /= sumXY;
123 blue /= sumXY;
124 alpha /= sumXY;
125
126 *dst++ = (red << RrDefaultRedOffset) |
127 (green << RrDefaultGreenOffset) |
128 (blue << RrDefaultBlueOffset) |
129 (alpha << RrDefaultAlphaOffset);
130 }
131 }
132 }
133
134 void RrImageDraw(RrPixel32 *target, RrTextureRGBA *rgba,
135 gint target_w, gint target_h,
136 RrRect *area)
137 {
138 RrPixel32 *dest;
139 RrPixel32 *source;
140 gint sw, sh, dw, dh;
141 gint col, num_pixels;
142
143 sw = rgba->width;
144 sh = rgba->height;
145
146 /* keep the ratio */
147 dw = area->width;
148 dh = (gint)(dw * ((gdouble)sh / sw));
149 if (dh > area->height) {
150 dh = area->height;
151 dw = (gint)(dh * ((gdouble)sw / sh));
152 }
153
154 if (!(dw && dh))
155 return; /* XXX sanity check */
156
157 if (sw != dw || sh != dh) {
158 /*if (!(rgba->cache && dw == rgba->cwidth && dh == rgba->cheight))*/ {
159 g_free(rgba->cache);
160 rgba->cache = g_new(RrPixel32, dw * dh);
161 ImageCopyResampled(rgba->cache, rgba->data, dw, dh, sw, sh);
162 rgba->cwidth = dw;
163 rgba->cheight = dh;
164 }
165 source = rgba->cache;
166 } else {
167 source = rgba->data;
168 }
169
170 /* copy source -> dest, and apply the alpha channel */
171 col = 0;
172 num_pixels = dw * dh;
173 dest = target + area->x + target_w * area->y;
174 while (num_pixels-- > 0) {
175 guchar alpha, r, g, b, bgr, bgg, bgb;
176
177 alpha = *source >> RrDefaultAlphaOffset;
178 r = *source >> RrDefaultRedOffset;
179 g = *source >> RrDefaultGreenOffset;
180 b = *source >> RrDefaultBlueOffset;
181
182 /* background color */
183 bgr = *dest >> RrDefaultRedOffset;
184 bgg = *dest >> RrDefaultGreenOffset;
185 bgb = *dest >> RrDefaultBlueOffset;
186
187 r = bgr + (((r - bgr) * alpha) >> 8);
188 g = bgg + (((g - bgg) * alpha) >> 8);
189 b = bgb + (((b - bgb) * alpha) >> 8);
190
191 *dest = ((r << RrDefaultRedOffset) |
192 (g << RrDefaultGreenOffset) |
193 (b << RrDefaultBlueOffset));
194
195 dest++;
196 source++;
197
198 if (++col >= dw) {
199 col = 0;
200 dest += target_w - dw;
201 }
202 }
203 }
This page took 0.042144 seconds and 5 git commands to generate.