1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 image.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
25 #define AVERAGE(a, b) ( ((((a) ^ (b)) & 0xfefefefeL) >> 1) + ((a) & (b)) )
27 static void scale_line(RrPixel32 *dest, RrPixel32 *source, gint w, gint dw)
30 gint int_part = w / dw;
31 gint fract_part = w % dw;
34 while (num_pixels-- > 0) {
45 static RrPixel32* scale_half(RrPixel32 *source, gint w, gint h)
47 RrPixel32 *out, *dest, *sourceline, *sourceline2;
51 sourceline2 = source + w;
56 out = dest = g_new(RrPixel32, dw * dh);
58 for (y = 0; y < dh; ++y) {
64 for (x = 0; x < dw; ++x) {
65 *dest++ = AVERAGE(AVERAGE(*s, *(s+1)),
66 AVERAGE(*s2, *(s2+1)));
71 sourceline2 += w << 1;
76 static RrPixel32* scale_rect(RrPixel32 *fullsource,
77 gint w, gint h, gint dw, gint dh)
79 RrPixel32 *out, *dest;
80 RrPixel32 *source = fullsource;
81 RrPixel32 *oldsource = NULL;
82 RrPixel32 *prev_source = NULL;
88 while (dw <= (w >> 1) && dh <= (h >> 1)) {
89 source = scale_half(source, w, h);
96 int_part = (h / dh) * w;
99 out = dest = g_new(RrPixel32, dw * dh);
101 while (num_pixels-- > 0) {
102 if (source == prev_source) {
103 memcpy(dest, dest - dw, dw * sizeof(RrPixel32));
105 scale_line(dest, source, w, dw);
106 prev_source = source;
122 void RrImageDraw(RrPixel32 *target, RrTextureRGBA *rgba,
123 gint target_w, gint target_h,
129 gint col, num_pixels;
136 dh = (int)(dw * ((double)sh / sw));
137 if (dh > area->height) {
139 dw = (int)(dh * ((double)sw / sh));
142 if (sw != dw || sh != dh) {
143 /*if (!(rgba->cache && dw == rgba->cwidth && dh == rgba->cheight))*/ {
145 rgba->cache = scale_rect(rgba->data, sw, sh, dw, dh);
149 source = rgba->cache;
154 /* copy source -> dest, and apply the alpha channel */
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;
161 alpha = *source >> RrDefaultAlphaOffset;
162 r = *source >> RrDefaultRedOffset;
163 g = *source >> RrDefaultGreenOffset;
164 b = *source >> RrDefaultBlueOffset;
166 /* background color */
167 bgr = *dest >> RrDefaultRedOffset;
168 bgg = *dest >> RrDefaultGreenOffset;
169 bgb = *dest >> RrDefaultBlueOffset;
171 r = bgr + (((r - bgr) * alpha) >> 8);
172 g = bgg + (((g - bgg) * alpha) >> 8);
173 b = bgb + (((b - bgb) * alpha) >> 8);
175 *dest = ((r << RrDefaultRedOffset) |
176 (g << RrDefaultGreenOffset) |
177 (b << RrDefaultBlueOffset));
184 dest += target_w - dw;