1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
use std::{
	convert::TryInto,
	ffi::c_void,
	fmt,
	ops::Deref,
	slice,
};

pub use mat_::*;

use crate::{
	core::{
		self,
		_InputArray,
		_InputOutputArray,
		_OutputArray,
		MatExpr,
		MatSize,
		MatStep,
		Point,
		Scalar,
		ToInputArray,
		ToInputOutputArray,
		ToOutputArray,
		UMat,
	},
	Error,
	platform_types::size_t,
	prelude::*,
	Result,
	sys,
};

mod mat_;

/// This sealed trait is implemented for types that are valid to use as Mat elements
pub trait DataType: Copy + private::Sealed {
	fn depth() -> i32;
	fn channels() -> i32;
	fn typ() -> i32;
}

macro_rules! data_type {
	($rust_type: ty, $mat_depth: expr, $channels: expr) => {
		impl $crate::core::DataType for $rust_type {
			#[inline(always)]
			fn depth() -> i32 { $mat_depth }

			#[inline(always)]
			fn channels() -> i32 { $channels }

			#[inline(always)]
			fn typ() -> i32 { $crate::core::CV_MAKETYPE($mat_depth, $channels) }
		}

		impl private::Sealed for $rust_type {}
	};
}

// int
data_type!(u8, core::CV_8U, 1);
data_type!(i8, core::CV_8S, 1);
data_type!(u16, core::CV_16U, 1);
data_type!(i16, core::CV_16S, 1);
data_type!(i32, core::CV_32S, 1);

// float
data_type!(f32, core::CV_32F, 1);
data_type!(f64, core::CV_64F, 1);

// vec int
data_type!(core::Vec2b, core::CV_8U, 2);
data_type!(core::Vec3b, core::CV_8U, 3);
data_type!(core::Vec4b, core::CV_8U, 4);
data_type!(core::Vec2<i8>, core::CV_8S, 2);
data_type!(core::Vec3<i8>, core::CV_8S, 3);
data_type!(core::Vec4<i8>, core::CV_8S, 4);
data_type!(core::Vec2<u16>, core::CV_16U, 2);
data_type!(core::Vec3<u16>, core::CV_16U, 3);
data_type!(core::Vec4<u16>, core::CV_16U, 4);
data_type!(core::Vec2s, core::CV_16S, 2);
data_type!(core::Vec3s, core::CV_16S, 3);
data_type!(core::Vec4s, core::CV_16S, 4);
data_type!(core::Vec2i, core::CV_32S, 2);
data_type!(core::Vec3i, core::CV_32S, 3);
data_type!(core::Vec4i, core::CV_32S, 4);
data_type!(core::Vec8i, core::CV_32S, 8);

// vec float
data_type!(core::Vec2f, core::CV_32F, 2);
data_type!(core::Vec3f, core::CV_32F, 3);
data_type!(core::Vec4f, core::CV_32F, 4);
data_type!(core::Vec2d, core::CV_64F, 2);
data_type!(core::Vec3d, core::CV_64F, 3);
data_type!(core::Vec4d, core::CV_64F, 4);

// scalar
data_type!(core::Scalar, core::CV_64F, 4);

// point
data_type!(core::Point2i, core::CV_32S, 2);
data_type!(core::Point2f, core::CV_32F, 2);
data_type!(core::Point2d, core::CV_64F, 2);

// point3
data_type!(core::Point3i, core::CV_32S, 2);
data_type!(core::Point3f, core::CV_32F, 2);
data_type!(core::Point3d, core::CV_64F, 2);

// size
data_type!(core::Size2i, core::CV_32S, 2);
data_type!(core::Size2f, core::CV_32F, 2);
data_type!(core::Size2d, core::CV_64F, 2);

// rect
data_type!(core::Rect2i, core::CV_32S, 4);
data_type!(core::Rect2f, core::CV_32F, 4);
data_type!(core::Rect2d, core::CV_64F, 4);


