Skip to content

Commit

Permalink
added noise generator
Browse files Browse the repository at this point in the history
bdudney committed Apr 9, 2012

Verified

This commit was signed with the committer’s verified signature.
1 parent 4519d18 commit f3b38bc
Showing 12 changed files with 211 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Convolver/Convolver.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
6E1A3C6F151789A200655F86 /* Accelerate.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6E1A3C6E151789A200655F86 /* Accelerate.framework */; };
6E1A3C7315178A3F00655F86 /* GFSImageConvolver.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E1A3C72151789B800655F86 /* GFSImageConvolver.m */; };
6E1A3C7415178A4D00655F86 /* phillip.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 6E1A3C70151789B800655F86 /* phillip.jpg */; };
6E6ED214152F8828003EC3CC /* GFSNoiseGenerator.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E6ED213152F8828003EC3CC /* GFSNoiseGenerator.m */; };
6E80C6D5151E4D37006F02D6 /* GFSVImageLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E80C6D4151E4D37006F02D6 /* GFSVImageLoader.m */; };
6E80C6D8151E526D006F02D6 /* GFSImageSeparator.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E80C6D7151E526D006F02D6 /* GFSImageSeparator.m */; };
/* End PBXBuildFile section */
@@ -42,6 +43,8 @@
6E1A3C70151789B800655F86 /* phillip.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = phillip.jpg; sourceTree = "<group>"; };
6E1A3C71151789B800655F86 /* GFSImageConvolver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GFSImageConvolver.h; sourceTree = "<group>"; };
6E1A3C72151789B800655F86 /* GFSImageConvolver.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GFSImageConvolver.m; sourceTree = "<group>"; };
6E6ED212152F8828003EC3CC /* GFSNoiseGenerator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GFSNoiseGenerator.h; sourceTree = "<group>"; };
6E6ED213152F8828003EC3CC /* GFSNoiseGenerator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GFSNoiseGenerator.m; sourceTree = "<group>"; };
6E80C6D3151E4D37006F02D6 /* GFSVImageLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GFSVImageLoader.h; sourceTree = "<group>"; };
6E80C6D4151E4D37006F02D6 /* GFSVImageLoader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GFSVImageLoader.m; sourceTree = "<group>"; };
6E80C6D6151E526D006F02D6 /* GFSImageSeparator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GFSImageSeparator.h; sourceTree = "<group>"; };
@@ -109,6 +112,8 @@
6E1A3C641517893100655F86 /* GFSViewController.h */,
6E1A3C651517893100655F86 /* GFSViewController.m */,
6E1A3C561517893100655F86 /* Supporting Files */,
6E6ED212152F8828003EC3CC /* GFSNoiseGenerator.h */,
6E6ED213152F8828003EC3CC /* GFSNoiseGenerator.m */,
);
path = Convolver;
sourceTree = "<group>";
@@ -195,6 +200,7 @@
6E1A3C7315178A3F00655F86 /* GFSImageConvolver.m in Sources */,
6E80C6D5151E4D37006F02D6 /* GFSVImageLoader.m in Sources */,
6E80C6D8151E526D006F02D6 /* GFSImageSeparator.m in Sources */,
6E6ED214152F8828003EC3CC /* GFSNoiseGenerator.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -2,4 +2,13 @@
<Bucket
type = "1"
version = "1.0">
<SymbolicBreakpoints>
<SymbolicBreakpoint
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "_isResizable"
moduleName = "">
</SymbolicBreakpoint>
</SymbolicBreakpoints>
</Bucket>
2 changes: 2 additions & 0 deletions Convolver/Convolver/GFSImageConvolver.h
Original file line number Diff line number Diff line change
@@ -46,6 +46,8 @@

- (id)initWithURL:(NSURL *)orignalImageURL;

- (id)initWithImageData:(NSData *)data imageSize:(CGSize)imageSize;

// memcopy the values into a new array of widthxheight shorts
- (void)setKernel:(short *)values width:(short)width height:(short)height;

16 changes: 16 additions & 0 deletions Convolver/Convolver/GFSImageConvolver.m
Original file line number Diff line number Diff line change
@@ -65,6 +65,22 @@ - (id)initWithURL:(NSURL *)orignalImageURL {

}

- (id)initWithImageData:(NSData *)data imageSize:(CGSize)imageSize {
self = [super initWithCompliantData:data imageSize:imageSize];
if(nil != self) {
short blurKernel[] = {
1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, 1.0
};
[self setKernel:blurKernel width:3 height:3];
// default to black background
self.backgroundColor = (GFSConvolverColor){0,0,0,0};
self.divsor = 81;
}
return self;
}

// memcopy the values into a new array of widthxheight shorts
- (void)setKernel:(short *)values width:(short)width height:(short)height {
short *newKernel = calloc(height * width, sizeof(short));
7 changes: 7 additions & 0 deletions Convolver/Convolver/GFSNoiseGenerator.h
Original file line number Diff line number Diff line change
@@ -10,4 +10,11 @@

@interface GFSNoiseGenerator : NSObject

- (id)initWithSize:(CGSize)size octaves:(NSUInteger)octaveCount;

@property(nonatomic, assign) NSUInteger octaveCount;
@property(nonatomic, assign) CGSize size;

- (UIImage *)noiseImage;

@end
146 changes: 146 additions & 0 deletions Convolver/Convolver/GFSNoiseGenerator.m
Original file line number Diff line number Diff line change
@@ -7,7 +7,153 @@
//

#import "GFSNoiseGenerator.h"
#import "GFSVImageLoader.h"
#import "GFSImageConvolver.h"

@interface GFSNoiseGenerator()

@property(nonatomic, strong) NSData *baseNoise;
@property(nonatomic, strong) GFSImageConvolver *horizontalConvolver;
@property(nonatomic, strong) GFSImageConvolver *verticalConvolver;
@property(nonatomic, strong) UIImage *bluredImage;

@end

@interface GFSNoiseGenerator(Private)

@end

@implementation GFSNoiseGenerator

@synthesize horizontalConvolver = _horizontalConvolver;
@synthesize verticalConvolver = _verticalConvolver;
@synthesize baseNoise = _baseNoise;
@synthesize bluredImage = _bluredImage;
@synthesize octaveCount = _octaveCount;
@synthesize size = _size;

- (id)initWithSize:(CGSize)size octaves:(NSUInteger)octaveCount {
self = [super init];
if(nil != self) {
self.size = size;
self.octaveCount = octaveCount;
}
return self;
}

typedef struct GFSConvolverLumance { // this order only works for big endian
uint8_t a;
uint8_t l;
} GFSConvolverLumance;


- (NSData *)baseLANoise {
if(nil == _baseNoise) {
NSUInteger count = self.size.width * self.size.height;
GFSConvolverLumance *colors = (GFSConvolverLumance *)calloc(count, sizeof(GFSConvolverLumance));
srandomdev();
for(NSUInteger i = 0;i < count;i++) {
uint8_t value = random() % 256;
colors[i].l = value;
colors[i].a = 255;
}
self.baseNoise = [NSData dataWithBytesNoCopy:(void *)colors
length:sizeof(GFSConvolverLumance) * count
freeWhenDone:YES];
}
return _baseNoise;
}

- (NSData *)baseRGBANoise {
if(nil == _baseNoise) {
NSUInteger count = self.size.width * self.size.height;
GFSConvolverColor *colors = (GFSConvolverColor *)calloc(count, sizeof(GFSConvolverColor));
srandomdev();
for(NSUInteger i = 0;i < count;i++) {
uint8_t value = random() % 256;
colors[i].r = value;
colors[i].g = value;
colors[i].b = value;
colors[i].a = 255;
}
self.baseNoise = [NSData dataWithBytesNoCopy:(void *)colors
length:sizeof(GFSConvolverColor) * count
freeWhenDone:YES];
}
return _baseNoise;
}

- (NSData *)baseNoise {
if(nil == _baseNoise) {
self.baseNoise = [self baseRGBANoise];
}
return _baseNoise;
}

- (NSArray *)noiseImages {
return nil;
}

- (GFSImageConvolver *)horizontalConvolver {
if(nil == _horizontalConvolver) {
self.horizontalConvolver = [[GFSImageConvolver alloc] initWithImageData:self.baseNoise imageSize:self.size];

short *motionBlurKernel = calloc(25, sizeof(short));
for(int i = 0;i < 25;i++) {
motionBlurKernel[i] = 1.0;
}
[self.horizontalConvolver setKernel:motionBlurKernel width:25 height:1];
self.horizontalConvolver.divsor = 25;
}
return _horizontalConvolver;
}

- (GFSImageConvolver *)verticalConvolver {
if(nil == _verticalConvolver) {
self.verticalConvolver = [[GFSImageConvolver alloc] initWithImageData:self.baseNoise imageSize:self.size];

short *motionBlurKernel = calloc(25, sizeof(short));
for(int i = 0;i < 25;i++) {
motionBlurKernel[i] = 1.0;
}
[self.verticalConvolver setKernel:motionBlurKernel width:1 height:25];
self.verticalConvolver.divsor = 25;
}
return _verticalConvolver;
}

- (UIImage *)noiseImage {
if(nil == self.bluredImage) {
// CGDataProviderRef dataProviderRef = CGDataProviderCreateWithCFData((__bridge CFDataRef)self.baseNoise);
// // divice RGB is fine for iOS but for the Mac we'd want to be more creative
// CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
// CGImageRef image = CGImageCreate(self.size.width, self.size.height,
// 4, 8 * 2, self.size.width * 2,
// colorSpace,
// kCGBitmapByteOrder32Big | kCGImageAlphaNoneSkipFirst,
// dataProviderRef,
// NULL, NO, kCGRenderingIntentDefault);
// self.perlinNoiseImage = [UIImage imageWithCGImage:image];
// CGImageRelease(image);
// CGDataProviderRelease(dataProviderRef);
// CGColorSpaceRelease(colorSpace);
UIGraphicsBeginImageContext(self.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ctx, [[UIColor whiteColor] CGColor]);
CGRect rect = CGRectMake(0.0, 0.0, self.size.width, self.size.height);
CGContextFillRect(ctx, rect);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextDrawImage(ctx, rect, (__bridge CGImageRef)self.horizontalConvolver.convolvedImage);
CGContextDrawImage(ctx, rect, (__bridge CGImageRef)self.verticalConvolver.convolvedImage);
self.bluredImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return _bluredImage;
}

@end


@implementation GFSNoiseGenerator(Private)

@end
1 change: 1 addition & 0 deletions Convolver/Convolver/GFSVImageLoader.h
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ typedef struct GFSConvolverColor { // this order only works for big endian
@interface GFSVImageLoader : NSObject

- (id)initWithURL:(NSURL *)orignalImageURL;
- (id)initWithCompliantData:(NSData *)data imageSize:(CGSize)imageSize;

@property(nonatomic, strong, readonly) NSURL *originalImageURL;
@property(nonatomic, strong, readonly) NSData *compliantData;
9 changes: 9 additions & 0 deletions Convolver/Convolver/GFSVImageLoader.m
Original file line number Diff line number Diff line change
@@ -42,6 +42,15 @@ - (id)initWithURL:(NSURL *)orignalImageURL {

}

- (id)initWithCompliantData:(NSData *)data imageSize:(CGSize)imageSize {
self = [super init];
if(nil != self) {
self.compliantData = data;
self.imageSize = imageSize;
}
return self;
}

@end

@implementation GFSVImageLoader(Private)
7 changes: 7 additions & 0 deletions Convolver/Convolver/GFSViewController.m
Original file line number Diff line number Diff line change
@@ -21,24 +21,29 @@
#import "GFSViewController.h"
#import "GFSImageConvolver.h"
#import "GFSImageSeparator.h"
#import "GFSNoiseGenerator.h"

@interface GFSViewController ()

@property(nonatomic, strong) GFSNoiseGenerator *noiseGenerator;
@property(nonatomic, strong) UIImage *originalImage;
@property(nonatomic, strong) GFSImageConvolver *convolver;
@property(nonatomic, strong) GFSImageSeparator *separator;
@property(nonatomic, weak) IBOutlet UIImageView *imageView;
@property(nonatomic, weak) IBOutlet UIImageView *noiseImage;
@property (weak, nonatomic) IBOutlet UILabel *divisorLabel;
@property(nonatomic, assign) BOOL displayingConvolvedImage;

@end

@implementation GFSViewController

@synthesize noiseGenerator = _noiseGenerator;
@synthesize originalImage = _originalImage;
@synthesize convolver = _convolver;
@synthesize separator = _separator;
@synthesize imageView = _imageView;
@synthesize noiseImage = _noiseImage;
@synthesize divisorLabel = _multiplierLayer;
@synthesize displayingConvolvedImage = _displayingConvolvedImage;

@@ -105,6 +110,8 @@ - (void)viewDidLoad {
self.imageView.image = [UIImage imageWithCGImage:(__bridge CGImageRef)self.convolver.convolvedImage];
self.displayingConvolvedImage = YES;
self.divisorLabel.text = @"1";
self.noiseGenerator = [[GFSNoiseGenerator alloc] initWithSize:CGSizeMake(256.0, 256.0) octaves:1];
self.noiseImage.image = self.noiseGenerator.noiseImage;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
10 changes: 8 additions & 2 deletions Convolver/Convolver/en.lproj/MainStoryboard.storyboard
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
<dependencies>
<deployment defaultVersion="1296" identifier="iOS"/>
<development defaultVersion="4200" identifier="xcode"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="1179"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="1181"/>
</dependencies>
<scenes>
<!--View Controller-->
@@ -109,6 +109,10 @@
<action selector="blue:" destination="2" eventType="touchUpInside" id="IMy-aV-8nS"/>
</connections>
</button>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" id="B65-oF-Mhc">
<rect key="frame" x="643" y="393" width="256" height="256"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<simulatedOrientationMetrics key="simulatedOrientationMetrics" orientation="landscapeRight"/>
@@ -117,6 +121,7 @@
<connections>
<outlet property="divisorLabel" destination="1z0-LR-ACf" id="iwN-I0-4p7"/>
<outlet property="imageView" destination="nOq-HK-XQw" id="Jbh-e8-CTe"/>
<outlet property="noiseImage" destination="B65-oF-Mhc" id="K64-q3-EmA"/>
<outlet property="view" destination="5" id="Uqp-Tk-rZq"/>
</connections>
</viewController>
@@ -129,12 +134,13 @@
<relationships>
<relationship kind="outlet" name="divisorLabel" candidateClass="UILabel"/>
<relationship kind="outlet" name="imageView" candidateClass="UIImageView"/>
<relationship kind="outlet" name="noiseImage" candidateClass="UIImageView"/>
</relationships>
</class>
</classes>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar" statusBarStyle="blackTranslucent"/>
<simulatedOrientationMetrics key="orientation"/>
<simulatedOrientationMetrics key="orientation" orientation="landscapeRight"/>
<simulatedScreenMetrics key="destination"/>
</simulatedMetricsContainer>
</document>
Binary file not shown.

0 comments on commit f3b38bc

Please sign in to comment.