Exemple #1
0
void down_sample( const SRC& src_view
                  , const DST& dst_view  )
{
    assert( src_view.dimensions() == dst_view.dimensions() );

    // @todo Take care of signed images. Bransform them into unsigned images
    // by adding half the value range to the channels.

    typedef SRC::value_type src_pixel_t;
    typedef DST::value_type dst_pixel_t;
    typedef channel_type<dst_pixel_t>::type dst_channel_t;

    boost::fusion::vector<src_pixel_t,src_pixel_t> minmax = minmax_channel_values<src_pixel_t>( src_view );

    const src_pixel_t& min = at_c<0>( minmax );
    const src_pixel_t& max = at_c<1>( minmax );

    // calculate the diff
    src_pixel_t diff;
    static_for_each( diff, min, max, calc_diff() );


    // sample down
    dst_channel_t dst_max = channel_traits< dst_channel_t >::max_value();

    for ( int y=0; y < src_view.height(); ++y )
    {
        SRC::x_iterator src_it = src_view.row_begin( y );
        DST::x_iterator dst_it = dst_view.row_begin( y );

        for ( int x = 0; x < src_view.width(); ++x )
        {
            typename src_pixel_t::const_reference src = src_it[x];
            typename dst_pixel_t::reference       dst = dst_it[x];

            for ( int i = 0; i < num_channels<src_pixel_t>::type::value; ++i )
            {
                if ( dynamic_at_c( diff, i ) == 0 )
                {
                    dynamic_at_c( dst, i ) = 0;
                }
                else
                {
                    dynamic_at_c( dst, i ) = static_cast<dst_channel_t>( dst_max * ( static_cast<float>( dynamic_at_c( src, i )
                                             - dynamic_at_c( min, i ))
                                             / static_cast<float>( dynamic_at_c( diff, i ))));
                } // else

            } //for

        } //for

    } // for
}