-
Notifications
You must be signed in to change notification settings - Fork 3
/
v002FilmTechnicolor3Plugin.m
201 lines (155 loc) · 5.92 KB
/
v002FilmTechnicolor3Plugin.m
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// v002FBOGLSLTemplatePlugIn.m
// v002FBOGLSLTemplate
//
// Created by vade on 6/30/08.
// Copyright (c) 2008 __MyCompanyName__. All rights reserved.
//
/* It's highly recommended to use CGL macros instead of changing the current context for plug-ins that perform OpenGL rendering */
#import <OpenGL/CGLMacro.h>
#import "v002FilmTechnicolor3Plugin.h"
#define kQCPlugIn_Name @"v002 Technicolor 3"
#define kQCPlugIn_Description @"Technicolor System 3 emulation. 3 Strip subtractive process."
#pragma mark -
#pragma mark Static Functions
static void _TextureReleaseCallback(CGLContextObj cgl_ctx, GLuint name, void* info)
{
glDeleteTextures(1, &name);
}
@implementation v002FilmTechnicolor3Plugin
@dynamic inputImage;
@dynamic inputAmount;
@dynamic outputImage;
+ (NSDictionary*) attributes
{
return [NSDictionary dictionaryWithObjectsAndKeys:kQCPlugIn_Name, QCPlugInAttributeNameKey,
[kQCPlugIn_Description stringByAppendingString:kv002DescriptionAddOnText], QCPlugInAttributeDescriptionKey,
kQCPlugIn_Category, @"categories", nil];
}
+ (NSDictionary*) attributesForPropertyPortWithKey:(NSString*)key
{
if([key isEqualToString:@"inputImage"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Image", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"inputAmount"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Amount", QCPortAttributeNameKey,
[NSNumber numberWithFloat:0.0], QCPortAttributeMinimumValueKey,
[NSNumber numberWithFloat:0.0], QCPortAttributeDefaultValueKey,
[NSNumber numberWithFloat:1.0], QCPortAttributeMaximumValueKey,
nil];
}
if([key isEqualToString:@"outputImage"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Image", QCPortAttributeNameKey, nil];
}
return nil;
}
+ (NSArray*) sortedPropertyPortKeys
{
return [NSArray arrayWithObjects:@"inputImage", @"inputAmount", nil];
}
+ (QCPlugInExecutionMode) executionMode
{
return kQCPlugInExecutionModeProcessor;
}
+ (QCPlugInTimeMode) timeMode
{
return kQCPlugInTimeModeNone;
}
- (id) init
{
if(self = [super init])
{
self.pluginShaderName = @"v002.film-technicolor3";
}
return self;
}
@end
@implementation v002FilmTechnicolor3Plugin (Execution)
- (BOOL) execute:(id<QCPlugInContext>)context atTime:(NSTimeInterval)time withArguments:(NSDictionary*)arguments
{
CGLContextObj cgl_ctx = [context CGLContextObj];
CGLLockContext(cgl_ctx);
id<QCPlugInInputImageSource> image = self.inputImage;
CGColorSpaceRef cspace = ([image shouldColorMatch]) ? [context colorSpace] : [image imageColorSpace];
if(image && [image lockTextureRepresentationWithColorSpace:cspace forBounds:[image imageBounds]])
{
[image bindTextureRepresentationToCGLContext:[context CGLContextObj] textureUnit:GL_TEXTURE0 normalizeCoordinates:YES];
GLuint finalOutput = [self renderToFBO:cgl_ctx width:[image imageBounds].size.width height:[image imageBounds].size.height bounds:[image imageBounds] texture:[image textureName] amount:self.inputAmount];
id provider = nil;
if(finalOutput != 0)
{
#if __BIG_ENDIAN__
#define v002QCPluginPixelFormat QCPlugInPixelFormatARGB8
#else
#define v002QCPluginPixelFormat QCPlugInPixelFormatBGRA8
#endif
// we have to use a 4 channel output format, I8 does not support alpha at fucking all, so if we want text with alpha, we need to use this and waste space. Ugh.
provider = [context outputImageProviderFromTextureWithPixelFormat:v002QCPluginPixelFormat pixelsWide:[image imageBounds].size.width pixelsHigh:[image imageBounds].size.height name:finalOutput flipped:NO releaseCallback:_TextureReleaseCallback releaseContext:NULL colorSpace:[context colorSpace] shouldColorMatch:[image shouldColorMatch]];
self.outputImage = provider;
}
[image unbindTextureRepresentationFromCGLContext:[context CGLContextObj] textureUnit:GL_TEXTURE0];
[image unlockTextureRepresentation];
self.outputImage = provider;
}
else
self.outputImage = nil;
CGLUnlockContext(cgl_ctx);
return YES;
}
- (GLuint) renderToFBO:(CGLContextObj)context width:(NSUInteger)pixelsWide height:(NSUInteger)pixelsHigh bounds:(NSRect)bounds texture:(GLuint)texture amount:(double)amount
{
GLsizei width = bounds.size.width, height = bounds.size.height;
CGLContextObj cgl_ctx = context;
[pluginFBO pushAttributes:cgl_ctx];
glEnable(GL_TEXTURE_RECTANGLE_EXT);
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, tex);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
[pluginFBO pushFBO:cgl_ctx];
[pluginFBO attachFBO:cgl_ctx withTexture:tex width:width height:height];
glColor4f(1.0, 1.0, 1.0, 1.0);
glEnable(GL_TEXTURE_RECTANGLE_EXT);
glBindTexture(GL_TEXTURE_RECTANGLE_EXT, texture);
// do not need blending if we use black border for alpha and replace env mode, saves a buffer wipe
// we can do this since our image draws over the complete surface of the FBO, no pixel goes untouched.
glDisable(GL_BLEND);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
// bind our shader program
glUseProgramObjectARB([pluginShader programObject]);
// set program vars
glUniform1iARB([pluginShader getUniformLocation:"tex0"], 0);
glUniform1fARB([pluginShader getUniformLocation:"amount"], amount);
// move to VA for rendering
GLfloat tex_coords[] =
{
1.0,1.0,
0.0,1.0,
0.0,0.0,
1.0,0.0
};
GLfloat verts[] =
{
width,height,
0.0,height,
0.0,0.0,
width,0.0
};
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glTexCoordPointer(2, GL_FLOAT, 0, tex_coords );
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, verts );
glDrawArrays( GL_TRIANGLE_FAN, 0, 4 ); // TODO: GL_QUADS or GL_TRIANGLE_FAN?
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
glDisableClientState(GL_VERTEX_ARRAY);
// disable shader program
glUseProgramObjectARB(NULL);
[pluginFBO detachFBO:cgl_ctx];
[pluginFBO popFBO:cgl_ctx];
[pluginFBO popAttributes:cgl_ctx];
return tex;
}
@end