Add more test images.
[seam-carving.git] / src / seam_main.cc
blob4f9c8880e52fd1802e8f6f2e35f9cab207350034
1 #include <iostream>
2 #include <stack>
3 #include <vector>
5 #include <boost/lexical_cast.hpp>
7 #include <mln/border/duplicate.hh>
8 #include <mln/io/ppm/save.hh>
9 #include <mln/level/fill.hh>
11 #include "all.hh"
13 using namespace mln;
14 using namespace mln::value;
16 static inline
17 void
18 add_bias (image2d<float> nrg, const char* path)
20 image2d<rgb8> tmp = load (path);
21 assert (tmp.domain () == nrg.domain ());
22 image2d<rgb8>::fwd_piter p (tmp.domain ());
24 for_all (p)
25 nrg (p) += tmp (p).red () - tmp (p).green ();
28 /// \file Find a vertical seam in an image.
30 int main (int argc, char** argv)
32 if (argc != 5 && argc != 6)
34 std::cerr << "usage: " << *argv
35 << " N in.ppm carved.ppm seams.ppm [bias.ppm]\n"
36 "where N is an integer > 0: the number of seams to carve.\n";
37 return 1;
40 unsigned nseam = 1;
42 try {
43 nseam = boost::lexical_cast<unsigned> (argv[1]);
45 catch (const boost::bad_lexical_cast&)
47 std::cerr << "invalid 1st argument" << std::endl;
48 return 2;
51 std::cerr << "initializing ... ";
52 image2d<rgb8> img = load (argv[2]);
53 border::duplicate (img);
54 image2d<float> nrg = get_nrg (img);
55 if (argc >= 6)
56 add_bias (nrg, argv[5]);
57 std::stack<std::vector<unsigned> > seams;
59 for (unsigned i = 0; i < nseam; ++i)
61 std::cerr << (i + 1) << "..";
62 seams.push (find_vertical_seam (nrg));
63 img = carve_vertical_seam (img, seams.top ());
64 nrg = carve_vertical_seam (nrg, seams.top ());
65 adjust_vertical_nrg (img, nrg, seams.top ());
66 border::duplicate (img);
67 image2d<float> new_nrg = get_nrg (img);
69 std::cerr << '.';
70 io::ppm::save (img, argv[3]);
71 for (unsigned i = 0; i < nseam; ++i)
73 img = uncarve_vertical_seam (img, seams.top ());
74 seams.pop ();
76 std::cerr << '.';
77 io::ppm::save (img, argv[4]);
78 std::cerr << std::endl;