Merge pull request #65 from SDWebImage/fix_encode_maxPixelSize_5_13

Fix the encoding maxPixelSize logic using 5.13.0 API
This commit is contained in:
DreamPiggy 2022-07-03 17:56:03 +08:00 committed by GitHub
commit c3797b9992
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 42 deletions

View File

@ -1,2 +1,2 @@
github "SDWebImage/SDWebImage" ~> 5.10 github "SDWebImage/SDWebImage" ~> 5.13
github "SDWebImage/libwebp-Xcode" ~> 1.0 github "SDWebImage/libwebp-Xcode" ~> 1.0

View File

@ -17,7 +17,7 @@ let package = Package(
dependencies: [ dependencies: [
// Dependencies declare other packages that this package depends on. // Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"), // .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/SDWebImage/SDWebImage.git", from: "5.10.0"), .package(url: "https://github.com/SDWebImage/SDWebImage.git", from: "5.13.0"),
.package(url: "https://github.com/SDWebImage/libwebp-Xcode.git", from: "1.1.0") .package(url: "https://github.com/SDWebImage/libwebp-Xcode.git", from: "1.1.0")
], ],
targets: [ targets: [

View File

@ -8,6 +8,7 @@ target 'SDWebImageWebPCoderExample' do
platform :ios, '9.0' platform :ios, '9.0'
project example_project_path project example_project_path
pod 'SDWebImageWebPCoder', :path => './' pod 'SDWebImageWebPCoder', :path => './'
pod 'SDWebImage', :path => '../SDWebImage'
end end
target 'SDWebImageWebPCoderTests' do target 'SDWebImageWebPCoderTests' do
@ -15,4 +16,5 @@ target 'SDWebImageWebPCoderTests' do
project test_project_path project test_project_path
pod 'Expecta' pod 'Expecta'
pod 'SDWebImageWebPCoder', :path => './' pod 'SDWebImageWebPCoder', :path => './'
pod 'SDWebImage', :path => '../SDWebImage'
end end

View File

@ -27,7 +27,7 @@ This is a SDWebImage coder plugin to support WebP image.
'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) SD_WEBP=1 WEBP_USE_INTRINSICS=1', 'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) SD_WEBP=1 WEBP_USE_INTRINSICS=1',
'USER_HEADER_SEARCH_PATHS' => '$(inherited) $(SRCROOT)/libwebp/src' 'USER_HEADER_SEARCH_PATHS' => '$(inherited) $(SRCROOT)/libwebp/src'
} }
s.dependency 'SDWebImage/Core', '~> 5.10' s.dependency 'SDWebImage/Core', '~> 5.13'
s.dependency 'libwebp', '~> 1.0' s.dependency 'libwebp', '~> 1.0'
end end

View File

@ -68,38 +68,6 @@ else OSSpinLockUnlock(&lock##_deprecated);
#endif #endif
#endif #endif
/// Calculate the actual thumnail pixel size
static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio, CGSize thumbnailSize) {
CGFloat width = fullSize.width;
CGFloat height = fullSize.height;
CGFloat resultWidth;
CGFloat resultHeight;
if (width == 0 || height == 0 || thumbnailSize.width == 0 || thumbnailSize.height == 0 || (width <= thumbnailSize.width && height <= thumbnailSize.height)) {
// Full Pixel
resultWidth = width;
resultHeight = height;
} else {
// Thumbnail
if (preserveAspectRatio) {
CGFloat pixelRatio = width / height;
CGFloat thumbnailRatio = thumbnailSize.width / thumbnailSize.height;
if (pixelRatio > thumbnailRatio) {
resultWidth = thumbnailSize.width;
resultHeight = ceil(thumbnailSize.width / pixelRatio);
} else {
resultHeight = thumbnailSize.height;
resultWidth = ceil(thumbnailSize.height * pixelRatio);
}
} else {
resultWidth = thumbnailSize.width;
resultHeight = thumbnailSize.height;
}
}
return CGSizeMake(resultWidth, resultHeight);
}
@interface SDWebPCoderFrame : NSObject @interface SDWebPCoderFrame : NSObject
@property (nonatomic, assign) NSUInteger index; // Frame index (zero based) @property (nonatomic, assign) NSUInteger index; // Frame index (zero based)
@ -231,7 +199,7 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio
int canvasWidth = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_WIDTH); int canvasWidth = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_WIDTH);
int canvasHeight = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_HEIGHT); int canvasHeight = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_HEIGHT);
// Check whether we need to use thumbnail // Check whether we need to use thumbnail
CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(canvasWidth, canvasHeight), preserveAspectRatio, thumbnailSize); CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(canvasWidth, canvasHeight) scaleSize:thumbnailSize preserveAspectRatio:preserveAspectRatio shouldScaleUp:NO];
if (!hasAnimation || decodeFirstFrame) { if (!hasAnimation || decodeFirstFrame) {
// first frame for animated webp image // first frame for animated webp image
@ -437,7 +405,7 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio
scale = 1; scale = 1;
} }
} }
CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(width, height), _preserveAspectRatio, _thumbnailSize); CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(width, height) scaleSize:_thumbnailSize preserveAspectRatio:_preserveAspectRatio shouldScaleUp:NO];
// Check whether we need to use thumbnail // Check whether we need to use thumbnail
if (!CGSizeEqualToSize(CGSizeMake(width, height), scaledSize)) { if (!CGSizeEqualToSize(CGSizeMake(width, height), scaledSize)) {
CGImageRef scaledImageRef = [SDImageCoderHelper CGImageCreateScaled:newImageRef size:scaledSize]; CGImageRef scaledImageRef = [SDImageCoderHelper CGImageCreateScaled:newImageRef size:scaledSize];
@ -860,8 +828,8 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio
} }
// Check if need to scale pixel size // Check if need to scale pixel size
if (maxPixelSize.width > 0 && maxPixelSize.height > 0 && (width > maxPixelSize.width || height > maxPixelSize.height)) { CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(width, height) scaleSize:maxPixelSize preserveAspectRatio:YES shouldScaleUp:NO];
CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(width, height), YES, maxPixelSize); if (!CGSizeEqualToSize(scaledSize, CGSizeMake(width, height))) {
result = WebPPictureRescale(&picture, scaledSize.width, scaledSize.height); result = WebPPictureRescale(&picture, scaledSize.width, scaledSize.height);
if (!result) { if (!result) {
WebPMemoryWriterClear(&writer); WebPMemoryWriterClear(&writer);
@ -1132,10 +1100,10 @@ static float GetFloatValueForKey(NSDictionary * _Nonnull dictionary, NSString *
} }
_canvas = canvas; _canvas = canvas;
} }
CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(_canvasWidth, _canvasHeight), _preserveAspectRatio, _thumbnailSize); CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(_canvasWidth, _canvasHeight) scaleSize:_thumbnailSize preserveAspectRatio:_preserveAspectRatio shouldScaleUp:NO];
imageRef = [self sd_drawnWebpImageWithCanvas:_canvas iterator:iter colorSpace:_colorSpace scaledSize:scaledSize]; imageRef = [self sd_drawnWebpImageWithCanvas:_canvas iterator:iter colorSpace:_colorSpace scaledSize:scaledSize];
} else { } else {
CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(iter.width, iter.height), _preserveAspectRatio, _thumbnailSize); CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(iter.width, iter.height) scaleSize:_thumbnailSize preserveAspectRatio:_preserveAspectRatio shouldScaleUp:NO];
imageRef = [self sd_createWebpImageWithData:iter.fragment colorSpace:_colorSpace scaledSize:scaledSize]; imageRef = [self sd_createWebpImageWithData:iter.fragment colorSpace:_colorSpace scaledSize:scaledSize];
} }
if (!imageRef) { if (!imageRef) {
@ -1213,7 +1181,7 @@ static float GetFloatValueForKey(NSDictionary * _Nonnull dictionary, NSString *
// Now the canvas is ready, which respects of dispose method behavior. Just do normal decoding and produce image. // Now the canvas is ready, which respects of dispose method behavior. Just do normal decoding and produce image.
// Check whether we need to use thumbnail // Check whether we need to use thumbnail
CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(_canvasWidth, _canvasHeight), _preserveAspectRatio, _thumbnailSize); CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(_canvasWidth, _canvasHeight) scaleSize:_thumbnailSize preserveAspectRatio:_preserveAspectRatio shouldScaleUp:NO];
CGImageRef imageRef = [self sd_drawnWebpImageWithCanvas:_canvas iterator:iter colorSpace:_colorSpace scaledSize:scaledSize]; CGImageRef imageRef = [self sd_drawnWebpImageWithCanvas:_canvas iterator:iter colorSpace:_colorSpace scaledSize:scaledSize];
if (!imageRef) { if (!imageRef) {
return nil; return nil;