2012年6月3日 星期日

Qt and GIL(generic image library) : 03--Design a generic absdiff

This time we will design an almost generic algorithm to calculate the absolute different between two views.

struct abs_func
{      
    template <typename T>
    T operator()(T const &in1, T const &in2) const {

        return absdiff_pixel(in1, in2); //same as the adbdiff_pixel in post 02
    }
};

/* calculate the absolute different between two views
 * This function would transform the value type(int, char and so on)
 * of sources into the the value type of the destination type.The range
 * of the sources would be scale to the range of destination type too,
 * it would not transform or scaling if the source and destination are
 * the same type.
 *
 * @parame
 * src_1 : first view or image
 * src_2 : second view or image
 * dst   : destination view or image
 *
 * exception : depend on the template parameter
 */
template<typename SrcView1, typename SrcView2, typename DstView>
void absdiff(SrcView1 const &src1, SrcView2 const &src2, DstView const &dst)
{           
    //convert the range of the view to the range of the DstView
    auto scale_src1 = boost::gil::color_converted_view<typename DstView::value_type>(src1);
    auto scale_src2 = boost::gil::color_converted_view<typename DstView::value_type>(src2);
    if(src1.width() == src2.width() && src1.height() == src2.height())
    {
        for (int y = 0; y != src1.height(); ++y) {
            auto src1_it = scale_src1.row_begin(y);
            auto src2_it = scale_src2.row_begin(y);
            auto dst_it = dst.row_begin(y);

            for (int x = 0; x != src1.width(); ++x)
                boost::gil::static_transform(src1_it[x], src2_it[x], dst_it[x], abs_func());
        }
    }
}
I say this algorithm is "almost generic" because I don't it could take care of the bit_aligned_pixel or not, I will post the answer after I find out.

沒有留言:

張貼留言