Exemple #1
0
inline void boost::gil::rescale(const SrcView& src, const DstView& dst, const Filter& filter)
{
  typedef typename SrcView::value_type src_pixel_t;
  typedef typename gil::detail::create_accum_pixel_type<src_pixel_t>::type accum_pixel_t;

  // construct weights tables

  detail::weight_table horizontal_weights;
  horizontal_weights.reset(filter, src.width(), dst.width());

  detail::weight_table vertical_weights;
  vertical_weights.reset(filter, src.height(), dst.height());

  // allocate intermediary pixels row

  std::vector<accum_pixel_t> intermediate_row( src.width() );

  typedef typename DstView::coord_t coord_t;
  coord_t const src_width  = src.width();
  coord_t const dst_height = dst.height();

  for (coord_t y=0; y<dst_height; y++)
  {
    // create the intermediate row by vertically sampling the source image pixels

    detail::weight_table::const_iterator const vtable_begin = vertical_weights.begin() + y;
    detail::weight_table::const_iterator const vtable_end   = vtable_begin + 1;

    for (coord_t x=0; x<src_width; x++)
    {
      detail::rescale_line<accum_pixel_t>(
          src.col_begin(x)
        , intermediate_row.begin() + x
        , vtable_begin
        , vtable_end
        );
    }

    // scale horizontally the intermediate row into the destination row

    detail::weight_table::const_iterator const htable_begin = horizontal_weights.begin();
    detail::weight_table::const_iterator const htable_end   = horizontal_weights.end();

    detail::rescale_line<accum_pixel_t>(
        intermediate_row.begin()
      , dst.row_begin(y)
      , htable_begin
      , htable_end
      );
  }
}
void blur(const SrcView& src, const DstView& dst) {
    typedef typename channel_type<DstView>::type dst_channel_t;

    int max_x = src.width() - 1;
    int width = src.width();
    for (int y=1; y<src.height()-1; ++y) {
        typename SrcView::x_iterator src_it = src.row_begin(y);
        typename DstView::x_iterator dst_it = dst.row_begin(y);

        for (int x=1; x < max_x; ++x) {
            for (int c=0; c< num_channels<rgb8_view_t>::value; ++c) {
            	dst_it[x][c] =
            		src_it[x-1-width][c] / 16 +
            		src_it[x-width][c] / 8 +
            		src_it[x-1-width][c] / 16 +

            		src_it[x-1][c] / 8 +
            		src_it[x][c] / 4 +
            		src_it[x-1][c] / 8 +

            		src_it[x-1+width][c] / 16 +
            		src_it[x+width][c] / 8 +
            		src_it[x-1+width][c] / 16;
            }
        }
    }
}
void sobel_xy(const SrcView& src, const DstView& dst) {
    typedef typename channel_type<DstView>::type dst_channel_t;

    int max_x = src.width() - 1;
    int width = src.width();
    for (int y=1; y<src.height()-1; ++y) {
        typename SrcView::x_iterator src_it = src.row_begin(y);
        typename DstView::x_iterator dst_it = dst.row_begin(y);

        for (int x=1; x < max_x; ++x) {
            for (int c=0; c< num_channels<rgb8_view_t>::value; ++c) {
            	dst_channel_t srcPlus1 = src_it[x+width][c]; //TODO should be replaced by line length
            	dst_channel_t srcMinus1 = src_it[x-width][c];
            	dst_channel_t sobelY = (srcPlus1 > srcMinus1) ?
            			srcPlus1 - srcMinus1 :
						srcMinus1 - srcPlus1;
            	srcPlus1 = src_it[x+1][c];
            	srcMinus1 = src_it[x-1][c];
            	dst_channel_t sobelX = (srcPlus1 > srcMinus1) ?
            			srcPlus1 - srcMinus1 :
						srcMinus1 - srcPlus1;
            	dst_it[x][c] = max(sobelX, sobelY);
            }
        }
    }
}
Exemple #4
0
inline void boost::gil::rescale_rows(const SrcView& src, const DstView& dst, const Filter& filter)
{
  typedef typename SrcView::value_type src_pixel_t;
  typedef typename gil::detail::create_accum_pixel_type<src_pixel_t>::type accum_pixel_t;

  if (src.width() != dst.width())
  {
    throw std::runtime_error("rescale_rows: width of source and destination views must match");
  }

  detail::weight_table weights;
  weights.reset(filter, src.height(), dst.height());

  detail::weight_table::const_iterator const table_begin = weights.begin();
  detail::weight_table::const_iterator const table_end   = weights.end();

  typedef typename DstView::coord_t coord_t;
  coord_t const width = dst.width();

  for (coord_t x=0; x<width; x++)
  {
    detail::rescale_line<accum_pixel_t>(
        src.col_begin(x)
      , dst.col_begin(x)
      , table_begin
      , table_end
      );
  }
}
Exemple #5
0
bool sample(nearest_neighbor_sampler, const SrcView& src, const point2<F>& p, DstP& result) {
    typename SrcView::point_t center(iround(p));
    if (center.x>=0 && center.y>=0 && center.x<src.width() && center.y<src.height()) {
        result=src(center.x,center.y);
        return true;
    }
    return false;
}
Exemple #6
0
bool sample(nearest_neighbor_sampler, const SrcView& src, const point2<F>& p, DstP& result) {
	///modif Adrien pour 64 bits : ptrdiff au lieu d'int
    point2<std::ptrdiff_t> center(iround(p));
    if (center.x>=0 && center.y>=0 && center.x<src.width() && center.y<src.height()) {
        result=src(center.x,center.y);
        return true;
    }
    return false;
}
Exemple #7
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;
}
void x_gradient(const SrcView& src, const DstView& dst) {
    typedef typename channel_type<DstView>::type dst_channel_t;

    for (int y=0; y<src.height(); ++y) {
        typename SrcView::x_iterator src_it = src.row_begin(y);
        typename DstView::x_iterator dst_it = dst.row_begin(y);

        for (int x=1; x<src.width()-1; ++x)
            static_transform(src_it[x-1], src_it[x+1], dst_it[x], 
                               halfdiff_cast_channels<dst_channel_t>());
    }
}
void x_gradient(const SrcView& src, const DstView& dst) {

    int max_x = src.width()-1;
    for (int y=0; y<src.height(); ++y) {
        typename SrcView::x_iterator src_it = src.row_begin(y);
        typename DstView::x_iterator dst_it = dst.row_begin(y);

        for (int x=1; x < max_x; ++x) {
            for (int c=0; c< num_channels<rgb8_view_t>::value; ++c) {
            	dst_it[x][c] = (src_it[x+1][c] - src_it[x-1][c])/2;
            }
        }
    }
}
void grayToRgbView(const SrcView& src, const DstView& dst) {
    typedef typename channel_type<DstView>::type dst_channel_t;

    int width = src.width();
    for (int y=0; y< src.height(); ++y) {
        typename SrcView::x_iterator src_it = src.row_begin(y);
        typename DstView::x_iterator dst_it = dst.row_begin(y);

        for (int x=0; x < width; ++x) {
        	dst_channel_t grayValue = (dst_channel_t) src_it[x];
            for (int c=0; c< num_channels<rgb8_view_t>::value; ++c) {
            	dst_it[x][c] = grayValue;
            }
        }
    }
}
void rgbToGrayView(const SrcView& src, const DstView& dst) {
    typedef typename channel_type<DstView>::type dst_channel_t;

    int width = src.width();
    for (int y=0; y< src.height(); ++y) {
        typename SrcView::x_iterator src_it = src.row_begin(y);
        typename DstView::x_iterator dst_it = dst.row_begin(y);

        for (int x=0; x < width; ++x) {
        	int combination = 0;
            for (int c=0; c< num_channels<rgb8_view_t>::value; ++c) {
            	combination += src_it[x][c] * RGB_TO_GRAY_INT[c];
            }
        	dst_it[x] = (dst_channel_t) (combination / 100);
        }
    }
}
void sobel_x(const SrcView& src, const DstView& dst) {
    typedef typename channel_type<DstView>::type dst_channel_t;

    int max_x = src.width()-1;
    for (int y=0; y<src.height(); ++y) {
        typename SrcView::x_iterator src_it = src.row_begin(y);
        typename DstView::x_iterator dst_it = dst.row_begin(y);

        for (int x=1; x < max_x; ++x) {
            for (int c=0; c< num_channels<rgb8_view_t>::value; ++c) {
            	dst_channel_t srcPlus1 = src_it[x+1][c];
            	dst_channel_t srcMinus1 = src_it[x-1][c];
            	dst_it[x][c] = (srcPlus1 > srcMinus1) ?
            			srcPlus1 - srcMinus1 :
						srcMinus1 - srcPlus1;
            }
        }
    }
}