Skip to content

Commit

Permalink
Another Objective-C plugin, showing how to do various things in Core …
Browse files Browse the repository at this point in the history
…Graphics.
  • Loading branch information
ccgus committed Sep 11, 2012
1 parent 2226ca6 commit 41a72c7
Show file tree
Hide file tree
Showing 5 changed files with 426 additions and 0 deletions.
28 changes: 28 additions & 0 deletions objective-c/RandomCoreGraphicsGenerator/Info.plist
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>FSPluginMenuName</key>
<string>Convert to Grayscale RandomCG</string>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>com.yourcompany.RandomCG</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>NSPrincipalClass</key>
<string>RandomCG</string>
</dict>
</plist>
20 changes: 20 additions & 0 deletions objective-c/RandomCoreGraphicsGenerator/RandomCG.h
@@ -0,0 +1,20 @@
//
// RandomCG.h
// RandomCG
//
// Created by August Mueller on 10/15/07.
// Copyright Flying Meat Inc 2007 . All rights reserved.
//


#import <Cocoa/Cocoa.h>
#import <QuartzCore/QuartzCore.h>
#import "ACPlugin.h"

#import "RandomCG.h"

@interface RandomCG : NSObject <ACPlugin> {

}

@end
111 changes: 111 additions & 0 deletions objective-c/RandomCoreGraphicsGenerator/RandomCG.m
@@ -0,0 +1,111 @@
//
// RandomCG.m
// RandomCG
//
// Created by August Mueller on 10/15/07.
// Copyright Flying Meat Inc 2007 . All rights reserved.
//

#import "RandomCG.h"

#define PI 3.14159265358979323846

@implementation RandomCG

+ (id)plugin {
return [[[self alloc] init] autorelease];
}

- (void) willRegister:(id<ACPluginManager>)pluginManager {

[pluginManager addFilterMenuTitle:@"Random CG Calls"
withSuperMenuTitle:@"Generator"
target:self
action:@selector(make:userObject:)
keyEquivalent:@""
keyEquivalentModifierMask:0
userObject:nil];
}

- (void)didRegister {

}

- (CIImage*)make:(CIImage*)image userObject:(id)uo {

int w = [image extent].size.width;
int h = [image extent].size.height;

CGColorSpaceRef cs = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);

CGContextRef context = CGBitmapContextCreate(nil, w, h, 8, 0, cs, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);

CGColorSpaceRelease(cs);

// Fill with a random color
CGContextSetRGBFillColor(context, (rand()%256)/255.0, (rand()%256)/255.0, (rand()%256)/255.0, 1);
CGContextBeginPath(context);
CGContextAddRect(context, CGRectMake(0, 0, w, h));
CGContextFillPath(context);

for (int i = 0; i < 20; i++) {
int numberOfSegments = rand() % 8;
int j;
float sx, sy;

CGContextBeginPath(context);
sx = rand()%w; sy = rand()%h;
CGContextMoveToPoint(context, rand()%w, rand()%h);
for (j = 0; j < numberOfSegments; j++) {
if (j % 2) {
CGContextAddLineToPoint(context, rand()%w, rand()%h);
}
else {
CGContextAddCurveToPoint(context, rand()%w, rand()%h,
rand()%w, rand()%h, rand()%h, rand()%h);
}
}
if(i % 2) {
CGContextAddCurveToPoint(context, rand()%w, rand()%h,
rand()%w, rand()%h, sx, sy);
CGContextClosePath(context);
CGContextSetRGBFillColor(context, (float)(rand()%256)/255,
(float)(rand()%256)/255, (float)(rand()%256)/255,
(float)(rand()%256)/255);
CGContextFillPath(context);
}
else {
CGContextSetLineWidth(context, (rand()%10)+2);
CGContextSetRGBStrokeColor(context, (float)(rand()%256)/255,
(float)(rand()%256)/255, (float)(rand()%256)/255,
(float)(rand()%256)/255);
CGContextStrokePath(context);
}
}


CGImageRef imageRef = CGBitmapContextCreateImage(context);

CGContextRelease(context);

CIImage *returnImage = [CIImage imageWithCGImage:imageRef];

CGImageRelease(imageRef);

return returnImage;
}

- (NSNumber*)worksOnShapeLayers:(id)userObject {
return [NSNumber numberWithBool:NO];
}

- (NSNumber*)validateForLayer:(id<ACLayer>)layer {

if ([layer layerType] == ACBitmapLayer) {
[NSNumber numberWithBool:YES];
}

return [NSNumber numberWithBool:NO];
}

@end

0 comments on commit 41a72c7

Please sign in to comment.