...
 
Commits (2)
......@@ -24,6 +24,9 @@
E32ABCD62273092400D74B7C /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E32ABCD42273092400D74B7C /* LaunchScreen.storyboard */; };
E32ABCDE22730AE400D74B7C /* Kernels.metal in Sources */ = {isa = PBXBuildFile; fileRef = E32ABCDD22730AE400D74B7C /* Kernels.metal */; };
E3C475F622AA763D00FCAF05 /* Task4.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3C475F522AA763D00FCAF05 /* Task4.swift */; };
E3D058A622B9430800118F2F /* Appendix3.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3D058A522B9430800118F2F /* Appendix3.swift */; };
E3D058A822B9431C00118F2F /* Appendix4.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3D058A722B9431C00118F2F /* Appendix4.swift */; };
E3D058AA22B945CF00118F2F /* Appendix5.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3D058A922B945CF00118F2F /* Appendix5.swift */; };
E3D6AE6D22B0F94A00ACADC5 /* Appendix1.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3D6AE6C22B0F94A00ACADC5 /* Appendix1.swift */; };
/* End PBXBuildFile section */
......@@ -47,6 +50,9 @@
E32ABCD72273092400D74B7C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
E32ABCDD22730AE400D74B7C /* Kernels.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = Kernels.metal; sourceTree = "<group>"; };
E3C475F522AA763D00FCAF05 /* Task4.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Task4.swift; sourceTree = "<group>"; };
E3D058A522B9430800118F2F /* Appendix3.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Appendix3.swift; sourceTree = "<group>"; };
E3D058A722B9431C00118F2F /* Appendix4.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Appendix4.swift; sourceTree = "<group>"; };
E3D058A922B945CF00118F2F /* Appendix5.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Appendix5.swift; sourceTree = "<group>"; };
E3D6AE6C22B0F94A00ACADC5 /* Appendix1.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Appendix1.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
......@@ -66,6 +72,9 @@
children = (
E3D6AE6C22B0F94A00ACADC5 /* Appendix1.swift */,
565508C422AA7D1D00884E5C /* Appendix2.swift */,
E3D058A522B9430800118F2F /* Appendix3.swift */,
E3D058A722B9431C00118F2F /* Appendix4.swift */,
E3D058A922B945CF00118F2F /* Appendix5.swift */,
);
path = Appendices;
sourceTree = "<group>";
......@@ -203,12 +212,15 @@
files = (
565C4CFD22798EBA00275692 /* Task5.swift in Sources */,
566774762278836C003F881E /* Task1.swift in Sources */,
E3D058A622B9430800118F2F /* Appendix3.swift in Sources */,
5667747922788388003F881E /* Task2.swift in Sources */,
56AD533B2278668B005B1E87 /* EditorViewController.swift in Sources */,
E32ABCDE22730AE400D74B7C /* Kernels.metal in Sources */,
E3C475F622AA763D00FCAF05 /* Task4.swift in Sources */,
5667747B22798BCA003F881E /* Task3.swift in Sources */,
E3D058AA22B945CF00118F2F /* Appendix5.swift in Sources */,
565508C522AA7D1D00884E5C /* Appendix2.swift in Sources */,
E3D058A822B9431C00118F2F /* Appendix4.swift in Sources */,
56AD53392278513B005B1E87 /* BrowserViewController.swift in Sources */,
E3D6AE6D22B0F94A00ACADC5 /* Appendix1.swift in Sources */,
E32ABCCE2273092300D74B7C /* MainViewController.swift in Sources */,
......
import CoreImage
let appendix3: Effect = { originalImage, depthMap in
/*
This function is already setup to utilize the `appendix3` metal kernel.
1. Go to the Kernels.metal file to complete the task.
*/
// Load the Metal library
let metalLibraryUrl = Bundle.main.url(forResource: "default", withExtension: "metallib")!
let metalLibraryData = try! Data(contentsOf: metalLibraryUrl)
// Find the kernel of interest.
let filter = try? CIWarpKernel(functionName: "appendix3", fromMetalLibraryData: metalLibraryData)
// Use the kernel to process the images
return filter?.apply(
extent: originalImage.extent,
roiCallback: { _,_ in originalImage.extent },
image: originalImage,
arguments: []
)
}
import CoreImage
let appendix4: Effect = { originalImage, depthMap in
/*
This function is already setup to utilize the `appendix4` metal kernel.
1. Go to the Kernels.metal file to complete the task.
*/
// Load the Metal library
let metalLibraryUrl = Bundle.main.url(forResource: "default", withExtension: "metallib")!
let metalLibraryData = try! Data(contentsOf: metalLibraryUrl)
// Find the kernel of interest.
let filter = try? CIKernel(functionName: "appendix4", fromMetalLibraryData: metalLibraryData)
// Use the kernel to process the images
return filter?.apply(
extent: originalImage.extent,
roiCallback: { _,_ in originalImage.extent },
arguments: [originalImage as Any, depthMap as Any]
)
}
import CoreImage
let appendix5: Effect = { originalImage, depthMap in
/*
This function is already setup to utilize the `appendix5` metal kernel.
1. Go to the Kernels.metal file to complete the task.
*/
// Load the Metal library
let metalLibraryUrl = Bundle.main.url(forResource: "default", withExtension: "metallib")!
let metalLibraryData = try! Data(contentsOf: metalLibraryUrl)
// Find the kernel of interest.
let filter = try? CIKernel(functionName: "appendix5", fromMetalLibraryData: metalLibraryData)
// Use the kernel to process the images
return filter?.apply(
extent: originalImage.extent,
roiCallback: { _,_ in originalImage.extent },
arguments: [originalImage as Any, depthMap as Any]
)
}
......@@ -16,7 +16,10 @@ class EditorViewController: UIViewController {
]
private let appendices: [Effect] = [
appendix1,
appendix2
appendix2,
appendix3,
appendix4,
appendix5
]
private var selectedEffect: Int = UserDefaults.standard.integer(forKey: "selectedEffect") {
didSet {
......
......@@ -40,9 +40,46 @@ extern "C" {
// Receives as argument the position of the pixel in the output image.
// Its job is to return a coordinate indicating where in the source image
// there should be sampled from.
float2 appendix1(destination dest) {
float2 c = dest.coord();
float columnWidth = 150.0;
float columnIndex = floor(c.x / columnWidth);
float2 sampleCoord;
if (int(columnIndex) % 2 == 0) {
sampleCoord = float2(c.x, c.y + 30.0);
} else {
sampleCoord = float2(c.x, c.y - 30.0);
}
return sampleCoord;
}
// A general kernel
float4 appendix2(sampler originalImage, sampler depthMap, destination dest) {
float2 c = dest.coord();
float depth = depthMap.sample(depthMap.coord()).r;
float distortAmount = (1.0 - min(depth, 1.0)) * 100.0;
float columnWidth = 150.0;
float columnIndex = floor(c.x / columnWidth);
float2 sampleCoord;
if (int(columnIndex) % 2 == 0) {
sampleCoord = float2(c.x, c.y + distortAmount);
} else {
sampleCoord = float2(c.x, c.y - distortAmount);
}
return originalImage.sample(originalImage.transform(sampleCoord));
}
// Warp kernel
// In this example, we create a tiling effect. This is achieved by returning
// the same sampling coordinate for pixels that lie within the same tile.
float2 appendix1(destination dest) {
float2 appendix3(destination dest) {
// Get the coordinate of the pixel in the output image
float2 c = dest.coord();
// Define the width and height of a tile
......@@ -59,10 +96,23 @@ extern "C" {
return sampleCoord;
}
// General kernel
float4 appendix4(sampler originalImage, sampler depthMap, destination d) {
float4 tintColor = float4(1.0, 0.5, 0.4, 1.0);
float4 originalColor = originalImage.sample(originalImage.coord());
float tintAmount = d.coord().x / originalImage.size().x;
return float4(
originalColor.r * (1.0 - tintAmount) + tintColor.r * tintAmount,
originalColor.g * (1.0 - tintAmount) + tintColor.g * tintAmount,
originalColor.b * (1.0 - tintAmount) + tintColor.b * tintAmount,
1.0
);
}
// A general kernel
// The destination parameter is optional for general kernel. Must be last.
float4 appendix2(sampler originalImage, sampler depthMap, destination d) {
float4 appendix5(sampler originalImage, sampler depthMap, destination d) {
// Take sample from samplers at coordinate that matches output/destination space
float4 colorSample = originalImage.sample(originalImage.coord());
float4 depthSample = depthMap.sample(depthMap.coord());
......