I've recently used this algorithm in the shapes demo and wanted to try it on an image, it is supposed to improve the appearance of a rotated image. This code is inefficient because it calculates many unnecessary pixels, I may fix this later.
deflng a-z
'const sw = 800
'const sh = 600
dim shared pi as double
pi = 4*atn(1)
img = _loadimage("leopardx.jpg", 32)
w = _width(img)
h = _height(img)
dim zoom as double
dim a as double
zoom = 2.5
a = 2*sqr(w*w/4 + h*h/4)*zoom
if h < a then h = a
screen _newimage(w + a*2, h, 32)
_putimage (0,0), img
dim rot as double
do
rot = rot + 0.1
line (w, 0)-step(a*2, a),_rgb(0,0,0),bf
rotzoom img, w + a/2, a/2, rot, zoom
rotzoomb img, w + a + a/2, a/2, rot, zoom
_display
_limit 30
loop until _keyhit = 27
sleep
system
sub rotzoomb(img, x0, y0, rot as double, zoom as double)
dim a as double
dim xx as double, yy as double
dim dx as double, dy as double
w = _width(img)
h = _height(img)
if zoom = 0 then zoom = 1
a = 2*sqr(w*w/4 + h*h/4)*zoom
_source img
for y=0 to a
for x=0 to a
xx = (x - a/2)*cos(rot)/zoom - (y - a/2)*sin(rot)/zoom + w/2
yy = (x - a/2)*sin(rot)/zoom + (y - a/2)*cos(rot)/zoom + h/2
if (int(xx) >=0 and int(xx) < w - 1 and int(yy) >= 0 and int(yy) < h - 1) then
tl = point(int(xx), int(yy))
tr = point(int(xx) + 1, int(yy))
bl = point(int(xx), int(yy) + 1)
br = point(int(xx) + 1, int(yy) + 1)
dx = xx - int(xx)
dy = yy - int(yy)
r = _round((1 - dy)*((1 - dx)* _red(tl) + dx* _red(tr)) + dy*((1 - dx)* _red(bl) + dx* _red(br)))
g = _round((1 - dy)*((1 - dx)*_green(tl) + dx*_green(tr)) + dy*((1 - dx)*_green(bl) + dx*_green(br)))
b = _round((1 - dy)*((1 - dx)* _blue(tl) + dx* _blue(tr)) + dy*((1 - dx)* _blue(bl) + dx* _blue(br)))
pset (x0 - a/2 + x, y0 - a/2 + y), _rgb(r, g, b)
elseif (int(xx) >=0 and int(xx) < w - 1 and int(yy) >= 0 and int(yy) < h - 1) then
pset (x0 - a/2 + x, y0 - a/2 + y), point(int(xx), int(yy))
end if
next
next
end sub
sub rotzoom(img, x0, y0, rot as double, zoom as double)
dim a as double
w = _width(img)
h = _height(img)
if zoom = 0 then zoom = 1
a = 2*sqr(w*w/4 + h*h/4)*zoom
_source img
for y=0 to a
for x=0 to a
xx = (x - a/2)*cos(rot)/zoom - (y - a/2)*sin(rot)/zoom + w/2
yy = (x - a/2)*sin(rot)/zoom + (y - a/2)*cos(rot)/zoom + h/2
if ((xx) >= 0 and (xx) < w and (yy) >=0 and (yy) < h) then
pset (x0 - a/2 + x, y0 - a/2 + y), point(int(xx), int(yy))
end if
next
next
end sub