-
Notifications
You must be signed in to change notification settings - Fork 1
/
cg.mm
127 lines (105 loc) · 3.79 KB
/
cg.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// clang++ cg.mm -framework Cocoa -framework QuartzCore && ./a.out
// Press q to quit.
// See the printf-ed comments below.
#include <Cocoa/Cocoa.h>
#include <QuartzCore/CALayer.h>
#include <QuartzCore/QuartzCore.h>
@interface MyLayer : CALayer
@end
@interface MainWindow : NSWindow
@end
@interface MyLayerDelegate : NSObject <CALayerDelegate>
@end
MainWindow* window = nil;
MyLayer* my_layer = nil;
int width = 32*16;
int height = 64;
CGRect draw_rect;
@implementation MyLayer
- (void)drawInContext:(CGContextRef)ctx {
for (size_t i = 0; i < 17; ++i) {
CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceExtendedSRGB);
CGFloat components[4] = {i/8.f, 0, 0, 1};
CGColorRef color = CGColorCreate(space, components);
CGContextSetFillColorWithColor(ctx, color);
CFRelease(color);
CGContextFillRect(ctx, CGRectMake(i*32, 32, 32, 32));
}
{
CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceDisplayP3);
CGFloat components[4] = {1, 0, 0, 1};
CGColorRef color = CGColorCreate(space, components);
CGContextSetFillColorWithColor(ctx, color);
CFRelease(color);
CGContextFillRect(ctx, CGRectMake(0, 0, 32, 32));
}
}
@end
@implementation MainWindow
- (void)keyDown:(NSEvent *)event {
if ([event isARepeat])
return;
NSString *characters = [event charactersIgnoringModifiers];
if ([characters length] != 1)
return;
switch ([characters characterAtIndex:0]) {
case 'q':
[NSApp terminate:nil];
break;
default:
break;
}
}
@end
int main(int argc, char* argv[]) {
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
NSMenu* menubar = [NSMenu alloc];
[NSApp setMainMenu:menubar];
window = [[MainWindow alloc]
initWithContentRect:NSMakeRect(0, 0, width, height)
styleMask:NSWindowStyleMaskResizable | NSWindowStyleMaskTitled
backing:NSBackingStoreBuffered
defer:NO];
[window setOpaque:YES];
CALayer* layer = [[CALayer alloc] init];
[[window contentView] setLayer:layer];
[[window contentView] setWantsLayer:YES];
my_layer = [[MyLayer alloc] init];
[my_layer setShouldRasterize:YES];
[my_layer setDrawsAsynchronously:NO];
CGFloat components[4] = {0, 2, 0, 1};
CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceExtendedSRGB);
CGColorRef color = CGColorCreate(space, components);
[layer addSublayer:my_layer];
[my_layer setFrame:CGRectMake(0, 0, width, height)];
[my_layer setBackgroundColor:color];
[my_layer setNeedsDisplay];
[window setTitle:@"Test"];
[window makeKeyAndOrderFront:nil];
printf("This program draws the same content to a CALayer and to an sRGB bitmap\n");
printf("The content is a gradient in kCGColorSpaceExtendedSRGB with red going from 0 to 2\n");
printf("When drawn to a CGBitmapContext, the color saturates and stops changing when red>1\n");
printf("In the CALayer, the gradient becomes a pale orange when red>1\n");
printf("* UNLESS, I force my monitor's profile to sRGB in Settings, then it saturates!\n");
printf("* (but not Display P3)\n");
printf("\n");
printf("Rendering to a bitmap context and reading it back. The graident is:\n");
{
uint32_t* pixels = new uint32_t[width*height];
memset(pixels, 0, 4*width*height);
CGContextRef ctx = CGBitmapContextCreate(
pixels, width, height, 8, 4*width, CGColorSpaceCreateWithName(kCGColorSpaceSRGB), kCGImageAlphaPremultipliedLast|kCGImageByteOrder32Little);
CFShow(ctx);
[my_layer drawInContext:ctx];
CFRelease(ctx);
printf("The gradient is:\n");
for (int x = 0; x < width; x += 16) {
printf(" At %d,%d, the color is %x\n", x, 0, pixels[x]);
}
printf("The color(display-p3 1 0 0) is: %x\n", pixels[32*width]);
}
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
return 0;
}