[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

impexbase.hxx
1 /************************************************************************/
2 /* */
3 /* Copyright 2012 Christoph Spiel */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35 
36 #ifndef VIGRA_IMPEXBASE_HXX
37 #define VIGRA_IMPEXBASE_HXX
38 
39 
40 #include <string>
41 #include "inspectimage.hxx"
42 #include "sized_int.hxx"
43 #include "utilities.hxx"
44 
45 
46 namespace vigra
47 {
48  typedef enum
49  {
50  UNSIGNED_INT_8,
51  UNSIGNED_INT_16,
52  UNSIGNED_INT_32,
53  SIGNED_INT_16,
54  SIGNED_INT_32,
55  IEEE_FLOAT_32,
56  IEEE_FLOAT_64
57  } pixel_t;
58 
59 
60  namespace detail
61  {
62  inline static pixel_t
63  pixel_t_of_string(const std::string& pixel_type)
64  {
65  if (pixel_type == "UINT8")
66  {
67  return UNSIGNED_INT_8;
68  }
69  else if (pixel_type == "UINT16")
70  {
71  return UNSIGNED_INT_16;
72  }
73  else if (pixel_type == "UINT32")
74  {
75  return UNSIGNED_INT_32;
76  }
77  else if (pixel_type == "INT16")
78  {
79  return SIGNED_INT_16;
80  }
81  else if (pixel_type == "INT32")
82  {
83  return SIGNED_INT_32;
84  }
85  else if (pixel_type == "FLOAT")
86  {
87  return IEEE_FLOAT_32;
88  }
89  else if (pixel_type == "DOUBLE")
90  {
91  return IEEE_FLOAT_64;
92  }
93  else
94  {
95  vigra_fail("vigra_ext::detail::pixel_t_of_string: unknown pixel type");
96  return UNSIGNED_INT_8; // NOT REACHED
97  }
98  }
99 
100 
101  struct identity
102  {
103  template <typename T>
104  T operator()(T x) const
105  {
106  return x;
107  }
108  };
109 
110 
111  typedef pair<double, double> range_t;
112 
113 
114  class linear_transform
115  {
116  public:
117  linear_transform(const range_t& source, const range_t& destination) :
118  scale_((destination.second - destination.first) / (source.second - source.first)),
119  offset_(destination.first / scale_ - source.first)
120  {}
121 
122  template <typename T>
123  double operator()(T x) const
124  {
125  return scale_ * (static_cast<double>(x) + offset_);
126  }
127 
128  private:
129  const double scale_;
130  const double offset_;
131  };
132 
133 
134  template <class Iterator, class Accessor>
135  inline static range_t
136  find_value_range(Iterator upper_left, Iterator lower_right, Accessor accessor,
137  /* is_scalar? */ VigraTrueType)
138  {
139  typedef typename Accessor::value_type value_type;
140 
141  FindMinMax<value_type> extrema;
142 
143  inspectImage(upper_left, lower_right, accessor, extrema);
144 
145  return range_t(static_cast<double>(extrema.min), static_cast<double>(extrema.max));
146  }
147 
148 
149  template <class Iterator, class Accessor>
150  inline static range_t
151  find_value_range(Iterator upper_left, Iterator lower_right, Accessor accessor,
152  /* is_scalar? */ VigraFalseType)
153  {
154  typedef typename Accessor::ElementAccessor element_accessor;
155  typedef typename element_accessor::value_type value_type;
156 
157  const int number_of_bands(static_cast<int>(accessor.size(upper_left)));
158  FindMinMax<value_type> extrema;
159 
160  for (int i = 0; i != number_of_bands; ++i)
161  {
162  element_accessor band(i, accessor);
163 
164  inspectImage(upper_left, lower_right, band, extrema);
165  }
166 
167  return range_t(static_cast<double>(extrema.min), static_cast<double>(extrema.max));
168  }
169 
170 
171  template <class SourceIterator, class SourceAccessor>
172  inline static range_t
173  find_source_value_range(const ImageExportInfo& export_info,
174  SourceIterator upper_left, SourceIterator lower_right, SourceAccessor accessor)
175  {
176  if (export_info.getFromMin() < export_info.getFromMax())
177  {
178  return range_t(export_info.getFromMin(), export_info.getFromMax());
179  }
180  else
181  {
182  typedef typename SourceAccessor::value_type SourceValueType;
183  typedef typename NumericTraits<SourceValueType>::isScalar is_scalar;
184 
185  const range_t range(find_value_range(upper_left, lower_right, accessor, is_scalar()));
186 
187  if (range.first < range.second)
188  {
189  return range_t(range.first, range.second);
190  }
191  else
192  {
193  return range_t(range.first, range.first + 1.0);
194  }
195  }
196  }
197 
198 
199  template <typename T>
200  inline static range_t
201  find_destination_value_range(const ImageExportInfo& export_info)
202  {
203  if (export_info.getToMin() < export_info.getToMax())
204  {
205  return range_t(export_info.getToMin(), export_info.getToMax());
206  }
207  else
208  {
209  return range_t(static_cast<double>(NumericTraits<T>::min()),
210  static_cast<double>(NumericTraits<T>::max()));
211  }
212  }
213 
214 
215  inline static range_t
216  find_destination_value_range(const ImageExportInfo& export_info, pixel_t pixel_type)
217  {
218  switch (pixel_type)
219  {
220  case UNSIGNED_INT_8: return find_destination_value_range<UInt8>(export_info);
221  case UNSIGNED_INT_16: return find_destination_value_range<UInt16>(export_info);
222  case UNSIGNED_INT_32: return find_destination_value_range<UInt32>(export_info);
223  case SIGNED_INT_16: return find_destination_value_range<Int16>(export_info);
224  case SIGNED_INT_32: return find_destination_value_range<Int32>(export_info);
225  case IEEE_FLOAT_32: return find_destination_value_range<float>(export_info);
226  case IEEE_FLOAT_64: return find_destination_value_range<double>(export_info);
227  default:
228  vigra_fail("vigra_ext::detail::find_destination_value_range: not reached");
229  return range_t(0.0, 0.0); // NOT REACHED
230  }
231  }
232  } // end namespace detail
233 } // end namespace vigra
234 
235 
236 #endif // VIGRA_IMPEXBASE_HXX

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
vigra 1.9.0 (Tue Oct 22 2013)