Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added downloadBytes callback to RawDataOutput #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion framework/Source/RawDataInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ public enum PixelFormat {
// TODO: Replace with texture caches where appropriate
public class RawDataInput: ImageSource {
public let targets = TargetContainer()
private var privatePixelFormat = PixelFormat.RGBA

public init() {

}

public func uploadBytes(bytes:[UInt8], size:Size, pixelFormat:PixelFormat, orientation:ImageOrientation = .Portrait) {
privatePixelFormat = pixelFormat == .Luminance ? PixelFormat.RGBA : pixelFormat
let dataFramebuffer = sharedImageProcessingContext.framebufferCache.requestFramebufferWithProperties(orientation:orientation, size:GLSize(size), textureOnly:true, internalFormat:pixelFormat.toGL(), format:pixelFormat.toGL())

glActiveTexture(GLenum(GL_TEXTURE1))
glBindTexture(GLenum(GL_TEXTURE_2D), dataFramebuffer.texture)
glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, size.glWidth(), size.glHeight(), 0, GLenum(pixelFormat.toGL()), GLenum(GL_UNSIGNED_BYTE), bytes)
glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, size.glWidth(), size.glHeight(), 0, GLenum(privatePixelFormat.toGL()), GLenum(GL_UNSIGNED_BYTE), bytes)

updateTargetsWithFramebuffer(dataFramebuffer)
}
Expand Down
19 changes: 17 additions & 2 deletions framework/Source/RawDataOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@

public class RawDataOutput: ImageConsumer {
public var dataAvailableCallback:([UInt8] -> ())?
public var downloadBytes:(([UInt8], Size, PixelFormat, ImageOrientation) -> ())?

public let sources = SourceContainer()
public let maximumInputs:UInt = 1
public var pixelFormat = PixelFormat.RGBA
private var privatePixelFormat = PixelFormat.RGBA

public init() {
}
Expand All @@ -28,13 +31,25 @@ public class RawDataOutput: ImageConsumer {

renderFramebuffer.activateFramebufferForRendering()
clearFramebufferWithColor(Color.Black)
renderQuadWithShader(sharedImageProcessingContext.passthroughShader, uniformSettings:ShaderUniformSettings(), vertices:standardImageVertices, inputTextures:[framebuffer.texturePropertiesForOutputRotation(.NoRotation)])
if pixelFormat == .Luminance {
privatePixelFormat = PixelFormat.RGBA
let luminanceShader = crashOnShaderCompileFailure("RawDataOutput"){try sharedImageProcessingContext.programForVertexShader(defaultVertexShaderForInputs(1), fragmentShader:LuminanceFragmentShader)}
renderQuadWithShader(luminanceShader, vertices:standardImageVertices, inputTextures:[framebuffer.texturePropertiesForTargetOrientation(renderFramebuffer.orientation)])
} else {
privatePixelFormat = pixelFormat
renderQuadWithShader(sharedImageProcessingContext.passthroughShader, uniformSettings:ShaderUniformSettings(), vertices:standardImageVertices, inputTextures:[framebuffer.texturePropertiesForOutputRotation(.NoRotation)])
}
framebuffer.unlock()

var data = [UInt8](count:Int(framebuffer.size.width * framebuffer.size.height * 4), repeatedValue:0)
glReadPixels(0, 0, framebuffer.size.width, framebuffer.size.height, GLenum(GL_RGBA), GLenum(GL_UNSIGNED_BYTE), &data)
renderFramebuffer.unlock()

dataAvailableCallback?(data)
if dataAvailableCallback != nil {
Copy link
Contributor

@zubco zubco Jun 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You already using optional chaining when calling dataAvailableCallback closure, so there is no need to check against nil value ;)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing these out. Creating another pull request.

dataAvailableCallback?(data)
}
if downloadBytes != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

downloadBytes?(data, Size(framebuffer.size), pixelFormat, framebuffer.orientation)
}
}
}