Fix the wrong behavior of current sd_blurredImageWithRadius, which calculate the wrong box size for consolve

This commit is contained in:
DreamPiggy 2020-01-15 21:35:15 +08:00
parent e376dad5f6
commit b8b7438ce9
2 changed files with 5 additions and 6 deletions

View File

@ -569,12 +569,13 @@ static inline CGImageRef _Nullable SDCreateCGImageFromCIImage(CIImage * _Nonnull
return self;
}
CGFloat scale = self.scale;
CGFloat inputRadius = blurRadius * scale;
#if SD_UIKIT || SD_MAC
if (self.CIImage) {
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:self.CIImage forKey:kCIInputImageKey];
// Blur Radius use pixel count
[filter setValue:@(blurRadius / 2) forKey:kCIInputRadiusKey];
[filter setValue:@(inputRadius) forKey:kCIInputRadiusKey];
CIImage *ciImage = filter.outputImage;
ciImage = [ciImage imageByCroppingToRect:CGRectMake(0, 0, self.size.width, self.size.height)];
#if SD_UIKIT
@ -586,7 +587,6 @@ static inline CGImageRef _Nullable SDCreateCGImageFromCIImage(CIImage * _Nonnull
}
#endif
CGFloat scale = self.scale;
CGImageRef imageRef = self.CGImage;
//convert to BGRA if it isn't
@ -640,9 +640,8 @@ static inline CGImageRef _Nullable SDCreateCGImageFromCIImage(CIImage * _Nonnull
//
// ... if d is odd, use three box-blurs of size 'd', centered on the output pixel.
//
CGFloat inputRadius = blurRadius * scale;
if (inputRadius - 2.0 < __FLT_EPSILON__) inputRadius = 2.0;
uint32_t radius = floor((inputRadius * 3.0 * sqrt(2 * M_PI) / 4 + 0.5) / 2);
uint32_t radius = floor(inputRadius * 3.0 * sqrt(2 * M_PI) / 4 + 0.5);
radius |= 1; // force radius to be odd so that the three box-blur methodology works.
int iterations;
if (blurRadius * scale < 0.5) iterations = 1;

View File

@ -172,7 +172,7 @@
}
- (void)test07UIImageTransformBlurWithImage:(UIImage *)testImage {
CGFloat radius = 50;
CGFloat radius = 25;
UIImage *blurredImage = [testImage sd_blurredImageWithRadius:radius];
expect(CGSizeEqualToSize(blurredImage.size, testImage.size)).beTruthy();
// Check left color, should be blurred