Skip to content

Commit 4d6b1c4

Browse files
authored
Merge pull request #23 from koher/dev-0.5.0
Develop 0.5.0
2 parents 5980fe6 + 09dc992 commit 4d6b1c4

52 files changed

Lines changed: 6048 additions & 5546 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ matrix:
22
include:
33
- os: osx
44
language: objective-c
5-
osx_image: xcode9.2
5+
osx_image: xcode9.4
66
script:
77
- set -o pipefail
88
- xcodebuild test -scheme EasyImagy-macOS -configuration Debug | xcpretty -c
@@ -11,15 +11,15 @@ matrix:
1111
- xcodebuild build -scheme EasyImagy-watchOS -configuration Debug -sdk watchsimulator -destination "platform=watchOS Simulator,name=Apple Watch Series 3 - 38mm" | xcpretty -c
1212
- os: osx
1313
language: generic
14-
osx_image: xcode9.2
14+
osx_image: xcode9.4
1515
script:
1616
- swift --version
1717
- swift build
1818
- swift test
1919
- os: linux
2020
language: generic
2121
sudo: required
22-
env: SWIFT_VERSION=4.0
22+
env: SWIFT_VERSION=4.1
2323
install:
2424
- eval "$(curl -sL https://swiftenv.fuller.li/install.sh)"
2525
script:

EasyImagy.xcodeproj/project.pbxproj

Lines changed: 43 additions & 31 deletions
Large diffs are not rendered by default.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

EasyImagy.xcodeproj/xcshareddata/xcschemes/EasyImagy-iOS.xcscheme

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0900"
3+
LastUpgradeVersion = "0940"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -26,7 +26,6 @@
2626
buildConfiguration = "Debug"
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29-
language = ""
3029
shouldUseLaunchSchemeArgsEnv = "YES">
3130
<Testables>
3231
<TestableReference
@@ -56,7 +55,6 @@
5655
buildConfiguration = "Debug"
5756
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
5857
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
59-
language = ""
6058
launchStyle = "0"
6159
useCustomWorkingDirectory = "NO"
6260
ignoresPersistentStateOnLaunch = "NO"

EasyImagy.xcodeproj/xcshareddata/xcschemes/EasyImagy-macOS.xcscheme

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0900"
3+
LastUpgradeVersion = "0940"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -26,7 +26,6 @@
2626
buildConfiguration = "Debug"
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29-
language = ""
3029
shouldUseLaunchSchemeArgsEnv = "YES">
3130
<Testables>
3231
<TestableReference
@@ -56,7 +55,6 @@
5655
buildConfiguration = "Debug"
5756
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
5857
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
59-
language = ""
6058
launchStyle = "0"
6159
useCustomWorkingDirectory = "NO"
6260
ignoresPersistentStateOnLaunch = "NO"

EasyImagy.xcodeproj/xcshareddata/xcschemes/EasyImagy-tvOS.xcscheme

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0900"
3+
LastUpgradeVersion = "0940"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -26,7 +26,6 @@
2626
buildConfiguration = "Debug"
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29-
language = ""
3029
shouldUseLaunchSchemeArgsEnv = "YES">
3130
<Testables>
3231
<TestableReference
@@ -56,7 +55,6 @@
5655
buildConfiguration = "Debug"
5756
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
5857
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
59-
language = ""
6058
launchStyle = "0"
6159
useCustomWorkingDirectory = "NO"
6260
ignoresPersistentStateOnLaunch = "NO"

EasyImagy.xcodeproj/xcshareddata/xcschemes/EasyImagy-watchOS.xcscheme

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0900"
3+
LastUpgradeVersion = "0940"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -26,7 +26,6 @@
2626
buildConfiguration = "Debug"
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29-
language = ""
3029
shouldUseLaunchSchemeArgsEnv = "YES">
3130
<Testables>
3231
</Testables>
@@ -37,7 +36,6 @@
3736
buildConfiguration = "Debug"
3837
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
3938
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
40-
language = ""
4139
launchStyle = "0"
4240
useCustomWorkingDirectory = "NO"
4341
ignoresPersistentStateOnLaunch = "NO"

README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ let image = Image<RGBA<UInt8>>(data: Data(/* ... */))!
8585
let image = Image<RGBA<UInt8>>(uiImage: imageView.image!) // from a UIImage
8686
```
8787

88+
```swift
89+
let image = Image<RGBA<UInt8>>(nsImage: imageView.image!) // from a NSImage
90+
```
91+
8892
```swift
8993
let image = Image<RGBA<UInt8>>(width: 640, height: 480, pixels: pixels) // from pixels
9094
```
@@ -185,7 +189,7 @@ let cropped = Image<RGBA<UInt8>>(slice) // Copying is executed here
185189

186190
### Conversion
187191

188-
`Image` can be converted by `map` as well as `Array`. Followings are the examples.
192+
`Image` can be converted by `map` in the same way as `Array`. Followings are the examples.
189193

190194
#### Grayscale
191195

@@ -260,6 +264,21 @@ let image = Image<RGBA<UInt8>>(nsImage: imageView.image!)
260264
imageView.image = image.nsImage
261265
```
262266

267+
### With CoreGraphics
268+
269+
```swift
270+
// Drawing on images with CoreGraphics
271+
var image = Image<PremultipliedRGBA<UInt8>>(uiImage: imageView.image!)
272+
image.withCGContext { context in
273+
context.setLineWidth(1)
274+
context.setStrokeColor(UIColor.red.cgColor)
275+
context.move(to: CGPoint(x: -1, y: -1))
276+
context.addLine(to: CGPoint(x: 640, y: 480))
277+
context.strokePath()
278+
}
279+
imageView.image = image.uiImage
280+
```
281+
263282
## Requirements
264283

