all repos — openbox @ 55ed757c5fc4a2ef465374adcd65069d2235955b

openbox fork - make it a bit more like ryudo

render/image.c (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "geom.h"
#include "image.h"
#include "color.h"

#include <glib.h>

#define AVERAGE(a, b)   ( ((((a) ^ (b)) & 0xfefefefeL) >> 1) + ((a) & (b)) )

static void scale_line(RrPixel32 *dest, RrPixel32 *source, gint w, gint dw)
{
    gint num_pixels = dw;
    gint int_part = w / dw;
    gint fract_part = w % dw;
    gint err = 0;

    while (num_pixels-- > 0) {
        *dest++ = *source;
        source += int_part;
        err += fract_part;
        if (err >= dw) {
            err -= dw;
            source++;
        }
    }
}

static RrPixel32* scale_half(RrPixel32 *source, gint w, gint h)
{
    RrPixel32 *out, *dest, *sourceline, *sourceline2;
    gint dw, dh, x, y;

    sourceline = source;
    sourceline2 = source + w;

    dw = w >> 1;
    dh = h >> 1;

    out = dest = g_new(RrPixel32, dw * dh);

    for (y = 0; y < dh; ++y) {
        RrPixel32 *s, *s2;

        s = sourceline;
        s2 = sourceline2;

        for (x = 0; x < dw; ++x) {
            *dest++ = AVERAGE(AVERAGE(*s, *(s+1)),
                              AVERAGE(*s2, *(s2+1)));
            s += 2;
            s2 += 2;
        }
        sourceline += w << 1;
        sourceline2 += w << 1;
    }
    return out;
}

static RrPixel32* scale_rect(RrPixel32 *fullsource,
                             gint w, gint h, gint dw, gint dh)
{
    RrPixel32 *out, *dest;
    RrPixel32 *source = fullsource;
    RrPixel32 *oldsource = NULL;
    RrPixel32 *prev_source = NULL;
    gint num_pixels;
    gint int_part;
    gint fract_part;
    gint err = 0;

    while (dw <= (w >> 1) && dh <= (h >> 1)) {
        source = scale_half(source, w, h);
        w >>= 1; h >>= 1;
        g_free(oldsource);
        oldsource = source;
    }

    num_pixels = dh;
    int_part = (h / dh) * w;
    fract_part = h % dh;

    out = dest = g_new(RrPixel32, dw * dh);

    while (num_pixels-- > 0) {
        if (source == prev_source) {
            memcpy(dest, dest - dw, dw * sizeof(RrPixel32));
        } else {
            scale_line(dest, source, w, dw);
            prev_source = source;
        }
        dest += dw;
        source += int_part;
        err += fract_part;
        if (err >= dh) {
            err -= dh;
            source += w;
        }
    }

    g_free(oldsource);

    return out;
}

void RrImageDraw(RrPixel32 *target, RrTextureRGBA *rgba,
                 gint target_w, gint target_h,
                 RrRect *area)
{
    RrPixel32 *dest;
    RrPixel32 *source;
    gint sw, sh, dw, dh;
    gint col, num_pixels;

    sw = rgba->width;
    sh = rgba->height;

    /* keep the ratio */
    dw = area->width;
    dh = (int)(dw * ((double)sh / sw));
    if (dh > area->height) {
        dh = area->height;
        dw = (int)(dh * ((double)sw / sh));
    }

    if (sw != dw || sh != dh) {
        /*if (!(rgba->cache && dw == rgba->cwidth && dh == rgba->cheight))*/ {
            g_free(rgba->cache);
            rgba->cache = scale_rect(rgba->data, sw, sh, dw, dh);
            rgba->cwidth = dw;
            rgba->cheight = dh;
        }
        source = rgba->cache;
    } else {
        source = rgba->data;
    }

    /* copy source -> dest, and apply the alpha channel */
    col = 0;
    num_pixels = dw * dh;
    dest = target + area->x + target_w * area->y;
    while (num_pixels-- > 0) {
        guchar alpha, r, g, b, bgr, bgg, bgb;

        alpha = *source >> RrDefaultAlphaOffset;
        r = *source >> RrDefaultRedOffset;
        g = *source >> RrDefaultGreenOffset;
        b = *source >> RrDefaultBlueOffset;
        
        /* background color */
        bgr = *dest >> RrDefaultRedOffset;
        bgg = *dest >> RrDefaultGreenOffset;
        bgb = *dest >> RrDefaultBlueOffset;

        r = bgr + (((r - bgr) * alpha) >> 8);
        g = bgg + (((g - bgg) * alpha) >> 8);
        b = bgb + (((b - bgb) * alpha) >> 8);

        *dest = ((r << RrDefaultRedOffset) |
                 (g << RrDefaultGreenOffset) |
                 (b << RrDefaultBlueOffset));

        dest++;
        source++;

        if (col++ >= dw) {
            col = 0;
            dest += target_w - dw;
        }
    }
}