Navigation Menu

Skip to content

Commit

Permalink
Fix the encoding maxPixelSize logic
Browse files Browse the repository at this point in the history
using the new calculation from SDWebImage Core
  • Loading branch information
dreampiggy committed Jun 22, 2022
1 parent 18834ca commit 52b24f6
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Cartfile
@@ -1,2 +1,2 @@
github "SDWebImage/SDWebImage" ~> 5.10
github "SDWebImage/SDWebImage" ~> 5.13
github "SDWebImage/libwebp-Xcode" ~> 1.0
2 changes: 1 addition & 1 deletion Package.swift
Expand Up @@ -17,7 +17,7 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
// .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")
],
targets: [
Expand Down
2 changes: 2 additions & 0 deletions Podfile
Expand Up @@ -8,11 +8,13 @@ target 'SDWebImageWebPCoderExample' do
platform :ios, '9.0'
project example_project_path
pod 'SDWebImageWebPCoder', :path => './'
pod 'SDWebImage', :path => '../SDWebImage'
end

target 'SDWebImageWebPCoderTests' do
platform :ios, '9.0'
project test_project_path
pod 'Expecta'
pod 'SDWebImageWebPCoder', :path => './'
pod 'SDWebImage', :path => '../SDWebImage'
end
2 changes: 1 addition & 1 deletion SDWebImageWebPCoder.podspec
Expand Up @@ -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',
'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'

end
46 changes: 7 additions & 39 deletions SDWebImageWebPCoder/Classes/SDImageWebPCoder.m
Expand Up @@ -68,38 +68,6 @@
#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

@property (nonatomic, assign) NSUInteger index; // Frame index (zero based)
Expand Down Expand Up @@ -231,7 +199,7 @@ - (UIImage *)decodedImageWithData:(NSData *)data options:(nullable SDImageCoderO
int canvasWidth = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_WIDTH);
int canvasHeight = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_HEIGHT);
// 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) {
// first frame for animated webp image
Expand Down Expand Up @@ -437,7 +405,7 @@ - (UIImage *)incrementalDecodedImageWithOptions:(SDImageCoderOptions *)options {
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
if (!CGSizeEqualToSize(CGSizeMake(width, height), scaledSize)) {
CGImageRef scaledImageRef = [SDImageCoderHelper CGImageCreateScaled:newImageRef size:scaledSize];
Expand Down Expand Up @@ -860,8 +828,8 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
}

// Check if need to scale pixel size
if (maxPixelSize.width > 0 && maxPixelSize.height > 0 && (width > maxPixelSize.width || height > maxPixelSize.height)) {
CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(width, height), YES, maxPixelSize);
CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(width, height) scaleSize:maxPixelSize preserveAspectRatio:YES shouldScaleUp:NO];
if (!CGSizeEqualToSize(scaledSize, CGSizeMake(width, height))) {
result = WebPPictureRescale(&picture, scaledSize.width, scaledSize.height);
if (!result) {
WebPMemoryWriterClear(&writer);
Expand Down Expand Up @@ -1132,10 +1100,10 @@ - (UIImage *)safeStaticImageFrame {
}
_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];
} 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];
}
if (!imageRef) {
Expand Down Expand Up @@ -1213,7 +1181,7 @@ - (UIImage *)safeAnimatedImageFrameAtIndex:(NSUInteger)index {

// 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
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];
if (!imageRef) {
return nil;
Expand Down

0 comments on commit 52b24f6

Please sign in to comment.