36 #ifndef VIGRA_IMPEXBASE_HXX
37 #define VIGRA_IMPEXBASE_HXX
41 #include "inspectimage.hxx"
42 #include "sized_int.hxx"
43 #include "utilities.hxx"
63 pixel_t_of_string(
const std::string& pixel_type)
65 if (pixel_type ==
"UINT8")
67 return UNSIGNED_INT_8;
69 else if (pixel_type ==
"UINT16")
71 return UNSIGNED_INT_16;
73 else if (pixel_type ==
"UINT32")
75 return UNSIGNED_INT_32;
77 else if (pixel_type ==
"INT16")
81 else if (pixel_type ==
"INT32")
85 else if (pixel_type ==
"FLOAT")
89 else if (pixel_type ==
"DOUBLE")
95 vigra_fail(
"vigra_ext::detail::pixel_t_of_string: unknown pixel type");
96 return UNSIGNED_INT_8;
103 template <
typename T>
104 T operator()(T x)
const
111 typedef pair<double, double> range_t;
114 class linear_transform
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)
122 template <
typename T>
123 double operator()(T x)
const
125 return scale_ * (
static_cast<double>(x) + offset_);
130 const double offset_;
134 template <
class Iterator,
class Accessor>
135 inline static range_t
136 find_value_range(Iterator upper_left, Iterator lower_right, Accessor accessor,
139 typedef typename Accessor::value_type value_type;
141 FindMinMax<value_type> extrema;
143 inspectImage(upper_left, lower_right, accessor, extrema);
145 return range_t(static_cast<double>(extrema.min), static_cast<double>(extrema.max));
149 template <
class Iterator,
class Accessor>
150 inline static range_t
151 find_value_range(Iterator upper_left, Iterator lower_right, Accessor accessor,
154 typedef typename Accessor::ElementAccessor element_accessor;
155 typedef typename element_accessor::value_type value_type;
157 const int number_of_bands(static_cast<int>(accessor.size(upper_left)));
158 FindMinMax<value_type> extrema;
160 for (
int i = 0; i != number_of_bands; ++i)
162 element_accessor band(i, accessor);
167 return range_t(static_cast<double>(extrema.min), static_cast<double>(extrema.max));
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)
176 if (export_info.getFromMin() < export_info.getFromMax())
178 return range_t(export_info.getFromMin(), export_info.getFromMax());
182 typedef typename SourceAccessor::value_type SourceValueType;
183 typedef typename NumericTraits<SourceValueType>::isScalar is_scalar;
185 const range_t range(find_value_range(upper_left, lower_right, accessor, is_scalar()));
187 if (range.first < range.second)
189 return range_t(range.first, range.second);
193 return range_t(range.first, range.first + 1.0);
199 template <
typename T>
200 inline static range_t
201 find_destination_value_range(
const ImageExportInfo& export_info)
203 if (export_info.getToMin() < export_info.getToMax())
205 return range_t(export_info.getToMin(), export_info.getToMax());
209 return range_t(static_cast<double>(NumericTraits<T>::min()),
210 static_cast<double>(NumericTraits<T>::max()));
215 inline static range_t
216 find_destination_value_range(
const ImageExportInfo& export_info, pixel_t pixel_type)
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);
228 vigra_fail(
"vigra_ext::detail::find_destination_value_range: not reached");
229 return range_t(0.0, 0.0);
236 #endif // VIGRA_IMPEXBASE_HXX