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
#[cfg(feature = "image_0-23")]
mod with_image_0_23 {
    use crate::image;
    use crate::tch;
    use crate::{common::*, FromCv, IntoCv, TchTensorAsImage, TchTensorImageShape, TryFromCv};
    use std::ops::Deref;

    impl<P, Container> FromCv<&image::ImageBuffer<P, Container>> for TchTensorAsImage
    where
        P: image::Pixel + 'static,
        P::Subpixel: 'static + tch::kind::Element,
        Container: Deref<Target = [P::Subpixel]>,
    {
        fn from_cv(from: &image::ImageBuffer<P, Container>) -> Self {
            let (width, height) = from.dimensions();
            let channels = P::CHANNEL_COUNT;
            let tensor =
                tch::Tensor::of_slice(&*from).view([width as i64, height as i64, channels as i64]);
            TchTensorAsImage {
                tensor,
                kind: TchTensorImageShape::Whc,
            }
        }
    }

    impl<P, Container> FromCv<image::ImageBuffer<P, Container>> for TchTensorAsImage
    where
        P: image::Pixel + 'static,
        P::Subpixel: 'static + tch::kind::Element,
        Container: Deref<Target = [P::Subpixel]>,
    {
        fn from_cv(from: image::ImageBuffer<P, Container>) -> Self {
            Self::from_cv(&from)
        }
    }

    impl TryFromCv<&image::DynamicImage> for TchTensorAsImage {
        type Error = Error;

        fn try_from_cv(from: &image::DynamicImage) -> Result<Self, Self::Error> {
            use image::DynamicImage as D;

            let tensor = match from {
                D::ImageLuma8(image) => image.into_cv(),
                D::ImageLumaA8(image) => image.into_cv(),
                D::ImageRgb8(image) => image.into_cv(),
                D::ImageRgba8(image) => image.into_cv(),
                D::ImageBgr8(image) => image.into_cv(),
                D::ImageBgra8(image) => image.into_cv(),
                _ => bail!("cannot convert an image with u16 components to a tensor"),
            };
            Ok(tensor)
        }
    }

    impl TryFromCv<image::DynamicImage> for TchTensorAsImage {
        type Error = Error;

        fn try_from_cv(from: image::DynamicImage) -> Result<Self, Self::Error> {
            Self::try_from_cv(&from)
        }
    }
}

#[cfg(feature = "image_0-24")]
mod with_image_0_24 {
    use crate::image;
    use crate::tch;
    use crate::{common::*, FromCv, IntoCv, TchTensorAsImage, TchTensorImageShape, TryFromCv};
    use std::ops::Deref;

    impl<P, Container> FromCv<&image::ImageBuffer<P, Container>> for TchTensorAsImage
    where
        P: image::Pixel + 'static,
        P::Subpixel: 'static + tch::kind::Element,
        Container: Deref<Target = [P::Subpixel]>,
    {
        fn from_cv(from: &image::ImageBuffer<P, Container>) -> Self {
            let (width, height) = from.dimensions();
            let channels = P::CHANNEL_COUNT;
            let tensor =
                tch::Tensor::of_slice(&*from).view([width as i64, height as i64, channels as i64]);
            TchTensorAsImage {
                tensor,
                kind: TchTensorImageShape::Whc,
            }
        }
    }

    impl<P, Container> FromCv<image::ImageBuffer<P, Container>> for TchTensorAsImage
    where
        P: image::Pixel + 'static,
        P::Subpixel: 'static + tch::kind::Element,
        Container: Deref<Target = [P::Subpixel]>,
    {
        fn from_cv(from: image::ImageBuffer<P, Container>) -> Self {
            Self::from_cv(&from)
        }
    }

    impl TryFromCv<&image::DynamicImage> for TchTensorAsImage {
        type Error = Error;

        fn try_from_cv(from: &image::DynamicImage) -> Result<Self, Self::Error> {
            use image::DynamicImage as D;

            let tensor = match from {
                D::ImageLuma8(image) => image.into_cv(),
                D::ImageLumaA8(image) => image.into_cv(),
                D::ImageRgb8(image) => image.into_cv(),
                D::ImageRgba8(image) => image.into_cv(),
                D::ImageRgb32F(image) => image.into_cv(),
                D::ImageRgba32F(image) => image.into_cv(),
                _ => bail!("the color type {:?} is not supported", from.color()),
            };
            Ok(tensor)
        }
    }

    impl TryFromCv<image::DynamicImage> for TchTensorAsImage {
        type Error = Error;

        fn try_from_cv(from: image::DynamicImage) -> Result<Self, Self::Error> {
            Self::try_from_cv(&from)
        }
    }
}