Skip to content

Commit

Permalink
Merge pull request #55 from SDWebImage/bugfix/quick_hack_support_hdr1…
Browse files Browse the repository at this point in the history
…0bit_pq

Quick hack to support 10 bit HDR AVIF using PQ transfer function...
  • Loading branch information
dreampiggy authored Jul 22, 2023
2 parents 2cad767 + e703334 commit 55c10aa
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 176 deletions.
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "SDWebImage/SDWebImage" ~> 5.10
github "SDWebImage/libavif-Xcode" >= 0.11.0
github "SDWebImage/libavif-Xcode" >= 0.11.2-rc1
7 changes: 4 additions & 3 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github "SDWebImage/SDWebImage" "5.10.2"
github "SDWebImage/libaom-Xcode" "1.0.2"
github "SDWebImage/libavif-Xcode" "0.8.1"
github "SDWebImage/SDWebImage" "5.17.0"
github "SDWebImage/libaom-Xcode" "3.0.0"
github "SDWebImage/libavif-Xcode" "0.11.2-rc1"
github "SDWebImage/libvmaf-Xcode" "2.3.1"
138 changes: 98 additions & 40 deletions SDWebImageAVIFCoder/Classes/ColorSpace.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,45 +164,37 @@ static void CalcTransferFunction(uint16_t const transferCharacteristics, vImageT
}
}
CGColorSpaceRef SDAVIFCreateColorSpaceMono(avifColorPrimaries const colorPrimaries, avifTransferCharacteristics const transferCharacteristics) {
if (@available(macOS 10.10, iOS 8.0, tvOS 8.0, *)) {
vImage_Error err;
vImageWhitePoint white;
vImageTransferFunction transfer;
CalcWhitePoint(colorPrimaries, &white);
CalcTransferFunction(transferCharacteristics, &transfer);
CGColorSpaceRef colorSpace = vImageCreateMonochromeColorSpaceWithWhitePointAndTransferFunction(&white, &transfer, kCGRenderingIntentDefault, kvImagePrintDiagnosticsToConsole, &err);
if(err != kvImageNoError) {
NSLog(@"[BUG] Failed to create monochrome color space: %ld", err);
if(colorSpace != NULL) {
CGColorSpaceRelease(colorSpace);
}
return NULL;
vImage_Error err;
vImageWhitePoint white;
vImageTransferFunction transfer;
CalcWhitePoint(colorPrimaries, &white);
CalcTransferFunction(transferCharacteristics, &transfer);
CGColorSpaceRef colorSpace = vImageCreateMonochromeColorSpaceWithWhitePointAndTransferFunction(&white, &transfer, kCGRenderingIntentDefault, kvImagePrintDiagnosticsToConsole, &err);
if(err != kvImageNoError) {
NSLog(@"[BUG] Failed to create monochrome color space: %ld", err);
if(colorSpace != NULL) {
CGColorSpaceRelease(colorSpace);
}
return colorSpace;
}else{
return NULL;
}
return colorSpace;
}

CGColorSpaceRef SDAVIFCreateColorSpaceRGB(avifColorPrimaries const colorPrimaries, avifTransferCharacteristics const transferCharacteristics) {
if (@available(macOS 10.10, iOS 8.0, tvOS 8.0, *)) {
vImage_Error err;
vImageRGBPrimaries primaries;
vImageTransferFunction transfer;
CalcRGBPrimaries(colorPrimaries, &primaries);
CalcTransferFunction(transferCharacteristics, &transfer);
CGColorSpaceRef colorSpace = vImageCreateRGBColorSpaceWithPrimariesAndTransferFunction(&primaries, &transfer, kCGRenderingIntentDefault, kvImagePrintDiagnosticsToConsole, &err);
if(err != kvImageNoError) {
NSLog(@"[BUG] Failed to create monochrome color space: %ld", err);
if(colorSpace != NULL) {
CGColorSpaceRelease(colorSpace);
}
return NULL;
vImage_Error err;
vImageRGBPrimaries primaries;
vImageTransferFunction transfer;
CalcRGBPrimaries(colorPrimaries, &primaries);
CalcTransferFunction(transferCharacteristics, &transfer);
CGColorSpaceRef colorSpace = vImageCreateRGBColorSpaceWithPrimariesAndTransferFunction(&primaries, &transfer, kCGRenderingIntentDefault, kvImagePrintDiagnosticsToConsole, &err);
if(err != kvImageNoError) {
NSLog(@"[BUG] Failed to create monochrome color space: %ld", err);
if(colorSpace != NULL) {
CGColorSpaceRelease(colorSpace);
}
return colorSpace;
}else{
return NULL;
}
return colorSpace;
}

void SDAVIFCalcColorSpaceMono(avifImage * avif, CGColorSpaceRef* ref, BOOL* shouldRelease) {
Expand All @@ -214,7 +206,7 @@ void SDAVIFCalcColorSpaceMono(avifImage * avif, CGColorSpaceRef* ref, BOOL* shou
});
}
if(avif->icc.data && avif->icc.size) {
if(@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)) {
if(@available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)) {
CFDataRef iccData = CFDataCreate(kCFAllocatorDefault, avif->icc.data, avif->icc.size);
*ref = CGColorSpaceCreateWithICCData(iccData);
CFRelease(iccData);
Expand Down Expand Up @@ -312,7 +304,7 @@ void SDAVIFCalcColorSpaceRGB(avifImage * avif, CGColorSpaceRef* ref, BOOL* shoul
});
}
if(avif->icc.data && avif->icc.size) {
if(@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)) {
if(@available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)) {
CFDataRef iccData = CFDataCreate(kCFAllocatorDefault, avif->icc.data, avif->icc.size);
*ref = CGColorSpaceCreateWithICCData(iccData);
CFRelease(iccData);
Expand All @@ -339,7 +331,7 @@ void SDAVIFCalcColorSpaceRGB(avifImage * avif, CGColorSpaceRef* ref, BOOL* shoul
static CGColorSpaceRef bt709 = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, watchOS 2.0, *)) {
bt709 = CGColorSpaceCreateWithName(kCGColorSpaceITUR_709);
} else {
bt709 = defaultColorSpace;
Expand All @@ -354,7 +346,7 @@ void SDAVIFCalcColorSpaceRGB(avifImage * avif, CGColorSpaceRef* ref, BOOL* shoul
static CGColorSpaceRef sRGB = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (@available(macOS 10.5, iOS 9.0, tvOS 9.0, *)) {
if (@available(macOS 10.5, iOS 9.0, tvOS 9.0, watchOS 2.0, *)) {
sRGB = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
} else {
sRGB = defaultColorSpace;
Expand All @@ -369,7 +361,7 @@ void SDAVIFCalcColorSpaceRGB(avifImage * avif, CGColorSpaceRef* ref, BOOL* shoul
static CGColorSpaceRef sRGBlinear = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)) {
if (@available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)) {
sRGBlinear = CGColorSpaceCreateWithName(kCGColorSpaceLinearSRGB);
} else {
sRGBlinear = defaultColorSpace;
Expand All @@ -385,7 +377,7 @@ void SDAVIFCalcColorSpaceRGB(avifImage * avif, CGColorSpaceRef* ref, BOOL* shoul
static CGColorSpaceRef bt2020 = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, watchOS 2.0, *)) {
bt2020 = CGColorSpaceCreateWithName(kCGColorSpaceITUR_2020);
} else {
bt2020 = defaultColorSpace;
Expand All @@ -395,12 +387,57 @@ void SDAVIFCalcColorSpaceRGB(avifImage * avif, CGColorSpaceRef* ref, BOOL* shoul
*shouldRelease = FALSE;
return;
}
if(colorPrimaries == AVIF_COLOR_PRIMARIES_BT2020 &&
transferCharacteristics == AVIF_TRANSFER_CHARACTERISTICS_SMPTE2084) {
static CGColorSpaceRef bt2020pq = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
CFStringRef colorSpaceName = NULL;
if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)) {
colorSpaceName = kCGColorSpaceITUR_2100_PQ;
} else if (@available(macOS 10.15.4, iOS 13.4, tvOS 13.4, watchOS 6.2, *)) {
colorSpaceName = kCGColorSpaceITUR_2020_PQ;
} else if (@available(macOS 10.14.6, iOS 12.6, tvOS 12.0, watchOS 5.0, *)) {
colorSpaceName = kCGColorSpaceITUR_2020_PQ_EOTF;
}
if (colorSpaceName) {
bt2020pq = CGColorSpaceCreateWithName(colorSpaceName);
} else {
bt2020pq = defaultColorSpace;
}
});
*ref = bt2020pq;
*shouldRelease = FALSE;
return;
}
if(colorPrimaries == AVIF_COLOR_PRIMARIES_BT2020 &&
transferCharacteristics == AVIF_TRANSFER_CHARACTERISTICS_HLG) {
static CGColorSpaceRef bt2020hlg = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
CFStringRef colorSpaceName = NULL;
if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)) {
colorSpaceName = kCGColorSpaceITUR_2100_HLG;
} else if (@available(macOS 10.15.6, iOS 12.6, tvOS 12.0, watchOS 5.0, *)) {
colorSpaceName = kCGColorSpaceITUR_2020_HLG;
}
if (colorSpaceName) {
bt2020hlg = CGColorSpaceCreateWithName(colorSpaceName);
} else {
bt2020hlg = defaultColorSpace;
}
});

