forked from BradLarson/GPUImage2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a PictureInput using SwiftGD and a sample application to go alo…
…ng with it.
- Loading branch information
1 parent
c6f86be
commit 48b7dc0
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// swift-tools-version:4.2 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "SimpleImageFilter", | ||
dependencies: [ | ||
.package(path: "../../../../GPUImage2") | ||
], | ||
targets: [ | ||
.target( | ||
name: "SimpleImageFilter", | ||
dependencies: ["GPUImage"], | ||
path: "Sources") | ||
] | ||
) |
18 changes: 18 additions & 0 deletions
18
examples/Linux-OpenGL/SimpleImageFilter/Sources/main.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import GPUImage | ||
import Foundation | ||
|
||
// For now, GLUT initialization is done in the render window, so that must come first in sequence | ||
let renderWindow = GLUTRenderWindow(width:1280, height:720, title:"Simple Video Filter") | ||
guard let pictureInput = PictureInput(path:"../../SharedAssets/Lambeau.jpg") else { | ||
fatalError("Could not load sample image") | ||
} | ||
let edgeDetection = SobelEdgeDetection() | ||
|
||
print("Edge detection") | ||
|
||
pictureInput --> edgeDetection --> renderWindow | ||
|
||
print("Processing") | ||
|
||
pictureInput.processImage(synchronously: true) | ||
renderWindow.loopWithFunction {Thread.sleep(forTimeInterval: 1.0)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#if canImport(COpenGL) | ||
import COpenGL | ||
#else | ||
import COpenGLES.gles2 | ||
#endif | ||
|
||
// apt-get install libgd-dev | ||
import Foundation | ||
import SwiftGD | ||
|
||
public class PictureInput: ImageSource { | ||
public let targets = TargetContainer() | ||
var imageFramebuffer:Framebuffer! | ||
var hasProcessedImage:Bool = false | ||
|
||
public init?(path:String, smoothlyScaleOutput:Bool = false, orientation:ImageOrientation = .portraitUpsideDown) { | ||
let location = URL(fileURLWithPath: path) | ||
guard let image = Image(url: location) else { return nil } | ||
let bitmapImage = try! image.export(as:.bmp(compression:false)) | ||
let widthToUseForTexture = GLint(image.size.width) | ||
let heightToUseForTexture = GLint(image.size.height) | ||
|
||
sharedImageProcessingContext.runOperationSynchronously{ | ||
do { | ||
self.imageFramebuffer = try Framebuffer(context:sharedImageProcessingContext, orientation:orientation, size:GLSize(width:widthToUseForTexture, height:heightToUseForTexture), textureOnly:true) | ||
print("Framebuffer created") | ||
} catch { | ||
fatalError("ERROR: Unable to initialize framebuffer of size (\(widthToUseForTexture), \(heightToUseForTexture)) with error: \(error)") | ||
} | ||
|
||
glBindTexture(GLenum(GL_TEXTURE_2D), self.imageFramebuffer.texture) | ||
// if (smoothlyScaleOutput) { | ||
// glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_LINEAR_MIPMAP_LINEAR) | ||
// } | ||
bitmapImage.withUnsafeBytes { (u8Ptr: UnsafePointer<UInt8>) in | ||
print("Image byte size: \(bitmapImage.count), width: \(widthToUseForTexture), height: \(heightToUseForTexture)") | ||
let imageData = UnsafeRawPointer(u8Ptr) + 54 | ||
glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, widthToUseForTexture, heightToUseForTexture, 0, GLenum(GL_RGB), GLenum(GL_UNSIGNED_BYTE), imageData) | ||
} | ||
|
||
// if (smoothlyScaleOutput) { | ||
// glGenerateMipmap(GLenum(GL_TEXTURE_2D)) | ||
// } | ||
glBindTexture(GLenum(GL_TEXTURE_2D), 0) | ||
} | ||
} | ||
|
||
public func processImage(synchronously:Bool = false) { | ||
if synchronously { | ||
sharedImageProcessingContext.runOperationSynchronously{ | ||
sharedImageProcessingContext.makeCurrentContext() | ||
self.updateTargetsWithFramebuffer(self.imageFramebuffer) | ||
self.hasProcessedImage = true | ||
} | ||
} else { | ||
sharedImageProcessingContext.runOperationAsynchronously{ | ||
sharedImageProcessingContext.makeCurrentContext() | ||
self.updateTargetsWithFramebuffer(self.imageFramebuffer) | ||
self.hasProcessedImage = true | ||
} | ||
} | ||
} | ||
|
||
public func transmitPreviousImage(to target:ImageConsumer, atIndex:UInt) { | ||
if hasProcessedImage { | ||
imageFramebuffer.lock() | ||
target.newFramebufferAvailable(imageFramebuffer, fromSourceIndex:atIndex) | ||
} | ||
} | ||
} |