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

vigra/impexbase.hxx |

    00001 /************************************************************************/
    00002 /*                                                                      */
    00003 /*               Copyright 2012 Christoph Spiel                         */
    00004 /*                                                                      */
    00005 /*    This file is part of the VIGRA computer vision library.           */
    00006 /*    The VIGRA Website is                                              */
    00007 /*        http://hci.iwr.uni-heidelberg.de/vigra/                       */
    00008 /*    Please direct questions, bug reports, and contributions to        */
    00009 /*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
    00010 /*        vigra@informatik.uni-hamburg.de                               */
    00011 /*                                                                      */
    00012 /*    Permission is hereby granted, free of charge, to any person       */
    00013 /*    obtaining a copy of this software and associated documentation    */
    00014 /*    files (the "Software"), to deal in the Software without           */
    00015 /*    restriction, including without limitation the rights to use,      */
    00016 /*    copy, modify, merge, publish, distribute, sublicense, and/or      */
    00017 /*    sell copies of the Software, and to permit persons to whom the    */
    00018 /*    Software is furnished to do so, subject to the following          */
    00019 /*    conditions:                                                       */
    00020 /*                                                                      */
    00021 /*    The above copyright notice and this permission notice shall be    */
    00022 /*    included in all copies or substantial portions of the             */
    00023 /*    Software.                                                         */
    00024 /*                                                                      */
    00025 /*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
    00026 /*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
    00027 /*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
    00028 /*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
    00029 /*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
    00030 /*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
    00031 /*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
    00032 /*    OTHER DEALINGS IN THE SOFTWARE.                                   */
    00033 /*                                                                      */
    00034 /************************************************************************/
    00035
    00036 #ifndef VIGRA_IMPEXBASE_HXX
    00037 #define VIGRA_IMPEXBASE_HXX
    00038
    00039
    00040 #include <string>
    00041 #include "inspectimage.hxx"
    00042 #include "sized_int.hxx"
    00043 #include "utilities.hxx"
    00044
    00045
    00046 namespace vigra
    00047 {
    00048     typedef enum
    00049     {
    00050         UNSIGNED_INT_8,
    00051         UNSIGNED_INT_16,
    00052         UNSIGNED_INT_32,
    00053         SIGNED_INT_16,
    00054         SIGNED_INT_32,
    00055         IEEE_FLOAT_32,
    00056         IEEE_FLOAT_64
    00057     } pixel_t;
    00058
    00059
    00060     namespace detail
    00061     {
    00062         inline static pixel_t
    00063         pixel_t_of_string(const std::string& pixel_type)
    00064         {
    00065             if (pixel_type == "UINT8")
    00066             {
    00067                 return UNSIGNED_INT_8;
    00068             }
    00069             else if (pixel_type == "UINT16")
    00070             {
    00071                 return UNSIGNED_INT_16;
    00072             }
    00073             else if (pixel_type == "UINT32")
    00074             {
    00075                 return UNSIGNED_INT_32;
    00076             }
    00077             else if (pixel_type == "INT16")
    00078             {
    00079                 return SIGNED_INT_16;
    00080             }
    00081             else if (pixel_type == "INT32")
    00082             {
    00083                 return SIGNED_INT_32;
    00084             }
    00085             else if (pixel_type == "FLOAT")
    00086             {
    00087                 return IEEE_FLOAT_32;
    00088             }
    00089             else if (pixel_type == "DOUBLE")
    00090             {
    00091                 return IEEE_FLOAT_64;
    00092             }
    00093             else
    00094             {
    00095                 vigra_fail("vigra_ext::detail::pixel_t_of_string: unknown pixel type");
    00096                 return UNSIGNED_INT_8; // NOT REACHED
    00097             }
    00098         }
    00099
    00100
    00101         struct identity
    00102         {
    00103             template <typename T>
    00104             T operator()(T x) const
    00105             {
    00106                 return x;
    00107             }
    00108         };
    00109
    00110
    00111         typedef pair<double, double> range_t;
    00112
    00113
    00114         class linear_transform
    00115         {
    00116         public:
    00117             linear_transform(const range_t& source, const range_t& destination) :
    00118                 scale_((destination.second - destination.first) / (source.second - source.first)),
    00119                 offset_(destination.first / scale_ - source.first)
    00120             {}
    00121
    00122             template <typename T>
    00123             double operator()(T x) const
    00124             {
    00125                 return scale_ * (static_cast<double>(x) + offset_);
    00126             }
    00127
    00128         private:
    00129             const double scale_;
    00130             const double offset_;
    00131         };
    00132
    00133
    00134         template <class Iterator, class Accessor>
    00135         inline static range_t
    00136         find_value_range(Iterator upper_left, Iterator lower_right, Accessor accessor,
    00137                          /* is_scalar? */ VigraTrueType)
    00138         {
    00139             typedef typename Accessor::value_type value_type;
    00140
    00141             FindMinMax<value_type> extrema;
    00142
    00143             inspectImage(upper_left, lower_right, accessor, extrema);
    00144
    00145             return range_t(static_cast<double>(extrema.min), static_cast<double>(extrema.max));
    00146         }
    00147
    00148
    00149         template <class Iterator, class Accessor>
    00150         inline static range_t
    00151         find_value_range(Iterator upper_left, Iterator lower_right, Accessor accessor,
    00152                          /* is_scalar? */ VigraFalseType)
    00153         {
    00154             typedef typename Accessor::ElementAccessor element_accessor;
    00155             typedef typename element_accessor::value_type value_type;
    00156
    00157             const int number_of_bands(static_cast<int>(accessor.size(upper_left)));
    00158             FindMinMax<value_type> extrema;
    00159
    00160             for (int i = 0; i != number_of_bands; ++i)
    00161             {
    00162                 element_accessor band(i, accessor);
    00163
    00164                 inspectImage(upper_left, lower_right, band, extrema);
    00165             }
    00166
    00167             return range_t(static_cast<double>(extrema.min), static_cast<double>(extrema.max));
    00168         }
    00169
    00170
    00171         template <class SourceIterator, class SourceAccessor>
    00172         inline static range_t
    00173         find_source_value_range(const ImageExportInfo& export_info,
    00174                                 SourceIterator upper_left, SourceIterator lower_right, SourceAccessor accessor)
    00175         {
    00176             if (export_info.getFromMin() < export_info.getFromMax())
    00177             {
    00178                 return range_t(export_info.getFromMin(), export_info.getFromMax());
    00179             }
    00180             else
    00181             {
    00182                 typedef typename SourceAccessor::value_type SourceValueType;
    00183                 typedef typename NumericTraits<SourceValueType>::isScalar is_scalar;
    00184
    00185                 const range_t range(find_value_range(upper_left, lower_right, accessor, is_scalar()));
    00186
    00187                 if (range.first < range.second)
    00188                 {
    00189                     return range_t(range.first, range.second);
    00190                 }
    00191                 else
    00192                 {
    00193                     return range_t(range.first, range.first + 1.0);
    00194                 }
    00195             }
    00196         }
    00197
    00198
    00199         template <typename T>
    00200         inline static range_t
    00201         find_destination_value_range(const ImageExportInfo& export_info)
    00202         {
    00203             if (export_info.getToMin() < export_info.getToMax())
    00204             {
    00205                 return range_t(export_info.getToMin(), export_info.getToMax());
    00206             }
    00207             else
    00208             {
    00209                 return range_t(static_cast<double>(NumericTraits<T>::min()),
    00210                                static_cast<double>(NumericTraits<T>::max()));
    00211             }
    00212         }
    00213
    00214
    00215         inline static range_t
    00216         find_destination_value_range(const ImageExportInfo& export_info, pixel_t pixel_type)
    00217         {
    00218             switch (pixel_type)
    00219             {
    00220             case UNSIGNED_INT_8: return find_destination_value_range<UInt8>(export_info);
    00221             case UNSIGNED_INT_16: return find_destination_value_range<UInt16>(export_info);
    00222             case UNSIGNED_INT_32: return find_destination_value_range<UInt32>(export_info);
    00223             case SIGNED_INT_16: return find_destination_value_range<Int16>(export_info);
    00224             case SIGNED_INT_32: return find_destination_value_range<Int32>(export_info);
    00225             case IEEE_FLOAT_32: return find_destination_value_range<float>(export_info);
    00226             case IEEE_FLOAT_64: return find_destination_value_range<double>(export_info);
    00227             default:
    00228                 vigra_fail("vigra_ext::detail::find_destination_value_range: not reached");
    00229                 return range_t(0.0, 0.0); // NOT REACHED
    00230             }
    00231         }
    00232     } // end namespace detail
    00233 } // end namespace vigra
    00234
    00235
    00236 #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 Nov 6 2012)