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 void alloc(int elemNumber) { 039 if(elemNumber>0) 040 super.create(elemNumber, 1, CvType.makeType(_depth, _channels)); 041 } 042 043 public void fromArray(byte...a) { 044 if(a==null || a.length==0) 045 return; 046 int num = a.length / _channels; 047 alloc(num); 048 put(0, 0, a); //TODO: check ret val! 049 } 050 051 public byte[] toArray() { 052 int num = checkVector(_channels, _depth); 053 if(num < 0) 054 throw new RuntimeException("Native Mat has unexpected type or size: " + toString()); 055 byte[] a = new byte[num * _channels]; 056 if(num == 0) 057 return a; 058 get(0, 0, a); //TODO: check ret val! 059 return a; 060 } 061 062 public void fromList(List<Byte> lb) { 063 if(lb==null || lb.size()==0) 064 return; 065 Byte ab[] = lb.toArray(new Byte[0]); 066 byte a[] = new byte[ab.length]; 067 for(int i=0; i<ab.length; i++) 068 a[i] = ab[i]; 069 fromArray(a); 070 } 071 072 public List<Byte> toList() { 073 byte[] a = toArray(); 074 Byte ab[] = new Byte[a.length]; 075 for(int i=0; i<a.length; i++) 076 ab[i] = a[i]; 077 return Arrays.asList(ab); 078 } 079}