MatExt.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. //
  2. // MatExt.swift
  3. //
  4. // Created by Giles Payne on 2020/01/19.
  5. //
  6. import Foundation
  7. let OpenCVErrorDomain = "OpenCVErrorDomain"
  8. enum OpenCVError : Int {
  9. case IncompatibleDataType = 10001
  10. case IncompatibleBufferSize
  11. }
  12. func throwIncompatibleDataType(typeName: String) throws {
  13. throw NSError(
  14. domain: OpenCVErrorDomain,
  15. code: OpenCVError.IncompatibleDataType.rawValue,
  16. userInfo: [
  17. NSLocalizedDescriptionKey: "Incompatible Mat type \(typeName)"
  18. ]
  19. )
  20. }
  21. func throwIncompatibleBufferSize(count: Int, channels: Int32) throws {
  22. throw NSError(
  23. domain: OpenCVErrorDomain,
  24. code: OpenCVError.IncompatibleBufferSize.rawValue,
  25. userInfo: [
  26. NSLocalizedDescriptionKey: "Provided data element number \(count) should be multiple of the Mat channels count \(channels)"
  27. ]
  28. )
  29. }
  30. public typealias T2<T> = (T, T)
  31. public typealias T3<T> = (T, T, T)
  32. public typealias T4<T> = (T, T, T, T)
  33. public extension Mat {
  34. convenience init(rows:Int32, cols:Int32, type:Int32, data:[Int8]) {
  35. let dataObject = data.withUnsafeBufferPointer { Data(buffer: $0) }
  36. self.init(rows: rows, cols: cols, type: type, data: dataObject)
  37. }
  38. convenience init(rows:Int32, cols:Int32, type:Int32, data:[Int8], step:Int) {
  39. let dataObject = data.withUnsafeBufferPointer { Data(buffer: $0) }
  40. self.init(rows: rows, cols: cols, type: type, data: dataObject, step:step)
  41. }
  42. @discardableResult func get(indices:[Int32], data:inout [Int8]) throws -> Int32 {
  43. let channels = CvType.channels(Int32(type()))
  44. if Int32(data.count) % channels != 0 {
  45. try throwIncompatibleBufferSize(count: data.count, channels: channels)
  46. } else if depth() != CvType.CV_8U && depth() != CvType.CV_8S {
  47. try throwIncompatibleDataType(typeName: CvType.type(toString: type()))
  48. }
  49. let count = Int32(data.count)
  50. return data.withUnsafeMutableBufferPointer { body in
  51. return __get(indices as [NSNumber], count: count, byteBuffer: body.baseAddress!)
  52. }
  53. }
  54. @discardableResult func get(indices:[Int32], data:inout [UInt8]) throws -> Int32 {
  55. let channels = CvType.channels(Int32(type()))
  56. if Int32(data.count) % channels != 0 {
  57. try throwIncompatibleBufferSize(count: data.count, channels: channels)
  58. } else if depth() != CvType.CV_8U {
  59. try throwIncompatibleDataType(typeName: CvType.type(toString: type()))
  60. }
  61. let count = Int32(data.count)
  62. return data.withUnsafeMutableBufferPointer { body in
  63. body.withMemoryRebound(to: Int8.self) { reboundBody in
  64. return __get(indices as [NSNumber], count: count, byteBuffer: reboundBody.baseAddress!)
  65. }
  66. }
  67. }
  68. @discardableResult func get(indices:[Int32], data:inout [Double]) throws -> Int32 {
  69. let channels = CvType.channels(Int32(type()))
  70. if Int32(data.count) % channels != 0 {
  71. try throwIncompatibleBufferSize(count: data.count, channels: channels)
  72. } else if depth() != CvType.CV_64F {
  73. try throwIncompatibleDataType(typeName: CvType.type(toString: type()))
  74. }
  75. let count = Int32(data.count)
  76. return data.withUnsafeMutableBufferPointer { body in
  77. return __get(indices as [NSNumber], count: count, doubleBuffer: body.baseAddress!)
  78. }
  79. }
  80. @discardableResult func get(indices:[Int32], data:inout [Float]) throws -> Int32 {
  81. let channels = CvType.channels(Int32(type()))
  82. if Int32(data.count) % channels != 0 {
  83. try throwIncompatibleBufferSize(count: data.count, channels: channels)
  84. } else if depth() != CvType.CV_32F {
  85. try throwIncompatibleDataType(typeName: CvType.type(toString: type()))
  86. }
  87. let count = Int32(data.count)
  88. return data.withUnsafeMutableBufferPointer { body in
  89. return __get(indices as [NSNumber], count: count, floatBuffer: body.baseAddress!)
  90. }
  91. }
  92. @discardableResult func get(indices:[Int32], data:inout [Int32]) throws -> Int32 {
  93. let channels = CvType.channels(Int32(type()))
  94. if Int32(data.count) % channels != 0 {
  95. try throwIncompatibleBufferSize(count: data.count, channels: channels)
  96. } else if depth() != CvType.CV_32S {
  97. try throwIncompatibleDataType(typeName: CvType.type(toString: type()))
  98. }
  99. let count = Int32(data.count)
  100. return data.withUnsafeMutableBufferPointer { body in
  101. return __get(indices as [NSNumber], count: count, intBuffer: body.baseAddress!)
  102. }
  103. }
  104. @discardableResult func get(indices:[Int32], data:inout [Int16]) throws -> Int32 {
  105. let channels = CvType.channels(Int32(type()))
  106. if Int32(data.count) % channels != 0 {
  107. try throwIncompatibleBufferSize(count: data.count, channels: channels)
  108. } else if depth() != CvType.CV_16U && depth() != CvType.CV_16S {
  109. try throwIncompatibleDataType(typeName: CvType.type(toString: type()))
  110. }
  111. let count = Int32(data.count)
  112. return data.withUnsafeMutableBufferPointer { body in
  113. return __get(indices as [NSNumber], count: count, shortBuffer: body.baseAddress!)
  114. }
  115. }
  116. @discardableResult func get(indices:[Int32], data:inout [UInt16]) throws -> Int32 {
  117. let channels = CvType.channels(Int32(type()))
  118. if Int32(data.count) % channels != 0 {
  119. try throwIncompatibleBufferSize(count: data.count, channels: channels)
  120. } else if depth() != CvType.CV_16U {
  121. try throwIncompatibleDataType(typeName: CvType.type(toString: type()))
  122. }
  123. let count = Int32(data.count)
  124. return data.withUnsafeMutableBufferPointer { body in
  125. body.withMemoryRebound(to: Int16.self) { reboundBody in
  126. return __get(indices as [NSNumber], count: count, shortBuffer: reboundBody.baseAddress!)
  127. }
  128. }
  129. }
  130. @discardableResult func get(row: Int32, col: Int32, data:inout [Int8]) throws -> Int32 {
  131. return try get(indices: [row, col], data: &data)
  132. }
  133. @discardableResult func get(row: Int32, col: Int32, data:inout [UInt8]) throws -> Int32 {
  134. return try get(indices: [row, col], data: &data)
  135. }
  136. @discardableResult func get(row: Int32, col: Int32, data:inout [Double]) throws -> Int32 {
  137. return try get(indices: [row, col], data: &data)
  138. }
  139. @discardableResult func get(row: Int32, col: Int32, data:inout [Float]) throws -> Int32 {
  140. return try get(indices: [row, col], data: &data)
  141. }
  142. @discardableResult func get(row: Int32, col: Int32, data:inout [Int32]) throws -> Int32 {
  143. return try get(indices: [row, col], data: &data)
  144. }
  145. @discardableResult func get(row: Int32, col: Int32, data:inout [Int16]) throws -> Int32 {
  146. return try get(indices: [row, col], data: &data)
  147. }
  148. @discardableResult func get(row: Int32, col: Int32, data:inout [UInt16]) throws -> Int32 {
  149. return try get(indices: [row, col], data: &data)
  150. }
  151. @discardableResult func put(indices:[Int32], data:[Int8]) throws -> Int32 {
  152. let channels = CvType.channels(Int32(type()))
  153. if Int32(data.count) % channels != 0 {
  154. try throwIncompatibleBufferSize(count: data.count, channels: channels)
  155. } else if depth() != CvType.CV_8U && depth() != CvType.CV_8S {
  156. try throwIncompatibleDataType(typeName: CvType.type(toString: type()))
  157. }
  158. let count = Int32(data.count)
  159. return data.withUnsafeBufferPointer { body in
  160. return __put(indices as [NSNumber], count: count, byteBuffer: body.baseAddress!)
  161. }
  162. }
  163. @discardableResult func put(indices:[Int32], data:[UInt8]) throws -> Int32 {
  164. let channels = CvType.channels(Int32(type()))
  165. if Int32(data.count) % channels != 0 {
  166. try throwIncompatibleBufferSize(count: data.count, channels: channels)
  167. } else if depth() != CvType.CV_8U {
  168. try throwIncompatibleDataType(typeName: CvType.type(toString: type()))
  169. }
  170. let count = Int32(data.count)
  171. return data.withUnsafeBufferPointer { body in
  172. body.withMemoryRebound(to: Int8.self) { reboundBody in
  173. return __put(indices as [NSNumber], count: count, byteBuffer: reboundBody.baseAddress!)
  174. }
  175. }
  176. }
  177. @discardableResult func put(indices:[Int32], data:[Int8], offset: Int, length: Int32) throws -> Int32 {
  178. let channels = CvType.channels(Int32(type()))
  179. if Int32(data.count) % channels != 0 {
  180. try throwIncompatibleBufferSize(count: data.count, channels: channels)
  181. } else if depth() != CvType.CV_8U && depth() != CvType.CV_8S {
  182. try throwIncompatibleDataType(typeName: CvType.type(toString: type()))
  183. }
  184. return data.withUnsafeBufferPointer { body in
  185. return __put(indices as [NSNumber], count: length, byteBuffer: body.baseAddress! + offset)
  186. }
  187. }
  188. // unlike other put:indices:data functions this one (with [Double]) should convert input values to correct type
  189. @discardableResult func put(indices:[Int32], data:[Double]) throws -> Int32 {
  190. let channels = CvType.channels(Int32(type()))
  191. if Int32(data.count) % channels != 0 {
  192. try throwIncompatibleBufferSize(count: data.count, channels: channels)
  193. }
  194. if depth() == CvType.CV_64F {
  195. let count = Int32(data.count)
  196. return data.withUnsafeBufferPointer { body in
  197. return __put(indices as [NSNumber], count: count, doubleBuffer: body.baseAddress!)
  198. }
  199. } else {
  200. return __put(indices as [NSNumber], data: data as [NSNumber])
  201. }
  202. }
  203. @discardableResult func put(indices:[Int32], data:[Float]) throws -> Int32 {
  204. let channels = CvType.channels(Int32(type()))
  205. if Int32(data.count) % channels != 0 {
  206. try throwIncompatibleBufferSize(count: data.count, channels: channels)
  207. } else if depth() != CvType.CV_32F {
  208. try throwIncompatibleDataType(typeName: CvType.type(toString: type()))
  209. }
  210. let count = Int32(data.count)
  211. return data.withUnsafeBufferPointer { body in
  212. return __put(indices as [NSNumber], count: count, floatBuffer: body.baseAddress!)
  213. }
  214. }
  215. @discardableResult func put(indices:[Int32], data:[Int32]) throws -> Int32 {
  216. let channels = CvType.channels(Int32(type()))
  217. if Int32(data.count) % channels != 0 {
  218. try throwIncompatibleBufferSize(count: data.count, channels: channels)
  219. } else if depth() != CvType.CV_32S {
  220. try throwIncompatibleDataType(typeName: CvType.type(toString: type()))
  221. }
  222. let count = Int32(data.count)
  223. return data.withUnsafeBufferPointer { body in
  224. return __put(indices as [NSNumber], count: count, intBuffer: body.baseAddress!)
  225. }
  226. }
  227. @discardableResult func put(indices:[Int32], data:[Int16]) throws -> Int32 {
  228. let channels = CvType.channels(Int32(type()))
  229. if Int32(data.count) % channels != 0 {
  230. try throwIncompatibleBufferSize(count: data.count, channels: channels)
  231. } else if depth() != CvType.CV_16U && depth() != CvType.CV_16S {
  232. try throwIncompatibleDataType(typeName: CvType.type(toString: type()))
  233. }
  234. let count = Int32(data.count)
  235. return data.withUnsafeBufferPointer { body in
  236. return __put(indices as [NSNumber], count: count, shortBuffer: body.baseAddress!)
  237. }
  238. }
  239. @discardableResult func put(indices:[Int32], data:[UInt16]) throws -> Int32 {
  240. let channels = CvType.channels(Int32(type()))
  241. if Int32(data.count) % channels != 0 {
  242. try throwIncompatibleBufferSize(count: data.count, channels: channels)
  243. } else if depth() != CvType.CV_16U {
  244. try throwIncompatibleDataType(typeName: CvType.type(toString: type()))
  245. }
  246. let count = Int32(data.count)
  247. return data.withUnsafeBufferPointer { body in
  248. body.withMemoryRebound(to: Int16.self) { reboundBody in
  249. return __put(indices as [NSNumber], count: count, shortBuffer: reboundBody.baseAddress!)
  250. }
  251. }
  252. }
  253. @discardableResult func put(row: Int32, col: Int32, data:[Int8]) throws -> Int32 {
  254. return try put(indices: [row, col], data: data)
  255. }
  256. @discardableResult func put(row: Int32, col: Int32, data:[UInt8]) throws -> Int32 {
  257. return try put(indices: [row, col], data: data)
  258. }
  259. @discardableResult func put(row: Int32, col: Int32, data: [Int8], offset: Int, length: Int32) throws -> Int32 {
  260. return try put(indices: [row, col], data: data, offset: offset, length: length)
  261. }
  262. @discardableResult func put(row: Int32, col: Int32, data: [Double]) throws -> Int32 {
  263. return try put(indices: [row, col], data: data)
  264. }
  265. @discardableResult func put(row: Int32, col: Int32, data: [Float]) throws -> Int32 {
  266. return try put(indices: [row, col], data: data)
  267. }
  268. @discardableResult func put(row: Int32, col: Int32, data: [Int32]) throws -> Int32 {
  269. return try put(indices: [row, col], data: data)
  270. }
  271. @discardableResult func put(row: Int32, col: Int32, data: [Int16]) throws -> Int32 {
  272. return try put(indices: [row, col], data: data)
  273. }
  274. @discardableResult func put(row: Int32, col: Int32, data: [UInt16]) throws -> Int32 {
  275. return try put(indices: [row, col], data: data)
  276. }
  277. @discardableResult func get(row: Int32, col: Int32) -> [Double] {
  278. return get(indices: [row, col])
  279. }
  280. @discardableResult func get(indices: [Int32]) -> [Double] {
  281. return __get(indices as [NSNumber]) as! [Double]
  282. }
  283. }
  284. public protocol Atable {
  285. static func getAt(m: Mat, indices:[Int32]) -> Self
  286. static func putAt(m: Mat, indices:[Int32], v: Self)
  287. static func getAt2c(m: Mat, indices:[Int32]) -> (Self, Self)
  288. static func putAt2c(m: Mat, indices:[Int32], v: (Self, Self))
  289. static func getAt3c(m: Mat, indices:[Int32]) -> (Self, Self, Self)
  290. static func putAt3c(m: Mat, indices:[Int32], v: (Self, Self, Self))
  291. static func getAt4c(m: Mat, indices:[Int32]) -> (Self, Self, Self, Self)
  292. static func putAt4c(m: Mat, indices:[Int32], v: (Self, Self, Self, Self))
  293. }
  294. public class MatAt<N: Atable> {
  295. init(mat: Mat, indices: [Int32]) {
  296. self.mat = mat
  297. self.indices = indices
  298. }
  299. private let mat: Mat
  300. private let indices: [Int32]
  301. public var v: N {
  302. get {
  303. return N.getAt(m: mat, indices: indices)
  304. }
  305. set(value) {
  306. N.putAt(m: mat, indices: indices, v: value)
  307. }
  308. }
  309. public var v2c: (N, N) {
  310. get {
  311. return N.getAt2c(m: mat, indices: indices)
  312. }
  313. set(value) {
  314. N.putAt2c(m: mat, indices: indices, v: value)
  315. }
  316. }
  317. public var v3c: (N, N, N) {
  318. get {
  319. return N.getAt3c(m: mat, indices: indices)
  320. }
  321. set(value) {
  322. N.putAt3c(m: mat, indices: indices, v: value)
  323. }
  324. }
  325. public var v4c: (N, N, N, N) {
  326. get {
  327. return N.getAt4c(m: mat, indices: indices)
  328. }
  329. set(value) {
  330. N.putAt4c(m: mat, indices: indices, v: value)
  331. }
  332. }
  333. }
  334. extension UInt8: Atable {
  335. public static func getAt(m: Mat, indices:[Int32]) -> UInt8 {
  336. var tmp = [UInt8](repeating: 0, count: 1)
  337. try! m.get(indices: indices, data: &tmp)
  338. return tmp[0]
  339. }
  340. public static func putAt(m: Mat, indices: [Int32], v: UInt8) {
  341. let tmp = [v]
  342. try! m.put(indices: indices, data: tmp)
  343. }
  344. public static func getAt2c(m: Mat, indices:[Int32]) -> (UInt8, UInt8) {
  345. var tmp = [UInt8](repeating: 0, count: 2)
  346. try! m.get(indices: indices, data: &tmp)
  347. return (tmp[0], tmp[1])
  348. }
  349. public static func putAt2c(m: Mat, indices: [Int32], v: (UInt8, UInt8)) {
  350. let tmp = [v.0, v.1]
  351. try! m.put(indices: indices, data: tmp)
  352. }
  353. public static func getAt3c(m: Mat, indices:[Int32]) -> (UInt8, UInt8, UInt8) {
  354. var tmp = [UInt8](repeating: 0, count: 3)
  355. try! m.get(indices: indices, data: &tmp)
  356. return (tmp[0], tmp[1], tmp[2])
  357. }
  358. public static func putAt3c(m: Mat, indices: [Int32], v: (UInt8, UInt8, UInt8)) {
  359. let tmp = [v.0, v.1, v.2]
  360. try! m.put(indices: indices, data: tmp)
  361. }
  362. public static func getAt4c(m: Mat, indices:[Int32]) -> (UInt8, UInt8, UInt8, UInt8) {
  363. var tmp = [UInt8](repeating: 0, count: 4)
  364. try! m.get(indices: indices, data: &tmp)
  365. return (tmp[0], tmp[1], tmp[2], tmp[3])
  366. }
  367. public static func putAt4c(m: Mat, indices: [Int32], v: (UInt8, UInt8, UInt8, UInt8)) {
  368. let tmp = [v.0, v.1, v.2, v.3]
  369. try! m.put(indices: indices, data: tmp)
  370. }
  371. }
  372. extension Int8: Atable {
  373. public static func getAt(m: Mat, indices:[Int32]) -> Int8 {
  374. var tmp = [Int8](repeating: 0, count: 1)
  375. try! m.get(indices: indices, data: &tmp)
  376. return tmp[0]
  377. }
  378. public static func putAt(m: Mat, indices: [Int32], v: Int8) {
  379. let tmp = [v]
  380. try! m.put(indices: indices, data: tmp)
  381. }
  382. public static func getAt2c(m: Mat, indices:[Int32]) -> (Int8, Int8) {
  383. var tmp = [Int8](repeating: 0, count: 2)
  384. try! m.get(indices: indices, data: &tmp)
  385. return (tmp[0], tmp[1])
  386. }
  387. public static func putAt2c(m: Mat, indices: [Int32], v: (Int8, Int8)) {
  388. let tmp = [v.0, v.1]
  389. try! m.put(indices: indices, data: tmp)
  390. }
  391. public static func getAt3c(m: Mat, indices:[Int32]) -> (Int8, Int8, Int8) {
  392. var tmp = [Int8](repeating: 0, count: 3)
  393. try! m.get(indices: indices, data: &tmp)
  394. return (tmp[0], tmp[1], tmp[2])
  395. }
  396. public static func putAt3c(m: Mat, indices: [Int32], v: (Int8, Int8, Int8)) {
  397. let tmp = [v.0, v.1, v.2]
  398. try! m.put(indices: indices, data: tmp)
  399. }
  400. public static func getAt4c(m: Mat, indices:[Int32]) -> (Int8, Int8, Int8, Int8) {
  401. var tmp = [Int8](repeating: 0, count: 4)
  402. try! m.get(indices: indices, data: &tmp)
  403. return (tmp[0], tmp[1], tmp[2], tmp[3])
  404. }
  405. public static func putAt4c(m: Mat, indices: [Int32], v: (Int8, Int8, Int8, Int8)) {
  406. let tmp = [v.0, v.1, v.2, v.3]
  407. try! m.put(indices: indices, data: tmp)
  408. }
  409. }
  410. extension Double: Atable {
  411. public static func getAt(m: Mat, indices:[Int32]) -> Double {
  412. var tmp = [Double](repeating: 0, count: 1)
  413. try! m.get(indices: indices, data: &tmp)
  414. return tmp[0]
  415. }
  416. public static func putAt(m: Mat, indices: [Int32], v: Double) {
  417. let tmp = [v]
  418. try! m.put(indices: indices, data: tmp)
  419. }
  420. public static func getAt2c(m: Mat, indices:[Int32]) -> (Double, Double) {
  421. var tmp = [Double](repeating: 0, count: 2)
  422. try! m.get(indices: indices, data: &tmp)
  423. return (tmp[0], tmp[1])
  424. }
  425. public static func putAt2c(m: Mat, indices: [Int32], v: (Double, Double)) {
  426. let tmp = [v.0, v.1]
  427. try! m.put(indices: indices, data: tmp)
  428. }
  429. public static func getAt3c(m: Mat, indices:[Int32]) -> (Double, Double, Double) {
  430. var tmp = [Double](repeating: 0, count: 3)
  431. try! m.get(indices: indices, data: &tmp)
  432. return (tmp[0], tmp[1], tmp[2])
  433. }
  434. public static func putAt3c(m: Mat, indices: [Int32], v: (Double, Double, Double)) {
  435. let tmp = [v.0, v.1, v.2]
  436. try! m.put(indices: indices, data: tmp)
  437. }
  438. public static func getAt4c(m: Mat, indices:[Int32]) -> (Double, Double, Double, Double) {
  439. var tmp = [Double](repeating: 0, count: 4)
  440. try! m.get(indices: indices, data: &tmp)
  441. return (tmp[0], tmp[1], tmp[2], tmp[3])
  442. }
  443. public static func putAt4c(m: Mat, indices: [Int32], v: (Double, Double, Double, Double)) {
  444. let tmp = [v.0, v.1, v.2, v.3]
  445. try! m.put(indices: indices, data: tmp)
  446. }
  447. }
  448. extension Float: Atable {
  449. public static func getAt(m: Mat, indices:[Int32]) -> Float {
  450. var tmp = [Float](repeating: 0, count: 1)
  451. try! m.get(indices: indices, data: &tmp)
  452. return tmp[0]
  453. }
  454. public static func putAt(m: Mat, indices: [Int32], v: Float) {
  455. let tmp = [v]
  456. try! m.put(indices: indices, data: tmp)
  457. }
  458. public static func getAt2c(m: Mat, indices:[Int32]) -> (Float, Float) {
  459. var tmp = [Float](repeating: 0, count: 2)
  460. try! m.get(indices: indices, data: &tmp)
  461. return (tmp[0], tmp[1])
  462. }
  463. public static func putAt2c(m: Mat, indices: [Int32], v: (Float, Float)) {
  464. let tmp = [v.0, v.1]
  465. try! m.put(indices: indices, data: tmp)
  466. }
  467. public static func getAt3c(m: Mat, indices:[Int32]) -> (Float, Float, Float) {
  468. var tmp = [Float](repeating: 0, count: 3)
  469. try! m.get(indices: indices, data: &tmp)
  470. return (tmp[0], tmp[1], tmp[2])
  471. }
  472. public static func putAt3c(m: Mat, indices: [Int32], v: (Float, Float, Float)) {
  473. let tmp = [v.0, v.1, v.2]
  474. try! m.put(indices: indices, data: tmp)
  475. }
  476. public static func getAt4c(m: Mat, indices:[Int32]) -> (Float, Float, Float, Float) {
  477. var tmp = [Float](repeating: 0, count: 4)
  478. try! m.get(indices: indices, data: &tmp)
  479. return (tmp[0], tmp[1], tmp[2], tmp[3])
  480. }
  481. public static func putAt4c(m: Mat, indices: [Int32], v: (Float, Float, Float, Float)) {
  482. let tmp = [v.0, v.1, v.2, v.3]
  483. try! m.put(indices: indices, data: tmp)
  484. }
  485. }
  486. extension Int32: Atable {
  487. public static func getAt(m: Mat, indices:[Int32]) -> Int32 {
  488. var tmp = [Int32](repeating: 0, count: 1)
  489. try! m.get(indices: indices, data: &tmp)
  490. return tmp[0]
  491. }
  492. public static func putAt(m: Mat, indices: [Int32], v: Int32) {
  493. let tmp = [v]
  494. try! m.put(indices: indices, data: tmp)
  495. }
  496. public static func getAt2c(m: Mat, indices:[Int32]) -> (Int32, Int32) {
  497. var tmp = [Int32](repeating: 0, count: 2)
  498. try! m.get(indices: indices, data: &tmp)
  499. return (tmp[0], tmp[1])
  500. }
  501. public static func putAt2c(m: Mat, indices: [Int32], v: (Int32, Int32)) {
  502. let tmp = [v.0, v.1]
  503. try! m.put(indices: indices, data: tmp)
  504. }
  505. public static func getAt3c(m: Mat, indices:[Int32]) -> (Int32, Int32, Int32) {
  506. var tmp = [Int32](repeating: 0, count: 3)
  507. try! m.get(indices: indices, data: &tmp)
  508. return (tmp[0], tmp[1], tmp[2])
  509. }
  510. public static func putAt3c(m: Mat, indices: [Int32], v: (Int32, Int32, Int32)) {
  511. let tmp = [v.0, v.1, v.2]
  512. try! m.put(indices: indices, data: tmp)
  513. }
  514. public static func getAt4c(m: Mat, indices:[Int32]) -> (Int32, Int32, Int32, Int32) {
  515. var tmp = [Int32](repeating: 0, count: 4)
  516. try! m.get(indices: indices, data: &tmp)
  517. return (tmp[0], tmp[1], tmp[2], tmp[3])
  518. }
  519. public static func putAt4c(m: Mat, indices: [Int32], v: (Int32, Int32, Int32, Int32)) {
  520. let tmp = [v.0, v.1, v.2, v.3]
  521. try! m.put(indices: indices, data: tmp)
  522. }
  523. }
  524. extension UInt16: Atable {
  525. public static func getAt(m: Mat, indices:[Int32]) -> UInt16 {
  526. var tmp = [UInt16](repeating: 0, count: 1)
  527. try! m.get(indices: indices, data: &tmp)
  528. return tmp[0]
  529. }
  530. public static func putAt(m: Mat, indices: [Int32], v: UInt16) {
  531. let tmp = [v]
  532. try! m.put(indices: indices, data: tmp)
  533. }
  534. public static func getAt2c(m: Mat, indices:[Int32]) -> (UInt16, UInt16) {
  535. var tmp = [UInt16](repeating: 0, count: 2)
  536. try! m.get(indices: indices, data: &tmp)
  537. return (tmp[0], tmp[1])
  538. }
  539. public static func putAt2c(m: Mat, indices: [Int32], v: (UInt16, UInt16)) {
  540. let tmp = [v.0, v.1]
  541. try! m.put(indices: indices, data: tmp)
  542. }
  543. public static func getAt3c(m: Mat, indices:[Int32]) -> (UInt16, UInt16, UInt16) {
  544. var tmp = [UInt16](repeating: 0, count: 3)
  545. try! m.get(indices: indices, data: &tmp)
  546. return (tmp[0], tmp[1], tmp[2])
  547. }
  548. public static func putAt3c(m: Mat, indices: [Int32], v: (UInt16, UInt16, UInt16)) {
  549. let tmp = [v.0, v.1, v.2]
  550. try! m.put(indices: indices, data: tmp)
  551. }
  552. public static func getAt4c(m: Mat, indices:[Int32]) -> (UInt16, UInt16, UInt16, UInt16) {
  553. var tmp = [UInt16](repeating: 0, count: 4)
  554. try! m.get(indices: indices, data: &tmp)
  555. return (tmp[0], tmp[1], tmp[2], tmp[3])
  556. }
  557. public static func putAt4c(m: Mat, indices: [Int32], v: (UInt16, UInt16, UInt16, UInt16)) {
  558. let tmp = [v.0, v.1, v.2, v.3]
  559. try! m.put(indices: indices, data: tmp)
  560. }
  561. }
  562. extension Int16: Atable {
  563. public static func getAt(m: Mat, indices:[Int32]) -> Int16 {
  564. var tmp = [Int16](repeating: 0, count: 1)
  565. try! m.get(indices: indices, data: &tmp)
  566. return tmp[0]
  567. }
  568. public static func putAt(m: Mat, indices: [Int32], v: Int16) {
  569. let tmp = [v]
  570. try! m.put(indices: indices, data: tmp)
  571. }
  572. public static func getAt2c(m: Mat, indices:[Int32]) -> (Int16, Int16) {
  573. var tmp = [Int16](repeating: 0, count: 2)
  574. try! m.get(indices: indices, data: &tmp)
  575. return (tmp[0], tmp[1])
  576. }
  577. public static func putAt2c(m: Mat, indices: [Int32], v: (Int16, Int16)) {
  578. let tmp = [v.0, v.1]
  579. try! m.put(indices: indices, data: tmp)
  580. }
  581. public static func getAt3c(m: Mat, indices:[Int32]) -> (Int16, Int16, Int16) {
  582. var tmp = [Int16](repeating: 0, count: 3)
  583. try! m.get(indices: indices, data: &tmp)
  584. return (tmp[0], tmp[1], tmp[2])
  585. }
  586. public static func putAt3c(m: Mat, indices: [Int32], v: (Int16, Int16, Int16)) {
  587. let tmp = [v.0, v.1, v.2]
  588. try! m.put(indices: indices, data: tmp)
  589. }
  590. public static func getAt4c(m: Mat, indices:[Int32]) -> (Int16, Int16, Int16, Int16) {
  591. var tmp = [Int16](repeating: 0, count: 4)
  592. try! m.get(indices: indices, data: &tmp)
  593. return (tmp[0], tmp[1], tmp[2], tmp[3])
  594. }
  595. public static func putAt4c(m: Mat, indices: [Int32], v: (Int16, Int16, Int16, Int16)) {
  596. let tmp = [v.0, v.1, v.2, v.3]
  597. try! m.put(indices: indices, data: tmp)
  598. }
  599. }
  600. /***
  601. * Example use:
  602. *
  603. * let elemantVal: UInt8 = mat.at(row: 50, col: 50).v
  604. * mat.at(row: 50, col: 50).v = 245
  605. *
  606. */
  607. public extension Mat {
  608. func at<N: Atable>(row: Int32, col: Int32) -> MatAt<N> {
  609. return MatAt(mat: self, indices: [row, col])
  610. }
  611. func at<N: Atable>(indices:[Int32]) -> MatAt<N> {
  612. return MatAt(mat: self, indices: indices)
  613. }
  614. }
  615. public extension Mat {
  616. static func *(lhs:Mat, rhs: Mat) -> Mat {
  617. return lhs.matMul(rhs)
  618. }
  619. }