From 0e9beed76df025928afab9d00360c7ece87b5db8 Mon Sep 17 00:00:00 2001 From: homm Date: Thu, 16 Jun 2016 20:04:20 +0300 Subject: [PATCH] add constants for filters: BOX, HAMMING, MITCHELL rearrange filters everywhere --- PIL/Image.py | 24 +++++++++++++++--------- libImaging/Imaging.h | 5 ++++- libImaging/Resample.c | 41 ++++++++++++++++++++--------------------- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 64f461358..ff78c84b7 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -168,13 +168,15 @@ MESH = 4 # resampling filters NEAREST = NONE = 0 -LANCZOS = ANTIALIAS = 1 +BOX = 4 BILINEAR = LINEAR = 2 +HAMMING = 5 BICUBIC = CUBIC = 3 +MITCHELL = 6 +LANCZOS = ANTIALIAS = 1 # dithers -NONE = 0 -NEAREST = 0 +NEAREST = NONE = 0 ORDERED = 1 # Not yet implemented RASTERIZE = 2 # Not yet implemented FLOYDSTEINBERG = 3 # default @@ -1518,16 +1520,20 @@ class Image(object): :param size: The requested size in pixels, as a 2-tuple: (width, height). :param resample: An optional resampling filter. This can be - one of :py:attr:`PIL.Image.NEAREST` (use nearest neighbour), - :py:attr:`PIL.Image.BILINEAR` (linear interpolation), - :py:attr:`PIL.Image.BICUBIC` (cubic spline interpolation), or - :py:attr:`PIL.Image.LANCZOS` (a high-quality downsampling filter). + one of :py:attr:`PIL.Image.NEAREST`, :py:attr:`PIL.Image.BOX`, + :py:attr:`PIL.Image.BILINEAR`, :py:attr:`PIL.Image.HAMMING`, + :py:attr:`PIL.Image.BICUBIC`, :py:attr:`PIL.Image.MITCHELL` or + :py:attr:`PIL.Image.LANCZOS`. If omitted, or if the image has mode "1" or "P", it is set :py:attr:`PIL.Image.NEAREST`. + See: :ref:`concept-filters`. :returns: An :py:class:`~PIL.Image.Image` object. """ - if resample not in (NEAREST, BILINEAR, BICUBIC, LANCZOS): + if resample not in ( + NEAREST, BILINEAR, BICUBIC, LANCZOS, + BOX, HAMMING, MITCHELL, + ): raise ValueError("unknown resampling filter") self.load() @@ -1560,7 +1566,7 @@ class Image(object): environment), or :py:attr:`PIL.Image.BICUBIC` (cubic spline interpolation in a 4x4 environment). If omitted, or if the image has mode "1" or "P", it is - set :py:attr:`PIL.Image.NEAREST`. + set :py:attr:`PIL.Image.NEAREST`. See :ref:`concept-filters`. :param expand: Optional expansion flag. If true, expands the output image to make it large enough to hold the entire rotated image. If false or omitted, make the output image the same size as the diff --git a/libImaging/Imaging.h b/libImaging/Imaging.h index adefbc65b..7c0d847cd 100644 --- a/libImaging/Imaging.h +++ b/libImaging/Imaging.h @@ -229,9 +229,12 @@ extern void ImagingError_Clear(void); /* standard filters */ #define IMAGING_TRANSFORM_NEAREST 0 -#define IMAGING_TRANSFORM_LANCZOS 1 +#define IMAGING_TRANSFORM_BOX 4 #define IMAGING_TRANSFORM_BILINEAR 2 +#define IMAGING_TRANSFORM_HAMMING 5 #define IMAGING_TRANSFORM_BICUBIC 3 +#define IMAGING_TRANSFORM_MITCHELL 6 +#define IMAGING_TRANSFORM_LANCZOS 1 typedef int (*ImagingTransformMap)(double* X, double* Y, int x, int y, void* data); diff --git a/libImaging/Resample.c b/libImaging/Resample.c index 23a83e151..01c9a2fcd 100644 --- a/libImaging/Resample.c +++ b/libImaging/Resample.c @@ -5,28 +5,11 @@ #define ROUND_UP(f) ((int) ((f) >= 0.0 ? (f) + 0.5F : (f) - 0.5F)) - struct filter { double (*filter)(double x); double support; }; -static inline double sinc_filter(double x) -{ - if (x == 0.0) - return 1.0; - x = x * M_PI; - return sin(x) / x; -} - -static inline double lanczos_filter(double x) -{ - /* truncated sinc */ - if (-3.0 <= x && x < 3.0) - return sinc_filter(x) * sinc_filter(x/3); - return 0.0; -} - static inline double bilinear_filter(double x) { if (x < 0.0) @@ -50,9 +33,25 @@ static inline double bicubic_filter(double x) #undef a } -static struct filter LANCZOS = { lanczos_filter, 3.0 }; +static inline double sinc_filter(double x) +{ + if (x == 0.0) + return 1.0; + x = x * M_PI; + return sin(x) / x; +} + +static inline double lanczos_filter(double x) +{ + /* truncated sinc */ + if (-3.0 <= x && x < 3.0) + return sinc_filter(x) * sinc_filter(x/3); + return 0.0; +} + static struct filter BILINEAR = { bilinear_filter, 1.0 }; static struct filter BICUBIC = { bicubic_filter, 2.0 }; +static struct filter LANCZOS = { lanczos_filter, 3.0 }; @@ -524,15 +523,15 @@ ImagingResample(Imaging imIn, int xsize, int ysize, int filter) /* check filter */ switch (filter) { - case IMAGING_TRANSFORM_LANCZOS: - filterp = &LANCZOS; - break; case IMAGING_TRANSFORM_BILINEAR: filterp = &BILINEAR; break; case IMAGING_TRANSFORM_BICUBIC: filterp = &BICUBIC; break; + case IMAGING_TRANSFORM_LANCZOS: + filterp = &LANCZOS; + break; default: return (Imaging) ImagingError_ValueError( "unsupported resampling filter"