Skip to content

Commit a4dba8f

Browse files
committed
feat: Added SDImageHDRType enum, to make it more standard instead of raw int value 0/1/2
1 parent ba8b93f commit a4dba8f

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

SDWebImage/Core/SDImageCoder.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderDecodeUseLazyDec
8989
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderDecodeScaleDownLimitBytes;
9090

9191
/**
92-
A Boolean value (NSNumber) to provide converting to HDR during decoding. Currently if number is 0, use SDR, else use HDR. But we may extend this option to use `NSUInteger` in the future (means, think this options as int number, but not actual boolean)
92+
A Boolean (`SDImageHDRType.rawValue`) value (stored inside NSNumber) to provide converting to HDR during decoding. Currently if number is 0 (`SDImageHDRTypeSDR`), use SDR, else use HDR. But we may extend this option to represent `SDImageHDRType` all cases in the future (means, think this options as uint number, but not actual boolean)
9393
@note Supported by iOS 17 and above when using ImageIO coder (third-party coder can support lower firmware)
9494
Defaults to @(NO), decoder will automatically convert SDR.
9595
@note works for `SDImageCoder`
@@ -98,9 +98,9 @@ FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderDecodeToHDR;
9898

9999
#pragma mark - Image Encoding Options
100100
/**
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 HDR (may use 8 bits per components, supported by 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.
101+
A NSUInteger (`SDImageHDRType.rawValue`) value (stored inside NSNumber) to provide converting to HDR during encoding. Read the below carefully to choose the value.
102+
@note 0(`SDImageHDRTypeSDR`) means SDR; 1(`SDImageHDRTypeISOHDR`) means ISO HDR (at least using 10 bits per components or above, supported by AVIF/HEIF/JPEG-XL); 2(`SDImageHDRTypeISOGainMap`) means ISO Gain Map HDR (may use 8 bits per components, supported by 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 pixels (1/4 resolution), which used to convert between HDR and SDR.
104104
@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`)
105105
@note Supported by iOS 18 and above when using ImageIO coder (third-party coder can support lower firmware)
106106
Defaults to @(0), encoder will automatically convert SDR.

SDWebImage/Core/SDImageCoderHelper.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ typedef NS_ENUM(NSUInteger, SDImageForceDecodePolicy) {
3333
SDImageForceDecodePolicyAlways
3434
};
3535

36+
/// These enum is used to represent the High Dynamic Range type during image encoding/decoding.
37+
/// There are alao other HDR type in history before ISO Standard (ISO 21496-1), including Google and Apple's old OSs captured photos, but which is non-standard and we do not support.
38+
typedef NS_ENUM(NSUInteger, SDImageHDRType) {
39+
/// SDR, mostly only 8 bits color per components, RGBA8
40+
SDImageHDRTypeSDR = 0,
41+
/// ISO HDR (supported by modern format only, like HEIF/AVIF/JPEG-XL)
42+
SDImageHDRTypeISOHDR = 1,
43+
/// ISO Gain Map based HDR (supported by nearly all format, including tranditional JPEG, which stored the gain map into XMP)
44+
SDImageHDRTypeISOGainMap = 2,
45+
};
46+
3647
/// Byte alignment the bytes size with alignment
3748
/// - Parameters:
3849
/// - size: The bytes size

SDWebImage/Core/SDImageIOAnimatedCoder.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,9 +919,9 @@ - (NSData *)encodedDataWithFrames:(NSArray<SDImageFrame *> *)frames loopCount:(N
919919
encodeToHDR = [options[SDImageCoderEncodeToHDR] unsignedIntegerValue];
920920
}
921921
if (@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)) {
922-
if (encodeToHDR == 1) {
922+
if (encodeToHDR == SDImageHDRTypeISOHDR) {
923923
properties[kSDCGImageDestinationEncodeRequest] = kSDCGImageDestinationEncodeToISOHDR;
924-
} else if (encodeToHDR == 2) {
924+
} else if (encodeToHDR == SDImageHDRTypeISOGainMap) {
925925
properties[kSDCGImageDestinationEncodeRequest] = kSDCGImageDestinationEncodeToISOGainmap;
926926
} else {
927927
properties[kSDCGImageDestinationEncodeRequest] = kSDCGImageDestinationEncodeToSDR;

SDWebImage/Core/SDImageIOCoder.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,9 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
405405
encodeToHDR = [options[SDImageCoderEncodeToHDR] unsignedIntegerValue];
406406
}
407407
if (@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)) {
408-
if (encodeToHDR == 1) {
408+
if (encodeToHDR == SDImageHDRTypeISOHDR) {
409409
properties[kSDCGImageDestinationEncodeRequest] = kSDCGImageDestinationEncodeToISOHDR;
410-
} else if (encodeToHDR == 2) {
410+
} else if (encodeToHDR == SDImageHDRTypeISOGainMap) {
411411
properties[kSDCGImageDestinationEncodeRequest] = kSDCGImageDestinationEncodeToISOGainmap;
412412
} else {
413413
properties[kSDCGImageDestinationEncodeRequest] = kSDCGImageDestinationEncodeToSDR;

Tests/Tests/SDImageCoderTests.m

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ - (void)test33ThatGainMapHDRDecodeWorks {
704704
}
705705

706706
- (void)test34ThatHDREncodeWorks {
707-
// FIXME: Encoding need iOS 18+/macOS 15+
707+
// FIXME: Encoding need iOS 18+/macOS 15+, No simulator
708708
// GitHub Action virtualization framework contains issue for Gain Map HDR convert:
709709
if (SDTestCase.isCI) {
710710
return;
@@ -720,7 +720,7 @@ - (void)test34ThatHDREncodeWorks {
720720
UIImage *HDRImage = [SDImageIOCoder.sharedCoder decodedImageWithData:data options:@{SDImageCoderDecodeToHDR : @(YES)}];
721721
float headroom = CGImageGetContentHeadroom(HDRImage.CGImage);
722722
expect(headroom).beGreaterThan(1);
723-
723+
#if !TARGET_OS_SIMULATOR
724724
NSArray *encodeFormats = @[@"heic", @"jpeg"];
725725
for (NSString *encodeFormat in encodeFormats) {
726726
NSLog(@"Testing HDR encodde from original : %@ to %@", decodeFormat, encodeFormat);
@@ -730,9 +730,9 @@ - (void)test34ThatHDREncodeWorks {
730730
// JPEG with XMP Gain Map
731731
format = SDImageFormatJPEG;
732732
}
733-
NSData *SDRData = [SDImageIOCoder.sharedCoder encodedDataWithImage:HDRImage format:format options:@{SDImageCoderEncodeToHDR : @(0)}];
734-
NSData *HDRData = [SDImageIOCoder.sharedCoder encodedDataWithImage:HDRImage format:format options:@{SDImageCoderEncodeToHDR : @(1)}];
735-
NSData *HDRGainMapData = [SDImageIOCoder.sharedCoder encodedDataWithImage:HDRImage format:format options:@{SDImageCoderEncodeToHDR : @(2)}];
733+
NSData *SDRData = [SDImageIOCoder.sharedCoder encodedDataWithImage:HDRImage format:format options:@{SDImageCoderEncodeToHDR : @(SDImageHDRTypeSDR)}];
734+
NSData *HDRData = [SDImageIOCoder.sharedCoder encodedDataWithImage:HDRImage format:format options:@{SDImageCoderEncodeToHDR : @(SDImageHDRTypeISOHDR)}];
735+
NSData *HDRGainMapData = [SDImageIOCoder.sharedCoder encodedDataWithImage:HDRImage format:format options:@{SDImageCoderEncodeToHDR : @(SDImageHDRTypeISOGainMap)}];
736736
expect(SDRData).notTo.beNil();
737737
expect(HDRData).notTo.beNil();
738738
expect(HDRGainMapData).notTo.beNil();
@@ -756,6 +756,7 @@ - (void)test34ThatHDREncodeWorks {
756756
expect(headroom).equal(1);
757757
CFRelease(source);
758758
}
759+
#endif
759760
}
760761
}
761762
}

0 commit comments

Comments
 (0)