Pillow/libImaging/Antialias.c

312 lines
8.5 KiB
C
Raw Normal View History

2010-07-31 06:52:47 +04:00
/*
* The Python Imaging Library
* $Id$
*
* pilopen antialiasing support
2010-07-31 06:52:47 +04:00
*
* history:
* 2002-03-09 fl Created (for PIL 1.1.3)
* 2002-03-10 fl Added support for mode "F"
*
* Copyright (c) 1997-2002 by Secret Labs AB
*
* See the README file for information on usage and redistribution.
*/
#include "Imaging.h"
#include <math.h>
/* resampling filters (from antialias.py) */
struct filter {
float (*filter)(float x);
float support;
};
static inline float sinc_filter(float x)
{
if (x == 0.0)
return 1.0;
x = x * M_PI;
return sin(x) / x;
}
static inline float antialias_filter(float x)
{
/* lanczos (truncated sinc) */
if (-3.0 <= x && x < 3.0)
return sinc_filter(x) * sinc_filter(x/3);
return 0.0;
}
static struct filter ANTIALIAS = { antialias_filter, 3.0 };
static inline float nearest_filter(float x)
{
if (-0.5 <= x && x < 0.5)
return 1.0;
return 0.0;
}
static struct filter NEAREST = { nearest_filter, 0.5 };
static inline float bilinear_filter(float x)
{
if (x < 0.0)
x = -x;
if (x < 1.0)
return 1.0-x;
return 0.0;
}
static struct filter BILINEAR = { bilinear_filter, 1.0 };
static inline float bicubic_filter(float x)
{
2014-10-24 12:46:51 +04:00
/* http://en.wikipedia.org/wiki/Bicubic_interpolation#Bicubic_convolution_algorithm */
2014-10-25 12:39:03 +04:00
#define a -0.5
2010-07-31 06:52:47 +04:00
if (x < 0.0)
x = -x;
if (x < 1.0)
2014-10-24 12:46:51 +04:00
return ((a + 2.0) * x - (a + 3.0)) * x*x + 1;
2010-07-31 06:52:47 +04:00
if (x < 2.0)
2014-10-24 12:46:51 +04:00
return (((x - 5) * x + 8) * x - 4) * a;
2010-07-31 06:52:47 +04:00
return 0.0;
#undef a
}
static struct filter BICUBIC = { bicubic_filter, 2.0 };
Imaging
ImagingStretchHorizaontal(Imaging imOut, Imaging imIn, int filter)
2010-07-31 06:52:47 +04:00
{
/* FIXME: this is a quick and straightforward translation from a
python prototype. might need some further C-ification... */
ImagingSectionCookie cookie;
struct filter *filterp;
float support, scale, filterscale;
2014-10-25 05:57:08 +04:00
float center, ww, ss;
int xx, yy, x, b, kmax, xmin, xmax;
int *xbounds;
float *k, *kk;
2010-07-31 06:52:47 +04:00
/* check modes */
if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0)
2014-10-25 03:53:36 +04:00
return (Imaging) ImagingError_ModeError();
2014-10-25 14:23:07 +04:00
if (imOut->ysize != imIn->ysize)
return (Imaging) ImagingError_ValueError(
"ImagingStretchHorizaontal requires equal heights"
);
2010-07-31 06:52:47 +04:00
/* check filter */
switch (filter) {
case IMAGING_TRANSFORM_NEAREST:
filterp = &NEAREST;
break;
case IMAGING_TRANSFORM_ANTIALIAS:
filterp = &ANTIALIAS;
break;
case IMAGING_TRANSFORM_BILINEAR:
filterp = &BILINEAR;
break;
case IMAGING_TRANSFORM_BICUBIC:
filterp = &BICUBIC;
break;
default:
return (Imaging) ImagingError_ValueError(
"unsupported resampling filter"
);
}
/* prepare for horizontal stretch */
filterscale = scale = (float) imIn->xsize / imOut->xsize;
2010-07-31 06:52:47 +04:00
/* determine support size (length of resampling filter) */
support = filterp->support;
if (filterscale < 1.0) {
filterscale = 1.0;
}
2010-07-31 06:52:47 +04:00
support = support * filterscale;
2014-10-25 05:37:33 +04:00
/* maximum number of coofs */
kmax = (int) ceil(support) * 2 + 1;
2010-07-31 06:52:47 +04:00
/* coefficient buffer (with rounding safety margin) */
2014-10-25 05:37:33 +04:00
kk = malloc(imOut->xsize * kmax * sizeof(float));
if ( ! kk)
return (Imaging) ImagingError_MemoryError();
2014-10-25 05:57:08 +04:00
xbounds = malloc(imOut->xsize * 2 * sizeof(int));
2014-10-25 05:37:33 +04:00
if ( ! xbounds) {
free(kk);
2010-07-31 06:52:47 +04:00
return (Imaging) ImagingError_MemoryError();
2014-10-25 05:37:33 +04:00
}
for (xx = 0; xx < imOut->xsize; xx++) {
k = &kk[xx * kmax];
center = (xx + 0.5) * scale;
ww = 0.0;
ss = 1.0 / filterscale;
2014-10-25 05:57:08 +04:00
xmin = (int) floor(center - support);
if (xmin < 0)
xmin = 0;
xmax = (int) ceil(center + support);
if (xmax > imIn->xsize)
xmax = imIn->xsize;
for (x = xmin; x < xmax; x++) {
2014-10-25 05:37:33 +04:00
float w = filterp->filter((x - center + 0.5) * ss) * ss;
2014-10-25 05:57:08 +04:00
k[x - xmin] = w;
2014-10-25 05:50:54 +04:00
ww += w;
2014-10-25 05:37:33 +04:00
}
2014-10-25 05:57:08 +04:00
for (x = 0; x < xmax - xmin; x++) {
2014-10-25 05:50:54 +04:00
if (ww != 0.0)
k[x] /= ww;
}
xbounds[xx * 2 + 0] = xmin;
xbounds[xx * 2 + 1] = xmax;
2014-10-25 05:37:33 +04:00
}
2010-07-31 06:52:47 +04:00
ImagingSectionEnter(&cookie);
/* horizontal stretch */
2014-10-25 05:41:38 +04:00
for (yy = 0; yy < imOut->ysize; yy++) {
if (imIn->image8) {
/* 8-bit grayscale */
2014-10-25 05:41:38 +04:00
for (xx = 0; xx < imOut->xsize; xx++) {
2014-10-25 05:50:54 +04:00
xmin = xbounds[xx * 2 + 0];
xmax = xbounds[xx * 2 + 1];
2014-10-25 05:41:38 +04:00
k = &kk[xx * kmax];
2014-10-25 05:50:54 +04:00
ss = 0.5;
2014-10-25 05:57:08 +04:00
for (x = xmin; x < xmax; x++)
ss = ss + imIn->image8[yy][x] * k[x - xmin];
if (ss < 0.5)
imOut->image8[yy][xx] = (UINT8) 0;
else if (ss >= 255.0)
imOut->image8[yy][xx] = (UINT8) 255;
else
imOut->image8[yy][xx] = (UINT8) ss;
2010-07-31 06:52:47 +04:00
}
} else
switch(imIn->type) {
case IMAGING_TYPE_UINT8:
/* n-bit grayscale */
2014-10-25 05:41:38 +04:00
for (xx = 0; xx < imOut->xsize; xx++) {
2014-10-25 05:50:54 +04:00
xmin = xbounds[xx * 2 + 0];
xmax = xbounds[xx * 2 + 1];
2014-10-25 05:41:38 +04:00
k = &kk[xx * kmax];
for (b = 0; b < imIn->bands; b++) {
if (imIn->bands == 2 && b)
b = 3; /* hack to deal with LA images */
2014-10-25 05:50:54 +04:00
ss = 0.5;
2014-10-25 05:57:08 +04:00
for (x = xmin; x < xmax; x++)
ss = ss + (UINT8) imIn->image[yy][x*4+b] * k[x - xmin];
2010-07-31 06:52:47 +04:00
if (ss < 0.5)
imOut->image[yy][xx*4+b] = (UINT8) 0;
2010-07-31 06:52:47 +04:00
else if (ss >= 255.0)
imOut->image[yy][xx*4+b] = (UINT8) 255;
2010-07-31 06:52:47 +04:00
else
imOut->image[yy][xx*4+b] = (UINT8) ss;
2010-07-31 06:52:47 +04:00
}
}
break;
case IMAGING_TYPE_INT32:
/* 32-bit integer */
2014-10-25 05:41:38 +04:00
for (xx = 0; xx < imOut->xsize; xx++) {
2014-10-25 05:50:54 +04:00
xmin = xbounds[xx * 2 + 0];
xmax = xbounds[xx * 2 + 1];
2014-10-25 05:41:38 +04:00
k = &kk[xx * kmax];
2010-07-31 06:52:47 +04:00
ss = 0.0;
2014-10-25 05:57:08 +04:00
for (x = xmin; x < xmax; x++)
ss = ss + IMAGING_PIXEL_I(imIn, x, yy) * k[x - xmin];
2014-10-25 05:50:54 +04:00
IMAGING_PIXEL_I(imOut, xx, yy) = (int) ss;
2010-07-31 06:52:47 +04:00
}
break;
case IMAGING_TYPE_FLOAT32:
/* 32-bit float */
2014-10-25 05:41:38 +04:00
for (xx = 0; xx < imOut->xsize; xx++) {
2014-10-25 05:50:54 +04:00
xmin = xbounds[xx * 2 + 0];
xmax = xbounds[xx * 2 + 1];
2014-10-25 05:41:38 +04:00
k = &kk[xx * kmax];
ss = 0.0;
2014-10-25 05:57:08 +04:00
for (x = xmin; x < xmax; x++)
ss = ss + IMAGING_PIXEL_F(imIn, x, yy) * k[x - xmin];
2014-10-25 05:50:54 +04:00
IMAGING_PIXEL_F(imOut, xx, yy) = ss;
2010-07-31 06:52:47 +04:00
}
break;
default:
ImagingSectionLeave(&cookie);
return (Imaging) ImagingError_ModeError();
}
2010-07-31 06:52:47 +04:00
}
ImagingSectionLeave(&cookie);
2014-10-25 05:37:33 +04:00
free(kk);
free(xbounds);
2010-07-31 06:52:47 +04:00
return imOut;
}
Imaging
ImagingStretch(Imaging imOut, Imaging imIn, int filter)
{
Imaging imTemp1, imTemp2, imTemp3;
int xsize = imOut->xsize;
int ysize = imOut->ysize;
if (strcmp(imIn->mode, "P") == 0 || strcmp(imIn->mode, "1") == 0)
return (Imaging) ImagingError_ModeError();
/* two-pass resize */
imTemp1 = ImagingNew(imIn->mode, xsize, imIn->ysize);
if ( ! imTemp1)
return NULL;
/* first pass */
if ( ! ImagingStretchHorizaontal(imTemp1, imIn, filter)) {
ImagingDelete(imTemp1);
return NULL;
}
imTemp2 = ImagingNew(imIn->mode, imIn->ysize, xsize);
if ( ! imTemp2) {
ImagingDelete(imTemp1);
return NULL;
}
/* transpose image once */
if ( ! ImagingTranspose(imTemp2, imTemp1)) {
ImagingDelete(imTemp1);
ImagingDelete(imTemp2);
return NULL;
}
ImagingDelete(imTemp1);
imTemp3 = ImagingNew(imIn->mode, ysize, xsize);
if ( ! imTemp3) {
ImagingDelete(imTemp2);
return NULL;
}
/* second pass */
if ( ! ImagingStretchHorizaontal(imTemp3, imTemp2, filter)) {
ImagingDelete(imTemp2);
ImagingDelete(imTemp3);
return NULL;
}
ImagingDelete(imTemp2);
/* transpose result */
if ( ! ImagingTranspose(imOut, imTemp3)) {
ImagingDelete(imTemp3);
return NULL;
}
ImagingDelete(imTemp3);
return imOut;
}