Merge pull request #970 from homm/fix-bicubic-stretch

Fix bicubic stretch interpolation
This commit is contained in:
wiredfool 2014-11-05 17:45:47 -08:00
commit 20be641329
2 changed files with 51 additions and 11 deletions

View File

@ -0,0 +1,40 @@
"""
Tests for ImagingCore.stretch functionality.
"""
from helper import unittest, PillowTestCase
from PIL import Image
im = Image.open("Tests/images/hopper.ppm").copy()
class TestImagingStretch(PillowTestCase):
def test_modes(self):
self.assertRaises(ValueError, im.convert("1").im.stretch,
(15, 12), Image.ANTIALIAS)
self.assertRaises(ValueError, im.convert("P").im.stretch,
(15, 12), Image.ANTIALIAS)
for mode in ["L", "I", "F", "RGB", "RGBA", "CMYK", "YCbCr"]:
s = im.convert(mode).im
r = s.stretch((15, 12), Image.ANTIALIAS)
self.assertEqual(r.mode, mode)
self.assertEqual(r.size, (15, 12))
self.assertEqual(r.bands, s.bands)
def test_reduce_filters(self):
# There is no Image.NEAREST because im.stretch implementation
# is not NEAREST for reduction. It should be removed
# or renamed to supersampling.
for f in [Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]:
r = im.im.stretch((15, 12), f)
self.assertEqual(r.mode, "RGB")
self.assertEqual(r.size, (15, 12))
def test_enlarge_filters(self):
for f in [Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]:
r = im.im.stretch((764, 414), f)
self.assertEqual(r.mode, "RGB")
self.assertEqual(r.size, (764, 414))

View File

@ -64,16 +64,14 @@ static struct filter BILINEAR = { bilinear_filter, 1.0 };
static inline float bicubic_filter(float x) static inline float bicubic_filter(float x)
{ {
/* FIXME: double-check this algorithm */ /* http://en.wikipedia.org/wiki/Bicubic_interpolation#Bicubic_convolution_algorithm */
/* FIXME: for best results, "a" should be -0.5 to -1.0, but we'll #define a -0.5
set it to zero for now, to match the 1.1 magnifying filter */
#define a 0.0
if (x < 0.0) if (x < 0.0)
x = -x; x = -x;
if (x < 1.0) if (x < 1.0)
return (((a + 2.0) * x) - (a + 3.0)) * x*x + 1; return ((a + 2.0) * x - (a + 3.0)) * x*x + 1;
if (x < 2.0) if (x < 2.0)
return (((a * x) - 5*a) * x + 8) * x - 4*a; return (((x - 5) * x + 8) * x - 4) * a;
return 0.0; return 0.0;
#undef a #undef a
} }
@ -95,7 +93,10 @@ ImagingStretch(Imaging imOut, Imaging imIn, int filter)
/* check modes */ /* check modes */
if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0)
return (Imaging) ImagingError_ModeError(); return (Imaging) ImagingError_ModeError();
if (strcmp(imIn->mode, "P") == 0 || strcmp(imIn->mode, "1") == 0)
return (Imaging) ImagingError_ModeError();
/* check filter */ /* check filter */
switch (filter) { switch (filter) {
@ -124,14 +125,13 @@ ImagingStretch(Imaging imOut, Imaging imIn, int filter)
/* prepare for vertical stretch */ /* prepare for vertical stretch */
filterscale = scale = (float) imIn->ysize / imOut->ysize; filterscale = scale = (float) imIn->ysize / imOut->ysize;
} else } else
return (Imaging) ImagingError_Mismatch(); return (Imaging) ImagingError_Mismatch();
/* determine support size (length of resampling filter) */ /* determine support size (length of resampling filter) */
support = filterp->support; support = filterp->support;
if (filterscale < 1.0) { if (filterscale < 1.0) {
filterscale = 1.0; filterscale = 1.0;
support = 0.5;
} }
support = support * filterscale; support = support * filterscale;
@ -154,7 +154,7 @@ ImagingStretch(Imaging imOut, Imaging imIn, int filter)
ymin = 0.0; ymin = 0.0;
ymax = ceil(center + support); ymax = ceil(center + support);
if (ymax > (float) imIn->ysize) if (ymax > (float) imIn->ysize)
ymax = (float) imIn->ysize; ymax = (float) imIn->ysize;
for (y = (int) ymin; y < (int) ymax; y++) { for (y = (int) ymin; y < (int) ymax; y++) {
float w = filterp->filter((y - center + 0.5) * ss) * ss; float w = filterp->filter((y - center + 0.5) * ss) * ss;
k[y - (int) ymin] = w; k[y - (int) ymin] = w;
@ -230,7 +230,7 @@ ImagingStretch(Imaging imOut, Imaging imIn, int filter)
xmin = 0.0; xmin = 0.0;
xmax = ceil(center + support); xmax = ceil(center + support);
if (xmax > (float) imIn->xsize) if (xmax > (float) imIn->xsize)
xmax = (float) imIn->xsize; xmax = (float) imIn->xsize;
for (x = (int) xmin; x < (int) xmax; x++) { for (x = (int) xmin; x < (int) xmax; x++) {
float w = filterp->filter((x - center + 0.5) * ss) * ss; float w = filterp->filter((x - center + 0.5) * ss) * ss;
k[x - (int) xmin] = w; k[x - (int) xmin] = w;