#[inline(always)]
unsafe fn convert_ptr<T>(r: &u8) -> &T {
	&*(r as *const _ as *const T)
}

#[inline(always)]
unsafe fn convert_ptr_mut<T>(r: &mut u8) -> &mut T {
	&mut *(r as *mut _ as *mut T)
}

fn match_format<T: DataType>(mat_type: i32) -> Result<()> {
	let out_type = T::typ();
	if mat_type == out_type {
		Ok(())
	} else {
		#[cfg(not(feature = "opencv-32"))]
			let mat_type = core::type_to_string(mat_type)?;
		#[cfg(not(feature = "opencv-32"))]
			let out_type = core::type_to_string(out_type)?;
		Err(Error::new(core::StsUnmatchedFormats, format!("Mat type is: {}, but requested type is: {}", mat_type, out_type)))
	}
}

fn match_dims(mat: &(impl MatTrait + ?Sized), dims: usize) -> Result<()> {
	let mat_dims = mat.dims() as usize;
	if mat_dims == dims {
		Ok(())
	} else {
		Err(Error::new(core::StsUnmatchedSizes, format!("Mat dims is: {}, but requested dims is: {}", mat_dims, dims)))
	}
}

fn match_indices(mat: &(impl MatTrait + ?Sized), idx: &[i32]) -> Result<()> {
	let size = mat.mat_size();
	match_dims(mat, idx.len())?;
	let mut out_of_bounds = size.iter()
		.enumerate()
		.filter(|&(i, &x)| idx[i] < 0 || idx[i] >= x);
	if let Some((out_dim, out_size)) = out_of_bounds.next() {
		Err(Error::new(core::StsOutOfRange, format!("Index: {} along dimension: {} out of bounds 0..{}", idx[out_dim], out_dim, out_size)))
	} else {
		Ok(())
	}
}

fn match_total(mat: &(impl MatTrait + ?Sized), idx: i32) -> Result<()> {
	let size = mat.total()?;
	if 0 <= idx && (idx as usize) < size {
		Ok(())
	} else {
		Err(Error::new(core::StsOutOfRange, format!("Index: {} out of bounds: 0..{}", idx, size)))
	}
}

fn match_is_continuous(mat: &(impl MatTrait + ?Sized)) -> Result<()> {
	if mat.is_continuous()? {
		Ok(())
	} else {
		Err(Error::new(core::StsUnmatchedSizes, "Mat is not continuous, operation is not applicable".to_string()))
	}
}

#[inline(always)]
fn idx_to_row_col(mat: &(impl MatTrait + ?Sized), i0: i32) -> Result<(i32, i32)> {
	Ok(if mat.is_continuous()? {
		(0, i0)
	} else {
		let mat_size = mat.size()?;
		if mat_size.width == 1 {
			(0, i0)
		} else if mat_size.height == 1 {
			(i0, 0)
		} else {
			let i = i0 / mat_size.height;
			(i, i0 - i * mat_size.height)
		}
	})
}

impl Mat {
	/// Create new `Mat` from the iterator of known size
	pub fn from_exact_iter<T: DataType>(s: impl ExactSizeIterator<Item=T>) -> Result<Self> {
		let mut out = unsafe { Self::new_rows_cols(s.len() as _, 1, T::typ()) }?;
		for (i, x) in s.enumerate() {
			unsafe { ({ out.at_unchecked_mut::<T>(i as _) }? as *mut T).write(x) };
		}
		Ok(out)
	}

	#[inline]
	pub fn from_slice<T: DataType>(s: &[T]) -> Result<Self> {
		Self::from_slice_2d(&[s])
	}

	pub fn from_slice_2d<T: DataType>(s: &[impl AsRef<[T]>]) -> Result<Self> {
		let row_count: i32 = s.len() as _;
		let col_count: i32 = if row_count > 0 {
			s[0].as_ref().len() as _
		} else {
			0
		};
		let mut out = unsafe { Self::new_rows_cols(row_count, col_count, T::typ()) }?;
		for (row_n, row) in s.iter().enumerate() {
			let trg = out.at_row_mut(row_n as _)?;
			let src = row.as_ref();
			if trg.len() != src.len() {
				return Err(Error::new(core::StsUnmatchedSizes, format!("Unexpected number of items: {} in a row index: {}, expected: {}", trg.len(), row_n, src.len())));
			}
			trg.copy_from_slice(src);
		}
		Ok(out)
	}

