Example #1
0
static float gradient(const SrcView& src, const point_t& point, Color channel) {
	point_t pR, pL, pU, pD;

	if (point.x == 0)
		pL = point_t(0, 0);
	else
		pL = point_t(-1, 0);

	if (point.y == 0)
		pD = point_t(0, 0);
	else
		pD = point_t(0, -1);

	auto xRight = src.xy_at(point + point_t(1, 0));
	auto xLeft = src.xy_at(point + pL);

	auto yUp = src.xy_at(point + point_t(0, 1));
	auto yDown = src.xy_at(point + pD);

	auto xx = fabs(get_color(*xRight, channel) - get_color(*xLeft, channel));
	auto yy = fabs(get_color(*yUp, channel) - get_color(*yDown, channel));

	return sqrt((powf(xx, 2) + powf(yy, 2)));
}
Example #2
0
bool sample(bilinear_sampler, const SrcView& src, const point2<F>& p, DstP& result) {
    typedef typename SrcView::value_type SrcP;
    ///modif
    point2<std::ptrdiff_t> p0(ifloor(p)); // the closest integer coordinate top left from p
    point2<F> frac(p.x-p0.x, p.y-p0.y);
    if (p0.x < 0 || p0.y < 0 || p0.x>=src.width() || p0.y>=src.height()) return false;

    pixel<F,devicen_layout_t<num_channels<SrcView>::value> > mp(0);                     // suboptimal
    typename SrcView::xy_locator loc=src.xy_at(p0.x,p0.y);

    if (p0.x+1<src.width()) {
        if (p0.y+1<src.height()) {
            // most common case - inside the image, not on the last row or column
            detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc,      (1-frac.x)*(1-frac.y),mp);
            detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1],   frac.x *(1-frac.y),mp);
            ++loc.y();
            detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc,      (1-frac.x)*   frac.y ,mp);
            detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1],   frac.x *   frac.y ,mp);
        } else {
            // on the last row, but not the bottom-right corner pixel
            detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc,      (1-frac.x),mp);
            detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1],   frac.x ,mp);
        }
    } else {
        if (p0.y+1<src.height()) {
            // on the last column, but not the bottom-right corner pixel
            detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc,      (1-frac.y),mp);
            ++loc.y();
            detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc,         frac.y ,mp);
        } else {
            // the bottom-right corner pixel
            detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc,1,mp);
        }
    }

    // Convert from floating point average value to the source type
    SrcP src_result;
    cast_pixel(mp,src_result);

    color_convert(src_result, result);
    return true;
}