-
Notifications
You must be signed in to change notification settings - Fork 1
/
v002GlitchImageReaderPlugin.m
288 lines (223 loc) · 7.64 KB
/
v002GlitchImageReaderPlugin.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//
// v002GlitchImageReader.m
// v002Glitch
//
// Created by vade on 12/23/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "v002GlitchImageReaderPlugin.h"
/* 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 "v002GlitchImageReaderPlugIn.h"
#define kQCPlugIn_Name @"v002 Glitch Image Reader"
#define kQCPlugIn_Description @""
static void _BufferReleaseCallback(const void* address, void* info)
{
// we dont free, because we want to keep the old data around should it not be updated...
free((void*)address);
}
@implementation v002GlitchImageReaderPlugIn
@synthesize fileData, originalFileData;
@dynamic outputImage;
@dynamic inputFilePath;
@dynamic inputGlitchOffset;
@dynamic inputGlitchLength;
@dynamic inputReGlitch;
+ (NSDictionary*) attributes
{
return [NSDictionary dictionaryWithObjectsAndKeys:kQCPlugIn_Name, QCPlugInAttributeNameKey, kQCPlugIn_Description, QCPlugInAttributeDescriptionKey, nil];
}
+ (NSDictionary*) attributesForPropertyPortWithKey:(NSString*)key
{
if([key isEqualToString:@"inputGlitchOffset"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Glitch Offset", QCPortAttributeNameKey,
[NSNumber numberWithInt:0], QCPortAttributeMinimumValueKey,
[NSNumber numberWithInt:0], QCPortAttributeDefaultValueKey,
[NSNumber numberWithInt:1], QCPortAttributeMaximumValueKey,
nil];
}
if([key isEqualToString:@"inputGlitchLength"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Glitch Length", QCPortAttributeNameKey,
[NSNumber numberWithInt:0], QCPortAttributeMinimumValueKey,
[NSNumber numberWithInt:0], QCPortAttributeDefaultValueKey,
[NSNumber numberWithInt:1], QCPortAttributeMaximumValueKey,
nil];
}
if([key isEqualToString:@"inputFilePath"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"File Path", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"inputReGlitch"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Re-Glitch", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"outputImage"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Image", QCPortAttributeNameKey, nil];
}
return nil;
}
+ (NSArray*) sortedPropertyPortKeys
{
return [NSArray arrayWithObjects:@"inputFilePath", @"inputGlitchOffset", nil];
}
+ (QCPlugInExecutionMode) executionMode
{
return kQCPlugInExecutionModeProvider;
}
+ (QCPlugInTimeMode) timeMode
{
// TODO: is this right?
return kQCPlugInTimeModeIdle;
}
- (id) init
{
if(self = [super init])
{
lock = [[NSLock alloc] init];
fileData = [[NSMutableData alloc] init];
makeNewProvider = TRUE;
}
return self;
}
- (void) finalize
{
[super finalize];
}
- (void) dealloc
{
[lock release];
[super dealloc];
}
@end
@implementation v002GlitchImageReaderPlugIn (Execution)
- (BOOL) startExecution:(id<QCPlugInContext>)context
{
return YES;
}
- (void) enableExecution:(id<QCPlugInContext>)context
{
}
- (BOOL) execute:(id<QCPlugInContext>)context atTime:(NSTimeInterval)time withArguments:(NSDictionary*)arguments
{
// setup width/height/rowBytes
if([self didValueForInputKeyChange:@"inputFilePath"])
{
if([[NSFileManager defaultManager] fileExistsAtPath:self.inputFilePath])
{
[self setOriginalFileData:[[NSMutableData alloc] initWithContentsOfFile:self.inputFilePath]];
[self setFileData:[[NSMutableData alloc] initWithBytes:[originalFileData bytes] length:[originalFileData length]]];
}
makeNewProvider = TRUE;
}
// now that we have the data, lets fuck with it
if([self didValueForInputKeyChange:@"inputGlitchOffset"] || [self didValueForInputKeyChange:@"inputGlitchLength"])
{
NSUInteger bufferLen = [originalFileData length];
// TODO: range checking argh.
NSRange byteRange = {self.inputGlitchOffset * bufferLen, self.inputGlitchLength * bufferLen};
unsigned char* glitchBuffer;
NSUInteger glitchLength = byteRange.length;
glitchBuffer = valloc(glitchLength);
// set memory values to random for now
for(int i = 0; i < glitchLength; i++)
{
*(glitchBuffer + i) = random() % 255;
}
if(!self.inputReGlitch)
{
// if we do a new glitch, replace with the original bytes
NSRange originalRange = {0, [originalFileData length]};
[fileData replaceBytesInRange:originalRange withBytes:[originalFileData bytes]];
}
// copy aBuffer into fileData
[fileData replaceBytesInRange:byteRange withBytes:glitchBuffer];
// clean up
free(glitchBuffer);
makeNewProvider = TRUE;
}
// create our image from our image data, from fastimage
if(fileData != nil && makeNewProvider)
{
// try and make an image with our glitched file data.
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef) fileData, NULL);
if(source == NULL)
return NO;
CGImageRef image = CGImageSourceCreateImageAtIndex(source, 0, NULL);
CFRelease(source);
if(image == NULL)
{
CGImageRelease(image);
return NO;
}
size_t rowBytes = CGImageGetWidth(image) * 4;
if(rowBytes % 16)
rowBytes = ((rowBytes / 16) + 1) * 16;
void* baseAddress = valloc(CGImageGetHeight(image) * rowBytes);
if(baseAddress == NULL)
{
CGImageRelease(image);
return NO;
}
// Create CGContext and draw image into it
CGContextRef bitmapContext = CGBitmapContextCreate(baseAddress, CGImageGetWidth(image), CGImageGetHeight(image), 8, rowBytes, [context colorSpace], kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
if(bitmapContext == NULL)
{
free(baseAddress);
CGImageRelease(image);
return NO;
}
CGRect bounds = CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image));
CGContextClearRect(bitmapContext, bounds);
CGContextDrawImage(bitmapContext, bounds, image);
// Create image provider from backing
#if __BIG_ENDIAN__
cachedProvider = [context outputImageProviderFromBufferWithPixelFormat:QCPlugInPixelFormatARGB8 pixelsWide:CGImageGetWidth(image) pixelsHigh:CGImageGetHeight(image) baseAddress:baseAddress bytesPerRow:rowBytes releaseCallback:_BufferReleaseCallback releaseContext:NULL colorSpace:[context colorSpace] shouldColorMatch:YES];
#else
cachedProvider = [context outputImageProviderFromBufferWithPixelFormat:QCPlugInPixelFormatBGRA8 pixelsWide:CGImageGetWidth(image) pixelsHigh:CGImageGetHeight(image) baseAddress:baseAddress bytesPerRow:rowBytes releaseCallback:_BufferReleaseCallback releaseContext:NULL colorSpace:[context colorSpace] shouldColorMatch:YES];
#endif
// We don't need the image and context anymore
CGImageRelease(image);
CGContextRelease(bitmapContext);
[cachedProvider retain];
makeNewProvider = FALSE;
}
self.outputImage = cachedProvider;
return YES;
}
- (void) disableExecution:(id<QCPlugInContext>)context
{
}
- (void) stopExecution:(id<QCPlugInContext>)context
{
[originalFileData release];
[fileData release];
}
/*
- (void) seekToFileOffsetInBackground:(NSNumber*) offset
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[fileHandle seekToFileOffset:[offset unsignedLongLongValue]];
NSData* someData = [fileHandle readDataOfLength:(rowBytes * height)];
[self performSelectorOnMainThread:@selector(returnDataOnMainThread:) withObject:someData waitUntilDone:NO];
[pool release];
}
- (void) returnDataOnMainThread:(NSData*) data
{
// update the contents of fileData - place contents of data into aBuffer
NSUInteger bufferLen = [data length];
unsigned char* aBuffer;
aBuffer = malloc(bufferLen);
[data getBytes:aBuffer] ;
NSRange byteRange = {0, bufferLen};
// copy aBuffer into fileData
[lock lock];
[fileData replaceBytesInRange:byteRange withBytes:aBuffer];
[lock unlock];
// clean up
free(aBuffer);
}
*/
@end