Skip to content

Commit c0c65f1

Browse files
committed
Update with the final changes from SDWebImage Core
1 parent 8bae908 commit c0c65f1

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

SDWebImageWebPCoder/Classes/SDImageWebPCoder.m

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
/// See more in #73
7373
static inline CGContextRef _Nullable CreateWebPCanvas(BOOL hasAlpha, CGSize canvasSize, CGSize thumbnailSize, BOOL preserveAspectRatio) {
7474
// 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;
7676
// Check whether we need to use thumbnail
7777
CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(canvasSize.width, canvasSize.height) scaleSize:thumbnailSize preserveAspectRatio:preserveAspectRatio shouldScaleUp:NO];
7878
CGContextRef canvas = CGBitmapContextCreate(NULL, scaledSize.width, scaledSize.height, 8, 0, [SDImageCoderHelper colorSpaceGetDeviceRGB], bitmapInfo);
@@ -171,20 +171,6 @@ WEBP_CSP_MODE ConvertCSPMode(CGBitmapInfo bitmapInfo) {
171171
return MODE_LAST;
172172
}
173173

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-
188174
@interface SDWebPCoderFrame : NSObject
189175

190176
@property (nonatomic, assign) NSUInteger index; // Frame index (zero based)
@@ -328,7 +314,7 @@ - (UIImage *)decodedImageWithData:(NSData *)data options:(nullable SDImageCoderO
328314
if (limitBytes > 0) {
329315
// Hack 32 BitsPerPixel
330316
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];
332318
// Override thumbnail size
333319
thumbnailSize = framePixelSize;
334320
preserveAspectRatio = YES;
@@ -477,7 +463,7 @@ - (void)updateIncrementalData:(NSData *)data finished:(BOOL)finished {
477463
if (_limitBytes > 0) {
478464
// Hack 32 BitsPerPixel
479465
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];
481467
// Override thumbnail size
482468
_thumbnailSize = framePixelSize;
483469
_preserveAspectRatio = YES;
@@ -631,7 +617,8 @@ - (nullable CGImageRef)sd_createWebpImageWithData:(WebPData)webpData colorSpace:
631617

632618
BOOL hasAlpha = config.input.has_alpha;
633619
// 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;
635622
WEBP_CSP_MODE mode = ConvertCSPMode(bitmapInfo);
636623
if (mode == MODE_LAST) {
637624
NSAssert(NO, @"Unsupported libwebp preferred CGBitmapInfo: %d", bitmapInfo);
@@ -659,8 +646,9 @@ - (nullable CGImageRef)sd_createWebpImageWithData:(WebPData)webpData colorSpace:
659646
// Read: https://github.com/path/FastImageCache#byte-alignment
660647
// A properly aligned bytes-per-row value must be a multiple of 8 pixels × bytes per pixel
661648
// 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;
663650
size_t bytesPerRow = SDByteAlign(width * (bitsPerPixel / 8), alignment);
651+
//size_t bytesPerRow = 6688;
664652

665653
void *rgba = WebPMalloc(bytesPerRow * height);
666654
config.output.is_external_memory = 1;
@@ -1069,7 +1057,7 @@ - (instancetype)initWithAnimatedImageData:(NSData *)data options:(nullable SDIma
10691057
if (_limitBytes > 0) {
10701058
// Hack 32 BitsPerPixel
10711059
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];
10731061
// Override thumbnail size
10741062
_thumbnailSize = framePixelSize;
10751063
_preserveAspectRatio = YES;

0 commit comments

Comments
 (0)