	pub fn try_into_typed<T: DataType>(self) -> Result<Mat_<T>> where Self: Sized {
		self.try_into()
	}
}

pub(crate) mod mat_forward {
	use super::*;

	#[inline(always)]
	pub fn at<T: DataType>(mat: &(impl MatTrait + ?Sized), i0: i32) -> Result<&T> {
		match_format::<T>(mat.typ()?)
			.and_then(|_| match_dims(mat, 2))
			.and_then(|_| match_total(mat, i0))
			.and_then(|_| unsafe { mat.at_unchecked(i0) })
	}

	#[inline(always)]
	pub fn at_mut<T: DataType>(mat: &mut (impl MatTrait + ?Sized), i0: i32) -> Result<&mut T> {
		match_format::<T>(mat.typ()?)
			.and_then(|_| match_dims(mat, 2))
			.and_then(|_| match_total(mat, i0))?;
		unsafe { mat.at_unchecked_mut(i0) }
	}

	#[inline(always)]
	pub fn at_2d<T: DataType>(mat: &(impl MatTrait + ?Sized), row: i32, col: i32) -> Result<&T> {
		match_format::<T>(mat.typ()?)
			.and_then(|_| match_indices(mat, &[row, col]))
			.and_then(|_| unsafe { mat.at_2d_unchecked(row, col) })
	}

	#[inline(always)]
	pub fn at_2d_mut<T: DataType>(mat: &mut (impl MatTrait + ?Sized), row: i32, col: i32) -> Result<&mut T> {
		match_format::<T>(mat.typ()?)
			.and_then(|_| match_indices(mat, &[row, col]))?;
		unsafe { mat.at_2d_unchecked_mut(row, col) }
	}

	#[inline(always)]
	pub fn at_pt<T: DataType>(mat: &(impl MatTrait + ?Sized), pt: Point) -> Result<&T> {
		at_2d(mat, pt.y, pt.x)
	}

	#[inline(always)]
	pub fn at_pt_mut<T: DataType>(mat: &mut (impl MatTrait + ?Sized), pt: Point) -> Result<&mut T> {
		at_2d_mut(mat, pt.y, pt.x)
	}

	#[inline(always)]
	pub fn at_3d<T: DataType>(mat: &(impl MatTrait + ?Sized), i0: i32, i1: i32, i2: i32) -> Result<&T> {
		match_format::<T>(mat.typ()?)
			.and_then(|_| match_indices(mat, &[i0, i1, i2]))
			.and_then(|_| unsafe { mat.at_3d_unchecked(i0, i1, i2) })
	}

	#[inline(always)]
	pub fn at_3d_mut<T: DataType>(mat: &mut (impl MatTrait + ?Sized), i0: i32, i1: i32, i2: i32) -> Result<&mut T> {
		match_format::<T>(mat.typ()?)
			.and_then(|_| match_indices(mat, &[i0, i1, i2]))?;
		unsafe { mat.at_3d_unchecked_mut(i0, i1, i2) }
	}

	#[inline(always)]
	pub fn at_nd<'s, T: core::DataType>(mat: &'s (impl MatTrait + ?Sized), idx: &[i32]) -> Result<&'s T> {
		match_format::<T>(mat.typ()?)
			.and_then(|_| match_indices(mat, idx))
			.and_then(|_| unsafe { mat.at_nd_unchecked(idx) })
	}

	#[inline(always)]
	pub fn at_nd_mut<'s, T: core::DataType>(mat: &'s mut (impl MatTrait + ?Sized), idx: &[i32]) -> Result<&'s mut T> {
		match_format::<T>(mat.typ()?)
			.and_then(|_| match_indices(mat, idx))?;
		unsafe { mat.at_nd_unchecked_mut(idx) }
	}
}

