Fredrik Lundh is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againpil-2009-raclette / libImaging / Antialias.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | /*
* The Python Imaging Library
* $Id: Antialias.c 2408 2005-05-15 09:31:27Z Fredrik $
*
* pilopen antialiasing support
*
* 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)
{
/* FIXME: double-check this algorithm */
/* FIXME: for best results, "a" should be -0.5 to -1.0, but we'll
set it to zero for now, to match the 1.1 magnifying filter */
#define a 0.0
if (x < 0.0)
x = -x;
if (x < 1.0)
return (((a + 2.0) * x) - (a + 3.0)) * x*x + 1;
if (x < 2.0)
return (((a * x) - 5*a) * x + 8) * x - 4*a;
return 0.0;
#undef a
}
static struct filter BICUBIC = { bicubic_filter, 2.0 };
Imaging
ImagingStretch(Imaging imOut, Imaging imIn, int filter)
{
/* 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;
float center, ww, ss, ymin, ymax, xmin, xmax;
int xx, yy, x, y, b;
float *k;
/* check modes */
if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0)
return (Imaging) ImagingError_ModeError();
/* 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"
);
}
if (imIn->ysize == imOut->ysize) {
/* prepare for horizontal stretch */
filterscale = scale = (float) imIn->xsize / imOut->xsize;
} else if (imIn->xsize == imOut->xsize) {
/* prepare for vertical stretch */
filterscale = scale = (float) imIn->ysize / imOut->ysize;
} else
return (Imaging) ImagingError_Mismatch();
/* determine support size (length of resampling filter) */
support = filterp->support;
if (filterscale < 1.0) {
filterscale = 1.0;
support = 0.5;
}
support = support * filterscale;
/* coefficient buffer (with rounding safety margin) */
k = malloc(((int) support * 2 + 10) * sizeof(float));
if (!k)
return (Imaging) ImagingError_MemoryError();
ImagingSectionEnter(&cookie);
if (imIn->xsize == imOut->xsize) {
/* vertical stretch */
for (yy = 0; yy < imOut->ysize; yy++) {
center = (yy + 0.5) * scale;
ww = 0.0;
ss = 1.0 / filterscale;
/* calculate filter weights */
ymin = floor(center - support);
if (ymin < 0.0)
ymin = 0.0;
ymax = ceil(center + support);
if (ymax > imIn->ysize)
ymax = imIn->ysize;
for (y = (int) ymin; y < (int) ymax; y++) {
float w = filterp->filter((y - center + 0.5) * ss) * ss;
k[y - (int) ymin] = w;
ww = ww + w;
}
if (ww == 0.0)
ww = 1.0;
else
ww = 1.0 / ww;
if (imIn->image8) {
/* 8-bit grayscale */
for (xx = 0; xx < imOut->xsize; xx++) {
ss = 0.0;
for (y = (int) ymin; y < (int) ymax; y++)
ss = ss + imIn->image8[y][xx] * k[y - (int) ymin];
ss = ss * ww + 0.5;
if (ss < 0.5)
imOut->image8[yy][xx] = 0;
else if (ss >= 255.0)
imOut->image8[yy][xx] = 255;
else
imOut->image8[yy][xx] = (UINT8) ss;
}
} else
switch(imIn->type) {
case IMAGING_TYPE_UINT8:
/* n-bit grayscale */
for (xx = 0; xx < imOut->xsize*4; xx++) {
/* FIXME: skip over unused pixels */
ss = 0.0;
for (y = (int) ymin; y < (int) ymax; y++)
ss = ss + (UINT8) imIn->image[y][xx] * k[y-(int) ymin];
ss = ss * ww + 0.5;
if (ss < 0.5)
imOut->image[yy][xx] = (UINT8) 0;
else if (ss >= 255.0)
imOut->image[yy][xx] = (UINT8) 255;
else
imOut->image[yy][xx] = (UINT8) ss;
}
break;
case IMAGING_TYPE_INT32:
/* 32-bit integer */
for (xx = 0; xx < imOut->xsize; xx++) {
ss = 0.0;
for (y = (int) ymin; y < (int) ymax; y++)
ss = ss + IMAGING_PIXEL_I(imIn, xx, y) * k[y - (int) ymin];
IMAGING_PIXEL_I(imOut, xx, yy) = ss * ww;
}
break;
case IMAGING_TYPE_FLOAT32:
/* 32-bit float */
for (xx = 0; xx < imOut->xsize; xx++) {
ss = 0.0;
for (y = (int) ymin; y < (int) ymax; y++)
ss = ss + IMAGING_PIXEL_F(imIn, xx, y) * k[y - (int) ymin];
IMAGING_PIXEL_F(imOut, xx, yy) = ss * ww;
}
break;
default:
ImagingSectionLeave(&cookie);
return (Imaging) ImagingError_ModeError();
}
}
} else {
/* horizontal stretch */
for (xx = 0; xx < imOut->xsize; xx++) {
center = (xx + 0.5) * scale;
ww = 0.0;
ss = 1.0 / filterscale;
xmin = floor(center - support);
if (xmin < 0.0)
xmin = 0.0;
xmax = ceil(center + support);
if (xmax > imIn->xsize)
xmax = imIn->xsize;
for (x = (int) xmin; x < (int) xmax; x++) {
float w = filterp->filter((x - center + 0.5) * ss) * ss;
k[x - (int) xmin] = w;
ww = ww + w;
}
if (ww == 0.0)
ww = 1.0;
else
ww = 1.0 / ww;
if (imIn->image8) {
/* 8-bit grayscale */
for (yy = 0; yy < imOut->ysize; yy++) {
ss = 0.0;
for (x = (int) xmin; x < (int) xmax; x++)
ss = ss + imIn->image8[yy][x] * k[x - (int) xmin];
ss = ss * ww + 0.5;
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;
}
} else
switch(imIn->type) {
case IMAGING_TYPE_UINT8:
/* n-bit grayscale */
for (yy = 0; yy < imOut->ysize; yy++) {
for (b = 0; b < imIn->bands; b++) {
if (imIn->bands == 2 && b)
b = 3; /* hack to deal with LA images */
ss = 0.0;
for (x = (int) xmin; x < (int) xmax; x++)
ss = ss + (UINT8) imIn->image[yy][x*4+b] * k[x - (int) xmin];
ss = ss * ww + 0.5;
if (ss < 0.5)
imOut->image[yy][xx*4+b] = (UINT8) 0;
else if (ss >= 255.0)
imOut->image[yy][xx*4+b] = (UINT8) 255;
else
imOut->image[yy][xx*4+b] = (UINT8) ss;
}
}
break;
case IMAGING_TYPE_INT32:
/* 32-bit integer */
for (yy = 0; yy < imOut->ysize; yy++) {
ss = 0.0;
for (x = (int) xmin; x < (int) xmax; x++)
ss = ss + IMAGING_PIXEL_I(imIn, x, yy) * k[x - (int) xmin];
IMAGING_PIXEL_I(imOut, xx, yy) = ss * ww;
}
break;
case IMAGING_TYPE_FLOAT32:
/* 32-bit float */
for (yy = 0; yy < imOut->ysize; yy++) {
ss = 0.0;
for (x = (int) xmin; x < (int) xmax; x++)
ss = ss + IMAGING_PIXEL_F(imIn, x, yy) * k[x - (int) xmin];
IMAGING_PIXEL_F(imOut, xx, yy) = ss * ww;
}
break;
default:
ImagingSectionLeave(&cookie);
return (Imaging) ImagingError_ModeError();
}
}
}
ImagingSectionLeave(&cookie);
free(k);
return imOut;
}
|