001package org.opencv.core;
002
003import java.util.Arrays;
004import java.util.List;
005
006
007public class MatOfRect extends Mat {
008    // 32SC4
009    private static final int _depth = CvType.CV_32S;
010    private static final int _channels = 4;
011
012    public MatOfRect() {
013        super();
014    }
015
016    protected MatOfRect(long addr) {
017        super(addr);
018        if( !empty() && checkVector(_channels, _depth) < 0 )
019            throw new IllegalArgumentException("Incompatible Mat");
020        //FIXME: do we need release() here?
021    }
022
023    public static MatOfRect fromNativeAddr(long addr) {
024        return new MatOfRect(addr);
025    }
026
027    public MatOfRect(Mat m) {
028        super(m, Range.all());
029        if( !empty() && checkVector(_channels, _depth) < 0 )
030            throw new IllegalArgumentException("Incompatible Mat");
031        //FIXME: do we need release() here?
032    }
033
034    public MatOfRect(Rect...a) {
035        super();
036        fromArray(a);
037    }
038
039    public void alloc(int elemNumber) {
040        if(elemNumber>0)
041            super.create(elemNumber, 1, CvType.makeType(_depth, _channels));
042    }
043
044    public void fromArray(Rect...a) {
045        if(a==null || a.length==0)
046            return;
047        int num = a.length;
048        alloc(num);
049        int buff[] = new int[num * _channels];
050        for(int i=0; i<num; i++) {
051            Rect r = a[i];
052            buff[_channels*i+0] = (int) r.x;
053            buff[_channels*i+1] = (int) r.y;
054            buff[_channels*i+2] = (int) r.width;
055            buff[_channels*i+3] = (int) r.height;
056        }
057        put(0, 0, buff); //TODO: check ret val!
058    }
059
060
061    public Rect[] toArray() {
062        int num = (int) total();
063        Rect[] a = new Rect[num];
064        if(num == 0)
065            return a;
066        int buff[] = new int[num * _channels];
067        get(0, 0, buff); //TODO: check ret val!
068        for(int i=0; i<num; i++)
069            a[i] = new Rect(buff[i*_channels], buff[i*_channels+1], buff[i*_channels+2], buff[i*_channels+3]);
070        return a;
071    }
072    public void fromList(List<Rect> lr) {
073        Rect ap[] = lr.toArray(new Rect[0]);
074        fromArray(ap);
075    }
076
077    public List<Rect> toList() {
078        Rect[] ar = toArray();
079        return Arrays.asList(ar);
080    }
081}