]> Dogcows Code - chaz/openbox/blob - render/image.c
use font 8point by default if none is specified
[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) 2006 Mikael Magnusson
5 Copyright (c) 2003 Ben Jansens
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "geom.h"
21 #include "image.h"
22 #include "color.h"
23
24 #include <glib.h>
25
26 #define FRACTION 12
27 #define FLOOR(i) ((i) & (~0UL << FRACTION))
28 #define AVERAGE(a, b) (((((a) ^ (b)) & 0xfefefefeL) >> 1) + ((a) & (b)))
29
30 static void ImageCopyResampled(RrPixel32 *dst, RrPixel32 *src,
31 gulong dstW, gulong dstH,
32 gulong srcW, gulong srcH)
33 {
34 gulong dstX, dstY, srcX, srcY;
35 gulong srcX1, srcX2, srcY1, srcY2;
36 gulong ratioX, ratioY;
37
38 ratioX = (srcW << FRACTION) / dstW;
39 ratioY = (srcH << FRACTION) / dstH;
40
41 srcY2 = 0;
42 for (dstY = 0; dstY < dstH; dstY++) {
43 srcY1 = srcY2;
44 srcY2 += ratioY;
45
46 srcX2 = 0;
47 for (dstX = 0; dstX < dstW; dstX++) {
48 gulong red = 0, green = 0, blue = 0, alpha = 0;
49 gulong portionX, portionY, portionXY, sumXY = 0;
50 RrPixel32 pixel;
51
52 srcX1 = srcX2;
53 srcX2 += ratioX;
54
55 for (srcY = srcY1; srcY < srcY2; srcY += (1UL << FRACTION)) {
56 if (srcY == srcY1) {
57 srcY = FLOOR(srcY);
58 portionY = (1UL << FRACTION) - (srcY1 - srcY);
59 if (portionY > srcY2 - srcY1)
60 portionY = srcY2 - srcY1;
61 }
62 else if (srcY == FLOOR(srcY2))
63 portionY = srcY2 - srcY;
64 else
65 portionY = (1UL << FRACTION);
66
67 for (srcX = srcX1; srcX < srcX2; srcX += (1UL << FRACTION)) {
68 if (srcX == srcX1) {
69 srcX = FLOOR(srcX);
70 portionX = (1UL << FRACTION) - (srcX1 - srcX);
71 if (portionX > srcX2 - srcX1)
72 portionX = srcX2 - srcX1;
73 }
74 else if (srcX == FLOOR(srcX2))
75 portionX = srcX2 - srcX;
76 else
77 portionX = (1UL << FRACTION);
78
79 portionXY = (portionX * portionY) >> FRACTION;
80 sumXY += portionXY;
81
82 pixel = *(src + (srcY >> FRACTION) * srcW
83 + (srcX >> FRACTION));
84 red += ((pixel >> RrDefaultRedOffset) & 0xFF)
85 * portionXY;
86 green += ((pixel >> RrDefaultGreenOffset) & 0xFF)
87 * portionXY;
88 blue += ((pixel >> RrDefaultBlueOffset) & 0xFF)
89 * portionXY;
90 alpha += ((pixel >> RrDefaultAlphaOffset) & 0xFF)
91 * portionXY;
92 }
93 }
94
95 g_assert(sumXY != 0);
96 red /= sumXY;
97 green /= sumXY;
98 blue /= sumXY;
99 alpha /= sumXY;
100
101 *dst++ = (red << RrDefaultRedOffset) |
102 (green << RrDefaultGreenOffset) |
103 (blue << RrDefaultBlueOffset) |
104 (alpha << RrDefaultAlphaOffset);
105 }
106 }
107 }
108
109 void RrImageDraw(RrPixel32 *target, RrTextureRGBA *rgba,
110 gint target_w, gint target_h,
111 RrRect *area)
112 {
113 RrPixel32 *dest;
114 RrPixel32 *source;
115 gint sw, sh, dw, dh;
116 gint col, num_pixels;
117
118 sw = rgba->width;
119 sh = rgba->height;
120
121 /* keep the ratio */
122 dw = area->width;
123 dh = (gint)(dw * ((gdouble)sh / sw));
124 if (dh > area->height) {
125 dh = area->height;
126 dw = (gint)(dh * ((gdouble)sw / sh));
127 }
128
129 if (!(dw && dh))
130 return; /* XXX sanity check */
131
132 if (sw != dw || sh != dh) {
133 /*if (!(rgba->cache && dw == rgba->cwidth && dh == rgba->cheight))*/ {
134 g_free(rgba->cache);
135 rgba->cache = g_new(RrPixel32, dw * dh);
136 ImageCopyResampled(rgba->cache, rgba->data, dw, dh, sw, sh);
137 rgba->cwidth = dw;
138 rgba->cheight = dh;
139 }
140 source = rgba->cache;
141 } else {
142 source = rgba->data;
143 }
144
145 /* copy source -> dest, and apply the alpha channel */
146 col = 0;
147 num_pixels = dw * dh;
148 dest = target + area->x + target_w * area->y;
149 while (num_pixels-- > 0) {
150 guchar alpha, r, g, b, bgr, bgg, bgb;
151
152 alpha = *source >> RrDefaultAlphaOffset;
153 r = *source >> RrDefaultRedOffset;
154 g = *source >> RrDefaultGreenOffset;
155 b = *source >> RrDefaultBlueOffset;
156
157 /* background color */
158 bgr = *dest >> RrDefaultRedOffset;
159 bgg = *dest >> RrDefaultGreenOffset;
160 bgb = *dest >> RrDefaultBlueOffset;
161
162 r = bgr + (((r - bgr) * alpha) >> 8);
163 g = bgg + (((g - bgg) * alpha) >> 8);
164 b = bgb + (((b - bgb) * alpha) >> 8);
165
166 *dest = ((r << RrDefaultRedOffset) |
167 (g << RrDefaultGreenOffset) |
168 (b << RrDefaultBlueOffset));
169
170 dest++;
171 source++;
172
173 if (++col >= dw) {
174 col = 0;
175 dest += target_w - dw;
176 }
177 }
178 }
This page took 0.044741 seconds and 4 git commands to generate.