001package org.opencv.core; 002 003import java.util.Arrays; 004import java.util.List; 005 006public class MatOfByte extends Mat { 007 // 8UC(x) 008 private static final int _depth = CvType.CV_8U; 009 private static final int _channels = 1; 010 011 public MatOfByte() { 012 super(); 013 } 014 015 protected MatOfByte(long addr) { 016 super(addr); 017 if( !empty() && checkVector(_channels, _depth) < 0 ) 018 throw new IllegalArgumentException("Incompatible Mat"); 019 //FIXME: do we need release() here? 020 } 021 022 public static MatOfByte fromNativeAddr(long addr) { 023 return new MatOfByte(addr); 024 } 025 026 public MatOfByte(Mat m) { 027 super(m, Range.all()); 028 if( !empty() && checkVector(_channels, _depth) < 0 ) 029 throw new IllegalArgumentException("Incompatible Mat"); 030 //FIXME: do we need release() here? 031 } 032 033 public MatOfByte(byte...a) { 034 super(); 035 fromArray(a); 036 } 037 038 public MatOfByte(int offset, int length, byte...a) { 039 super(); 040 fromArray(offset, length, a); 041 } 042 043 public void alloc(int elemNumber) { 044 if(elemNumber>0) 045 super.create(elemNumber, 1, CvType.makeType(_depth, _channels)); 046 } 047 048 public void fromArray(byte...a) { 049 if(a==null || a.length==0) 050 return; 051 int num = a.length / _channels; 052 alloc(num); 053 put(0, 0, a); //TODO: check ret val! 054 } 055 056 public void fromArray(int offset, int length, byte...a) { 057 if (offset < 0) 058 throw new IllegalArgumentException("offset < 0"); 059 if (a == null) 060 throw new NullPointerException(); 061 if (length < 0 || length + offset > a.length) 062 throw new IllegalArgumentException("invalid 'length' parameter: " + Integer.toString(length)); 063 if (a.length == 0) 064 return; 065 int num = length / _channels; 066 alloc(num); 067 put(0, 0, a, offset, length); //TODO: check ret val! 068 } 069 070 public byte[] toArray() { 071 int num = checkVector(_channels, _depth); 072 if(num < 0) 073 throw new RuntimeException("Native Mat has unexpected type or size: " + toString()); 074 byte[] a = new byte[num * _channels]; 075 if(num == 0) 076 return a; 077 get(0, 0, a); //TODO: check ret val! 078 return a; 079 } 080 081 public void fromList(List<Byte> lb) { 082 if(lb==null || lb.size()==0) 083 return; 084 Byte ab[] = lb.toArray(new Byte[0]); 085 byte a[] = new byte[ab.length]; 086 for(int i=0; i<ab.length; i++) 087 a[i] = ab[i]; 088 fromArray(a); 089 } 090 091 public List<Byte> toList() { 092 byte[] a = toArray(); 093 Byte ab[] = new Byte[a.length]; 094 for(int i=0; i<a.length; i++) 095 ab[i] = a[i]; 096 return Arrays.asList(ab); 097 } 098}