pub trait MatTraitManual: MatTrait {
	/// Like `Mat::at()` but performs no bounds or type checks
	///
	/// # Safety
	/// Caller must ensure that index is within Mat bounds
	unsafe fn at_unchecked<T: DataType>(&self, i0: i32) -> Result<&T> {
		let mat_size = self.size()?;
		let (i, j) = if self.is_continuous()? || mat_size.width == 1 {
			(0, i0)
		} else if mat_size.height == 1 {
			(i0, 0)
		} else {
			let i = i0 / mat_size.height;
			(i, i0 - i * mat_size.height)
		};
		self.ptr_2d(i, j)
			.map(|ptr| convert_ptr(ptr))
	}

	/// Like `Mat::at_mut()` but performs no bounds or type checks
	/// # Safety
	/// Caller must ensure that index is within Mat bounds
	unsafe fn at_unchecked_mut<T: DataType>(&mut self, i0: i32) -> Result<&mut T> {
		let (i, j) = idx_to_row_col(self, i0)?;
		self.ptr_2d_mut(i, j)
			.map(|ptr| convert_ptr_mut(ptr))
	}

	/// Like `Mat::at_2d()` but performs no bounds or type checks
	/// # Safety
	/// Caller must ensure that indices are within Mat bounds
	unsafe fn at_2d_unchecked<T: DataType>(&self, row: i32, col: i32) -> Result<&T> {
		self.ptr_2d(row, col)
			.map(|ptr| convert_ptr(ptr))
	}

	/// Like `Mat::at_2d_mut()` but performs no bounds or type checks
	/// # Safety
	/// Caller must ensure that indices are within Mat bounds
	unsafe fn at_2d_unchecked_mut<T: DataType>(&mut self, row: i32, col: i32) -> Result<&mut T> {
		self.ptr_2d_mut(row, col)
			.map(|ptr| convert_ptr_mut(ptr))
	}

	/// Like `Mat::at_pt()` but performs no bounds or type checks
	/// # Safety
	/// Caller must ensure that point is within Mat bounds
	unsafe fn at_pt_unchecked<T: DataType>(&self, pt: Point) -> Result<&T> {
		self.at_2d_unchecked(pt.y, pt.x)
	}

	/// Like `Mat::at_pt_mut()` but performs no bounds or type checks
	/// # Safety
	/// Caller must ensure that point is within Mat bounds
	unsafe fn at_pt_unchecked_mut<T: DataType>(&mut self, pt: Point) -> Result<&mut T> {
		self.at_2d_unchecked_mut(pt.y, pt.x)
	}

	/// Like `Mat::at_3d()` but performs no bounds or type checks
	/// # Safety
	/// Caller must ensure that indices are within Mat bounds
	unsafe fn at_3d_unchecked<T: DataType>(&self, i0: i32, i1: i32, i2: i32) -> Result<&T> {
		self.ptr_3d(i0, i1, i2)
			.map(|ptr| convert_ptr(ptr))
	}

	/// Like `Mat::at_3d_mut()` but performs no bounds or type checks
	/// # Safety
	/// Caller must ensure that indices are within Mat bounds
	unsafe fn at_3d_unchecked_mut<T: DataType>(&mut self, i0: i32, i1: i32, i2: i32) -> Result<&mut T> {
		self.ptr_3d_mut(i0, i1, i2)
			.map(|ptr| convert_ptr_mut(ptr))
	}

	/// Like `Mat::at_nd()` but performs no bounds or type checks
	/// # Safety
	/// Caller must ensure that indices are within Mat bounds
	unsafe fn at_nd_unchecked<T: core::DataType>(&self, idx: &[i32]) -> Result<&T> {
		self.ptr_nd(idx)
			.map(|ptr| convert_ptr(ptr))
	}

