Skip to content

Commit a434de6

Browse files
committed
Support animated jxl encoding, refactory the code
libjxl API required to close the input after the last animated frame finished
1 parent 2a6eba2 commit a434de6

File tree

2 files changed

+221
-215
lines changed

2 files changed

+221
-215
lines changed

Example/SDWebImageJPEGXLCoder/SDViewController.m

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,44 @@ - (void)viewDidLoad {
3535
self.imageView2.contentMode = UIViewContentModeScaleAspectFit;
3636
[self.view addSubview:self.imageView2];
3737

38-
NSURL *staticURL = [NSURL URLWithString:@"https://jpegxl.info/logo.jxl"];
39-
NSURL *animatedURL = [NSURL URLWithString:@"https://jpegxl.info/anim_jxl_logo.jxl"];
38+
NSURL *staticURL = [NSURL URLWithString:@"https://jpegxl.info/images/dice.jxl"];
39+
NSURL *animatedURL = [NSURL URLWithString:@"https://jpegxl.info/images/anim-icos.jxl"];
4040

4141
[self.imageView1 sd_setImageWithURL:staticURL placeholderImage:nil options:0 context:nil progress:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
4242
if (image) {
4343
NSLog(@"%@", @"Static JPEG-XL load success");
4444
}
45-
// TODO, JXL encoding
45+
// static JXL encoding
4646
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
4747
NSUInteger maxFileSize = 4096;
4848
NSData *jxlData = [SDImageJPEGXLCoder.sharedCoder encodedDataWithImage:image format:SDImageFormatJPEGXL options:@{SDImageCoderEncodeMaxFileSize : @(maxFileSize)}];
4949
if (jxlData) {
50-
NSLog(@"%@", @"JPEG-XL encoding success");
50+
NSLog(@"Static JPEG-XL encode success, bytes: %lu", (unsigned long)jxlData.length);
51+
dispatch_async(dispatch_get_main_queue(), ^{
52+
UIImage *animatedImage = [UIImage sd_imageWithData:jxlData];
53+
self.imageView2.image = animatedImage;
54+
});
5155
}
5256
});
5357
}];
54-
[self.imageView2 sd_setImageWithURL:animatedURL placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
55-
if (image) {
56-
NSLog(@"%@", @"Animated JPEG-XL load success");
57-
}
58-
}];
58+
// [self.imageView2 sd_setImageWithURL:animatedURL placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
59+
// if (image) {
60+
// NSLog(@"%@", @"Animated JPEG-XL load success");
61+
// }
62+
// // animated JXL encoding
63+
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
64+
// NSData *jxlData = [SDImageJPEGXLCoder.sharedCoder encodedDataWithImage:image format:SDImageFormatJPEGXL options:@{
65+
// SDImageCoderEncodeJXLDistance : @(3.0),
66+
// }];
67+
// if (jxlData) {
68+
// NSLog(@"Animated JPEG-XL encode success, bytes: %lu", (unsigned long)jxlData.length);
69+
// dispatch_async(dispatch_get_main_queue(), ^{
70+
// UIImage *animatedImage = [UIImage sd_imageWithData:jxlData];
71+
// self.imageView1.image = animatedImage;
72+
// });
73+
// }
74+
// });
75+
// }];
5976

6077
[self testHDREncoding];
6178
}
@@ -79,13 +96,13 @@ - (void)testHDREncoding {
7996
- (void)encodeJXLWithImage:(UIImage *)image {
8097
NSCParameterAssert(image);
8198
NSDictionary *frameSetting = @{
82-
@(JXL_ENC_FRAME_SETTING_EFFORT) : @(1),
83-
@(JXL_ENC_FRAME_SETTING_BROTLI_EFFORT) : @(0)
99+
@(JXL_ENC_FRAME_SETTING_EFFORT) : @(10),
100+
@(JXL_ENC_FRAME_SETTING_BROTLI_EFFORT) : @(11)
84101
};
85102
// fastest encoding speed but largest compressed size, you can adjust options here
86103
NSData *data = [SDImageJPEGXLCoder.sharedCoder encodedDataWithImage:image format:SDImageFormatJPEGXL options:@{
87104
// SDImageCoderEncodeCompressionQuality : @0.68,
88-
SDImageCoderEncodeJXLDistance : @(1.0),
105+
SDImageCoderEncodeJXLDistance : @(3.0),
89106
SDImageCoderEncodeJXLFrameSetting : frameSetting,
90107
}];
91108
NSCParameterAssert(data);

0 commit comments

Comments
 (0)