Merge pull request #10489 from SarenT:offset-mat_put
Adding capability to parse subsections of a byte array in Java bindings (#10489) * Adding capability to parse subsections of a byte array in Java bindings. (Because Java lacks pointers. Therefore, reading images within a subsection of a byte array is impossible by Java's nature and limitations. Because of this, many IO functions in Java require additional parameters offset and length to define, which section of an array to be read.) * Corrected according to the review. Previous interfaces were restored, instead internal interfaces were modified to provide subsampling of java byte arrays. * Adding tests and test related files. * Adding missing files for the test. * Simplified the test * Check was corrected according to discussion. An OutOfRangeException will be thrown instead of returning. * java: update MatOfByte implementation checks / tests
This commit is contained in:
committed by
Alexander Alekhin
parent
9deaddcdff
commit
c6d9ce8fd3
@@ -1015,6 +1015,21 @@ public class Mat {
|
||||
throw new java.lang.UnsupportedOperationException("Mat data type is not compatible: " + t);
|
||||
}
|
||||
|
||||
// javadoc:Mat::put(row,col,data,offset,length)
|
||||
public int put(int row, int col, byte[] data, int offset, int length) {
|
||||
int t = type();
|
||||
if (data == null || length % CvType.channels(t) != 0)
|
||||
throw new java.lang.UnsupportedOperationException(
|
||||
"Provided data element number (" +
|
||||
(data == null ? 0 : data.length) +
|
||||
") should be multiple of the Mat channels count (" +
|
||||
CvType.channels(t) + ")");
|
||||
if (CvType.depth(t) == CvType.CV_8U || CvType.depth(t) == CvType.CV_8S) {
|
||||
return nPutBwOffset(nativeObj, row, col, length, offset, data);
|
||||
}
|
||||
throw new java.lang.UnsupportedOperationException("Mat data type is not compatible: " + t);
|
||||
}
|
||||
|
||||
// javadoc:Mat::get(row,col,data)
|
||||
public int get(int row, int col, byte[] data) {
|
||||
int t = type();
|
||||
@@ -1318,6 +1333,8 @@ public class Mat {
|
||||
|
||||
private static native int nPutB(long self, int row, int col, int count, byte[] data);
|
||||
|
||||
private static native int nPutBwOffset(long self, int row, int col, int count, int offset, byte[] data);
|
||||
|
||||
private static native int nGetB(long self, int row, int col, int count, byte[] vals);
|
||||
|
||||
private static native int nGetS(long self, int row, int col, int count, short[] vals);
|
||||
|
||||
@@ -35,6 +35,11 @@ public class MatOfByte extends Mat {
|
||||
fromArray(a);
|
||||
}
|
||||
|
||||
public MatOfByte(int offset, int length, byte...a) {
|
||||
super();
|
||||
fromArray(offset, length, a);
|
||||
}
|
||||
|
||||
public void alloc(int elemNumber) {
|
||||
if(elemNumber>0)
|
||||
super.create(elemNumber, 1, CvType.makeType(_depth, _channels));
|
||||
@@ -48,6 +53,20 @@ public class MatOfByte extends Mat {
|
||||
put(0, 0, a); //TODO: check ret val!
|
||||
}
|
||||
|
||||
public void fromArray(int offset, int length, byte...a) {
|
||||
if (offset < 0)
|
||||
throw new IllegalArgumentException("offset < 0");
|
||||
if (a == null)
|
||||
throw new NullPointerException();
|
||||
if (length < 0 || length + offset > a.length)
|
||||
throw new IllegalArgumentException("invalid 'length' parameter: " + Integer.toString(length));
|
||||
if (a.length == 0)
|
||||
return;
|
||||
int num = length / _channels;
|
||||
alloc(num);
|
||||
put(0, 0, a, offset, length); //TODO: check ret val!
|
||||
}
|
||||
|
||||
public byte[] toArray() {
|
||||
int num = checkVector(_channels, _depth);
|
||||
if(num < 0)
|
||||
|
||||
Reference in New Issue
Block a user