Last active
August 29, 2015 14:00
-
-
Save capnslipp/11388990 to your computer and use it in GitHub Desktop.
Quick test of CGBitmapContextCreate's CGImageAlphaInfo modes, for http://stackoverflow.com/questions/19674740/opengl-es2-premultiplied-vs-straight-alpha-blending/23256585#comments-23256585
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "TestCGBitmapContextCreate_iOSTests.h" | |
| @implementation TestCGBitmapContextCreate_iOSTests | |
| - (void)setUp | |
| { | |
| [super setUp]; | |
| _colorSpace = CGColorSpaceCreateDeviceRGB(); | |
| } | |
| - (void)tearDown | |
| { | |
| CGColorSpaceRelease(_colorSpace); | |
| [super tearDown]; | |
| } | |
| static const size_t kWidth = 512, kHeight = 512; | |
| static const size_t kBitsPerComponent = 8; | |
| static const size_t kComponentsPerPixel = 4; | |
| - (void)subTestCGImageAlphaMode:(CGImageAlphaInfo)alphaMode expectSuccess:(BOOL)shouldBeSuccessful | |
| { | |
| size_t bytesPerRow = kBitsPerComponent * kComponentsPerPixel * kWidth / 8; | |
| CGContextRef cgContext = CGBitmapContextCreate( | |
| NULL, // data | |
| kWidth, kHeight, | |
| kBitsPerComponent, bytesPerRow, | |
| _colorSpace, | |
| alphaMode | |
| ); | |
| NSLog(@"\t" @"cgContext with CGImageAlphaInfo %d: %@", alphaMode, cgContext); | |
| if (shouldBeSuccessful) | |
| STAssertNotNil((__bridge id)cgContext, @"Unable to create context with CGImageAlphaInfo %d", alphaMode); | |
| else | |
| STAssertNil((__bridge id)cgContext, @"Unexpectedly created context with CGImageAlphaInfo %d", alphaMode); | |
| CGContextRelease(cgContext); | |
| } | |
| - (void)subTestCGImageAlphaMode:(CGImageAlphaInfo)alphaMode { | |
| [self subTestCGImageAlphaMode:alphaMode expectSuccess:YES]; | |
| } | |
| - (void)testAllCGImageAlphaModes | |
| { | |
| [self subTestCGImageAlphaMode:kCGImageAlphaNone]; | |
| [self subTestCGImageAlphaMode:kCGImageAlphaPremultipliedLast]; | |
| [self subTestCGImageAlphaMode:kCGImageAlphaPremultipliedFirst]; | |
| [self subTestCGImageAlphaMode:kCGImageAlphaLast]; | |
| [self subTestCGImageAlphaMode:kCGImageAlphaFirst]; | |
| [self subTestCGImageAlphaMode:kCGImageAlphaNoneSkipLast]; | |
| [self subTestCGImageAlphaMode:kCGImageAlphaNoneSkipFirst]; | |
| [self subTestCGImageAlphaMode:kCGImageAlphaOnly]; | |
| } | |
| - (void)testInvalidCGImageAlphaModes | |
| { | |
| unsigned int highestCGImageAlphaMode = 0; | |
| highestCGImageAlphaMode = (kCGImageAlphaNone > highestCGImageAlphaMode) ? kCGImageAlphaNone : highestCGImageAlphaMode; | |
| highestCGImageAlphaMode = (kCGImageAlphaPremultipliedLast > highestCGImageAlphaMode) ? kCGImageAlphaPremultipliedLast : highestCGImageAlphaMode; | |
| highestCGImageAlphaMode = (kCGImageAlphaPremultipliedFirst > highestCGImageAlphaMode) ? kCGImageAlphaPremultipliedFirst : highestCGImageAlphaMode; | |
| highestCGImageAlphaMode = (kCGImageAlphaLast > highestCGImageAlphaMode) ? kCGImageAlphaLast : highestCGImageAlphaMode; | |
| highestCGImageAlphaMode = (kCGImageAlphaFirst > highestCGImageAlphaMode) ? kCGImageAlphaFirst : highestCGImageAlphaMode; | |
| highestCGImageAlphaMode = (kCGImageAlphaNoneSkipLast > highestCGImageAlphaMode) ? kCGImageAlphaNoneSkipLast : highestCGImageAlphaMode; | |
| highestCGImageAlphaMode = (kCGImageAlphaNoneSkipFirst > highestCGImageAlphaMode) ? kCGImageAlphaNoneSkipFirst : highestCGImageAlphaMode; | |
| highestCGImageAlphaMode = (kCGImageAlphaOnly > highestCGImageAlphaMode) ? kCGImageAlphaOnly : highestCGImageAlphaMode; | |
| for (int invalidModeI = (highestCGImageAlphaMode + 1); invalidModeI <= kCGBitmapAlphaInfoMask; ++invalidModeI) { | |
| [self subTestCGImageAlphaMode:invalidModeI expectSuccess:NO]; | |
| } | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment