From 0705dbe288222d709c3b21a8ab831ea2767c10c5 Mon Sep 17 00:00:00 2001 From: Sergey Minakov Date: Tue, 22 Nov 2016 13:52:49 +0300 Subject: [PATCH] (#3) started refactor --- Sources/MagickWand/MagickWand.swift | 17 ++++--- .../Wand/ImageWand/ImageWand+Size.swift | 6 +-- .../Wand/PixelWand/PixelWand+Color.swift | 4 +- .../WandTypes/MagickWand+Colors.swift | 4 +- .../MagickWand/WandTypes/MagickWand+RGB.swift | 2 +- .../WandTypes/MagickWand+Size.swift | 45 ++++++++++++++++++ Tests/MagickWandTests/rect.png | Bin 0 -> 347 bytes 7 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 Sources/MagickWand/WandTypes/MagickWand+Size.swift create mode 100644 Tests/MagickWandTests/rect.png diff --git a/Sources/MagickWand/MagickWand.swift b/Sources/MagickWand/MagickWand.swift index ee82556..8b71a3f 100644 --- a/Sources/MagickWand/MagickWand.swift +++ b/Sources/MagickWand/MagickWand.swift @@ -28,7 +28,6 @@ public struct MagickWand { - public typealias Size = (width: Int, height: Int) public typealias Resolution = (width: Double, height: Double) public typealias HSL = (hue: Double, saturation: Double, lightness: Double) @@ -44,11 +43,7 @@ public struct MagickWand { } public static var isInstantiated: Bool { - #if os(Linux) - return IsMagickInstantiated().bool - #else - return IsMagickWandInstantiated().bool - #endif + return IsMagickWandInstantiated().bool } public static var version: String { @@ -58,16 +53,20 @@ public struct MagickWand { return String(cString: pointer) } +} - internal static func getString(from wandPointer: OpaquePointer, using method: (OpaquePointer!) -> (UnsafeMutablePointer!)) -> String? { +extension MagickWand { + + internal static func getString(from wandPointer: OpaquePointer?, + using method: (OpaquePointer!) -> (UnsafeMutablePointer!)) -> String? { guard let pointer = method(wandPointer) else { return nil } - + defer { MagickRelinquishMemory(pointer) } - + return String(cString: pointer) } } diff --git a/Sources/MagickWand/Wand/ImageWand/ImageWand+Size.swift b/Sources/MagickWand/Wand/ImageWand/ImageWand+Size.swift index 7c3feaf..ffe98a3 100644 --- a/Sources/MagickWand/Wand/ImageWand/ImageWand+Size.swift +++ b/Sources/MagickWand/Wand/ImageWand/ImageWand+Size.swift @@ -36,7 +36,7 @@ extension ImageWand { Int(MagickGetImageHeight(self.pointer)) ) - return (width, height) + return MagickWand.Size(width: width, height: height) } public var resolution: MagickWand.Resolution { @@ -50,11 +50,11 @@ extension ImageWand { public func size(for dimension: Int) -> MagickWand.Size { let size = self.size - var result = (width: 0, height: 0) + var result = MagickWand.Size(width: 0, height: 0) if size.width == 0 || size.height == 0 { - return (0, 0) + return .zero } let ratio = Double(size.height) / Double(size.width) diff --git a/Sources/MagickWand/Wand/PixelWand/PixelWand+Color.swift b/Sources/MagickWand/Wand/PixelWand/PixelWand+Color.swift index 7d528b8..694a540 100644 --- a/Sources/MagickWand/Wand/PixelWand/PixelWand+Color.swift +++ b/Sources/MagickWand/Wand/PixelWand/PixelWand+Color.swift @@ -40,13 +40,13 @@ public struct Colors { self.pointer = pointer } - public var rgb: MagickWand.RGB { + public var rgb: MagickWand.RGBA { let red = PixelGetRed(self.pointer) let green = PixelGetGreen(self.pointer) let blue = PixelGetBlue(self.pointer) let alpha = PixelGetAlpha(self.pointer) - return MagickWand.RGB(red, green, blue, alpha) + return MagickWand.RGBA(red, green, blue, alpha) } public var hsl: MagickWand.HSL { diff --git a/Sources/MagickWand/WandTypes/MagickWand+Colors.swift b/Sources/MagickWand/WandTypes/MagickWand+Colors.swift index 1e30377..d61cd19 100644 --- a/Sources/MagickWand/WandTypes/MagickWand+Colors.swift +++ b/Sources/MagickWand/WandTypes/MagickWand+Colors.swift @@ -710,8 +710,8 @@ transparent transparent rgba( 0, 0, 0, 0.0) #00000000 return "" } - var rgb: MagickWand.RGB { - return MagickWand.RGB(0, 0, 0) + var rgb: MagickWand.RGBA { + return MagickWand.RGBA(0, 0, 0) } var hex: String { diff --git a/Sources/MagickWand/WandTypes/MagickWand+RGB.swift b/Sources/MagickWand/WandTypes/MagickWand+RGB.swift index f3038f5..ff9cd60 100644 --- a/Sources/MagickWand/WandTypes/MagickWand+RGB.swift +++ b/Sources/MagickWand/WandTypes/MagickWand+RGB.swift @@ -28,7 +28,7 @@ extension MagickWand { - public struct RGB { + public struct RGBA { private(set) var red: Double private(set) var green: Double diff --git a/Sources/MagickWand/WandTypes/MagickWand+Size.swift b/Sources/MagickWand/WandTypes/MagickWand+Size.swift new file mode 100644 index 0000000..529761a --- /dev/null +++ b/Sources/MagickWand/WandTypes/MagickWand+Size.swift @@ -0,0 +1,45 @@ +// MagickWand+Orientation.swift +// +// Copyright (c) 2016 Sergey Minakov +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#if os(Linux) + import CMagickWandLinux +#else + import CMagickWandOSX +#endif + +extension MagickWand { + + public struct Size { + + static let zero = Size() + + var width: Int = 0 + var height: Int = 0 + + init() { } + + init(width: Int, height: Int) { + self.width = width + self.height = height + } + } +} diff --git a/Tests/MagickWandTests/rect.png b/Tests/MagickWandTests/rect.png new file mode 100644 index 0000000000000000000000000000000000000000..03bbeabd5f723de26f2fa53bad4deb75e45c3608 GIT binary patch literal 347 zcmeAS@N?(olHy`uVBq!ia0vp^DL`z*!3HE(nbz$CQjEnx?oJHr&dIz4vPC^z978G? z-_G91dniEQD2L~7@x=d;H`J7b)&?*6EV^vVgnK8SP0z_*dw=zL3Fh~P(@Rd=cW`X` zE5H>rA(5?Z?qzPx69&wO&%JCFNtw}@`234g8>{8Kd!O%qUmpLq+MLbqd8Kn_m14=F zNlhz$S;h%i1$sJ${FTw?n7d+$!m7GH@fN?3Ac0W(VGD=y)yvnzd0# tT;#~t{;y3Ao!nfHejTrJTyTU{{Oldpji=uFECGfVgQu&X%Q~loCII#IkFfv% literal 0 HcmV?d00001