*ref = bt2020hlg;
*shouldRelease = FALSE;
return;
}
if(colorPrimaries == AVIF_COLOR_PRIMARIES_BT2020 &&
transferCharacteristics == AVIF_TRANSFER_CHARACTERISTICS_LINEAR) {
static CGColorSpaceRef bt2020linear = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (@available(macOS 10.14.3, iOS 12.3, tvOS 12.3, *)) {
if (@available(macOS 10.14.3, iOS 12.3, tvOS 12.3, watchOS 5.0, *)) {
bt2020linear = CGColorSpaceCreateWithName(kCGColorSpaceExtendedLinearITUR_2020);
} else {
bt2020linear = defaultColorSpace;
Expand All @@ -415,7 +452,7 @@ void SDAVIFCalcColorSpaceRGB(avifImage * avif, CGColorSpaceRef* ref, BOOL* shoul
static CGColorSpaceRef p3 = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (@available(macOS 10.11.2, iOS 9.3, tvOS 9.3, *)) {
if (@available(macOS 10.11.2, iOS 9.3, tvOS 9.3, watchOS 2.2, *)) {
p3 = CGColorSpaceCreateWithName(kCGColorSpaceDisplayP3);
} else {
p3 = defaultColorSpace;
Expand All @@ -425,12 +462,33 @@ void SDAVIFCalcColorSpaceRGB(avifImage * avif, CGColorSpaceRef* ref, BOOL* shoul
*shouldRelease = FALSE;
return;
}
if(colorPrimaries == AVIF_COLOR_PRIMARIES_SMPTE432 /* Display P3 */ &&
transferCharacteristics == AVIF_TRANSFER_CHARACTERISTICS_SMPTE2084) {
static CGColorSpaceRef p3pq = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
CFStringRef colorSpaceName = NULL;
if (@available(macOS 10.15.4, iOS 13.4, tvOS 13.4, watchOS 6.2, *)) {
colorSpaceName = kCGColorSpaceDisplayP3_PQ;
} else if (@available(macOS 10.14.6, iOS 12.6, tvOS 12.0, watchOS 5.0, *)) {
colorSpaceName = kCGColorSpaceDisplayP3_PQ_EOTF;
}
if (colorSpaceName) {
p3pq = CGColorSpaceCreateWithName(colorSpaceName);
} else {
p3pq = defaultColorSpace;
}
});
*ref = p3pq;
*shouldRelease = FALSE;
return;
}
if(colorPrimaries == AVIF_COLOR_PRIMARIES_SMPTE432 /* Display P3 */ &&
transferCharacteristics == AVIF_TRANSFER_CHARACTERISTICS_HLG) {
static CGColorSpaceRef p3hlg = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (@available(macOS 10.14.6, iOS 13.0, tvOS 13.0, *)) {
if (@available(macOS 10.14.6, iOS 13.0, tvOS 13.0, watchOS 5.0, *)) {
p3hlg = CGColorSpaceCreateWithName(kCGColorSpaceDisplayP3_HLG);
} else {
p3hlg = defaultColorSpace;
Expand All @@ -446,7 +504,7 @@ void SDAVIFCalcColorSpaceRGB(avifImage * avif, CGColorSpaceRef* ref, BOOL* shoul
static CGColorSpaceRef p3linear = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (@available(macOS 10.14.3, iOS 12.3, tvOS 12.3, *)) {
if (@available(macOS 10.14.3, iOS 12.3, tvOS 12.3, watchOS 5.0, *)) {
p3linear = CGColorSpaceCreateWithName(kCGColorSpaceExtendedLinearDisplayP3);
} else {
p3linear = defaultColorSpace;
Expand Down
Loading

0 comments on commit 55c10aa

Please sign in to comment.