Add tests for Mat.at function
authorGiles Payne <gilespayne@gmail.com>
Sun, 6 Jun 2021 05:19:30 +0000 (14:19 +0900)
committerGiles Payne <gilespayne@gmail.com>
Sun, 6 Jun 2021 10:34:48 +0000 (19:34 +0900)
modules/core/misc/objc/common/MatExt.swift
modules/core/misc/objc/test/MatTest.swift

index 032274af05c4c1d3777f53734f13ac2d368d22f2..5ce3a5e6fb566ca270e42532813d28555da8fc16 100644 (file)
@@ -33,6 +33,10 @@ func throwIncompatibleBufferSize(count: Int, channels: Int32) throws {
     )
 }
 
+public typealias T2<T> = (T, T)
+public typealias T3<T> = (T, T, T)
+public typealias T4<T> = (T, T, T, T)
+
 public extension Mat {
 
     convenience init(rows:Int32, cols:Int32, type:Int32, data:[Int8]) {
@@ -263,7 +267,7 @@ public class MatAt<N: Atable> {
 
     private let mat: Mat
     private let indices: [Int32]
-    var v: N {
+    public var v: N {
         get {
             return N.getAt(m: mat, indices: indices)
         }
@@ -271,7 +275,7 @@ public class MatAt<N: Atable> {
             N.putAt(m: mat, indices: indices, v: value)
         }
     }
-    var v2c: (N, N) {
+    public var v2c: (N, N) {
         get {
             return N.getAt2c(m: mat, indices: indices)
         }
@@ -279,7 +283,7 @@ public class MatAt<N: Atable> {
             N.putAt2c(m: mat, indices: indices, v: value)
         }
     }
-    var v3c: (N, N, N) {
+    public var v3c: (N, N, N) {
         get {
             return N.getAt3c(m: mat, indices: indices)
         }
@@ -287,7 +291,7 @@ public class MatAt<N: Atable> {
             N.putAt3c(m: mat, indices: indices, v: value)
         }
     }
-    var v4c: (N, N, N, N) {
+    public var v4c: (N, N, N, N) {
         get {
             return N.getAt4c(m: mat, indices: indices)
         }
index af26eb0bdb20d013e2af1eee820268ed1db6805b..14c440b5eb889e81600a977ad2058ee81c77c626 100644 (file)
@@ -1143,4 +1143,28 @@ class MatTests: OpenCVTestCase {
         XCTAssertEqual(5, bufferOut[63*80 + 63])
     }
 
+    func testMatAt() {
+        let uc1 = Mat(rows: 2, cols: 3, type: CvType.CV_8U)
+        try! uc1.put(row: 0, col: 0, data: [1, 2, 3, 4, 5, 6] as [Int8])
+        XCTAssertEqual(UInt8(1), uc1.at(row: 0, col: 0).v)
+        XCTAssertEqual(UInt8(2), uc1.at(row: 0, col: 1).v)
+        XCTAssertEqual(UInt8(3), uc1.at(row: 0, col: 2).v)
+        XCTAssertEqual(UInt8(4), uc1.at(row: 1, col: 0).v)
+        XCTAssertEqual(UInt8(5), uc1.at(row: 1, col: 1).v)
+        XCTAssertEqual(UInt8(6), uc1.at(row: 1, col: 2).v)
+        uc1.at(row: 0, col: 0).v = UInt8(7)
+        uc1.at(row: 0, col: 1).v = UInt8(8)
+        uc1.at(row: 0, col: 2).v = UInt8(9)
+        uc1.at(row: 1, col: 0).v = UInt8(10)
+        uc1.at(row: 1, col: 1).v = UInt8(11)
+        uc1.at(row: 1, col: 2).v = UInt8(12)
+        var data = [Int8](repeating: 0, count: 6)
+        try! uc1.get(row: 0, col: 0, data: &data)
+        XCTAssertEqual(data, [7, 8, 9, 10, 11, 12] as [Int8])
+        let (b, g, r): T3<UInt8>  = rgbLena.at(row: 0, col: 0).v3c
+        XCTAssertEqual(b, UInt8(128))
+        XCTAssertEqual(g, UInt8(138))
+        XCTAssertEqual(r, UInt8(225))
+    }
+
 }