	/// Like `Mat::at_nd_mut()` but performs no bounds or type checks
	/// # Safety
	/// Caller must ensure that indices are within Mat bounds
	unsafe fn at_nd_unchecked_mut<T: core::DataType>(&mut self, idx: &[i32]) -> Result<&mut T> {
		self.ptr_nd_mut(idx)
			.map(|ptr| convert_ptr_mut(ptr))
	}

	/// Return a complete read-only row
	fn at_row<T: DataType>(&self, row: i32) -> Result<&[T]> {
		match_format::<T>(self.typ()?)
			.and_then(|_| match_indices(self, &[row, 0]))
			.and_then(|_| unsafe { self.at_row_unchecked(row) })
	}

	/// Like `Mat::at_row()` but performs no bounds or type checks
	/// # Safety
	/// Caller must ensure that index is within Mat bounds
	unsafe fn at_row_unchecked<T: DataType>(&self, row: i32) -> Result<&[T]> {
		let width = self.size()?.width as usize;
		self.ptr(row)
			.map(|x| slice::from_raw_parts(convert_ptr(x), width))
	}

	/// Return a complete writeable row
	fn at_row_mut<T: DataType>(&mut self, row: i32) -> Result<&mut [T]> {
		match_format::<T>(self.typ()?)
			.and_then(|_| match_indices(self, &[row, 0]))?;
		unsafe { self.at_row_unchecked_mut(row) }
	}

	/// Like `Mat::at_row_mut()` but performs no bounds or type checks
	/// # Safety
	/// Caller must ensure that index is within Mat bounds
	unsafe fn at_row_unchecked_mut<T: DataType>(&mut self, row: i32) -> Result<&mut [T]> {
		let width = self.size()?.width as usize;
		self.ptr_mut(row)
			.map(|x| slice::from_raw_parts_mut(convert_ptr_mut(x), width))
	}

	fn size(&self) -> Result<core::Size> {
		extern "C" { fn cv_manual_Mat_size(instance: *const c_void) -> sys::Result<core::Size>; }
		unsafe { cv_manual_Mat_size(self.as_raw_Mat()) }
			.into_result()
	}

	fn is_allocated(&self) -> bool {
		extern "C" { fn cv_manual_Mat_is_allocated(instance: *const c_void) -> bool; }
		unsafe { cv_manual_Mat_is_allocated(self.as_raw_Mat()) }
	}

	/// Sets all or some of the array elements to the specified value.
	///
	/// ## Parameters
	/// * s: Assigned scalar converted to the actual array type.
	fn set(&mut self, s: Scalar) -> Result<()> {
		extern "C" { fn cv_manual_Mat_set(instance: *mut c_void, s: *const Scalar) -> sys::Result_void; }
		unsafe { cv_manual_Mat_set(self.as_raw_mut_Mat(), &s) }
			.into_result()
	}

	fn data(&self) -> Result<&u8> {
		extern "C" { fn cv_manual_Mat_data(instance: *const c_void) -> sys::Result<*const u8>; }
		unsafe { cv_manual_Mat_data(self.as_raw_Mat()) }
			.into_result()
			.and_then(|x| unsafe { x.as_ref() }.ok_or_else(|| Error::new(core::StsNullPtr, "Function returned Null pointer".to_string())))
	}

	fn data_typed<T: DataType>(&self) -> Result<&[T]> {
		match_format::<T>(self.typ()?)
			.and_then(|_| match_is_continuous(self))
			.and_then(|_| unsafe { self.data_typed_unchecked() })
	}

	unsafe fn data_typed_unchecked<T: DataType>(&self) -> Result<&[T]> {
		let total = self.total()?;
		self.data().map(|x| slice::from_raw_parts(x as *const _ as *const _, total))
	}

	fn data_typed_mut<T: DataType>(&mut self) -> Result<&mut [T]> {
		match_format::<T>(self.typ()?)
			.and_then(|_| match_is_continuous(self))?;
		unsafe { self.data_typed_unchecked_mut() }
	}

