|
68 | 68 | #endif |
69 | 69 | #endif |
70 | 70 |
|
71 | | -/// Calculate the actual thumnail pixel size |
72 | | -static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio, CGSize thumbnailSize) { |
73 | | - CGFloat width = fullSize.width; |
74 | | - CGFloat height = fullSize.height; |
75 | | - CGFloat resultWidth; |
76 | | - CGFloat resultHeight; |
77 | | - |
78 | | - if (width == 0 || height == 0 || thumbnailSize.width == 0 || thumbnailSize.height == 0 || (width <= thumbnailSize.width && height <= thumbnailSize.height)) { |
79 | | - // Full Pixel |
80 | | - resultWidth = width; |
81 | | - resultHeight = height; |
82 | | - } else { |
83 | | - // Thumbnail |
84 | | - if (preserveAspectRatio) { |
85 | | - CGFloat pixelRatio = width / height; |
86 | | - CGFloat thumbnailRatio = thumbnailSize.width / thumbnailSize.height; |
87 | | - if (pixelRatio > thumbnailRatio) { |
88 | | - resultWidth = thumbnailSize.width; |
89 | | - resultHeight = ceil(thumbnailSize.width / pixelRatio); |
90 | | - } else { |
91 | | - resultHeight = thumbnailSize.height; |
92 | | - resultWidth = ceil(thumbnailSize.height * pixelRatio); |
93 | | - } |
94 | | - } else { |
95 | | - resultWidth = thumbnailSize.width; |
96 | | - resultHeight = thumbnailSize.height; |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - return CGSizeMake(resultWidth, resultHeight); |
101 | | -} |
102 | | - |
103 | 71 | @interface SDWebPCoderFrame : NSObject |
104 | 72 |
|
105 | 73 | @property (nonatomic, assign) NSUInteger index; // Frame index (zero based) |
@@ -231,7 +199,7 @@ - (UIImage *)decodedImageWithData:(NSData *)data options:(nullable SDImageCoderO |
231 | 199 | int canvasWidth = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_WIDTH); |
232 | 200 | int canvasHeight = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_HEIGHT); |
233 | 201 | // Check whether we need to use thumbnail |
234 | | - CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(canvasWidth, canvasHeight), preserveAspectRatio, thumbnailSize); |
| 202 | + CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(canvasWidth, canvasHeight) scaleSize:thumbnailSize preserveAspectRatio:preserveAspectRatio shouldScaleUp:NO]; |
235 | 203 |
|
236 | 204 | if (!hasAnimation || decodeFirstFrame) { |
237 | 205 | // first frame for animated webp image |
@@ -437,7 +405,7 @@ - (UIImage *)incrementalDecodedImageWithOptions:(SDImageCoderOptions *)options { |
437 | 405 | scale = 1; |
438 | 406 | } |
439 | 407 | } |
440 | | - CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(width, height), _preserveAspectRatio, _thumbnailSize); |
| 408 | + CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(width, height) scaleSize:_thumbnailSize preserveAspectRatio:_preserveAspectRatio shouldScaleUp:NO]; |
441 | 409 | // Check whether we need to use thumbnail |
442 | 410 | if (!CGSizeEqualToSize(CGSizeMake(width, height), scaledSize)) { |
443 | 411 | CGImageRef scaledImageRef = [SDImageCoderHelper CGImageCreateScaled:newImageRef size:scaledSize]; |
@@ -860,8 +828,8 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef |
860 | 828 | } |
861 | 829 |
|
862 | 830 | // Check if need to scale pixel size |
863 | | - if (maxPixelSize.width > 0 && maxPixelSize.height > 0 && (width > maxPixelSize.width || height > maxPixelSize.height)) { |
864 | | - CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(width, height), YES, maxPixelSize); |
| 831 | + CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(width, height) scaleSize:maxPixelSize preserveAspectRatio:YES shouldScaleUp:NO]; |
| 832 | + if (!CGSizeEqualToSize(scaledSize, CGSizeMake(width, height))) { |
865 | 833 | result = WebPPictureRescale(&picture, scaledSize.width, scaledSize.height); |
866 | 834 | if (!result) { |
867 | 835 | WebPMemoryWriterClear(&writer); |
@@ -1132,10 +1100,10 @@ - (UIImage *)safeStaticImageFrame { |
1132 | 1100 | } |
1133 | 1101 | _canvas = canvas; |
1134 | 1102 | } |
1135 | | - CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(_canvasWidth, _canvasHeight), _preserveAspectRatio, _thumbnailSize); |
| 1103 | + CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(_canvasWidth, _canvasHeight) scaleSize:_thumbnailSize preserveAspectRatio:_preserveAspectRatio shouldScaleUp:NO]; |
1136 | 1104 | imageRef = [self sd_drawnWebpImageWithCanvas:_canvas iterator:iter colorSpace:_colorSpace scaledSize:scaledSize]; |
1137 | 1105 | } else { |
1138 | | - CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(iter.width, iter.height), _preserveAspectRatio, _thumbnailSize); |
| 1106 | + CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(iter.width, iter.height) scaleSize:_thumbnailSize preserveAspectRatio:_preserveAspectRatio shouldScaleUp:NO]; |
1139 | 1107 | imageRef = [self sd_createWebpImageWithData:iter.fragment colorSpace:_colorSpace scaledSize:scaledSize]; |
1140 | 1108 | } |
1141 | 1109 | if (!imageRef) { |
@@ -1213,7 +1181,7 @@ - (UIImage *)safeAnimatedImageFrameAtIndex:(NSUInteger)index { |
1213 | 1181 |
|
1214 | 1182 | // Now the canvas is ready, which respects of dispose method behavior. Just do normal decoding and produce image. |
1215 | 1183 | // Check whether we need to use thumbnail |
1216 | | - CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(_canvasWidth, _canvasHeight), _preserveAspectRatio, _thumbnailSize); |
| 1184 | + CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(_canvasWidth, _canvasHeight) scaleSize:_thumbnailSize preserveAspectRatio:_preserveAspectRatio shouldScaleUp:NO]; |
1217 | 1185 | CGImageRef imageRef = [self sd_drawnWebpImageWithCanvas:_canvas iterator:iter colorSpace:_colorSpace scaledSize:scaledSize]; |
1218 | 1186 | if (!imageRef) { |
1219 | 1187 | return nil; |
|
0 commit comments