new scaling, based on the Bresenham algorithm

This commit is contained in:
Dana Jansens 2003-09-04 06:23:27 +00:00
parent cb3aaab102
commit 55ed757c5f

View file

@ -4,70 +4,166 @@
#include <glib.h> #include <glib.h>
void RrImageDraw(RrPixel32 *target, RrTextureRGBA *rgba, RrRect *area) #define AVERAGE(a, b) ( ((((a) ^ (b)) & 0xfefefefeL) >> 1) + ((a) & (b)) )
static void scale_line(RrPixel32 *dest, RrPixel32 *source, gint w, gint dw)
{ {
RrPixel32 *draw = rgba->data; gint num_pixels = dw;
gint c, i, e, t, sfw, sfh; gint int_part = w / dw;
sfw = area->width; gint fract_part = w % dw;
sfh = area->height; gint err = 0;
g_assert(rgba->data != NULL); while (num_pixels-- > 0) {
*dest++ = *source;
if ((rgba->width != sfw || rgba->height != sfh) && source += int_part;
(rgba->width != rgba->cwidth || rgba->height != rgba->cheight)) { err += fract_part;
double dx = rgba->width / (double)sfw; if (err >= dw) {
double dy = rgba->height / (double)sfh; err -= dw;
double px = 0.0; source++;
double py = 0.0; }
int iy = 0; }
}
/* scale it and cache it */
if (rgba->cache != NULL) static RrPixel32* scale_half(RrPixel32 *source, gint w, gint h)
g_free(rgba->cache); {
rgba->cache = g_new(RrPixel32, sfw * sfh); RrPixel32 *out, *dest, *sourceline, *sourceline2;
rgba->cwidth = sfw; gint dw, dh, x, y;
rgba->cheight = sfh;
for (i = 0, c = 0, e = sfw*sfh; i < e; ++i) { sourceline = source;
rgba->cache[i] = rgba->data[(int)px + iy]; sourceline2 = source + w;
if (++c >= sfw) {
c = 0; dw = w >> 1;
px = 0; dh = h >> 1;
py += dy;
iy = (int)py * rgba->width; out = dest = g_new(RrPixel32, dw * dh);
} else
px += dx; for (y = 0; y < dh; ++y) {
} RrPixel32 *s, *s2;
/* do we use the cache we may have just created, or the original? */ s = sourceline;
if (rgba->width != sfw || rgba->height != sfh) s2 = sourceline2;
draw = rgba->cache;
for (x = 0; x < dw; ++x) {
/* apply the alpha channel */ *dest++ = AVERAGE(AVERAGE(*s, *(s+1)),
for (i = 0, c = 0, t = area->x, e = sfw*sfh; i < e; ++i, ++t) { AVERAGE(*s2, *(s2+1)));
guchar alpha, r, g, b, bgr, bgg, bgb; s += 2;
s2 += 2;
alpha = draw[i] >> RrDefaultAlphaOffset; }
r = draw[i] >> RrDefaultRedOffset; sourceline += w << 1;
g = draw[i] >> RrDefaultGreenOffset; sourceline2 += w << 1;
b = draw[i] >> RrDefaultBlueOffset; }
return out;
if (c >= sfw) { }
c = 0;
t += area->width - sfw; static RrPixel32* scale_rect(RrPixel32 *fullsource,
} gint w, gint h, gint dw, gint dh)
{
/* background color */ RrPixel32 *out, *dest;
bgr = target[t] >> RrDefaultRedOffset; RrPixel32 *source = fullsource;
bgg = target[t] >> RrDefaultGreenOffset; RrPixel32 *oldsource = NULL;
bgb = target[t] >> RrDefaultBlueOffset; RrPixel32 *prev_source = NULL;
gint num_pixels;
r = bgr + (((r - bgr) * alpha) >> 8); gint int_part;
g = bgg + (((g - bgg) * alpha) >> 8); gint fract_part;
b = bgb + (((b - bgb) * alpha) >> 8); gint err = 0;
target[t] = (r << RrDefaultRedOffset) while (dw <= (w >> 1) && dh <= (h >> 1)) {
| (g << RrDefaultGreenOffset) source = scale_half(source, w, h);
| (b << RrDefaultBlueOffset); 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;
} }
} }
} }