	unsafe fn data_typed_unchecked_mut<T: DataType>(&mut self) -> Result<&mut [T]> {
		let total = self.total()?;
		Ok(slice::from_raw_parts_mut(self.data_mut() as *mut _ as *mut _, total))
	}

	fn to_vec_2d<T: DataType>(&self) -> Result<Vec<Vec<T>>> {
		match_format::<T>(self.typ()?)
			.and_then(|_| match_dims(self, 2))
			.and_then(|_| {
				let size = Self::size(self)?;
				let width = size.width as usize;
				if self.is_continuous()? {
					let data = self.data_typed()?;
					Ok((0..size.height)
						.map(|row_n| {
							let row_n = row_n as usize;
							let mut row = Vec::with_capacity(width);
							row.extend_from_slice(&data[row_n * width..(row_n + 1) * width]);
							row
						})
						.collect()
					)
				} else {
					Ok((0..size.height)
						.map(|row_n| {
							self.at_row(row_n).map(|src_row| {
								let mut row = Vec::with_capacity(width);
								row.extend_from_slice(src_row);
								row
							})
						})
						.collect::<Result<_>>()?
					)
				}
			})
	}
}

impl<T: MatTrait + ?Sized> MatTraitManual for T {}

impl ToInputArray for Mat {
	#[inline]
	fn input_array(&self) -> Result<_InputArray> {
		_InputArray::from_mat(self)
	}
}

impl ToInputArray for &Mat {
	#[inline]
	fn input_array(&self) -> Result<_InputArray> {
		(*self).input_array()
	}
}

impl ToOutputArray for Mat {
	#[inline]
	fn output_array(&mut self) -> Result<_OutputArray> {
		_OutputArray::from_mat_mut(self)
	}
}

impl ToOutputArray for &mut Mat {
	#[inline]
	fn output_array(&mut self) -> Result<_OutputArray> {
		(*self).output_array()
	}
}

impl ToInputOutputArray for Mat {
	#[inline]
	fn input_output_array(&mut self) -> Result<_InputOutputArray> {
		_InputOutputArray::from_mat_mut(self)
	}
}

impl ToInputOutputArray for &mut Mat {
	#[inline]
	fn input_output_array(&mut self) -> Result<_InputOutputArray> {
		(*self).input_output_array()
	}
}

impl fmt::Debug for Mat {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		let typ = self.typ();
		let depth = self.depth();
		#[cfg(not(feature = "opencv-32"))]
		let typ = typ.and_then(core::type_to_string);
		#[cfg(not(feature = "opencv-32"))]
		let depth = depth.and_then(core::depth_to_string);
		f.debug_struct("Mat")
			.field("type", &typ.map_err(|_| fmt::Error)?)
			.field("flags", &self.flags())
			.field("channels", &self.channels().map_err(|_| fmt::Error)?)
			.field("depth", &depth.map_err(|_| fmt::Error)?)
			.field("dims", &self.dims())
			.field("size", &self.size().map_err(|_| fmt::Error)?)
			.field("rows", &self.rows())
			.field("cols", &self.cols())
			.field("elem_size", &self.elem_size().map_err(|_| fmt::Error)?)
			.field("elem_size1", &self.elem_size1().map_err(|_| fmt::Error)?)
			.field("total", &self.total().map_err(|_| fmt::Error)?)
			.field("is_continuous", &self.is_continuous().map_err(|_| fmt::Error)?)
			.field("is_submatrix", &self.is_submatrix().map_err(|_| fmt::Error)?)
			.finish()
	}
}

pub trait UMatTraitManual: UMatTrait {
	#[inline]
	fn size(&self) -> Result<core::Size> {
		extern "C" { fn cv_manual_UMat_size(instance: *const c_void) -> sys::Result<core::Size>; }
		unsafe { cv_manual_UMat_size(self.as_raw_UMat()) }
			.into_result()
	}
}

impl<T: UMatTrait> UMatTraitManual for T {}

