diff options
author | Damiano Galassi <[email protected]> | 2017-08-31 10:04:28 +0200 |
---|---|---|
committer | Damiano Galassi <[email protected]> | 2017-08-31 10:04:28 +0200 |
commit | ae2627cedefb29d6f0743175613a2e5597465be8 (patch) | |
tree | 07994cc7d25455fb182ed4d72d4717f83e49bfa8 /macosx | |
parent | 43dc0cdb9d18c43771ca8f9ed52b19550c2c1b55 (diff) |
MacGui: move the color space code out of HBCore.
Diffstat (limited to 'macosx')
-rw-r--r-- | macosx/HBCore.m | 181 | ||||
-rw-r--r-- | macosx/HBImageUtilities.h | 11 | ||||
-rw-r--r-- | macosx/HBImageUtilities.m | 184 | ||||
-rw-r--r-- | macosx/HandBrake.xcodeproj/project.pbxproj | 6 |
4 files changed, 206 insertions, 176 deletions
diff --git a/macosx/HBCore.m b/macosx/HBCore.m index 2d2f592bc..acd4c187e 100644 --- a/macosx/HBCore.m +++ b/macosx/HBCore.m @@ -9,6 +9,7 @@ #import "HBJob+HBJobConversion.h" #import "HBDVDDetector.h" #import "HBUtilities.h" +#import "HBImageUtilities.h" #import "HBStateFormatter+Private.h" #import "HBTitle+Private.h" @@ -314,178 +315,6 @@ typedef void (^HBCoreCleanupHandler)(void); } #pragma mark - Preview images - -- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle flipped:(BOOL)flipped CF_RETURNS_RETAINED -{ - CGFloat angleInRadians = angle * (M_PI / 180); - CGFloat width = CGImageGetWidth(imgRef); - CGFloat height = CGImageGetHeight(imgRef); - - CGRect imgRect = CGRectMake(0, 0, width, height); - CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians); - CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform); - - CGColorSpaceRef colorSpace = CGImageGetColorSpace(imgRef); - CGContextRef bmContext = CGBitmapContextCreate(NULL, - (size_t)rotatedRect.size.width, - (size_t)rotatedRect.size.height, - 8, - 0, - colorSpace, - kCGImageAlphaPremultipliedFirst); - CGContextSetAllowsAntialiasing(bmContext, FALSE); - CGContextSetInterpolationQuality(bmContext, kCGInterpolationNone); - - // Rotate - CGContextTranslateCTM(bmContext, - + (rotatedRect.size.width / 2), - + (rotatedRect.size.height / 2)); - CGContextRotateCTM(bmContext, -angleInRadians); - CGContextTranslateCTM(bmContext, - - (rotatedRect.size.width / 2), - - (rotatedRect.size.height / 2)); - - // Flip - if (flipped) - { - CGAffineTransform flipHorizontal = CGAffineTransformMake(-1, 0, 0, 1, floor(rotatedRect.size.width), 0); - CGContextConcatCTM(bmContext, flipHorizontal); - } - - CGContextDrawImage(bmContext, - CGRectMake((rotatedRect.size.width - width)/2.0f, - (rotatedRect.size.height - height)/2.0f, - width, - height), - imgRef); - - CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext); - CFRelease(bmContext); - - return rotatedImage; -} - -- (CGColorSpaceRef)copyColorSpaceWithColorPrimaries:(int)colorPrimaries -{ - const CGFloat whitePoint[] = {0.95047, 1.0, 1.08883}; - const CGFloat blackPoint[] = {0, 0, 0}; - - // See https://developer.apple.com/library/content/technotes/tn2257/_index.html - const CGFloat gamma[] = {1.961, 1.961, 1.961}; - - // RGB/XYZ Matrices (D65 white point) - switch (colorPrimaries) { - case HB_COLR_PRI_EBUTECH: - { - // Rec. 601, 625 line - const CGFloat matrix[] = {0.4305538, 0.2220043, 0.0201822, - 0.3415498, 0.7066548, 0.1295534, - 0.1783523, 0.0713409, 0.9393222}; - return CGColorSpaceCreateCalibratedRGB(whitePoint, blackPoint, gamma, matrix); - } - case HB_COLR_PRI_SMPTEC: - { - // Rec. 601, 525 line - const CGFloat matrix[] = {0.3935209, 0.2123764, 0.0187391, - 0.3652581, 0.7010599, 0.1119339, - 0.1916769, 0.0865638, 0.9583847}; - return CGColorSpaceCreateCalibratedRGB(whitePoint, blackPoint, gamma, matrix); - } - case HB_COLR_PRI_BT2020: - { - // Rec. 2020 - const CGFloat matrix[] = {0.6369580, 0.2627002, 0.0000000, - 0.1446169, 0.6779981, 0.0280727, - 0.1688810, 0.0593017, 1.0609851}; - return CGColorSpaceCreateCalibratedRGB(whitePoint, blackPoint, gamma, matrix); - } - case HB_COLR_PRI_BT709: - default: - { - // Rec. 709 - const CGFloat matrix[] = {0.4124564, 0.2126729, 0.0193339, - 0.3575761, 0.7151522, 0.1191920, - 0.1804375, 0.0721750, 0.9503041}; - return CGColorSpaceCreateCalibratedRGB(whitePoint, blackPoint, gamma, matrix); - } - } -} - -- (CGColorSpaceRef)copyColorSpaceWithPrimaries:(int)primaries transfer:(int)transfer matrix:(int)matrix -{ - if (NSAppKitVersionNumber < NSAppKitVersionNumber10_11) - { - return [self copyColorSpaceWithColorPrimaries:primaries]; - } - - CFStringRef primariesKey = NULL; - switch (primaries) - { - case HB_COLR_PRI_EBUTECH: - primariesKey = kCVImageBufferColorPrimaries_EBU_3213; - break; - - case HB_COLR_PRI_SMPTEC: - primariesKey = kCVImageBufferColorPrimaries_SMPTE_C; - break; - - case HB_COLR_PRI_BT2020: - primariesKey = kCVImageBufferColorPrimaries_ITU_R_2020; - break; - - case HB_COLR_PRI_BT709: - default: - primariesKey = kCVImageBufferColorPrimaries_ITU_R_709_2; - } - - CFStringRef transferKey = NULL; - switch (transfer) - { - case HB_COLR_TRA_SMPTE240M: - transferKey = kCVImageBufferTransferFunction_SMPTE_240M_1995; - break; - - case HB_COLR_TRA_BT2020_10: - case HB_COLR_TRA_BT2020_12: - transferKey = kCVImageBufferTransferFunction_ITU_R_2020; - break; - - case HB_COLR_TRA_BT709: - default: - transferKey = kCVImageBufferTransferFunction_ITU_R_709_2; - } - - CFStringRef matrixKey = NULL; - switch (matrix) - { - case HB_COLR_MAT_SMPTE170M: - matrixKey = kCVImageBufferYCbCrMatrix_ITU_R_601_4; - break; - - case HB_COLR_MAT_SMPTE240M: - matrixKey = kCVImageBufferYCbCrMatrix_SMPTE_240M_1995; - break; - - case HB_COLR_MAT_BT2020_NCL: - case HB_COLR_MAT_BT2020_CL: - matrixKey = kCVImageBufferYCbCrMatrix_ITU_R_2020; - break; - - case HB_COLR_MAT_BT709: - default: - matrixKey = kCVImageBufferYCbCrMatrix_ITU_R_709_2;; - } - - const void *keys[3] = { kCVImageBufferColorPrimariesKey, kCVImageBufferTransferFunctionKey, kCVImageBufferYCbCrMatrixKey }; - const void *values[3] = { primariesKey, transferKey, matrixKey}; - CFDictionaryRef attachments = CFDictionaryCreate(NULL, keys, values, 3, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); - - CGColorSpaceRef colorSpace = CVImageBufferCreateColorSpaceFromAttachments(attachments); - - CFRelease(attachments); - - return colorSpace; -} - (CGImageRef)copyImageAtIndex:(NSUInteger)index forTitle:(HBTitle *)title @@ -516,9 +345,9 @@ typedef void (^HBCoreCleanupHandler)(void); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaNone; CFMutableDataRef imgData = CFDataCreateMutable(kCFAllocatorDefault, 3 * image->width * image->height); CGDataProviderRef provider = CGDataProviderCreateWithCFData(imgData); - CGColorSpaceRef colorSpace = [self copyColorSpaceWithPrimaries:title.hb_title->color_prim - transfer:title.hb_title->color_transfer - matrix:title.hb_title->color_matrix]; + CGColorSpaceRef colorSpace = copyColorSpace(title.hb_title->color_prim, + title.hb_title->color_transfer, + title.hb_title->color_matrix); img = CGImageCreate(image->width, image->height, @@ -555,7 +384,7 @@ typedef void (^HBCoreCleanupHandler)(void); if (angle || flipped) { - CGImageRef rotatedImg = [self CGImageRotatedByAngle:img angle:angle flipped:flipped]; + CGImageRef rotatedImg = CGImageRotated(img, angle, flipped); CGImageRelease(img); return rotatedImg; } diff --git a/macosx/HBImageUtilities.h b/macosx/HBImageUtilities.h new file mode 100644 index 000000000..67f8d00e4 --- /dev/null +++ b/macosx/HBImageUtilities.h @@ -0,0 +1,11 @@ +/* HBColorUtilities.h $ + + This file is part of the HandBrake source code. + Homepage: <http://handbrake.fr/>. + It may be used under the terms of the GNU General Public License. */ + +#import <Foundation/Foundation.h> + +CGImageRef CGImageRotated(CGImageRef imgRef, CGFloat angle, BOOL flipped) CF_RETURNS_RETAINED; +CGColorSpaceRef copyColorSpace(int primaries, int transfer, int matrix); +CGImageRef jnw_decompressedImage(CGImageRef CGImage); diff --git a/macosx/HBImageUtilities.m b/macosx/HBImageUtilities.m new file mode 100644 index 000000000..1c5f62174 --- /dev/null +++ b/macosx/HBImageUtilities.m @@ -0,0 +1,184 @@ +/* HBColorUtilities.m $ + + This file is part of the HandBrake source code. + Homepage: <http://handbrake.fr/>. + It may be used under the terms of the GNU General Public License. */ + +#import "HBImageUtilities.h" +#import <Cocoa/Cocoa.h> +#include "hb.h" + +CGImageRef CGImageRotated(CGImageRef imgRef, CGFloat angle, BOOL flipped) CF_RETURNS_RETAINED +{ + CGFloat angleInRadians = angle * (M_PI / 180); + CGFloat width = CGImageGetWidth(imgRef); + CGFloat height = CGImageGetHeight(imgRef); + + CGRect imgRect = CGRectMake(0, 0, width, height); + CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians); + CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform); + + CGColorSpaceRef colorSpace = CGImageGetColorSpace(imgRef); + CGContextRef bmContext = CGBitmapContextCreate(NULL, + (size_t)rotatedRect.size.width, + (size_t)rotatedRect.size.height, + 8, + 0, + colorSpace, + kCGImageAlphaPremultipliedFirst); + CGContextSetAllowsAntialiasing(bmContext, FALSE); + CGContextSetInterpolationQuality(bmContext, kCGInterpolationNone); + + // Rotate + CGContextTranslateCTM(bmContext, + + (rotatedRect.size.width / 2), + + (rotatedRect.size.height / 2)); + CGContextRotateCTM(bmContext, -angleInRadians); + CGContextTranslateCTM(bmContext, + - (rotatedRect.size.width / 2), + - (rotatedRect.size.height / 2)); + + // Flip + if (flipped) + { + CGAffineTransform flipHorizontal = CGAffineTransformMake(-1, 0, 0, 1, floor(rotatedRect.size.width), 0); + CGContextConcatCTM(bmContext, flipHorizontal); + } + + CGContextDrawImage(bmContext, + CGRectMake((rotatedRect.size.width - width)/2.0f, + (rotatedRect.size.height - height)/2.0f, + width, + height), + imgRef); + + CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext); + CFRelease(bmContext); + + return rotatedImage; +} + +static CGColorSpaceRef copyColorSpaceOld(int colorPrimaries) +{ + const CGFloat whitePoint[] = {0.95047, 1.0, 1.08883}; + const CGFloat blackPoint[] = {0, 0, 0}; + + // See https://developer.apple.com/library/content/technotes/tn2257/_index.html + const CGFloat gamma[] = {1.961, 1.961, 1.961}; + + // RGB/XYZ Matrices (D65 white point) + switch (colorPrimaries) { + case HB_COLR_PRI_EBUTECH: + { + // Rec. 601, 625 line + const CGFloat matrix[] = {0.4305538, 0.2220043, 0.0201822, + 0.3415498, 0.7066548, 0.1295534, + 0.1783523, 0.0713409, 0.9393222}; + return CGColorSpaceCreateCalibratedRGB(whitePoint, blackPoint, gamma, matrix); + } + case HB_COLR_PRI_SMPTEC: + { + // Rec. 601, 525 line + const CGFloat matrix[] = {0.3935209, 0.2123764, 0.0187391, + 0.3652581, 0.7010599, 0.1119339, + 0.1916769, 0.0865638, 0.9583847}; + return CGColorSpaceCreateCalibratedRGB(whitePoint, blackPoint, gamma, matrix); + } + case HB_COLR_PRI_BT2020: + { + // Rec. 2020 + const CGFloat matrix[] = {0.6369580, 0.2627002, 0.0000000, + 0.1446169, 0.6779981, 0.0280727, + 0.1688810, 0.0593017, 1.0609851}; + return CGColorSpaceCreateCalibratedRGB(whitePoint, blackPoint, gamma, matrix); + } + case HB_COLR_PRI_BT709: + default: + { + // Rec. 709 + const CGFloat matrix[] = {0.4124564, 0.2126729, 0.0193339, + 0.3575761, 0.7151522, 0.1191920, + 0.1804375, 0.0721750, 0.9503041}; + return CGColorSpaceCreateCalibratedRGB(whitePoint, blackPoint, gamma, matrix); + } + } +} + +CGColorSpaceRef copyColorSpace(int primaries, int transfer, int matrix) +{ + if (NSAppKitVersionNumber < NSAppKitVersionNumber10_11) + { + return copyColorSpaceOld(primaries); + } + + CFStringRef primariesKey = NULL; + switch (primaries) + { + case HB_COLR_PRI_EBUTECH: + primariesKey = kCVImageBufferColorPrimaries_EBU_3213; + break; + + case HB_COLR_PRI_SMPTEC: + primariesKey = kCVImageBufferColorPrimaries_SMPTE_C; + break; + + case HB_COLR_PRI_BT2020: + primariesKey = kCVImageBufferColorPrimaries_ITU_R_2020; + break; + + case HB_COLR_PRI_BT709: + default: + primariesKey = kCVImageBufferColorPrimaries_ITU_R_709_2; + } + + CFStringRef transferKey = NULL; + switch (transfer) + { + case HB_COLR_TRA_SMPTE240M: + transferKey = kCVImageBufferTransferFunction_SMPTE_240M_1995; + break; + + case HB_COLR_TRA_BT2020_10: + case HB_COLR_TRA_BT2020_12: + transferKey = kCVImageBufferTransferFunction_ITU_R_2020; + break; + + case HB_COLR_TRA_SMPTEST2084: + transferKey = CFSTR("SMPTE_ST_2084_PQ"); //kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ; + break; + + case HB_COLR_TRA_BT709: + default: + transferKey = kCVImageBufferTransferFunction_ITU_R_709_2; + } + + CFStringRef matrixKey = NULL; + switch (matrix) + { + case HB_COLR_MAT_SMPTE170M: + matrixKey = kCVImageBufferYCbCrMatrix_ITU_R_601_4; + break; + + case HB_COLR_MAT_SMPTE240M: + matrixKey = kCVImageBufferYCbCrMatrix_SMPTE_240M_1995; + break; + + case HB_COLR_MAT_BT2020_NCL: + case HB_COLR_MAT_BT2020_CL: + matrixKey = kCVImageBufferYCbCrMatrix_ITU_R_2020; + break; + + case HB_COLR_MAT_BT709: + default: + matrixKey = kCVImageBufferYCbCrMatrix_ITU_R_709_2;; + } + + const void *keys[3] = { kCVImageBufferColorPrimariesKey, kCVImageBufferTransferFunctionKey, kCVImageBufferYCbCrMatrixKey }; + const void *values[3] = { primariesKey, transferKey, matrixKey}; + CFDictionaryRef attachments = CFDictionaryCreate(NULL, keys, values, 3, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); + + CGColorSpaceRef colorSpace = CVImageBufferCreateColorSpaceFromAttachments(attachments); + CFRelease(attachments); + + return colorSpace; +} diff --git a/macosx/HandBrake.xcodeproj/project.pbxproj b/macosx/HandBrake.xcodeproj/project.pbxproj index b206d5182..9f2d9ebfe 100644 --- a/macosx/HandBrake.xcodeproj/project.pbxproj +++ b/macosx/HandBrake.xcodeproj/project.pbxproj @@ -218,6 +218,7 @@ A9BC24C91A69293E007DC41A /* HBAttributedStringAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = A9BC24C81A69293E007DC41A /* HBAttributedStringAdditions.m */; }; A9C0DB85197E7B0000DF55B3 /* SubtitlesDefaults.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9C0DB83197E7B0000DF55B3 /* SubtitlesDefaults.xib */; }; A9C1839D1A716BCC00C897C2 /* HBTitleSelection.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9C1839B1A716BCC00C897C2 /* HBTitleSelection.xib */; }; + A9CE0A921F57EC3400724577 /* HBImageUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = A9CE0A911F57EC3400724577 /* HBImageUtilities.m */; }; A9CF25F11990D62C0023F727 /* Presets.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9CF25EF1990D62C0023F727 /* Presets.xib */; }; A9CF25F71990D6820023F727 /* HBPresetsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A9CF25F61990D6820023F727 /* HBPresetsViewController.m */; }; A9D0FA771C1C284D0003F2A9 /* HBFocusRingView.m in Sources */ = {isa = PBXBuildFile; fileRef = A9D0FA761C1C284D0003F2A9 /* HBFocusRingView.m */; }; @@ -529,6 +530,8 @@ A9C9F88719A733FE00DC8923 /* HBHUDView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HBHUDView.h; sourceTree = "<group>"; }; A9C9F88819A733FE00DC8923 /* HBHUDView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HBHUDView.m; sourceTree = "<group>"; }; A9CAC26E1CCB6B0F00A39E72 /* HBPlayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HBPlayer.h; sourceTree = "<group>"; }; + A9CE0A911F57EC3400724577 /* HBImageUtilities.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HBImageUtilities.m; sourceTree = "<group>"; }; + A9CE0A931F57EC4600724577 /* HBImageUtilities.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HBImageUtilities.h; sourceTree = "<group>"; }; A9CF25F01990D62C0023F727 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = Presets.xib; sourceTree = "<group>"; }; A9CF25F21990D64E0023F727 /* HBPreset.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HBPreset.h; sourceTree = "<group>"; }; A9CF25F31990D64E0023F727 /* HBPreset.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HBPreset.m; sourceTree = "<group>"; }; @@ -961,6 +964,8 @@ children = ( 273F209714ADBE670021BE6D /* HBDVDDetector.h */, 273F209814ADBE670021BE6D /* HBDVDDetector.m */, + A9CE0A931F57EC4600724577 /* HBImageUtilities.h */, + A9CE0A911F57EC3400724577 /* HBImageUtilities.m */, A9160A331AE7A165009A7818 /* HBCodingUtilities.h */, A9160A341AE7A165009A7818 /* HBCodingUtilities.m */, A997D8EB1A4ABB0900E19B6F /* HBPresetCoding.h */, @@ -1565,6 +1570,7 @@ A91CE2F81C7DB96D0068F46F /* HBPreset.m in Sources */, A98F38071C7DCA7E00E469C8 /* HBStateFormatter+Private.m in Sources */, A91CE2F91C7DB96D0068F46F /* HBMutablePreset.m in Sources */, + A9CE0A921F57EC3400724577 /* HBImageUtilities.m in Sources */, A91CE2FA1C7DB96D0068F46F /* HBTreeNode.m in Sources */, A91CE2B01C7DA9FB0068F46F /* HBUtilities.m in Sources */, A91CE27D1C7DA7320068F46F /* HBCore.m in Sources */, |