vigra - vigra/impexbase.hxx Source File

[ 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 == "BILEVEL")
66 {
67 return UNSIGNED_INT_8;
68 }
69 else if (pixel_type == "UINT8")
70 {
71 return UNSIGNED_INT_8;
72 }
73 else if (pixel_type == "UINT16")
74 {
75 return UNSIGNED_INT_16;
76 }
77 else if (pixel_type == "UINT32")
78 {
79 return UNSIGNED_INT_32;
80 }
81 else if (pixel_type == "INT16")
82 {
83 return SIGNED_INT_16;
84 }
85 else if (pixel_type == "INT32")
86 {
87 return SIGNED_INT_32;
88 }
89 else if (pixel_type == "FLOAT")
90 {
91 return IEEE_FLOAT_32;
92 }
93 else if (pixel_type == "DOUBLE")
94 {
95 return IEEE_FLOAT_64;
96 }
97 else
98 {
99 vigra_fail("vigra_ext::detail::pixel_t_of_string: unknown pixel type");
100 return UNSIGNED_INT_8; // NOT REACHED
101 }
102 }
103
104
105 struct identity
106 {
107 template <typename T>
108 T operator()(T x) const
109 {
110 return x;
111 }
112 };
113
114
115 typedef pair<double, double> range_t;
116
117
118 class linear_transform
119 {
120 public:
121 linear_transform(const range_t& source, const range_t& destination) :
122 scale_((destination.second - destination.first) / (source.second - source.first)),
123 offset_(destination.first / scale_ - source.first)
124 {}
125
126 template <typename T>
127 double operator()(T x) const
128 {
129 return scale_ * (static_cast<double>(x) + offset_);
130 }
131
132 private:
133 const double scale_;
134 const double offset_;
135 };
136
137
138 template <class Iterator, class Accessor>
139 inline static range_t
140 find_value_range(Iterator upper_left, Iterator lower_right, Accessor accessor,
141 /* is_scalar? */ VigraTrueType)
142 {
143 typedef typename Accessor::value_type value_type;
144
145 FindMinMax<value_type> extrema;
146
147 inspectImage(upper_left, lower_right, accessor, extrema);
148
149 return range_t(static_cast<double>(extrema.min), static_cast<double>(extrema.max));
150 }
151
152
153 template <class Iterator, class Accessor>
154 inline static range_t
155 find_value_range(Iterator upper_left, Iterator lower_right, Accessor accessor,
156 /* is_scalar? */ VigraFalseType)
157 {
158 typedef typename Accessor::ElementAccessor element_accessor;
159 typedef typename element_accessor::value_type value_type;
160
161 const int number_of_bands(static_cast<int>(accessor.size(upper_left)));
162 FindMinMax<value_type> extrema;
163
164 for (int i = 0; i != number_of_bands; ++i)
165 {
166 element_accessor band(i, accessor);
167
168 inspectImage(upper_left, lower_right, band, extrema);
169 }
170
171 return range_t(static_cast<double>(extrema.min), static_cast<double>(extrema.max));
172 }
173
174
175 template <class SourceIterator, class SourceAccessor>
176 inline static range_t
177 find_source_value_range(const ImageExportInfo& export_info,
178 SourceIterator upper_left, SourceIterator lower_right, SourceAccessor accessor)
179 {
180 if (export_info.getFromMin() < export_info.getFromMax())
181 {
182 return range_t(export_info.getFromMin(), export_info.getFromMax());
183 }
184 else
185 {
186 typedef typename SourceAccessor::value_type SourceValueType;
187 typedef typename NumericTraits<SourceValueType>::isScalar is_scalar;
188
189 const range_t range(find_value_range(upper_left, lower_right, accessor, is_scalar()));
190
191 if (range.first < range.second)
192 {
193 return range_t(range.first, range.second);
194 }
195 else
196 {
197 return range_t(range.first, range.first + 1.0);
198 }
199 }
200 }
201
202
203 template <typename T>
204 inline static range_t
205 find_destination_value_range(const ImageExportInfo& export_info)
206 {
207 if (export_info.getToMin() < export_info.getToMax())
208 {
209 return range_t(export_info.getToMin(), export_info.getToMax());
210 }
211 else
212 {
213 return range_t(static_cast<double>(NumericTraits<T>::min()),
214 static_cast<double>(NumericTraits<T>::max()));
215 }
216 }
217
218
219 inline static range_t
220 find_destination_value_range(const ImageExportInfo& export_info, pixel_t pixel_type)
221 {
222 switch (pixel_type)
223 {
224 case UNSIGNED_INT_8: return find_destination_value_range<UInt8>(export_info);
225 case UNSIGNED_INT_16: return find_destination_value_range<UInt16>(export_info);
226 case UNSIGNED_INT_32: return find_destination_value_range<UInt32>(export_info);
227 case SIGNED_INT_16: return find_destination_value_range<Int16>(export_info);
228 case SIGNED_INT_32: return find_destination_value_range<Int32>(export_info);
229 case IEEE_FLOAT_32: return find_destination_value_range<float>(export_info);
230 case IEEE_FLOAT_64: return find_destination_value_range<double>(export_info);
231 default:
232 vigra_fail("vigra_ext::detail::find_destination_value_range: not reached");
233 return range_t(0.0, 0.0); // NOT REACHED
234 }
235 }
236 } // end namespace detail
237 } // end namespace vigra
238
239
240 #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.10.0 (Thu Jan 8 2015)