From 5170e479fb44ae201fa1e14408867d6f83187b1c Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 5 Jun 2017 00:59:08 -0500 Subject: [PATCH] working on ColorTransform --- Sources/ColorTransform.swift | 54 +++++++++++++++++++++++------------- Sources/Intent.swift | 18 ++++++++++++ Sources/Pipeline.swift | 3 +- Sources/Profile.swift | 7 ----- Sources/Protocols.swift | 4 +-- 5 files changed, 56 insertions(+), 30 deletions(-) create mode 100644 Sources/Intent.swift diff --git a/Sources/ColorTransform.swift b/Sources/ColorTransform.swift index 0af83ad..0cabc05 100644 --- a/Sources/ColorTransform.swift +++ b/Sources/ColorTransform.swift @@ -15,6 +15,11 @@ public final class ColorTransform { internal let internalPointer: cmsHTRANSFORM + public let context: Context? + + /// Other CMS object wrappers / reference-backed value types to retain (besides the context) + private let retain: [Any] + // MARK: - Initialization deinit { @@ -22,40 +27,49 @@ public final class ColorTransform { cmsDeleteTransform(internalPointer) } - internal init(_ internalPointer: cmsHTRANSFORM) { - - self.internalPointer = internalPointer - } - - /* /// Creates a color transform for translating bitmaps. - public init?(input: (profile: Profile, format: UInt), - output: (profile: Profile, format: UInt), - intent: cmsUInt32Number, - flags: cmsUInt32Number, + public init?(input: (profile: Profile, format: cmsUInt32Number), + output: (profile: Profile, format: cmsUInt32Number), + intent: Intent, + flags: cmsUInt32Number = 0, context: Context? = nil) { - // TODO: cmsCreateExtendedTransform - guard let internalPointer = cmsCreateTransformTHR(context?.internalPointer, - input.profile.internalPointer, - cmsUInt32Number(input.format), - output.profile.internalPointer, - cmsUInt32Number(output.format), - intent, + input.profile.internalReference.reference.internalPointer, + input.format, + output.profile.internalReference.reference.internalPointer, + output.format, + intent.rawValue, flags) else { return nil } self.internalPointer = internalPointer - }*/ + self.context = context + self.retain = [input.profile, output.profile] + } // MARK: - Methods /// Translates bitmaps according of parameters setup when creating the color transform. public func transform(_ bitmap: Data) -> Data { - // FIXME - return Data() + let internalPointer = self.internalPointer + + var output = Data(count: bitmap.count) + + bitmap.withUnsafeBytes { (inputBytes: UnsafePointer) in + + let inputBytes = UnsafeRawPointer(inputBytes) + + output.withUnsafeMutableBytes { (outputBytes: UnsafeMutablePointer) in + + let outputBytes = UnsafeMutableRawPointer(outputBytes) + + cmsDoTransform(internalPointer, inputBytes, outputBytes, cmsUInt32Number(bitmap.count)) + } + } + + return output } } diff --git a/Sources/Intent.swift b/Sources/Intent.swift new file mode 100644 index 0000000..cf668bd --- /dev/null +++ b/Sources/Intent.swift @@ -0,0 +1,18 @@ +// +// Intent.swift +// LittleCMS +// +// Created by Alsey Coleman Miller on 6/5/17. +// +// + +import CLCMS + +/// ICC Intent +public enum Intent: UInt32 { + + case perceptual = 0 + case relativeColorimetric = 1 + case saturation = 2 + case abosoluteColorimetric = 3 +} diff --git a/Sources/Pipeline.swift b/Sources/Pipeline.swift index 64c5d29..e560845 100644 --- a/Sources/Pipeline.swift +++ b/Sources/Pipeline.swift @@ -26,7 +26,8 @@ public struct Pipeline { self.internalReference = CopyOnWrite(internalReference) } - /// Allocates an empty pipeline. + /// Creates an empty pipeline. + /// /// Final Input and output channels must be specified at creation time. public init?(channels: (input: UInt, output: UInt), context: Context? = nil) { diff --git a/Sources/Profile.swift b/Sources/Profile.swift index 27482e4..20355a3 100644 --- a/Sources/Profile.swift +++ b/Sources/Profile.swift @@ -160,13 +160,6 @@ internal extension Profile { cmsCloseProfile(internalPointer) } - @inline(__always) - internal init(_ internalPointer: cmsHPROFILE) { - - self.internalPointer = internalPointer - self.context = Reference.context(for: internalPointer) - } - /// Creates a fake NULL profile. /// /// This profile return 1 channel as always 0. diff --git a/Sources/Protocols.swift b/Sources/Protocols.swift index 06ed84b..4031adf 100644 --- a/Sources/Protocols.swift +++ b/Sources/Protocols.swift @@ -24,8 +24,6 @@ internal protocol HandleObject { associatedtype InternalPointer - init(_ internalPointer: InternalPointer) - var internalPointer: InternalPointer { get } } @@ -39,6 +37,8 @@ internal protocol DuplicableHandle: CopyableHandle { /// The Little CMS Function that creates a duplicate of the handler. static var cmsDuplicate: cmsDuplicateFunction { get } + + init(_ internalPointer: InternalPointer) } extension DuplicableHandle {