265284
- Swift 4 or later
@@ -280,7 +299,7 @@ import PackageDescription
280299
let package = Package(
281300
...
282301
dependencies: [
283-
.package(url: "https://github.com/koher/EasyImagy.git", from: "0.4.0"),
302+
.package(url: "https://github.com/koher/EasyImagy.git", from: "0.5.0"),
284303
],
285304
targets: [
286305
.target(
@@ -297,7 +316,7 @@ let package = Package(
297316
**Cartfile**
298317

299318
```
300-
github "koher/EasyImagy" "0.4.0"
319+
github "koher/EasyImagy" ~> 0.5.0
301320
```
302321

303322
### Manually

Sources/EasyImagy/AnyImage.swift

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
import Foundation
22

33
public struct AnyImage<Pixel> : ImageProtocol {
4+
public typealias SubImage = AnyImage<Pixel>
5+
public typealias Element = Pixel // FIXME: Remove this line in the future. Swift 4.1 needs it to build `AnyImage`.
6+
47
private var box: AnyImageBox<Pixel>
58
private let lock = NSLock()
69

7-
public init<I : ImageProtocol>(_ image: I) where I.Pixel == Pixel {
8-
box = ImageBox<I>(image)
9-
}
10+
public let xRange: CountableRange<Int>
11+
public let yRange: CountableRange<Int>
1012

11-
public init(width: Int, height: Int, pixels: [Pixel]) {
12-
self.init(Image<Pixel>(width: width, height: height, pixels: pixels))
13+
private init(box: AnyImageBox<Pixel>, xRange: CountableRange<Int>, yRange: CountableRange<Int>) {
14+
self.box = box
15+
self.xRange = xRange
16+
self.yRange = yRange
1317
}
1418

15-
public var xRange: CountableRange<Int> {
16-
return box.xRange
19+
internal init<I : ImageProtocol>(_ image: I, xRange: CountableRange<Int>, yRange: CountableRange<Int>) where I.Pixel == Pixel {
20+
self.box = ImageBox<I>(image)
21+
self.xRange = xRange
22+
self.yRange = yRange
23+
}
24+
25+
public init<I : ImageProtocol>(_ image: I) where I.Pixel == Pixel {
26+
self.init(image, xRange: image.xRange, yRange: image.yRange)
1727
}
1828

19-
public var yRange: CountableRange<Int> {
20-
return box.yRange
29+
public init(width: Int, height: Int, pixels: [Pixel]) {
30+
self.init(Image<Pixel>(width: width, height: height, pixels: pixels))
2131
}
2232

2333
public subscript(x: Int, y: Int) -> Pixel {
@@ -36,25 +46,21 @@ public struct AnyImage<Pixel> : ImageProtocol {
3646
}
3747
}
3848

39-
public subscript(xRange: CountableRange<Int>, yRange: CountableRange<Int>) -> ImageSlice<Pixel> {
40-
return box[xRange, yRange]
49+
public subscript(xRange: CountableRange<Int>, yRange: CountableRange<Int>) -> AnyImage<Pixel> {
50+
return AnyImage(box: self.box, xRange: xRange, yRange: yRange)
4151
}
42-
43-
public func makeIterator() -> AnyIterator<Pixel> {
44-
return box.makeIterator()
52+
}
53+
54+
extension AnyImage : Equatable where Pixel : Equatable {
55+
public static func ==(lhs: AnyImage<Pixel>, rhs: AnyImage<Pixel>) -> Bool {
56+
guard lhs.width == rhs.width else { return false }
57+
guard lhs.height == rhs.height else { return false }
58+
return zip(lhs, rhs).reduce(true) { $0 && $1.0 == $1.1 }
4559
}
4660
}
4761

4862
extension AnyImage {
4963
private class AnyImageBox<Pixel> {
50-
public var xRange: CountableRange<Int> {
51-
fatalError("Abstract")
52-
}
53-
54-
public var yRange: CountableRange<Int> {
55-
fatalError("Abstract")
56-
}
57-
5864
public subscript(x: Int, y: Int) -> Pixel {
5965
get {
6066
fatalError("Abstract")
@@ -65,14 +71,6 @@ extension AnyImage {
6571
}
6672
}
6773

68-
public subscript(xRange: CountableRange<Int>, yRange: CountableRange<Int>) -> ImageSlice<Pixel> {
69-
fatalError("Abstract")
70-
}
71-
72-
public func makeIterator() -> AnyIterator<Pixel> {
73-
fatalError("Abstract")
74-
}
75-
7674
public func copied() -> AnyImageBox<Pixel> {
7775
fatalError("Abstract")
7876
}
@@ -85,14 +83,6 @@ extension AnyImage {
8583
self.base = base
8684
}
8785

88-
override public var xRange: CountableRange<Int> {
89-
return base.xRange
90-
}
91-
92-
override public var yRange: CountableRange<Int> {
93-
return base.yRange
94-
}
95-
9686
override public subscript(x: Int, y: Int) -> I.Pixel {
9787
get {
9888
return base[x, y]
@@ -103,14 +93,6 @@ extension AnyImage {
10393
}
10494
}
10595

106-
override public subscript(xRange: CountableRange<Int>, yRange: CountableRange<Int>) -> ImageSlice<I.Pixel> {
107-
return base[xRange, yRange]
108-
}
109-
110-
override public func makeIterator() -> AnyIterator<I.Pixel> {
111-
return AnyIterator(base.makeIterator())
112-
}
113-
11496
override public func copied() -> ImageBox<I> {
11597
return ImageBox<I>(base)
11698
}

0 commit comments

Comments
 (0)