From 60f1d82686048b9d66271ac091ee49eff2e3b35d Mon Sep 17 00:00:00 2001 From: mikebikemusic Date: Thu, 9 Jun 2016 11:58:18 -0700 Subject: [PATCH 1/2] Added downloadBytes callback to RawDataOutput Added downloadBytes callback to RawDataOutput, to make it symmetrical with the uploadBytes method in RawDataInput. Fixed bugs in the Luminance format for RawDataInput and RawDataOutput. --- framework/Source/RawDataInput.swift | 4 +++- framework/Source/RawDataOutput.swift | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/framework/Source/RawDataInput.swift b/framework/Source/RawDataInput.swift index 4a216edb..752f894e 100644 --- a/framework/Source/RawDataInput.swift +++ b/framework/Source/RawDataInput.swift @@ -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) } diff --git a/framework/Source/RawDataOutput.swift b/framework/Source/RawDataOutput.swift index 6985bc7c..f3e5a67a 100644 --- a/framework/Source/RawDataOutput.swift +++ b/framework/Source/RawDataOutput.swift @@ -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() { } @@ -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 { + dataAvailableCallback?(data) + } + if downloadBytes != nil { + downloadBytes?(data, Size(framebuffer.size), pixelFormat, framebuffer.orientation) + } } } \ No newline at end of file From b65bd0ae6fa4be3c3fe375c050c7d3dcfd29b96a Mon Sep 17 00:00:00 2001 From: mikebikemusic Date: Fri, 10 Jun 2016 07:27:48 -0700 Subject: [PATCH 2/2] Removed unnecessary code. Optional function pointers don't need a nil check. --- framework/Source/RawDataOutput.swift | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/framework/Source/RawDataOutput.swift b/framework/Source/RawDataOutput.swift index f3e5a67a..d84c24f2 100644 --- a/framework/Source/RawDataOutput.swift +++ b/framework/Source/RawDataOutput.swift @@ -45,11 +45,7 @@ public class RawDataOutput: ImageConsumer { glReadPixels(0, 0, framebuffer.size.width, framebuffer.size.height, GLenum(GL_RGBA), GLenum(GL_UNSIGNED_BYTE), &data) renderFramebuffer.unlock() - if dataAvailableCallback != nil { - dataAvailableCallback?(data) - } - if downloadBytes != nil { - downloadBytes?(data, Size(framebuffer.size), pixelFormat, framebuffer.orientation) - } + dataAvailableCallback?(data) + downloadBytes?(data, Size(framebuffer.size), pixelFormat, framebuffer.orientation) } } \ No newline at end of file