impl ToInputArray for UMat {
	#[inline]
	fn input_array(&self) -> Result<_InputArray> {
		_InputArray::from_umat(self)
	}
}

impl ToInputArray for &UMat {
	#[inline]
	fn input_array(&self) -> Result<_InputArray> {
		(*self).input_array()
	}
}

impl ToOutputArray for UMat {
	#[inline]
	fn output_array(&mut self) -> Result<_OutputArray> {
		_OutputArray::from_umat_mut(self)
	}
}

impl ToOutputArray for &mut UMat {
	#[inline]
	fn output_array(&mut self) -> Result<_OutputArray> {
		(*self).output_array()
	}
}

impl ToInputOutputArray for UMat {
	#[inline]
	fn input_output_array(&mut self) -> Result<_InputOutputArray> {
		_InputOutputArray::from_umat_mut(self)
	}
}

impl ToInputOutputArray for &mut UMat {
	#[inline]
	fn input_output_array(&mut self) -> Result<_InputOutputArray> {
		(*self).input_output_array()
	}
}

#[cfg(feature = "opencv-32")]
pub trait MatSizeTraitManual: MatSizeTrait {
	#[inline]
	fn dims(&self) -> Result<i32> {
		extern "C" { fn cv_manual_MatSize_dims(instance: *const c_void) -> i32; }
		Ok(unsafe { cv_manual_MatSize_dims(self.as_raw_MatSize()) })
	}
}

#[cfg(feature = "opencv-32")]
impl<T: MatSizeTrait> MatSizeTraitManual for T {}

impl Deref for MatSize {
	type Target = [i32];

	fn deref(&self) -> &Self::Target {
		extern "C" { fn cv_manual_MatSize_deref(instance: *const c_void) -> *const i32; }
		let ptr = unsafe { cv_manual_MatSize_deref(self.as_raw_MatSize()) };
		unsafe { slice::from_raw_parts(ptr, self.dims().expect("Cannot get dims") as usize) }
	}
}

impl fmt::Debug for MatSize {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		writeln!(f, "{:#?}", self.deref())
	}
}

impl Deref for MatStep {
	type Target = [size_t];

	fn deref(&self) -> &Self::Target {
		extern "C" { fn cv_manual_MatStep_deref(instance: *const c_void) -> *const size_t; }
		let ptr = unsafe { cv_manual_MatStep_deref(self.as_raw_MatStep()) };
		unsafe { slice::from_raw_parts(ptr, 2) }
	}
}

impl fmt::Debug for MatStep {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		writeln!(f, "{:#?}", self.deref())
	}
}

pub trait MatConstIteratorTraitManual: MatConstIteratorTrait {
	#[inline]
	fn typ(&self) -> i32 {
		extern "C" { fn cv_manual_MatConstIterator_type(instance: *const c_void) -> i32; }
		unsafe { cv_manual_MatConstIterator_type(self.as_raw_MatConstIterator()) }
	}

	#[inline]
	fn has_elements(&self) -> bool {
		extern "C" { fn cv_manual_MatConstIterator_has_elements(instance: *const c_void) -> bool; }
		unsafe { cv_manual_MatConstIterator_has_elements(self.as_raw_MatConstIterator()) }
	}

	fn current<T: DataType>(&self) -> Result<&T> {
		match_format::<T>(self.typ())?;
		if self.has_elements() {
			self.try_deref().map(|ptr| unsafe { convert_ptr(ptr) })
		} else {
			Err(Error::new(core::StsOutOfRange, "MatConstIterator doesn't have any more elements".to_owned()))
		}
	}
}

impl<T: MatConstIteratorTrait> MatConstIteratorTraitManual for T {}

impl ToInputArray for MatExpr {
	#[inline]
	fn input_array(&self) -> Result<_InputArray> {
		_InputArray::from_matexpr(self)
	}
}

impl ToInputArray for &MatExpr {
	#[inline]
	fn input_array(&self) -> Result<_InputArray> {
		(*self).input_array()
	}
}

mod private {
	pub trait Sealed {}
}