round values with initial .5

this improves performance and readabillity
This commit is contained in:
homm 2014-10-12 15:13:37 +04:00
parent bc24685344
commit e38a64b21b

View File

@ -10,15 +10,6 @@
#include "Imaging.h" #include "Imaging.h"
static inline UINT8 clip(double in)
{
if (in >= 255.0)
return (UINT8) 255;
if (in <= 0.0)
return (UINT8) 0;
return (UINT8) (in + 0.5);
}
static Imaging static Imaging
gblur(Imaging im, Imaging imOut, float radius, float effectiveScale, int channels) gblur(Imaging im, Imaging imOut, float radius, float effectiveScale, int channels)
{ {
@ -138,7 +129,7 @@ gblur(Imaging im, Imaging imOut, float radius, float effectiveScale, int channel
output image */ output image */
for (x = 0; x < im->xsize; x++) { for (x = 0; x < im->xsize; x++) {
for (y = 0; y < im->ysize; y++) { for (y = 0; y < im->ysize; y++) {
newPixel[0] = newPixel[1] = newPixel[2] = newPixel[3] = 0; newPixel[0] = newPixel[1] = newPixel[2] = newPixel[3] = .5;
/* for each neighbor pixel, factor in its value/weighting to the /* for each neighbor pixel, factor in its value/weighting to the
current pixel */ current pixel */
for (pix = 0; pix < window; pix++) { for (pix = 0; pix < window; pix++) {
@ -158,31 +149,23 @@ gblur(Imaging im, Imaging imOut, float radius, float effectiveScale, int channel
(x * channels) + channel]) * (maskData[pix]); (x * channels) + channel]) * (maskData[pix]);
} }
} }
/* if the image is RGBX or RGBA, copy the 4th channel data to
newPixel, so it gets put in imOut */
if (hasAlpha) {
newPixel[3] = (float) ((UINT8 *) & im->image32[y][x])[3];
}
/* pack the channels into an INT32 so we can put them back in
the PIL image */
newPixelFinals = 0;
if (channels == 1) { if (channels == 1) {
newPixelFinals = clip(newPixel[0]); imOut->image8[y][x] = (UINT8)(newPixel[0]);
} else { } else {
/* if the image is RGBX or RGBA, copy the 4th channel data to
newPixel, so it gets put in imOut */
if (hasAlpha) {
newPixel[3] = (float) ((UINT8 *) & im->image32[y][x])[3];
}
/* for RGB, the fourth channel isn't used anyways, so just /* for RGB, the fourth channel isn't used anyways, so just
pack a 0 in there, this saves checking the mode for each pack a 0 in there, this saves checking the mode for each
pixel. */ pixel. */
/* this doesn't work on little-endian machines... fix it! */ /* this might don't work on little-endian machines... fix it! */
newPixelFinals = imOut->image32[y][x] =
clip(newPixel[0]) | clip(newPixel[1]) << 8 | (UINT8)(newPixel[0]) | (UINT8)(newPixel[1]) << 8 |
clip(newPixel[2]) << 16 | clip(newPixel[3]) << 24; (UINT8)(newPixel[2]) << 16 | (UINT8)(newPixel[3]) << 24;
}
/* set the resulting pixel in imOut */
if (channels == 1) {
imOut->image8[y][x] = (UINT8) newPixelFinals;
} else {
imOut->image32[y][x] = newPixelFinals;
} }
} }
} }
@ -196,6 +179,15 @@ gblur(Imaging im, Imaging imOut, float radius, float effectiveScale, int channel
return imOut; return imOut;
} }
static inline UINT8 clip(double in)
{
if (in >= 255.0)
return (UINT8) 255;
if (in <= 0.0)
return (UINT8) 0;
return (UINT8) (in + 0.5);
}
Imaging ImagingGaussianBlur(Imaging im, Imaging imOut, float radius, Imaging ImagingGaussianBlur(Imaging im, Imaging imOut, float radius,
float effectiveScale) float effectiveScale)
{ {