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
use num_traits::{AsPrimitive, PrimInt, Signed};
use std::fmt::{Debug, Display};
use std::mem::size_of;
use std::ops::AddAssign;
pub trait CastFromPrimitive<T>: Copy + 'static {
fn cast_from(v: T) -> Self;
}
macro_rules! impl_cast_from_primitive {
( $T:ty => $U:ty ) => {
impl CastFromPrimitive<$U> for $T {
#[inline(always)]
fn cast_from(v: $U) -> Self { v as Self }
}
};
( $T:ty => { $( $U:ty ),* } ) => {
$( impl_cast_from_primitive!($T => $U); )*
};
}
impl_cast_from_primitive!(u8 => { u32, u64, usize });
impl_cast_from_primitive!(u8 => { i8, i64, isize });
impl_cast_from_primitive!(u16 => { u32, u64, usize });
impl_cast_from_primitive!(u16 => { i8, i64, isize });
impl_cast_from_primitive!(i16 => { u32, u64, usize });
impl_cast_from_primitive!(i16 => { i8, i64, isize });
impl_cast_from_primitive!(i32 => { u32, u64, usize });
impl_cast_from_primitive!(i32 => { i8, i64, isize });
pub trait RegisteredPrimitive:
PrimInt
+ AsPrimitive<u8>
+ AsPrimitive<i16>
+ AsPrimitive<u16>
+ AsPrimitive<i32>
+ AsPrimitive<u32>
+ AsPrimitive<usize>
+ CastFromPrimitive<u8>
+ CastFromPrimitive<i16>
+ CastFromPrimitive<u16>
+ CastFromPrimitive<i32>
+ CastFromPrimitive<u32>
+ CastFromPrimitive<usize>
{
}
impl RegisteredPrimitive for u8 {}
impl RegisteredPrimitive for u16 {}
impl RegisteredPrimitive for i16 {}
impl RegisteredPrimitive for i32 {}
macro_rules! impl_cast_from_pixel_to_primitive {
( $T:ty ) => {
impl<T: RegisteredPrimitive> CastFromPrimitive<T> for $T {
#[inline(always)]
fn cast_from(v: T) -> Self {
v.as_()
}
}
};
}
impl_cast_from_pixel_to_primitive!(u8);
impl_cast_from_pixel_to_primitive!(i16);
impl_cast_from_pixel_to_primitive!(u16);
impl_cast_from_pixel_to_primitive!(i32);
impl_cast_from_pixel_to_primitive!(u32);
pub enum PixelType {
U8,
U16,
}
pub trait Pixel:
RegisteredPrimitive
+ Into<u32>
+ Into<i32>
+ Debug
+ Display
+ Send
+ Sync
+ 'static
{
type Coeff: Coefficient;
fn type_enum() -> PixelType;
fn to_asm_stride(in_stride: usize) -> isize {
(in_stride * size_of::<Self>()) as isize
}
}
impl Pixel for u8 {
type Coeff = i16;
fn type_enum() -> PixelType {
PixelType::U8
}
}
impl Pixel for u16 {
type Coeff = i32;
fn type_enum() -> PixelType {
PixelType::U16
}
}
pub trait Coefficient:
RegisteredPrimitive + Into<i32> + AddAssign + Signed + Debug + 'static
{
type Pixel: Pixel;
}
impl Coefficient for i16 {
type Pixel = u8;
}
impl Coefficient for i32 {
type Pixel = u16;
}