opencv/manual/core/mat/
mat_.rs1use std::convert::TryFrom;
2use std::ffi::c_void;
3use std::fmt;
4use std::marker::PhantomData;
5
6use super::{match_format, DataType, MatMatcher};
7use crate::boxed_ref::{BoxedRef, BoxedRefMut};
8use crate::core::{
9 Mat, MatTrait, MatTraitConst, MatTraitConstManual, MatTraitManual, Point, ToInputArray, ToInputOutputArray, ToOutputArray,
10 _InputArray, _InputOutputArray, _OutputArray,
11};
12use crate::traits::Boxed;
13use crate::{Error, Result};
14
15pub struct Mat_<T> {
21 inner: Mat,
22 _type: PhantomData<T>,
23}
24
25impl<T: DataType> TryFrom<Mat> for Mat_<T> {
26 type Error = Error;
27
28 #[inline]
29 fn try_from(mat: Mat) -> Result<Self, Self::Error> {
30 match_format::<T>(mat.typ()).map(|_| Self {
31 inner: mat,
32 _type: PhantomData,
33 })
34 }
35}
36
37impl<T: DataType> From<Mat_<T>> for Mat {
38 #[inline]
39 fn from(s: Mat_<T>) -> Self {
40 s.inner
41 }
42}
43
44impl<T: DataType> Mat_<T> {
45 #[inline]
46 pub fn into_untyped(self) -> Mat {
47 self.into()
48 }
49
50 #[inline]
51 pub fn as_untyped(&self) -> &Mat {
52 &self.inner
53 }
54
55 #[inline]
56 pub fn as_raw_Mat_(&self) -> *const c_void {
57 #![allow(non_snake_case)]
58 self.as_raw_Mat()
59 }
60
61 #[inline]
62 pub fn as_raw_mut_Mat_(&mut self) -> *mut c_void {
63 #![allow(non_snake_case)]
64 self.as_raw_mut_Mat()
65 }
66
67 #[inline]
69 pub fn at(&self, i0: i32) -> Result<&T> {
70 self
71 .match_total(i0)
72 .and_then(|_| self.match_max_dims(2))
73 .map(|_| unsafe { self.at_unchecked(i0) })
74 }
75
76 #[inline]
78 pub fn at_2d(&self, row: i32, col: i32) -> Result<&T> {
79 self
80 .match_indices(&[row, col])
81 .and_then(|_| unsafe { self.at_2d_unchecked(row, col) })
82 }
83
84 #[inline]
86 pub fn at_3d(&self, i0: i32, i1: i32, i2: i32) -> Result<&T> {
87 self
88 .match_indices(&[i0, i1, i2])
89 .and_then(|_| unsafe { self.at_3d_unchecked(i0, i1, i2) })
90 }
91
92 #[inline]
94 pub fn at_nd(&self, idx: &[i32]) -> Result<&T> {
95 self.match_indices(idx).and_then(|_| unsafe { self.at_nd_unchecked(idx) })
96 }
97
98 #[inline]
100 pub fn at_pt(&self, pt: Point) -> Result<&T> {
101 self.at_2d(pt.y, pt.x)
102 }
103
104 #[inline]
106 pub fn at_row(&self, row: i32) -> Result<&[T]> {
107 self
108 .match_indices(&[row, 0])
109 .and_then(|_| unsafe { self.at_row_unchecked(row) })
110 }
111
112 #[inline]
114 pub fn at_mut(&mut self, i0: i32) -> Result<&mut T> {
115 self.match_total(i0)?;
116 Ok(unsafe { self.at_unchecked_mut(i0) })
117 }
118
119 #[inline]
121 pub fn at_2d_mut(&mut self, row: i32, col: i32) -> Result<&mut T> {
122 self.match_indices(&[row, col])?;
123 unsafe { self.at_2d_unchecked_mut(row, col) }
124 }
125
126 #[inline]
128 pub fn at_3d_mut(&mut self, i0: i32, i1: i32, i2: i32) -> Result<&mut T> {
129 self.match_indices(&[i0, i1, i2])?;
130 unsafe { self.at_3d_unchecked_mut(i0, i1, i2) }
131 }
132
133 #[inline]
135 pub fn at_nd_mut(&mut self, idx: &[i32]) -> Result<&mut T> {
136 self.match_indices(idx)?;
137 unsafe { self.at_nd_unchecked_mut(idx) }
138 }
139
140 #[inline]
142 pub fn at_pt_mut(&mut self, pt: Point) -> Result<&mut T> {
143 self.at_2d_mut(pt.y, pt.x)
144 }
145
146 #[inline]
148 pub fn at_row_mut(&mut self, row: i32) -> Result<&mut [T]> {
149 self.match_indices(&[row, 0])?;
150 unsafe { self.at_row_unchecked_mut(row) }
151 }
152
153 #[inline]
155 pub fn data_typed(&self) -> Result<&[T]> {
156 self
157 .match_is_continuous()
158 .and_then(|_| unsafe { self.data_typed_unchecked() })
159 }
160
161 #[inline]
163 pub fn data_typed_mut(&mut self) -> Result<&mut [T]> {
164 self.match_is_continuous()?;
165 unsafe { self.data_typed_unchecked_mut() }
166 }
167}
168
169impl<T> MatTraitConst for Mat_<T> {
170 #[inline]
171 fn as_raw_Mat(&self) -> *const c_void {
172 self.inner.as_raw_Mat()
173 }
174}
175
176impl<T> MatTrait for Mat_<T> {
177 #[inline]
178 fn as_raw_mut_Mat(&mut self) -> *mut c_void {
179 self.inner.as_raw_mut_Mat()
180 }
181}
182
183impl<T> Boxed for Mat_<T> {
184 #[inline]
185 unsafe fn from_raw(ptr: *mut c_void) -> Self {
186 Self {
187 inner: unsafe { Mat::from_raw(ptr) },
188 _type: PhantomData,
189 }
190 }
191
192 #[inline]
193 fn into_raw(self) -> *mut c_void {
194 self.inner.into_raw()
195 }
196
197 #[inline]
198 fn as_raw(&self) -> *const c_void {
199 self.inner.as_raw()
200 }
201
202 #[inline]
203 fn as_raw_mut(&mut self) -> *mut c_void {
204 self.inner.as_raw_mut()
205 }
206}
207
208impl<T> ToInputArray for Mat_<T> {
209 #[inline]
210 fn input_array(&self) -> Result<BoxedRef<'_, _InputArray>> {
211 self.inner.input_array()
212 }
213}
214
215impl<T> ToOutputArray for Mat_<T> {
216 #[inline]
217 fn output_array(&mut self) -> Result<BoxedRefMut<'_, _OutputArray>> {
218 self.inner.output_array()
219 }
220}
221
222impl<T> ToInputOutputArray for Mat_<T> {
223 #[inline]
224 fn input_output_array(&mut self) -> Result<BoxedRefMut<'_, _InputOutputArray>> {
225 self.inner.input_output_array()
226 }
227}
228
229impl<T> fmt::Debug for Mat_<T> {
230 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
231 self.inner.fmt(f)
232 }
233}