Debian Codegraph @

cvd/convert_image.h

raw symbols SCIP
1 #ifndef CVD_CONVERT_IMAGE_H
2 #define CVD_CONVERT_IMAGE_H
3
4 #include <cvd/image.h>
5 #include <cvd/internal/convert_pixel_types.h>
6 #include <cvd/internal/rgb_components.h>
7 #include <type_traits>
8
9 namespace CVD
10 {
11
12 // The most general case: one row at a time
13
14 template <class From, class To, class Conv = typename Pixel::DefaultConversion<From, To>::type, int both_pod = std::is_trivially_copyable<From>::value&& std::is_trivially_copyable<To>::value>
15 struct ConvertImage
16 {
17 static void convert(const BasicImage<From>& from, BasicImage<To>& to)
18 {
19 for(int r = 0; r < from.size().y; r++)
20 Pixel::ConvertPixels<From, To, Conv>::convert(from[r], to[r], from.size().x);
21 };
22 };
23
24 // The blat case: memcpy all data at once
25 template <class T>
26 struct ConvertImage<T, T, Pixel::GenericConversion<T, T>, 1>
27 {
28 static void convert(const BasicImage<T>& from, BasicImage<T>& to)
29 {
30 for(int y = 0; y < from.size().y; y++)
31 memcpy(to[y], from[y], from.size().x * sizeof(T));
32 };
33 };
34
35 template <>
36 struct ConvertImage<Rgb<byte>, byte, Pixel::CIE<Rgb<byte>, byte>, 1>
37 {
38 static void convert(const BasicImage<Rgb<byte>>& from, BasicImage<byte>& to);
39 };
40
41 template <class Conv, class C, class D>
42 void convert_image(const BasicImage<C>& from, BasicImage<D>& to)
43 {
44 if(from.size() != to.size())
45 throw Exceptions::Image::IncompatibleImageSizes(__FUNCTION__);
46 ConvertImage<C, D, Conv>::convert(from, to);
47 }
48
49 template <template <class From, class To> class Conv, class C, class D>
50 void convert_image(const BasicImage<C>& from, BasicImage<D>& to)
51 {
52 if(from.size() != to.size())
53 throw Exceptions::Image::IncompatibleImageSizes(__FUNCTION__);
54 ConvertImage<C, D, Conv<C, D>>::convert(from, to);
55 }
56
57 template <class C, class D>
58 void convert_image(const BasicImage<C>& from, BasicImage<D>& to)
59 {
60 if(from.size() != to.size())
61 throw Exceptions::Image::IncompatibleImageSizes(__FUNCTION__);
62 ConvertImage<C, D>::convert(from, to);
63 }
64
65 /// Convert an image from one type to another using a specified conversion.
66 /// @param D The destination image pixel type
67 /// @param C The source image pixel type
68 /// @param Conv The conversion to use
69 /// @param from The image to convert from
70 /// @ingroup gImageIO
71 template <class D, class Conv, class C>
72 Image<D> convert_image(const BasicImage<C>& from)
73 {
74 Image<D> to(from.size());
75 convert_image<Conv>(from, to);
76 return to;
77 }
78
79 template <class D, template <class From, class To> class Conv, class C>
80 Image<D> convert_image(const BasicImage<C>& from)
81 {
82 Image<D> to(from.size());
83 convert_image<Conv>(from, to);
84 return to;
85 }
86
87 /// Convert an image from one type to another using the default.
88 /// @param D The destination image pixel type
89 /// @param C The source image pixel type
90 /// @param from The image to convert from
91 /// @ingroup gImageIO
92 template <class D, class C>
93 Image<D> convert_image(const BasicImage<C>& from)
94 {
95 Image<D> to(from.size());
96 convert_image(from, to);
97 return to;
98 }
99
100 // Function name changed from 'convert_image' to prevent compile-time
101 // error arising from the clash with a function of same name declared above.
102
103 /// Convert an image from one type to another using the default, returning a pair of images.
104 /// @param D1 The first destination image pixel type
105 /// @param D2 The second destination image pixel type
106 /// @param C The source image pixel type
107 /// @param from The image to convert from
108 /// @ingroup gImageIO
109 template <class D1, class D2, class C>
110 std::pair<Image<D1>, Image<D2>> convert_image_pair(const BasicImage<C>& from)
111 {
112 std::pair<Image<D1>, Image<D2>> to(Image<D1>(from.size()), Image<D2>(from.size()));
113 convert_image(from, to.first);
114 convert_image(from, to.second);
115 return to;
116 }
117
118 #ifndef DOXYGEN_IGNORE_INTERNAL
119 namespace Internal
120 {
121 template <class C>
122 class ImageConverter
123 {
124 };
125 template <class C>
126 struct ImagePromise<ImageConverter<C>>
127 {
128 ImagePromise(const BasicImage<C>& im)
129 : i(im)
130 {
131 }
132
133 const BasicImage<C>& i;
134 template <class D>
135 void execute(Image<D>& j)
136 {
137 j.resize(i.size());
138 convert_image(i, j);
139 }
140 };
141 };
142 template <class C>
143 Internal::ImagePromise<Internal::ImageConverter<C>> convert_image(const BasicImage<C>& c)
144 {
145 return Internal::ImagePromise<Internal::ImageConverter<C>>(c);
146 }
147 #else
148 ///Convert an image from one type to another using the default.
149 ///Type deduction is automatic, and D does not need to be specified. The following usage will work:
150 ///
151 /// \code
152 /// Image<byte> a;
153 /// Image<byte> b;
154 /// ...
155 /// b = convert_image(a);
156 /// \endcode
157 /// Note that this is performed using lazy evaluation, so convertion happens on evaluation of assignment.
158 /// @param D The destination image pixel type
159 /// @param C The source image pixel type
160 /// @param from The image to convert from
161 /// @return The converted image
162 /// @ingroup gImageIO
163 template <class D>
164 Image<D> convert_image(const BasicImage<C>& from);
165
166 #endif
167 /// Can two types be converted with CVD::convert_image?
168 /// @ingroup gImageIO
169 template <class In, class Out>
170 struct IsConvertible
171 {
172 static const bool is = Pixel::DefaultConvertible<In>::is & Pixel::DefaultConvertible<Out>::is;
173 };
174
175 /// Can individual pixels of two types be converted with ConvertPixels::convert()?
176 /// E.g. Bayer patterns or YUV411 can usually not.
177 /// @ingroup gImageIO
178 template <class In, class Out>
179 struct PixelByPixelConvertible
180 {
181 static const bool is = Pixel::DefaultConvertible<In>::is & Pixel::DefaultConvertible<Out>::is;
182 };
183
184 /// Identity conversion by memcpy is always supported
185 template <class InOut>
186 struct PixelByPixelConvertible<InOut, InOut>
187 {
188 static const bool is = 1;
189 };
190
191 }
192
193 #endif