Skip to content

Instantly share code, notes, and snippets.

View FlexMonkey's full-sized avatar
🙂
Personal repositories are no longer maintained!

simon gladman FlexMonkey

🙂
Personal repositories are no longer maintained!
View GitHub Profile
@FlexMonkey
FlexMonkey / gist:a3fca959b5e566b4ccc785b00b75d396
Created May 17, 2016 17:30
CIImage from named NSImage for Cocoa
let monalisa = NSImage(named: "monalisa.jpg")!
let tiffData = monalisa.TIFFRepresentation!
let bitmap = NSBitmapImageRep(data: tiffData)
let image = CIImage(bitmapImageRep: bitmap!)!
@FlexMonkey
FlexMonkey / gist:b62ae8a930d15579fb89a0fd937517d7
Created April 16, 2016 06:31
imageOrientationToTiffOrientation
func imageOrientationToTiffOrientation(value: UIImageOrientation) -> Int32
{
switch (value)
{
case UIImageOrientation.Up:
return 1
case UIImageOrientation.Down:
return 3
case UIImageOrientation.Left:
return 8
@FlexMonkey
FlexMonkey / gist:1065a5272cb8bd7c3a904cd677f258d2
Created April 1, 2016 04:05
Bresenham's Line Algorithm for Metal Compute Shader
void drawLine(texture2d<float, access::write> targetTexture, uint2 start, uint2 end);
void drawLine(texture2d<float, access::write> targetTexture, uint2 start, uint2 end)
{
int x = int(start.x);
int y = int(start.y);
int dx = abs(x - int(end.x));
int dy = abs(y - int(end.y));
@FlexMonkey
FlexMonkey / gist:8b3ab538242f3376b2c9
Created March 20, 2016 17:52
Normalise a CIVector - ideal for use as a convolution matrix
extension CIVector
{
func normalize() -> CIVector
{
var sum: CGFloat = 0
for i in 0 ..< self.count
{
sum += self.valueAtIndex(i)
}
@FlexMonkey
FlexMonkey / gist:0d938db6e42ca97911a5
Created March 18, 2016 11:23
CIVector extension to multiply each component by a single CIFloat
extension CIVector
{
func multiply(value: CGFloat) -> CIVector
{
let n = self.count
var targetArray = [CGFloat]()
for i in 0 ..< n
{
targetArray.append(self.valueAtIndex(i) * value)
@FlexMonkey
FlexMonkey / gist:eb95719f73cd2b6380b4
Created February 4, 2016 20:00
UnrolledKuwaharaFilter - replace a GLSL loop with a Swift one. Performance is rubbish, this is here for reference only!
class UnrolledKuwaharaFilter: CIFilter
{
var inputImage: CIImage?
var inputRadius: CGFloat = 15
{
didSet
{
if Int(inputRadius) != Int(oldValue)
{
@FlexMonkey
FlexMonkey / gist:8f3b5678bf52098f371a
Created January 31, 2016 06:32
CGFloat extension - saturate() and clamped smootherStep()
extension CGFloat
{
func saturate() -> CGFloat
{
return self < 0 ? 0 : self > 1 ? 1 : self
}
func smootherStep() -> CGFloat
{
let x = self.saturate()
@FlexMonkey
FlexMonkey / gist:d2b1cbd7e1b2caf8d31b
Last active January 28, 2016 17:35
Synchronously create a UIImage from an UIImagePickerControllerReferenceURL
// Could benefit from some defensive coding :)
import Photos
let url = info[UIImagePickerControllerReferenceURL] as! NSURL
let fetchResult = PHAsset.fetchAssetsWithALAssetURLs([url], options: nil)
let asset = fetchResult.firstObject as! PHAsset
@FlexMonkey
FlexMonkey / gist:c4c77c9562cf252a5660
Last active January 29, 2016 16:55
Compares two images to see if they have identical content
func UIImageEqualToImage(image1: UIImage, _ image2: UIImage, ciContext: CIContext? = nil) -> Bool
{
guard let
ciImage1 = CIImage(image: image1),
ciImage2 = CIImage(image: image2) where image1.size == image2.size else
{
return false
}
let ctx = ciContext ?? CIContext()
@FlexMonkey
FlexMonkey / gist:f108cadba2a128dcb514
Last active January 22, 2016 05:32
CGRect extension gets and sets a rectangle's corners
extension CGRect
{
/// Return corners in order of top left, top right, bottom left, bottom right
var corners: [CGPoint]
{
return [
CGPoint(x: minX, y: minY),
CGPoint(x: maxX, y: minY),
CGPoint(x: minX, y: maxY),