Skip to content

Commit 0d028c6

Browse files
committed
feat: added encodeToHDR related options for Apple ImageIO coder
This just forward the options, no extra logic
1 parent e618417 commit 0d028c6

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

SDWebImage/Core/SDImageCoder.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ typedef NSString * SDImageCoderOption NS_STRING_ENUM;
1515
typedef NSDictionary<SDImageCoderOption, id> SDImageCoderOptions;
1616
typedef NSMutableDictionary<SDImageCoderOption, id> SDImageCoderMutableOptions;
1717

18-
#pragma mark - Coder Options
18+
#pragma mark - Image Decoding Options
1919
// These options are for image decoding
2020
/**
2121
A Boolean value indicating whether to decode the first frame only for animated image during decoding. (NSNumber). If not provide, decode animated image if need.
@@ -96,7 +96,18 @@ FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderDecodeScaleDownL
9696
*/
9797
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderDecodeToHDR;
9898

99-
// These options are for image encoding
99+
#pragma mark - Image Encoding Options
100+
/**
101+
A NSUInteger value (NSNumber) to provide converting to HDR during encoding. Read the below carefully to choose the value.
102+
@note 0 means SDR; 1 means ISO HDR (at least using 10 bits per components or above, supported by AVIF/HEIF/JPEG-XL); 2 means ISO Gain Map Image (may use 8 bits per components, supported AVIF/HEIF/JPEG-XL, as well as traditional JPEG)
103+
@note Gain Map like a mask image with metadata, which contains the depth/bright information for each pixel (or smaller, 1/4 resolution), which used to convert between HDR and SDR.
104+
@note If you use CIImage as HDR pipeline, you can export as CGImage for encoding. (But it's also recommanded to use CIImage's `JPEGRepresentationOfImage` or `HEIFRepresentationOfImage`)
105+
@note Supported by iOS 18 and above when using ImageIO coder (third-party coder can support lower firmware)
106+
Defaults to @(0), encoder will automatically convert SDR.
107+
@note works for `SDImageCoder`
108+
*/
109+
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeToHDR;
110+
100111
/**
101112
A Boolean value indicating whether to encode the first frame only for animated image during encoding. (NSNumber). If not provide, encode animated image if need.
102113
@note works for `SDImageCoder`.

SDWebImage/Core/SDImageCoder.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
SDImageCoderOption const SDImageCoderDecodeScaleDownLimitBytes = @"decodeScaleDownLimitBytes";
1919
SDImageCoderOption const SDImageCoderDecodeToHDR = @"decodeToHDR";
2020

21+
SDImageCoderOption const SDImageCoderEncodeToHDR = @"encodeToHDR";
2122
SDImageCoderOption const SDImageCoderEncodeFirstFrameOnly = @"encodeFirstFrameOnly";
2223
SDImageCoderOption const SDImageCoderEncodeCompressionQuality = @"encodeCompressionQuality";
2324
SDImageCoderOption const SDImageCoderEncodeBackgroundColor = @"encodeBackgroundColor";

SDWebImage/Core/SDImageIOAnimatedCoder.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,21 @@ - (NSData *)encodedDataWithFrames:(NSArray<SDImageFrame *> *)frames loopCount:(N
895895
maxPixelSize = maxPixelSizeValue.CGSizeValue;
896896
#endif
897897
}
898+
// HDR Encoding
899+
NSUInteger encodeToHDR = 0;
900+
if (options[SDImageCoderEncodeToHDR]) {
901+
encodeToHDR = [options[SDImageCoderEncodeToHDR] unsignedIntegerValue];
902+
}
903+
if (@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)) {
904+
if (encodeToHDR == 1) {
905+
properties[(__bridge NSString *)kCGImageDestinationEncodeRequest] = (__bridge NSString *)kCGImageDestinationEncodeToISOHDR;
906+
} else if (encodeToHDR == 2) {
907+
properties[(__bridge NSString *)kCGImageDestinationEncodeRequest] = (__bridge NSString *)kCGImageDestinationEncodeToISOGainmap;
908+
} else {
909+
properties[(__bridge NSString *)kCGImageDestinationEncodeRequest] = (__bridge NSString *)kCGImageDestinationEncodeToSDR;
910+
}
911+
}
912+
898913
CGFloat pixelWidth = (CGFloat)CGImageGetWidth(imageRef);
899914
CGFloat pixelHeight = (CGFloat)CGImageGetHeight(imageRef);
900915
CGFloat finalPixelSize = 0;

SDWebImage/Core/SDImageIOCoder.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,21 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
381381
maxPixelSize = maxPixelSizeValue.CGSizeValue;
382382
#endif
383383
}
384+
// HDR Encoding
385+
NSUInteger encodeToHDR = 0;
386+
if (options[SDImageCoderEncodeToHDR]) {
387+
encodeToHDR = [options[SDImageCoderEncodeToHDR] unsignedIntegerValue];
388+
}
389+
if (@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)) {
390+
if (encodeToHDR == 1) {
391+
properties[(__bridge NSString *)kCGImageDestinationEncodeRequest] = (__bridge NSString *)kCGImageDestinationEncodeToISOHDR;
392+
} else if (encodeToHDR == 2) {
393+
properties[(__bridge NSString *)kCGImageDestinationEncodeRequest] = (__bridge NSString *)kCGImageDestinationEncodeToISOGainmap;
394+
} else {
395+
properties[(__bridge NSString *)kCGImageDestinationEncodeRequest] = (__bridge NSString *)kCGImageDestinationEncodeToSDR;
396+
}
397+
}
398+
384399
CGFloat pixelWidth = (CGFloat)CGImageGetWidth(imageRef);
385400
CGFloat pixelHeight = (CGFloat)CGImageGetHeight(imageRef);
386401
CGFloat finalPixelSize = 0;

0 commit comments

Comments
 (0)