mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-10 16:22:22 +03:00
two ImagingStretchHorizaontal pass with two transposes
This commit is contained in:
parent
c8471bcbda
commit
40f9f48680
|
@ -79,7 +79,7 @@ static inline float bicubic_filter(float x)
|
||||||
static struct filter BICUBIC = { bicubic_filter, 2.0 };
|
static struct filter BICUBIC = { bicubic_filter, 2.0 };
|
||||||
|
|
||||||
Imaging
|
Imaging
|
||||||
ImagingStretchPass(Imaging imOut, Imaging imIn, int filter)
|
ImagingStretchHorizaontal(Imaging imOut, Imaging imIn, int filter)
|
||||||
{
|
{
|
||||||
/* FIXME: this is a quick and straightforward translation from a
|
/* FIXME: this is a quick and straightforward translation from a
|
||||||
python prototype. might need some further C-ification... */
|
python prototype. might need some further C-ification... */
|
||||||
|
@ -87,14 +87,19 @@ ImagingStretchPass(Imaging imOut, Imaging imIn, int filter)
|
||||||
ImagingSectionCookie cookie;
|
ImagingSectionCookie cookie;
|
||||||
struct filter *filterp;
|
struct filter *filterp;
|
||||||
float support, scale, filterscale;
|
float support, scale, filterscale;
|
||||||
float center, ww, ss, ymin, ymax, xmin, xmax;
|
float center, ww, ss, xmin, xmax;
|
||||||
int xx, yy, x, y, b;
|
int xx, yy, x, b;
|
||||||
float *k;
|
float *k;
|
||||||
|
|
||||||
/* 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 (imOut->ysize != imIn->ysize)
|
||||||
|
return (Imaging) ImagingError_ValueError(
|
||||||
|
"ImagingStretchHorizaontal requires equal heights"
|
||||||
|
);
|
||||||
|
|
||||||
/* check filter */
|
/* check filter */
|
||||||
switch (filter) {
|
switch (filter) {
|
||||||
case IMAGING_TRANSFORM_NEAREST:
|
case IMAGING_TRANSFORM_NEAREST:
|
||||||
|
@ -115,14 +120,8 @@ ImagingStretchPass(Imaging imOut, Imaging imIn, int filter)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (imIn->ysize == imOut->ysize) {
|
/* prepare for horizontal stretch */
|
||||||
/* prepare for horizontal stretch */
|
filterscale = scale = (float) imIn->xsize / imOut->xsize;
|
||||||
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) */
|
/* determine support size (length of resampling filter) */
|
||||||
support = filterp->support;
|
support = filterp->support;
|
||||||
|
@ -139,162 +138,83 @@ ImagingStretchPass(Imaging imOut, Imaging imIn, int filter)
|
||||||
return (Imaging) ImagingError_MemoryError();
|
return (Imaging) ImagingError_MemoryError();
|
||||||
|
|
||||||
ImagingSectionEnter(&cookie);
|
ImagingSectionEnter(&cookie);
|
||||||
if (imIn->xsize == imOut->xsize) {
|
/* horizontal stretch */
|
||||||
/* vertical stretch */
|
for (xx = 0; xx < imOut->xsize; xx++) {
|
||||||
for (yy = 0; yy < imOut->ysize; yy++) {
|
center = (xx + 0.5) * scale;
|
||||||
center = (yy + 0.5) * scale;
|
ww = 0.0;
|
||||||
ww = 0.0;
|
ss = 1.0 / filterscale;
|
||||||
ss = 1.0 / filterscale;
|
xmin = floor(center - support);
|
||||||
/* calculate filter weights */
|
if (xmin < 0.0)
|
||||||
ymin = floor(center - support);
|
xmin = 0.0;
|
||||||
if (ymin < 0.0)
|
xmax = ceil(center + support);
|
||||||
ymin = 0.0;
|
if (xmax > (float) imIn->xsize)
|
||||||
ymax = ceil(center + support);
|
xmax = (float) imIn->xsize;
|
||||||
if (ymax > (float) imIn->ysize)
|
for (x = (int) xmin; x < (int) xmax; x++) {
|
||||||
ymax = (float) imIn->ysize;
|
float w = filterp->filter((x - center + 0.5) * ss) * ss;
|
||||||
for (y = (int) ymin; y < (int) ymax; y++) {
|
k[x - (int) xmin] = w;
|
||||||
float w = filterp->filter((y - center + 0.5) * ss) * ss;
|
ww = ww + w;
|
||||||
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 (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;
|
||||||
}
|
}
|
||||||
if (ww == 0.0)
|
} else
|
||||||
ww = 1.0;
|
switch(imIn->type) {
|
||||||
else
|
case IMAGING_TYPE_UINT8:
|
||||||
ww = 1.0 / ww;
|
/* n-bit grayscale */
|
||||||
if (imIn->image8) {
|
for (yy = 0; yy < imOut->ysize; yy++) {
|
||||||
/* 8-bit grayscale */
|
for (b = 0; b < imIn->bands; b++) {
|
||||||
for (xx = 0; xx < imOut->xsize; xx++) {
|
if (imIn->bands == 2 && b)
|
||||||
ss = 0.0;
|
b = 3; /* hack to deal with LA images */
|
||||||
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;
|
ss = 0.0;
|
||||||
for (y = (int) ymin; y < (int) ymax; y++)
|
for (x = (int) xmin; x < (int) xmax; x++)
|
||||||
ss = ss + (UINT8) imIn->image[y][xx] * k[y-(int) ymin];
|
ss = ss + (UINT8) imIn->image[yy][x*4+b] * k[x - (int) xmin];
|
||||||
ss = ss * ww + 0.5;
|
ss = ss * ww + 0.5;
|
||||||
if (ss < 0.5)
|
if (ss < 0.5)
|
||||||
imOut->image[yy][xx] = (UINT8) 0;
|
imOut->image[yy][xx*4+b] = (UINT8) 0;
|
||||||
else if (ss >= 255.0)
|
else if (ss >= 255.0)
|
||||||
imOut->image[yy][xx] = (UINT8) 255;
|
imOut->image[yy][xx*4+b] = (UINT8) 255;
|
||||||
else
|
else
|
||||||
imOut->image[yy][xx] = (UINT8) ss;
|
imOut->image[yy][xx*4+b] = (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) = (int) 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();
|
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
} else {
|
case IMAGING_TYPE_INT32:
|
||||||
/* horizontal stretch */
|
/* 32-bit integer */
|
||||||
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 > (float) imIn->xsize)
|
|
||||||
xmax = (float) 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++) {
|
for (yy = 0; yy < imOut->ysize; yy++) {
|
||||||
ss = 0.0;
|
ss = 0.0;
|
||||||
for (x = (int) xmin; x < (int) xmax; x++)
|
for (x = (int) xmin; x < (int) xmax; x++)
|
||||||
ss = ss + imIn->image8[yy][x] * k[x - (int) xmin];
|
ss = ss + IMAGING_PIXEL_I(imIn, x, yy) * k[x - (int) xmin];
|
||||||
ss = ss * ww + 0.5;
|
IMAGING_PIXEL_I(imOut, xx, yy) = (int) ss * ww;
|
||||||
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
|
break;
|
||||||
switch(imIn->type) {
|
case IMAGING_TYPE_FLOAT32:
|
||||||
case IMAGING_TYPE_UINT8:
|
/* 32-bit float */
|
||||||
/* n-bit grayscale */
|
for (yy = 0; yy < imOut->ysize; yy++) {
|
||||||
for (yy = 0; yy < imOut->ysize; yy++) {
|
ss = 0.0;
|
||||||
for (b = 0; b < imIn->bands; b++) {
|
for (x = (int) xmin; x < (int) xmax; x++)
|
||||||
if (imIn->bands == 2 && b)
|
ss = ss + IMAGING_PIXEL_F(imIn, x, yy) * k[x - (int) xmin];
|
||||||
b = 3; /* hack to deal with LA images */
|
IMAGING_PIXEL_F(imOut, xx, yy) = ss * ww;
|
||||||
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) = (int) 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();
|
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
|
default:
|
||||||
|
ImagingSectionLeave(&cookie);
|
||||||
|
return (Imaging) ImagingError_ModeError();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ImagingSectionLeave(&cookie);
|
ImagingSectionLeave(&cookie);
|
||||||
|
|
||||||
|
@ -307,34 +227,58 @@ ImagingStretchPass(Imaging imOut, Imaging imIn, int filter)
|
||||||
Imaging
|
Imaging
|
||||||
ImagingStretch(Imaging imOut, Imaging imIn, int filter)
|
ImagingStretch(Imaging imOut, Imaging imIn, int filter)
|
||||||
{
|
{
|
||||||
Imaging imTemp;
|
Imaging imTemp1, imTemp2, imTemp3;
|
||||||
int xsize = imOut->xsize;
|
int xsize = imOut->xsize;
|
||||||
int ysize = imOut->ysize;
|
int ysize = imOut->ysize;
|
||||||
|
|
||||||
if (strcmp(imIn->mode, "P") == 0 || strcmp(imIn->mode, "1") == 0)
|
if (strcmp(imIn->mode, "P") == 0 || strcmp(imIn->mode, "1") == 0)
|
||||||
return (Imaging) ImagingError_ModeError();
|
return (Imaging) ImagingError_ModeError();
|
||||||
|
|
||||||
/* two-pass resize: minimize size of intermediate image */
|
/* two-pass resize */
|
||||||
if ((Py_ssize_t) imIn->xsize * ysize < (Py_ssize_t) xsize * imIn->ysize)
|
imTemp1 = ImagingNew(imIn->mode, xsize, imIn->ysize);
|
||||||
imTemp = ImagingNew(imIn->mode, imIn->xsize, ysize);
|
if ( ! imTemp1)
|
||||||
else
|
|
||||||
imTemp = ImagingNew(imIn->mode, xsize, imIn->ysize);
|
|
||||||
if ( ! imTemp)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
/* first pass */
|
/* first pass */
|
||||||
if ( ! ImagingStretchPass(imTemp, imIn, filter)) {
|
if ( ! ImagingStretchHorizaontal(imTemp1, imIn, filter)) {
|
||||||
ImagingDelete(imTemp);
|
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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* second pass */
|
/* second pass */
|
||||||
if ( ! ImagingStretchPass(imOut, imTemp, filter)) {
|
if ( ! ImagingStretchHorizaontal(imTemp3, imTemp2, filter)) {
|
||||||
ImagingDelete(imTemp);
|
ImagingDelete(imTemp2);
|
||||||
|
ImagingDelete(imTemp3);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
ImagingDelete(imTemp2);
|
||||||
|
|
||||||
ImagingDelete(imTemp);
|
/* transpose result */
|
||||||
|
if ( ! ImagingTranspose(imOut, imTemp3)) {
|
||||||
|
ImagingDelete(imTemp3);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
ImagingDelete(imTemp3);
|
||||||
|
|
||||||
return imOut;
|
return imOut;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user