Open vadixidav opened 5 years ago
IIRC from_buffer
copies its data but I may be wrong here.
Checking array length cannot be done before Mat is constructed because different CvType require different buffer size. Only opencv
itself could tell us if buffer if big enough so I think it's better to just handle OutOfBoundException in C++ or something, and return Result istead of mat.
Oh, I see, so right now it is just undefined behavior if your buffer is the wrong size. I will create the appropriate unit tests then and change this to get back a Result.
There are some hepers class (CResult, in instance) that helps you to convert C++ Exception into rust Result. See https://github.com/nebgnahz/cv-rs/blob/b60210381ef21198cf842bfeab398b077085f0f9/native/common.h#L68-L87
In the documentation I found
from_buffer
. This method indicates that it is possible to actually invoke a double-free without doing anything. Additionally, the source offrom_buffer
doesn't check to see if the slice is big enough! This means that undefined behavior and segfaults could happen.To fix this, we should create a
SliceMat
. This would be a cv::Mat that does not own its data. It would be templated with a lifetime param <'a> so that when it is created from a slice it wont be allowed to outlive the slice due to the Rust borrow checker. We then also need to make a newtype wrapper around just thecv::Mat
pointer that opencv has and move all the shared functionality we have into there. We can call this wrapperMatRef
. We thenimpl Deref<MatRef> for Mat
andimpl Deref<MatRef> for SliceMat
.