Skip to content

Commit 1abc2ca

Browse files
committed
Update the README about the new encoding feature
1 parent fda2e2f commit 1abc2ca

File tree

2 files changed

+112
-20
lines changed

2 files changed

+112
-20
lines changed

Example/SDWebImageJPEGXLCoder/SDViewController.m

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,20 @@ - (void)viewDidLoad {
5555
}
5656
});
5757
}];
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-
// }];
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+
}
70+
});
71+
}];
7672

7773
[self testHDREncoding];
7874
}

README.md

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ This coder supports the HDR/SDR decoding, as well as JPEG-XL aniamted image.
1515
## Notes
1616

1717
1. This coder supports animation via UIImageView/NSImageView, no SDAnimatedImageView currently (Because the current coder API need codec supports non-sequential frame decoding, but libjxl does not have. Will remove this limit in SDWebImage 6.0)
18-
2. This coder does not supports JPEG-XL encoding (Because I have no time :))
19-
3. Apple's ImageIO supports JPEGXL decoding from iOS 17/tvOS 17/watchOS 10/macOS 14 (via: [WWDC2023](https://developer.apple.com/videos/play/wwdc2023/10122/)), so SDWebImage on those platform can also decode JPEGXL images using `SDImageIOCoder` (but no animated JPEG-XL support)
18+
2. Apple's ImageIO supports JPEGXL decoding from iOS 17/tvOS 17/watchOS 10/macOS 14 (via: [WWDC2023](https://developer.apple.com/videos/play/wwdc2023/10122/)), so SDWebImage on those platform can also decode JPEGXL images using `SDImageIOCoder` (but no animated JPEG-XL support)
19+
3. From v0.2.0, this coder support JXL encoding, including HDR, static JXL, animated JXL encoding as well (a huge work...)
2020

2121
## Requirements
2222

@@ -102,6 +102,102 @@ imageView.sd_setImage(with: JPEGXLURL)
102102

103103
Note: You can also test animated JPEG-XL on UIImageView/NSImageView and WebImage (via SwiftUI port)
104104

105+
### Decoding
106+
107+
+ Objective-C
108+
109+
```objective-c
110+
// JPEGXL image decoding
111+
NSData *JPEGXLData;
112+
UIImage *image = [[SDImageJPEGXLCoder sharedCoder] decodedImageWithData:JPEGXLData options:nil];
113+
```
114+
115+
+ Swift
116+
117+
```swift
118+
// JPEGXL image decoding
119+
let JPEGXLData: Data
120+
let image = SDImageJPEGXLCoder.shared.decodedImage(with: data, options: nil)
121+
```
122+
123+
### Encoding
124+
125+
+ Objective-c
126+
127+
```objective-c
128+
// JPEGXL image encoding
129+
UIImage *image;
130+
NSData *JPEGXLData = [[SDImageJPEGXLCoder sharedCoder] encodedDataWithImage:image format:SDImageFormatJPEGXL options:nil];
131+
// Encode Quality
132+
NSData *lossyJPEGXLData = [[SDImageJPEGXLCoder sharedCoder] encodedDataWithImage:image format:SDImageFormatJPEGXL options:@{SDImageCoderEncodeCompressionQuality : @(0.1)}]; // [0, 1] compression quality
133+
```
134+
135+
+ Swift
136+
137+
```swift
138+
// JPEGXL image encoding
139+
let image: UIImage
140+
let JPEGXLData = SDImageJPEGXLCoder.shared.encodedData(with: image, format: .jpegxl, options: nil)
141+
// Encode Quality
142+
let lossyJPEGXLData = SDImageJPEGXLCoder.shared.encodedData(with: image, format: .jpegxl, options: [.encodeCompressionQuality: 0.1]) // [0, 1] compression quality
143+
```
144+
145+
### Animated JPEG-XL Encoding
146+
147+
+ Objective-c
148+
149+
```objective-c
150+
// Animated encoding
151+
NSMutableArray<SDImageFrames *> *frames = [NSMutableArray array];
152+
for (size_t i = 0; i < images.count; i++) {
153+
SDImageFrame *frame = [SDImageFrame frameWithImage:images[i] duration:0.1];
154+
[frames appendObject:frame];
155+
}
156+
NSData *animatedData = [[SDImageJPEGXLCoder sharedCoder] encodedDataWithFrames:frames loopCount:0 format:SDImageFormatJPEGXL options:nil];
157+
```
158+
159+
+ Swift
160+
161+
```swift
162+
// Animated encoding
163+
var frames: [SDImageFrame] = []
164+
for i in 0..<images.count {
165+
let frame = SDImageFrame(image: images[i], duration: 0.1)
166+
frames.append(frame)
167+
}
168+
let animatedData = SDImageJPEGXLCoder.shared.encodedData(with: frames, loopCount: 0, format: .jpegxl, options: nil)
169+
```
170+
171+
### Advanced jxl codec options
172+
173+
For advanced user who want detailed control like `cjxl` command line tool, you can pass the underlying encode options in
174+
175+
+ Objective-C
176+
177+
```objective-c
178+
NSDictionary *frameSetting = @{
179+
@(JXL_ENC_FRAME_SETTING_EFFORT) : @(10),
180+
@(JXL_ENC_FRAME_SETTING_BROTLI_EFFORT) : @(11)
181+
};
182+
NSData *data = [SDImageJPEGXLCoder.sharedCoder encodedDataWithImage:image format:SDImageFormatJPEGXL options:@{
183+
SDImageCoderEncodeJXLDistance : @(3.0), // jxl -distance
184+
SDImageCoderEncodeJXLFrameSetting : frameSetting, // jxl -effort
185+
}];
186+
```
187+
188+
+ Swift
189+
190+
```swift
191+
let frameSetting = [
192+
JXL_ENC_FRAME_SETTING_EFFORT.rawValue : 10,
193+
JXL_ENC_FRAME_SETTING_BROTLI_EFFORT.rawValue : 11
194+
]
195+
let data = SDImageJPEGXLCoder.shared.encodedData(with: image, format: .jpegxl, options: [
196+
.encodeJXLDistance : 3.0, // jxl -distance
197+
.encodeJXLFrameSetting : frameSetting, // jxl -effort
198+
]);
199+
```
200+
105201
## Example
106202

107203
To run the example project, clone the repo, and run `pod install` from the root directory first. Then open `SDWebImageJPEGXLCoder.xcworkspace`.

0 commit comments

Comments
 (0)