Skip to content

Commit 19ccb78

Browse files
committed
Provide the common fallback for thumbnail decoding
1 parent c1a684a commit 19ccb78

File tree

1 file changed

+9
-34
lines changed

1 file changed

+9
-34
lines changed

SDWebImageJPEGXLCoder/Classes/SDImageJPEGXLCoder.m

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,6 @@ static void FreeImageData(void *info, const void *data, size_t size) {
3131
free((void *)data);
3232
}
3333

34-
/// Calculate the actual thumnail pixel size
35-
static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio, CGSize thumbnailSize) {
36-
CGFloat width = fullSize.width;
37-
CGFloat height = fullSize.height;
38-
CGFloat resultWidth;
39-
CGFloat resultHeight;
40-
41-
if (width == 0 || height == 0 || thumbnailSize.width == 0 || thumbnailSize.height == 0 || (width <= thumbnailSize.width && height <= thumbnailSize.height)) {
42-
// Full Pixel
43-
resultWidth = width;
44-
resultHeight = height;
45-
} else {
46-
// Thumbnail
47-
if (preserveAspectRatio) {
48-
CGFloat pixelRatio = width / height;
49-
CGFloat thumbnailRatio = thumbnailSize.width / thumbnailSize.height;
50-
if (pixelRatio > thumbnailRatio) {
51-
resultWidth = thumbnailSize.width;
52-
resultHeight = ceil(thumbnailSize.width / pixelRatio);
53-
} else {
54-
resultHeight = thumbnailSize.height;
55-
resultWidth = ceil(thumbnailSize.height * pixelRatio);
56-
}
57-
} else {
58-
resultWidth = thumbnailSize.width;
59-
resultHeight = thumbnailSize.height;
60-
}
61-
}
62-
63-
return CGSizeMake(resultWidth, resultHeight);
64-
}
65-
6634
@implementation SDImageJPEGXLCoder
6735

6836
+ (instancetype)sharedCoder {
@@ -334,12 +302,19 @@ - (nullable CGImageRef)sd_createJXLImageWithDec:(JxlDecoder *)dec info:(JxlBasic
334302
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferSize, FreeImageData);
335303
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
336304
BOOL shouldInterpolate = YES;
337-
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitmapInfo, provider, NULL, shouldInterpolate, renderingIntent);
305+
CGImageRef originImageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitmapInfo, provider, NULL, shouldInterpolate, renderingIntent);
338306
CGDataProviderRelease(provider);
339307

340-
if (!imageRef) {
308+
if (!originImageRef) {
341309
return nil;
342310
}
311+
// TODO: In SDWebImage 6.0 API, coder can choose `whether I supports thumbnail decoding`
312+
// if return false, we provide a common implementation `after the full image is decoded`
313+
// do not repeat code in each coder plugin repo :(
314+
CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(width, height) scaleSize:thumbnailSize preserveAspectRatio:preserveAspectRatio shouldScaleUp:NO];
315+
CGImageRef imageRef = [SDImageCoderHelper CGImageCreateScaled:originImageRef size:scaledSize];
316+
CGImageRelease(originImageRef);
317+
343318
return imageRef;
344319
}
345320

0 commit comments

Comments
 (0)