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 / testspull/10582/head
parent
9deaddcdff
commit
c6d9ce8fd3
4 changed files with 124 additions and 9 deletions
@ -0,0 +1,70 @@ |
|||||||
|
package org.opencv.test.core; |
||||||
|
|
||||||
|
import java.util.Arrays; |
||||||
|
|
||||||
|
import org.opencv.core.Core; |
||||||
|
import org.opencv.core.CvException; |
||||||
|
import org.opencv.core.CvType; |
||||||
|
import org.opencv.core.Mat; |
||||||
|
import org.opencv.core.MatOfByte; |
||||||
|
import org.opencv.core.MatOfDouble; |
||||||
|
import org.opencv.test.OpenCVTestCase; |
||||||
|
import org.opencv.imgcodecs.Imgcodecs; |
||||||
|
|
||||||
|
public class MatOfByteTest extends OpenCVTestCase { |
||||||
|
|
||||||
|
public void testMatOfSubByteArray() { |
||||||
|
byte[] inputBytes = { 1,2,3,4,5 }; |
||||||
|
|
||||||
|
MatOfByte m0 = new MatOfByte(inputBytes); |
||||||
|
MatOfByte m1 = new MatOfByte(0, inputBytes.length, inputBytes); |
||||||
|
MatOfByte m2 = new MatOfByte(1, inputBytes.length - 2, inputBytes); |
||||||
|
|
||||||
|
assertEquals(5.0, m0.size().height); |
||||||
|
assertEquals(1.0, m0.size().width); |
||||||
|
|
||||||
|
assertEquals(m0.get(0, 0)[0], m1.get(0, 0)[0]); |
||||||
|
assertEquals(m0.get((int) m0.size().height - 1, 0)[0], m1.get((int) m1.size().height - 1, 0)[0]); |
||||||
|
|
||||||
|
assertEquals(3.0, m2.size().height); |
||||||
|
assertEquals(1.0, m2.size().width); |
||||||
|
|
||||||
|
assertEquals(2.0, m2.get(0, 0)[0]); |
||||||
|
assertEquals(3.0, m2.get(1, 0)[0]); |
||||||
|
assertEquals(4.0, m2.get(2, 0)[0]); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public void testMatOfSubByteArray_BadArg() { |
||||||
|
byte[] inputBytes = { 1,2,3,4,5 }; |
||||||
|
|
||||||
|
try { |
||||||
|
MatOfByte m1 = new MatOfByte(-1, inputBytes.length, inputBytes); |
||||||
|
fail("Missing check: offset < 0"); |
||||||
|
} catch (IllegalArgumentException e) { |
||||||
|
// pass
|
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
MatOfByte m1 = new MatOfByte(0, inputBytes.length, null); |
||||||
|
fail("Missing check: NullPointerException"); |
||||||
|
} catch (NullPointerException e) { |
||||||
|
// pass
|
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
MatOfByte m1 = new MatOfByte(0, -1, inputBytes); |
||||||
|
fail("Missing check: length < 0"); |
||||||
|
} catch (IllegalArgumentException e) { |
||||||
|
// pass
|
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
MatOfByte m1 = new MatOfByte(1, inputBytes.length, inputBytes); |
||||||
|
fail("Missing check: buffer bounds"); |
||||||
|
} catch (IllegalArgumentException e) { |
||||||
|
// pass
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue