[pypy-commit] extradoc extradoc: bilinear interpolation to prevent pixelization in the center

hakanardo noreply at buildbot.pypy.org
Tue Jun 21 08:15:57 CEST 2011


Author: Hakan Ardo <hakan at debian.org>
Branch: extradoc
Changeset: r3762:b88cf61c3833
Date: 2011-06-21 08:20 +0200
http://bitbucket.org/pypy/extradoc/changeset/b88cf61c3833/

Log:	bilinear interpolation to prevent pixelization in the center

diff --git a/talk/iwtc11/benchmarks/image/magnify.py b/talk/iwtc11/benchmarks/image/magnify.py
--- a/talk/iwtc11/benchmarks/image/magnify.py
+++ b/talk/iwtc11/benchmarks/image/magnify.py
@@ -1,5 +1,19 @@
 from plain import Image
-from math import atan2, sqrt, sin, cos
+from math import atan2, sqrt, sin, cos, ceil, floor 
+
+class BilinImage(Image):
+    def __getitem__(self, (x, y)):
+        if isinstance(x, float) and isinstance(y, float):
+            x0, x1 = int(floor(x)), int(ceil(x))
+            y0, y1 = int(floor(y)), int(ceil(y))
+            xoff, yoff = x-x0, y-y0
+            return (1.0-xoff)*(1.0-yoff) * self[x0, y0] + \
+                   (1.0-xoff)*(    yoff) * self[x0, y1] + \
+                   (    xoff)*(1.0-yoff) * self[x1, y0] + \
+                   (    xoff)*(    yoff) * self[x1, y1]
+        else:
+            return Image.__getitem__(self, (x, y))
+    
 
 def magnify(img):
     out = Image(img.width, img.height, typecode='B')
@@ -18,6 +32,23 @@
                 out[x,y] = img[x,y]
     return out
 
+def magnify_bilin(img):
+    out = Image(img.width, img.height, typecode='B')
+    out.data[:] = img.data
+    maxr = img.height/3
+    for y in xrange(img.height/2 - maxr, img.height/2 + maxr):
+        for x in xrange(img.width/2 - maxr, img.width/2 + maxr):
+            dx, dy = x - img.width/2, y - img.height/2
+            a = atan2(dy, dx)
+            r = sqrt(dx ** 2 + dy ** 2)
+            if r < maxr:
+                nr = r*r / maxr
+                nx, ny = nr*cos(a), nr*sin(a)
+                out[x,y] = min(int(img[nx + img.width/2, ny + img.height/2]), 255)
+            else:
+                out[x,y] = img[x,y]
+    return out
+
 if __name__ == '__main__':
     from io import mplayer, view
     import sys
@@ -36,8 +67,9 @@
         pass
 
     start = start0 = time()
-    for fcnt, img in enumerate(mplayer(Image, fn)):
-        view(magnify(img))
+    for fcnt, img in enumerate(mplayer(BilinImage, fn)):
+        #view(magnify(img))
+        view(magnify_bilin(img))
         print 1.0 / (time() - start), 'fps, ', (fcnt-2) / (time() - start0), 'average fps'
         start = time()
         if fcnt==2:
diff --git a/talk/iwtc11/benchmarks/image/plain.py b/talk/iwtc11/benchmarks/image/plain.py
--- a/talk/iwtc11/benchmarks/image/plain.py
+++ b/talk/iwtc11/benchmarks/image/plain.py
@@ -30,7 +30,7 @@
         for y in xrange(border, self.height-border):
             for x in xrange(border, self.width-border):
                 yield x, y
-                
+
 
 def sobel_magnitude(a):
     b = Image(a.width, a.height, typecode='B')


More information about the pypy-commit mailing list