return cols();
}
+ // javadoc:Mat::at(clazz, row, col)
+ @SuppressWarnings("unchecked")
+ public <T> Atable<T> at(Class<T> clazz, int row, int col) {
+ if (clazz == Byte.class || clazz == byte.class) {
+ return (Atable<T>)new AtableByte(this, row, col);
+ } else if (clazz == Double.class || clazz == double.class) {
+ return (Atable<T>)new AtableDouble(this, row, col);
+ } else if (clazz == Float.class || clazz == float.class) {
+ return (Atable<T>)new AtableFloat(this, row, col);
+ } else if (clazz == Integer.class || clazz == int.class) {
+ return (Atable<T>)new AtableInteger(this, row, col);
+ } else if (clazz == Short.class || clazz == short.class) {
+ return (Atable<T>)new AtableShort(this, row, col);
+ } else {
+ throw new RuntimeException("Unsupported class type");
+ }
+ }
+
+ // javadoc:Mat::at(clazz, idx)
+ @SuppressWarnings("unchecked")
+ public <T> Atable<T> at(Class<T> clazz, int[] idx) {
+ if (clazz == Byte.class || clazz == byte.class) {
+ return (Atable<T>)new AtableByte(this, idx);
+ } else if (clazz == Double.class || clazz == double.class) {
+ return (Atable<T>)new AtableDouble(this, idx);
+ } else if (clazz == Float.class || clazz == float.class) {
+ return (Atable<T>)new AtableFloat(this, idx);
+ } else if (clazz == Integer.class || clazz == int.class) {
+ return (Atable<T>)new AtableInteger(this, idx);
+ } else if (clazz == Short.class || clazz == short.class) {
+ return (Atable<T>)new AtableShort(this, idx);
+ } else {
+ throw new RuntimeException("Unsupported class parameter");
+ }
+ }
+
+ public static class Tuple2<T> {
+ public Tuple2(T _0, T _1) {
+ this._0 = _0;
+ this._1 = _1;
+ }
+
+ public T get_0() {
+ return _0;
+ }
+
+ public T get_1() {
+ return _1;
+ }
+
+ private final T _0;
+ private final T _1;
+ }
+
+ public static class Tuple3<T> {
+ public Tuple3(T _0, T _1, T _2) {
+ this._0 = _0;
+ this._1 = _1;
+ this._2 = _2;
+ }
+
+ public T get_0() {
+ return _0;
+ }
+
+ public T get_1() {
+ return _1;
+ }
+
+ public T get_2() {
+ return _2;
+ }
+
+ private final T _0;
+ private final T _1;
+ private final T _2;
+ }
+
+ public static class Tuple4<T> {
+ public Tuple4(T _0, T _1, T _2, T _3) {
+ this._0 = _0;
+ this._1 = _1;
+ this._2 = _2;
+ this._3 = _3;
+ }
+
+ public T get_0() {
+ return _0;
+ }
+
+ public T get_1() {
+ return _1;
+ }
+
+ public T get_2() {
+ return _2;
+ }
+
+ public T get_3() {
+ return _3;
+ }
+
+ private final T _0;
+ private final T _1;
+ private final T _2;
+ private final T _3;
+ }
+
+ public interface Atable<T> {
+ T getV();
+ void setV(T v);
+ Tuple2<T> getV2c();
+ void setV2c(Tuple2<T> v);
+ Tuple3<T> getV3c();
+ void setV3c(Tuple3<T> v);
+ Tuple4<T> getV4c();
+ void setV4c(Tuple4<T> v);
+ }
+
+ private static class AtableBase {
+
+ protected AtableBase(Mat mat, int row, int col) {
+ this.mat = mat;
+ indices = new int[2];
+ indices[0] = row;
+ indices[1] = col;
+ }
+
+ protected AtableBase(Mat mat, int[] indices) {
+ this.mat = mat;
+ this.indices = indices;
+ }
+
+ protected final Mat mat;
+ protected final int[] indices;
+ }
+
+ private static class AtableByte extends AtableBase implements Atable<Byte> {
+
+ public AtableByte(Mat mat, int row, int col) {
+ super(mat, row, col);
+ }
+
+ public AtableByte(Mat mat, int[] indices) {
+ super(mat, indices);
+ }
+
+ @Override
+ public Byte getV() {
+ byte[] data = new byte[1];
+ mat.get(indices, data);
+ return data[0];
+ }
+
+ @Override
+ public void setV(Byte v) {
+ byte[] data = new byte[] { v };
+ mat.put(indices, data);
+ }
+
+ @Override
+ public Tuple2<Byte> getV2c() {
+ byte[] data = new byte[2];
+ mat.get(indices, data);
+ return new Tuple2<Byte>(data[0], data[1]);
+ }
+
+ @Override
+ public void setV2c(Tuple2<Byte> v) {
+ byte[] data = new byte[] { v._0, v._1 };
+ mat.put(indices, data);
+ }
+
+ @Override
+ public Tuple3<Byte> getV3c() {
+ byte[] data = new byte[3];
+ mat.get(indices, data);
+ return new Tuple3<Byte>(data[0], data[1], data[2]);
+ }
+
+ @Override
+ public void setV3c(Tuple3<Byte> v) {
+ byte[] data = new byte[] { v._0, v._1, v._2 };
+ mat.put(indices, data);
+ }
+
+ @Override
+ public Tuple4<Byte> getV4c() {
+ byte[] data = new byte[4];
+ mat.get(indices, data);
+ return new Tuple4<Byte>(data[0], data[1], data[2], data[3]);
+ }
+
+ @Override
+ public void setV4c(Tuple4<Byte> v) {
+ byte[] data = new byte[] { v._0, v._1, v._2, v._3 };
+ mat.put(indices, data);
+ }
+ }
+
+ private static class AtableDouble extends AtableBase implements Atable<Double> {
+
+ public AtableDouble(Mat mat, int row, int col) {
+ super(mat, row, col);
+ }
+
+ public AtableDouble(Mat mat, int[] indices) {
+ super(mat, indices);
+ }
+
+ @Override
+ public Double getV() {
+ double[] data = new double[1];
+ mat.get(indices, data);
+ return data[0];
+ }
+
+ @Override
+ public void setV(Double v) {
+ double[] data = new double[] { v };
+ mat.put(indices, data);
+ }
+
+ @Override
+ public Tuple2<Double> getV2c() {
+ double[] data = new double[2];
+ mat.get(indices, data);
+ return new Tuple2<Double>(data[0], data[1]);
+ }
+
+ @Override
+ public void setV2c(Tuple2<Double> v) {
+ double[] data = new double[] { v._0, v._1 };
+ mat.put(indices, data);
+ }
+
+ @Override
+ public Tuple3<Double> getV3c() {
+ double[] data = new double[3];
+ mat.get(indices, data);
+ return new Tuple3<Double>(data[0], data[1], data[2]);
+ }
+
+ @Override
+ public void setV3c(Tuple3<Double> v) {
+ double[] data = new double[] { v._0, v._1, v._2 };
+ mat.put(indices, data);
+ }
+
+ @Override
+ public Tuple4<Double> getV4c() {
+ double[] data = new double[4];
+ mat.get(indices, data);
+ return new Tuple4<Double>(data[0], data[1], data[2], data[3]);
+ }
+
+ @Override
+ public void setV4c(Tuple4<Double> v) {
+ double[] data = new double[] { v._0, v._1, v._2, v._3 };
+ mat.put(indices, data);
+ }
+ }
+
+ private static class AtableFloat extends AtableBase implements Atable<Float> {
+
+ public AtableFloat(Mat mat, int row, int col) {
+ super(mat, row, col);
+ }
+
+ public AtableFloat(Mat mat, int[] indices) {
+ super(mat, indices);
+ }
+
+ @Override
+ public Float getV() {
+ float[] data = new float[1];
+ mat.get(indices, data);
+ return data[0];
+ }
+
+ @Override
+ public void setV(Float v) {
+ float[] data = new float[] { v };
+ mat.put(indices, data);
+ }
+
+ @Override
+ public Tuple2<Float> getV2c() {
+ float[] data = new float[2];
+ mat.get(indices, data);
+ return new Tuple2<Float>(data[0], data[1]);
+ }
+
+ @Override
+ public void setV2c(Tuple2<Float> v) {
+ float[] data = new float[] { v._0, v._1 };
+ mat.put(indices, data);
+ }
+
+ @Override
+ public Tuple3<Float> getV3c() {
+ float[] data = new float[3];
+ mat.get(indices, data);
+ return new Tuple3<Float>(data[0], data[1], data[2]);
+ }
+
+ @Override
+ public void setV3c(Tuple3<Float> v) {
+ float[] data = new float[] { v._0, v._1, v._2 };
+ mat.put(indices, data);
+ }
+
+ @Override
+ public Tuple4<Float> getV4c() {
+ float[] data = new float[4];
+ mat.get(indices, data);
+ return new Tuple4<Float>(data[0], data[1], data[2], data[3]);
+ }
+
+ @Override
+ public void setV4c(Tuple4<Float> v) {
+ double[] data = new double[] { v._0, v._1, v._2, v._3 };
+ mat.put(indices, data);
+ }
+ }
+
+ private static class AtableInteger extends AtableBase implements Atable<Integer> {
+
+ public AtableInteger(Mat mat, int row, int col) {
+ super(mat, row, col);
+ }
+
+ public AtableInteger(Mat mat, int[] indices) {
+ super(mat, indices);
+ }
+
+ @Override
+ public Integer getV() {
+ int[] data = new int[1];
+ mat.get(indices, data);
+ return data[0];
+ }
+
+ @Override
+ public void setV(Integer v) {
+ int[] data = new int[] { v };
+ mat.put(indices, data);
+ }
+
+ @Override
+ public Tuple2<Integer> getV2c() {
+ int[] data = new int[2];
+ mat.get(indices, data);
+ return new Tuple2<Integer>(data[0], data[1]);
+ }
+
+ @Override
+ public void setV2c(Tuple2<Integer> v) {
+ int[] data = new int[] { v._0, v._1 };
+ mat.put(indices, data);
+ }
+
+ @Override
+ public Tuple3<Integer> getV3c() {
+ int[] data = new int[3];
+ mat.get(indices, data);
+ return new Tuple3<Integer>(data[0], data[1], data[2]);
+ }
+
+ @Override
+ public void setV3c(Tuple3<Integer> v) {
+ int[] data = new int[] { v._0, v._1, v._2 };
+ mat.put(indices, data);
+ }
+
+ @Override
+ public Tuple4<Integer> getV4c() {
+ int[] data = new int[4];
+ mat.get(indices, data);
+ return new Tuple4<Integer>(data[0], data[1], data[2], data[3]);
+ }
+
+ @Override
+ public void setV4c(Tuple4<Integer> v) {
+ int[] data = new int[] { v._0, v._1, v._2, v._3 };
+ mat.put(indices, data);
+ }
+ }
+
+ private static class AtableShort extends AtableBase implements Atable<Short> {
+
+ public AtableShort(Mat mat, int row, int col) {
+ super(mat, row, col);
+ }
+
+ public AtableShort(Mat mat, int[] indices) {
+ super(mat, indices);
+ }
+
+ @Override
+ public Short getV() {
+ short[] data = new short[1];
+ mat.get(indices, data);
+ return data[0];
+ }
+
+ @Override
+ public void setV(Short v) {
+ short[] data = new short[] { v };
+ mat.put(indices, data);
+ }
+
+ @Override
+ public Tuple2<Short> getV2c() {
+ short[] data = new short[2];
+ mat.get(indices, data);
+ return new Tuple2<Short>(data[0], data[1]);
+ }
+
+ @Override
+ public void setV2c(Tuple2<Short> v) {
+ short[] data = new short[] { v._0, v._1 };
+ mat.put(indices, data);
+ }
+
+ @Override
+ public Tuple3<Short> getV3c() {
+ short[] data = new short[3];
+ mat.get(indices, data);
+ return new Tuple3<Short>(data[0], data[1], data[2]);
+ }
+
+ @Override
+ public void setV3c(Tuple3<Short> v) {
+ short[] data = new short[] { v._0, v._1, v._2 };
+ mat.put(indices, data);
+ }
+
+ @Override
+ public Tuple4<Short> getV4c() {
+ short[] data = new short[4];
+ mat.get(indices, data);
+ return new Tuple4<Short>(data[0], data[1], data[2], data[3]);
+ }
+
+ @Override
+ public void setV4c(Tuple4<Short> v) {
+ short[] data = new short[] { v._0, v._1, v._2, v._3 };
+ mat.put(indices, data);
+ }
+ }
+
// javadoc:Mat::getNativeObjAddr()
public long getNativeObjAddr() {
return nativeObj;
--- /dev/null
+package org.opencv.core
+
+import org.opencv.core.Mat.*
+import java.lang.RuntimeException
+
+/***
+ * Example use:
+ *
+ * val (b, g, r) = mat.at<UByte>(50, 50).v3c
+ * mat.at<UByte>(50, 50).val = T3(245u, 113u, 34u)
+ *
+ */
+@Suppress("UNCHECKED_CAST")
+inline fun <reified T> Mat.at(row: Int, col: Int) : Atable<T> =
+ when (T::class) {
+ Byte::class, Double::class, Float::class, Int::class, Short::class -> this.at(
+ T::class.java,
+ row,
+ col
+ )
+ UByte::class -> AtableUByte(this, row, col) as Atable<T>
+ else -> throw RuntimeException("Unsupported class type")
+ }
+
+@Suppress("UNCHECKED_CAST")
+inline fun <reified T> Mat.at(idx: IntArray) : Atable<T> =
+ when (T::class) {
+ Byte::class, Double::class, Float::class, Int::class, Short::class -> this.at(
+ T::class.java,
+ idx
+ )
+ UByte::class -> AtableUByte(this, idx) as Atable<T>
+ else -> throw RuntimeException("Unsupported class type")
+ }
+
+class AtableUByte(val mat: Mat, val indices: IntArray): Atable<UByte> {
+
+ constructor(mat: Mat, row: Int, col: Int) : this(mat, intArrayOf(row, col))
+
+ override fun getV(): UByte {
+ val data = ByteArray(1)
+ mat[indices, data]
+ return data[0].toUByte()
+ }
+
+ override fun setV(v: UByte) {
+ val data = byteArrayOf(v.toByte())
+ mat.put(indices, data)
+ }
+
+ override fun getV2c(): Tuple2<UByte> {
+ val data = ByteArray(2)
+ mat[indices, data]
+ return Tuple2(data[0].toUByte(), data[1].toUByte())
+ }
+
+ override fun setV2c(v: Tuple2<UByte>) {
+ val data = byteArrayOf(v._0.toByte(), v._1.toByte())
+ mat.put(indices, data)
+ }
+
+ override fun getV3c(): Tuple3<UByte> {
+ val data = ByteArray(3)
+ mat[indices, data]
+ return Tuple3(data[0].toUByte(), data[1].toUByte(), data[2].toUByte())
+ }
+
+ override fun setV3c(v: Tuple3<UByte>) {
+ val data = byteArrayOf(v._0.toByte(), v._1.toByte(), v._2.toByte())
+ mat.put(indices, data)
+ }
+
+ override fun getV4c(): Tuple4<UByte> {
+ val data = ByteArray(4)
+ mat[indices, data]
+ return Tuple4(data[0].toUByte(), data[1].toUByte(), data[2].toUByte(), data[3].toUByte())
+ }
+
+ override fun setV4c(v: Tuple4<UByte>) {
+ val data = byteArrayOf(v._0.toByte(), v._1.toByte(), v._2.toByte(), v._3.toByte())
+ mat.put(indices, data)
+ }
+}
+
+operator fun <T> Tuple2<T>.component1(): T = this._0
+operator fun <T> Tuple2<T>.component2(): T = this._1
+
+operator fun <T> Tuple3<T>.component1(): T = this._0
+operator fun <T> Tuple3<T>.component2(): T = this._1
+operator fun <T> Tuple3<T>.component3(): T = this._2
+
+operator fun <T> Tuple4<T>.component1(): T = this._0
+operator fun <T> Tuple4<T>.component2(): T = this._1
+operator fun <T> Tuple4<T>.component3(): T = this._2
+operator fun <T> Tuple4<T>.component4(): T = this._3
+
+fun <T> T2(_0: T, _1: T) : Tuple2<T> = Tuple2(_0, _1)
+fun <T> T3(_0: T, _1: T, _2: T) : Tuple3<T> = Tuple3(_0, _1, _2)
+fun <T> T4(_0: T, _1: T, _2: T, _3: T) : Tuple4<T> = Tuple4(_0, _1, _2, _3)