Skip to content

Commit 125efe9

Browse files
committed
Add withUnsafe... methods to Image
1 parent 9d97445 commit 125efe9

2 files changed

Lines changed: 121 additions & 1 deletion

File tree

Sources/EasyImagy/Image.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,21 @@ extension Image {
4747
return pixels.makeIterator()
4848
}
4949
}
50+
51+
extension Image { // Pointers
52+
public func withUnsafeBufferPointer<R>(_ body: (UnsafeBufferPointer<Pixel>) throws -> R) rethrows -> R {
53+
return try pixels.withUnsafeBufferPointer(body)
54+
}
55+
56+
public mutating func withUnsafeMutableBufferPointer<R>(_ body: (inout UnsafeMutableBufferPointer<Pixel>) throws -> R) rethrows -> R {
57+
return try pixels.withUnsafeMutableBufferPointer(body)
58+
}
59+
60+
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
61+
return try pixels.withUnsafeBytes(body)
62+
}
63+
64+
public mutating func withUnsafeMutableBytes<R>(_ body: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R {
65+
return try pixels.withUnsafeMutableBytes(body)
66+
}
67+
}

Tests/EasyImagyTests/ImageTests.swift

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,110 @@ class ImageTests: XCTestCase {
154154
XCTAssertNil(image.pixelAt(x: 0, y: -1))
155155
XCTAssertNil(image.pixelAt(x: 0, y: 2))
156156
}
157+
158+
func testWithUnsafeBufferPointer() {
159+
let image = Image<UInt8>(width: 3, height: 2, pixels: [
160+
1, 2, 3,
161+
4, 5, 6,
162+
])
163+
164+
image.withUnsafeBufferPointer { p in
165+
XCTAssertEqual(p.count, 6)
166+
167+
XCTAssertEqual(p[0], 1)
168+
XCTAssertEqual(p[1], 2)
169+
XCTAssertEqual(p[2], 3)
170+
XCTAssertEqual(p[3], 4)
171+
XCTAssertEqual(p[4], 5)
172+
XCTAssertEqual(p[5], 6)
173+
}
174+
}
157175

158-
func testCopyOnWritePerformanceOfCopy() { // Fast
176+
func testWithUnsafeMutableBufferPointer() {
177+
var image = Image<UInt8>(width: 3, height: 2, pixels: [
178+
1, 2, 3,
179+
4, 5, 6,
180+
])
181+
182+
image.withUnsafeMutableBufferPointer { p in
183+
XCTAssertEqual(p.count, 6)
184+
185+
XCTAssertEqual(p[0], 1)
186+
XCTAssertEqual(p[1], 2)
187+
XCTAssertEqual(p[2], 3)
188+
XCTAssertEqual(p[3], 4)
189+
XCTAssertEqual(p[4], 5)
190+
XCTAssertEqual(p[5], 6)
191+
192+
p[0] += 10
193+
p[1] += 10
194+
p[2] += 10
195+
p[3] += 10
196+
p[4] += 10
197+
p[5] += 10
198+
}
199+
200+
XCTAssertEqual(image.pixelAt(x: 0, y: 0), 11)
201+
XCTAssertEqual(image.pixelAt(x: 1, y: 0), 12)
202+
XCTAssertEqual(image.pixelAt(x: 2, y: 0), 13)
203+
204+
XCTAssertEqual(image.pixelAt(x: 0, y: 1), 14)
205+
XCTAssertEqual(image.pixelAt(x: 1, y: 1), 15)
206+
XCTAssertEqual(image.pixelAt(x: 2, y: 1), 16)
207+
}
208+
209+
func testWithUnsafeBytes() {
210+
let image = Image<UInt8>(width: 3, height: 2, pixels: [
211+
1, 2, 3,
212+
4, 5, 6,
213+
])
214+
215+
image.withUnsafeBytes { p in
216+
XCTAssertEqual(p.count, 6)
217+
218+
XCTAssertEqual(p[0], 1)
219+
XCTAssertEqual(p[1], 2)
220+
XCTAssertEqual(p[2], 3)
221+
XCTAssertEqual(p[3], 4)
222+
XCTAssertEqual(p[4], 5)
223+
XCTAssertEqual(p[5], 6)
224+
}
225+
}
226+
227+
func testWithUnsafeMutableBytes() {
228+
var image = Image<UInt8>(width: 3, height: 2, pixels: [
229+
1, 2, 3,
230+
4, 5, 6,
231+
])
232+
233+
image.withUnsafeMutableBytes { p in
234+
XCTAssertEqual(p.count, 6)
235+
236+
XCTAssertEqual(p[0], 1)
237+
XCTAssertEqual(p[1], 2)
238+
XCTAssertEqual(p[2], 3)
239+
XCTAssertEqual(p[3], 4)
240+
XCTAssertEqual(p[4], 5)
241+
XCTAssertEqual(p[5], 6)
242+
243+
p[0] += 10
244+
p[1] += 10
245+
p[2] += 10
246+
p[3] += 10
247+
p[4] += 10
248+
p[5] += 10
249+
}
250+
251+
XCTAssertEqual(image.pixelAt(x: 0, y: 0), 11)
252+
XCTAssertEqual(image.pixelAt(x: 1, y: 0), 12)
253+
XCTAssertEqual(image.pixelAt(x: 2, y: 0), 13)
254+
255+
XCTAssertEqual(image.pixelAt(x: 0, y: 1), 14)
256+
XCTAssertEqual(image.pixelAt(x: 1, y: 1), 15)
257+
XCTAssertEqual(image.pixelAt(x: 2, y: 1), 16)
258+
}
259+
260+
func testCopyOnWritePerformanceOfCopy() { // Fast
159261
let image = Image<RGBA<UInt8>>(width: 8192, height: 8192, pixel: RGBA.transparent)
160262
measure {
161263
let copy = image

0 commit comments

Comments
 (0)