|
72 | 72 | /// See more in #73 |
73 | 73 | static inline CGContextRef _Nullable CreateWebPCanvas(BOOL hasAlpha, CGSize canvasSize, CGSize thumbnailSize, BOOL preserveAspectRatio) { |
74 | 74 | // From SDWebImage v5.17.0, use runtime detection of bitmap info instead of hardcode. |
75 | | - CGBitmapInfo bitmapInfo = [SDImageCoderHelper preferredBitmapInfo:hasAlpha]; |
| 75 | + CGBitmapInfo bitmapInfo = [SDImageCoderHelper preferredPixelFormat:hasAlpha].bitmapInfo; |
76 | 76 | // Check whether we need to use thumbnail |
77 | 77 | CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(canvasSize.width, canvasSize.height) scaleSize:thumbnailSize preserveAspectRatio:preserveAspectRatio shouldScaleUp:NO]; |
78 | 78 | CGContextRef canvas = CGBitmapContextCreate(NULL, scaledSize.width, scaledSize.height, 8, 0, [SDImageCoderHelper colorSpaceGetDeviceRGB], bitmapInfo); |
@@ -171,20 +171,6 @@ WEBP_CSP_MODE ConvertCSPMode(CGBitmapInfo bitmapInfo) { |
171 | 171 | return MODE_LAST; |
172 | 172 | } |
173 | 173 |
|
174 | | -// TODO, share this logic for multiple coders, or do refactory in v6.0 (The coder plugin should provide image information back to Core, like `CGImageSourceCopyPropertiesAtIndex`) |
175 | | -static inline CGSize SDCalculateScaleDownPixelSize(NSUInteger limitBytes, CGSize originalSize, NSUInteger frameCount, NSUInteger bytesPerPixel) { |
176 | | - if (CGSizeEqualToSize(originalSize, CGSizeZero)) return CGSizeMake(1, 1); |
177 | | - NSUInteger totalFramePixelSize = limitBytes / bytesPerPixel / (frameCount ?: 1); |
178 | | - CGFloat ratio = originalSize.height / originalSize.width; |
179 | | - CGFloat width = sqrt(totalFramePixelSize / ratio); |
180 | | - CGFloat height = width * ratio; |
181 | | - width = MAX(1, floor(width)); |
182 | | - height = MAX(1, floor(height)); |
183 | | - CGSize size = CGSizeMake(width, height); |
184 | | - |
185 | | - return size; |
186 | | -} |
187 | | - |
188 | 174 | @interface SDWebPCoderFrame : NSObject |
189 | 175 |
|
190 | 176 | @property (nonatomic, assign) NSUInteger index; // Frame index (zero based) |
@@ -328,7 +314,7 @@ - (UIImage *)decodedImageWithData:(NSData *)data options:(nullable SDImageCoderO |
328 | 314 | if (limitBytes > 0) { |
329 | 315 | // Hack 32 BitsPerPixel |
330 | 316 | CGSize imageSize = CGSizeMake(canvasWidth, canvasHeight); |
331 | | - CGSize framePixelSize = SDCalculateScaleDownPixelSize(limitBytes, imageSize, frameCount, 4); |
| 317 | + CGSize framePixelSize = [SDImageCoderHelper scaledSizeWithImageSize:imageSize limitBytes:limitBytes bytesPerPixel:4 frameCount:frameCount]; |
332 | 318 | // Override thumbnail size |
333 | 319 | thumbnailSize = framePixelSize; |
334 | 320 | preserveAspectRatio = YES; |
@@ -477,7 +463,7 @@ - (void)updateIncrementalData:(NSData *)data finished:(BOOL)finished { |
477 | 463 | if (_limitBytes > 0) { |
478 | 464 | // Hack 32 BitsPerPixel |
479 | 465 | CGSize imageSize = CGSizeMake(_canvasWidth, _canvasHeight); |
480 | | - CGSize framePixelSize = SDCalculateScaleDownPixelSize(_limitBytes, imageSize, _frameCount, 4); |
| 466 | + CGSize framePixelSize = [SDImageCoderHelper scaledSizeWithImageSize:imageSize limitBytes:_limitBytes bytesPerPixel:4 frameCount:_frameCount]; |
481 | 467 | // Override thumbnail size |
482 | 468 | _thumbnailSize = framePixelSize; |
483 | 469 | _preserveAspectRatio = YES; |
@@ -631,7 +617,8 @@ - (nullable CGImageRef)sd_createWebpImageWithData:(WebPData)webpData colorSpace: |
631 | 617 |
|
632 | 618 | BOOL hasAlpha = config.input.has_alpha; |
633 | 619 | // From SDWebImage v5.17.0, use runtime detection of bitmap info instead of hardcode. |
634 | | - CGBitmapInfo bitmapInfo = [SDImageCoderHelper preferredBitmapInfo:hasAlpha]; |
| 620 | + SDImagePixelFormat pixelFormat = [SDImageCoderHelper preferredPixelFormat:hasAlpha]; |
| 621 | + CGBitmapInfo bitmapInfo = pixelFormat.bitmapInfo; |
635 | 622 | WEBP_CSP_MODE mode = ConvertCSPMode(bitmapInfo); |
636 | 623 | if (mode == MODE_LAST) { |
637 | 624 | NSAssert(NO, @"Unsupported libwebp preferred CGBitmapInfo: %d", bitmapInfo); |
@@ -659,8 +646,9 @@ - (nullable CGImageRef)sd_createWebpImageWithData:(WebPData)webpData colorSpace: |
659 | 646 | // Read: https://github.com/path/FastImageCache#byte-alignment |
660 | 647 | // A properly aligned bytes-per-row value must be a multiple of 8 pixels × bytes per pixel |
661 | 648 | // For a typical ARGB image, the aligned bytes-per-row value is a multiple of 64. |
662 | | - size_t alignment = [SDImageCoderHelper preferredByteAlignment]; |
| 649 | + size_t alignment = pixelFormat.alignment; |
663 | 650 | size_t bytesPerRow = SDByteAlign(width * (bitsPerPixel / 8), alignment); |
| 651 | + //size_t bytesPerRow = 6688; |
664 | 652 |
|
665 | 653 | void *rgba = WebPMalloc(bytesPerRow * height); |
666 | 654 | config.output.is_external_memory = 1; |
@@ -1069,7 +1057,7 @@ - (instancetype)initWithAnimatedImageData:(NSData *)data options:(nullable SDIma |
1069 | 1057 | if (_limitBytes > 0) { |
1070 | 1058 | // Hack 32 BitsPerPixel |
1071 | 1059 | CGSize imageSize = CGSizeMake(_canvasWidth, _canvasHeight); |
1072 | | - CGSize framePixelSize = SDCalculateScaleDownPixelSize(_limitBytes, imageSize, _frameCount, 4); |
| 1060 | + CGSize framePixelSize = [SDImageCoderHelper scaledSizeWithImageSize:imageSize limitBytes:_limitBytes bytesPerPixel:4 frameCount:_frameCount]; |
1073 | 1061 | // Override thumbnail size |
1074 | 1062 | _thumbnailSize = framePixelSize; |
1075 | 1063 | _preserveAspectRatio = YES; |
|
0 commit comments