This source file includes following definitions.
- __imlib_RotateSampleInside
- __imlib_RotateAAInside
- __check_inside_coords
- __imlib_RotateSample
- __imlib_RotateAA
- __imlib_BlendImageToImageSkewed
#include "common.h"
#include "rotate.h"
#include "blend.h"
#define INTERP(v1, v2, f) \
(((v1) << _ROTATE_PREC) + (((v2) - (v1)) * (f)))
#define INTERP_VAL1(x_VAL, dest, l, r, x) \
x_VAL(dest) = (INTERP(x_VAL(l), x_VAL(r), (x)) >> _ROTATE_PREC)
#define INTERP_VAL1_A0(dest, v1, v2, f1, f2) \
A_VAL(dest) = ((INTERP(A_VAL(v1), A_VAL(v2), (f1)) * \
(f2)) >> (2 * _ROTATE_PREC))
#define INTERP_VAL2(x_VAL, dest, ul, ur, ll, lr, x, y) \
x_VAL(dest) = (INTERP(INTERP(x_VAL(ul), x_VAL(ur), (x)), \
INTERP(x_VAL(ll), x_VAL(lr), (x)), \
(y)) >> (2 * _ROTATE_PREC))
#define INTERP_ARGB(dest, src, sow, x, y) do { \
INTERP_VAL2(R_VAL, (dest), (src), (src) + 1, (src) + (sow), (src) + (sow) + 1, (x) & _ROTATE_PREC_BITS, (y) & _ROTATE_PREC_BITS); \
INTERP_VAL2(G_VAL, (dest), (src), (src) + 1, (src) + (sow), (src) + (sow) + 1, (x) & _ROTATE_PREC_BITS, (y) & _ROTATE_PREC_BITS); \
INTERP_VAL2(B_VAL, (dest), (src), (src) + 1, (src) + (sow), (src) + (sow) + 1, (x) & _ROTATE_PREC_BITS, (y) & _ROTATE_PREC_BITS); \
INTERP_VAL2(A_VAL, (dest), (src), (src) + 1, (src) + (sow), (src) + (sow) + 1, (x) & _ROTATE_PREC_BITS, (y) & _ROTATE_PREC_BITS); \
} while (0)
#define INTERP_RGB_A0(dest, v1, v2, f, f2) do { \
INTERP_VAL1(R_VAL, (dest), (v1), (v2), (f) & _ROTATE_PREC_BITS); \
INTERP_VAL1(G_VAL, (dest), (v1), (v2), (f) & _ROTATE_PREC_BITS); \
INTERP_VAL1(B_VAL, (dest), (v1), (v2), (f) & _ROTATE_PREC_BITS); \
INTERP_VAL1_A0(dest, (v1), (v2), (f) & _ROTATE_PREC_BITS, (f2) & _ROTATE_PREC_BITS); \
} while (0)
#define INTERP_A000(dest, v, f1, f2) do { \
*(dest) = *(v); \
A_VAL(dest) = (A_VAL(dest) * \
((f1) & _ROTATE_PREC_BITS) * ((f2) & _ROTATE_PREC_BITS)) >> (2 * _ROTATE_PREC); \
} while (0)
static void
__imlib_RotateSampleInside(DATA32 * src, DATA32 * dest, int sow, int dow,
int dw, int dh, int x, int y,
int dxh, int dyh, int dxv, int dyv)
{
int i;
if ((dw < 1) || (dh < 1))
return;
while (1)
{
i = dw - 1;
do
{
*dest = src[(x >> _ROTATE_PREC) + ((y >> _ROTATE_PREC) * sow)];
x += dxh;
y += dyh;
dest++;
}
while (--i >= 0);
if (--dh <= 0)
break;
x += dxv - dw * dxh;
y += dyv - dw * dyh;
dest += (dow - dw);
}
}
static void
__imlib_RotateAAInside(DATA32 * src, DATA32 * dest, int sow, int dow,
int dw, int dh, int x, int y,
int dxh, int dyh, int dxv, int dyv)
{
int i;
if ((dw < 1) || (dh < 1))
return;
while (1)
{
i = dw - 1;
do
{
DATA32 *src_x_y = (src + (x >> _ROTATE_PREC) +
((y >> _ROTATE_PREC) * sow));
INTERP_ARGB(dest, src_x_y, sow, x, y);
x += dxh;
y += dyh;
dest++;
}
while (--i >= 0);
if (--dh <= 0)
break;
x += dxv - dw * dxh;
y += dyv - dw * dyh;
dest += (dow - dw);
}
}
static int
__check_inside_coords(int x, int y, int dxh, int dyh, int dxv, int dyv,
int dw, int dh, int sw, int sh)
{
sw <<= _ROTATE_PREC;
sh <<= _ROTATE_PREC;
if (((unsigned)x >= sw) || ((unsigned)y >= sh))
return 0;
x += dxh * dw;
y += dyh * dw;
if (((unsigned)x >= sw) || ((unsigned)y >= sh))
return 0;
x += dxv * dh;
y += dyv * dh;
if (((unsigned)x >= sw) || ((unsigned)y >= sh))
return 0;
x -= dxh * dw;
y -= dyh * dw;
if (((unsigned)x >= sw) || ((unsigned)y >= sh))
return 0;
return 1;
}
void
__imlib_RotateSample(DATA32 * src, DATA32 * dest, int sow, int sw, int sh,
int dow, int dw, int dh, int x, int y,
int dxh, int dyh, int dxv, int dyv)
{
int i;
if ((dw < 1) || (dh < 1))
return;
if (__check_inside_coords(x, y, dxh, dyh, dxv, dyv, dw, dh, sw, sh))
{
__imlib_RotateSampleInside(src, dest, sow, dow, dw, dh, x, y,
dxh, dyh, dxv, dyv);
return;
}
sw <<= _ROTATE_PREC;
sh <<= _ROTATE_PREC;
while (1)
{
i = dw - 1;
do
{
if (((unsigned)x < sw) && ((unsigned)y < sh))
*dest = src[(x >> _ROTATE_PREC) + ((y >> _ROTATE_PREC) * sow)];
else
*dest = 0;
x += dxh;
y += dyh;
dest++;
}
while (--i >= 0);
if (--dh <= 0)
break;
x += dxv - dw * dxh;
y += dyv - dw * dyh;
dest += (dow - dw);
}
}
void
__imlib_RotateAA(DATA32 * src, DATA32 * dest, int sow, int sw, int sh,
int dow, int dw, int dh, int x, int y,
int dxh, int dyh, int dxv, int dyv)
{
int i;
if ((dw < 1) || (dh < 1))
return;
if (__check_inside_coords(x, y, dxh, dyh, dxv, dyv, dw, dh, sw - 1, sh - 1))
{
__imlib_RotateAAInside(src, dest, sow, dow, dw, dh, x, y,
dxh, dyh, dxv, dyv);
return;
}
sw--;
sh--;
sw <<= _ROTATE_PREC;
sh <<= _ROTATE_PREC;
while (1)
{
i = dw - 1;
do
{
DATA32 *src_x_y = (src + (x >> _ROTATE_PREC) +
((y >> _ROTATE_PREC) * sow));
if ((unsigned)x < sw)
{
if ((unsigned)y < sh)
{
INTERP_ARGB(dest, src_x_y, sow, x, y);
}
else if ((unsigned)(y - sh) < _ROTATE_PREC_MAX)
{
INTERP_RGB_A0(dest, src_x_y, src_x_y + 1, x, ~y);
}
else if ((unsigned)(~y) < _ROTATE_PREC_MAX)
{
INTERP_RGB_A0(dest, src_x_y + sow, src_x_y + sow + 1, x,
y);
}
else
*dest = 0;
}
else if ((unsigned)(x - sw) < (_ROTATE_PREC_MAX))
{
if ((unsigned)y < sh)
{
INTERP_RGB_A0(dest, src_x_y, src_x_y + sow, y, ~x);
}
else if ((unsigned)(y - sh) < _ROTATE_PREC_MAX)
{
INTERP_A000(dest, src_x_y, ~x, ~y);
}
else if ((unsigned)(~y) < _ROTATE_PREC_MAX)
{
INTERP_A000(dest, src_x_y + sow, ~x, y);
}
else
*dest = 0;
}
else if ((unsigned)(~x) < _ROTATE_PREC_MAX)
{
if ((unsigned)y < sh)
{
INTERP_RGB_A0(dest, src_x_y + 1, src_x_y + sow + 1, y,
x);
}
else if ((unsigned)(y - sh) < _ROTATE_PREC_MAX)
{
INTERP_A000(dest, src_x_y + 1, x, ~y);
}
else if ((unsigned)(~y) < _ROTATE_PREC_MAX)
{
INTERP_A000(dest, src_x_y + sow + 1, x, y);
}
else
*dest = 0;
}
else
*dest = 0;
x += dxh;
y += dyh;
dest++;
}
while (--i >= 0);
if (--dh <= 0)
break;
x += dxv - dw * dxh;
y += dyv - dw * dyh;
dest += (dow - dw);
}
}
#define LINESIZE 16
void
__imlib_BlendImageToImageSkewed(ImlibImage * im_src, ImlibImage * im_dst,
char aa, char blend, char merge_alpha,
int ssx, int ssy, int ssw, int ssh,
int ddx, int ddy,
int hsx, int hsy, int vsx, int vsy,
ImlibColorModifier * cm, ImlibOp op,
int clx, int cly, int clw, int clh)
{
int x, y, dxh, dyh, dxv, dyv, i;
double xy2;
DATA32 *data, *src;
int do_mmx;
if ((ssw < 0) || (ssh < 0))
return;
if ((!(im_src->data)) && (im_src->loader) && (im_src->loader->load))
im_src->loader->load(im_src, NULL, 0, 1);
if (!im_src->data)
return;
if ((!(im_dst->data)) && (im_dst->loader) && (im_dst->loader->load))
im_dst->loader->load(im_dst, NULL, 0, 1);
if (!im_dst->data)
return;
if (vsx | vsy)
{
xy2 = (double)(hsx * vsy - vsx * hsy) / _ROTATE_PREC_MAX;
if (xy2 == 0.0)
return;
dxh = (double)(ssw * vsy) / xy2;
dxv = (double)-(ssw * vsx) / xy2;
dyh = (double)-(ssh * hsy) / xy2;
dyv = (double)(ssh * hsx) / xy2;
}
else
{
xy2 = (double)(hsx * hsx + hsy * hsy) / _ROTATE_PREC_MAX;
if (xy2 == 0.0)
return;
dxh = (double)(ssw * hsx) / xy2;
dyh = (double)-(ssw * hsy) / xy2;
dxv = -dyh;
dyv = dxh;
}
x = -(ddx * dxh + ddy * dxv);
y = -(ddx * dyh + ddy * dyv);
if (ssx < 0)
{
x += ssx * _ROTATE_PREC_MAX;
ssw += ssx;
ssx = 0;
}
if (ssy < 0)
{
y += ssy * _ROTATE_PREC_MAX;
ssh += ssy;
ssy = 0;
}
if ((ssw + ssx) > im_src->w)
ssw = im_src->w - ssx;
if ((ssh + ssy) > im_src->h)
ssh = im_src->h - ssy;
src = im_src->data + ssx + ssy * im_src->w;
data = malloc(im_dst->w * LINESIZE * sizeof(DATA32));
if (!data)
return;
if (aa)
{
x += _ROTATE_PREC_MAX;
y += _ROTATE_PREC_MAX;
}
#ifdef DO_MMX_ASM
do_mmx = __imlib_get_cpuid() & CPUID_MMX;
#endif
for (i = 0; i < im_dst->h; i += LINESIZE)
{
int x2, y2, w, h, l, r;
h = MIN(LINESIZE, im_dst->h - i);
x2 = x + h * dxv;
y2 = y + h * dyv;
w = ssw << _ROTATE_PREC;
h = ssh << _ROTATE_PREC;
if (aa)
{
w += 2 << _ROTATE_PREC;
h += 2 << _ROTATE_PREC;
}
if (dxh > 0)
{
if (dyh > 0)
{
l = MAX(-MAX(y, y2) / dyh, -MAX(x, x2) / dxh);
r = MIN((h - MIN(y, y2)) / dyh, (w - MIN(x, x2)) / dxh);
}
else if (dyh < 0)
{
l = MAX(-MAX(x, x2) / dxh, (h - MIN(y, y2)) / dyh);
r = MIN(-MAX(y, y2) / dyh, (w - MIN(x, x2)) / dxh);
}
else
{
l = -MAX(x, x2) / dxh;
r = (w - MIN(x, x2)) / dxh;
}
}
else if (dxh < 0)
{
if (dyh > 0)
{
l = MAX(-MAX(y, y2) / dyh, (w - MIN(x, x2)) / dxh);
r = MIN(-MAX(x, x2) / dxh, (h - MIN(y, y2)) / dyh);
}
else if (dyh < 0)
{
l = MAX((h - MIN(y, y2)) / dyh, (w - MIN(x, x2)) / dxh);
r = MIN(-MAX(y, y2) / dyh, -MAX(x, x2) / dxh);
}
else
{
l = (w - MIN(x, x2)) / dxh;
r = -MAX(x, x2) / dxh;
}
}
else
{
if (dyh > 0)
{
l = -MAX(y, y2) / dyh;
r = (h - MIN(y, y2)) / dyh;
}
else if (dyh < 0)
{
l = (h - MIN(y, y2)) / dyh;
r = -MAX(y, y2) / dyh;
}
else
{
l = 0;
r = 0;
}
}
l--;
r += 2;
if (l < 0)
l = 0;
if (r > im_dst->w)
r = im_dst->w;
if (r <= l)
{
x = x2;
y = y2;
continue;
}
w = r - l;
h = MIN(LINESIZE, im_dst->h - i);
x += l * dxh;
y += l * dyh;
if (aa)
{
x -= _ROTATE_PREC_MAX;
y -= _ROTATE_PREC_MAX;
#ifdef DO_MMX_ASM
if (do_mmx)
__imlib_mmx_RotateAA(src, data, im_src->w, ssw, ssh, w, w, h,
x, y, dxh, dyh, dxv, dyv);
else
#endif
__imlib_RotateAA(src, data, im_src->w, ssw, ssh, w, w, h,
x, y, dxh, dyh, dxv, dyv);
}
else
{
__imlib_RotateSample(src, data, im_src->w, ssw, ssh, w, w, h,
x, y, dxh, dyh, dxv, dyv);
}
__imlib_BlendRGBAToData(data, w, h, im_dst->data,
im_dst->w, im_dst->h, 0, 0, l, i, w, h,
blend, merge_alpha, cm, op, 0);
x = x2;
y = y2;
}
free(data);
}