libcvd
convert_image.h
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>
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())
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())
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())
63 }
64 
71 template <class D, class Conv, class C>
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>
81 {
82  Image<D> to(from.size());
83  convert_image<Conv>(from, to);
84  return to;
85 }
86 
92 template <class D, class C>
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 
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>
123  {
124  };
125  template <class 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>
144 {
146 }
147 #else
148 template <class D>
165 
166 #endif
167 template <class In, class Out>
171 {
173 };
174 
178 template <class In, class Out>
180 {
182 };
183 
185 template <class InOut>
186 struct PixelByPixelConvertible<InOut, InOut>
187 {
188  static const bool is = 1;
189 };
190 
191 }
192 
193 #endif
Can two types be converted with CVD::convert_image?
Definition: convert_image.h:170
A colour consisting of red, green and blue components.
Definition: rgb.h:25
All classes and functions are within the CVD namespace.
Definition: argb.h:6
ImageRef size() const
What is the size of this image?
Definition: image.h:557
void resize(const ImageRef &size)
Resize the image (destroying the data).
Definition: image.h:731
Definition: convert_image.h:122
All pixel types which are DefaultConvertible can be converted freely from one to another.
Definition: convert_pixel_types.h:464
Definition: convert_image.h:15
unsigned char byte
An 8-bit datatype.
Definition: byte.h:8
Definition: image.h:62
A generic image class to manage a block of arbitrarily padded data as an image.
Definition: image.h:273
void convert_image(const BasicImage< bayer_bggr > &from, BasicImage< byte > &to)
Convert Bayer pattern of various forms to greyscale data.
Definition: convert_pixel_types.h:441
A full image which manages its own data.
Definition: image.h:623
std::pair< Image< D1 >, Image< D2 > > convert_image_pair(const BasicImage< C > &from)
Convert an image from one type to another using the default, returning a pair of images.
Definition: convert_image.h:110
Input images have incompatible dimensions.
Definition: image.h:42
Can individual pixels of two types be converted with ConvertPixels::convert()? E.g.
Definition: convert_image.h:179