OSDN Git Service

ライブビューデータを拾うために検討中。その4。
[gokigen/FujiCam.git] / opencv41 / src / main / java / org / opencv / core / MatOfByte.java
1 package org.opencv.core;
2
3 import java.util.Arrays;
4 import java.util.List;
5
6 public class MatOfByte extends Mat {
7     // 8UC(x)
8     private static final int _depth = CvType.CV_8U;
9     private static final int _channels = 1;
10
11     public MatOfByte() {
12         super();
13     }
14
15     protected MatOfByte(long addr) {
16         super(addr);
17         if( !empty() && checkVector(_channels, _depth) < 0 )
18             throw new IllegalArgumentException("Incompatible Mat");
19         //FIXME: do we need release() here?
20     }
21
22     public static MatOfByte fromNativeAddr(long addr) {
23         return new MatOfByte(addr);
24     }
25
26     public MatOfByte(Mat m) {
27         super(m, Range.all());
28         if( !empty() && checkVector(_channels, _depth) < 0 )
29             throw new IllegalArgumentException("Incompatible Mat");
30         //FIXME: do we need release() here?
31     }
32
33     public MatOfByte(byte...a) {
34         super();
35         fromArray(a);
36     }
37
38     public MatOfByte(int offset, int length, byte...a) {
39         super();
40         fromArray(offset, length, a);
41     }
42
43     public void alloc(int elemNumber) {
44         if(elemNumber>0)
45             super.create(elemNumber, 1, CvType.makeType(_depth, _channels));
46     }
47
48     public void fromArray(byte...a) {
49         if(a==null || a.length==0)
50             return;
51         int num = a.length / _channels;
52         alloc(num);
53         put(0, 0, a); //TODO: check ret val!
54     }
55
56     public void fromArray(int offset, int length, byte...a) {
57         if (offset < 0)
58             throw new IllegalArgumentException("offset < 0");
59         if (a == null)
60             throw new NullPointerException();
61         if (length < 0 || length + offset > a.length)
62             throw new IllegalArgumentException("invalid 'length' parameter: " + Integer.toString(length));
63         if (a.length == 0)
64             return;
65         int num = length / _channels;
66         alloc(num);
67         put(0, 0, a, offset, length); //TODO: check ret val!
68     }
69
70     public byte[] toArray() {
71         int num = checkVector(_channels, _depth);
72         if(num < 0)
73             throw new RuntimeException("Native Mat has unexpected type or size: " + toString());
74         byte[] a = new byte[num * _channels];
75         if(num == 0)
76             return a;
77         get(0, 0, a); //TODO: check ret val!
78         return a;
79     }
80
81     public void fromList(List<Byte> lb) {
82         if(lb==null || lb.size()==0)
83             return;
84         Byte ab[] = lb.toArray(new Byte[0]);
85         byte a[] = new byte[ab.length];
86         for(int i=0; i<ab.length; i++)
87             a[i] = ab[i];
88         fromArray(a);
89     }
90
91     public List<Byte> toList() {
92         byte[] a = toArray();
93         Byte ab[] = new Byte[a.length];
94         for(int i=0; i<a.length; i++)
95             ab[i] = a[i];
96         return Arrays.asList(ab);
97     }
98 }