#include #include #include #include #include "movie.h" #include "putable.h" using namespace std; movie::movie(const picture& begin, const picture& end, int initial_nframes, int initial_delay) : nframes(initial_nframes), delay(initial_delay), v(nframes) { //Dissolve the begin picture into the end picture. for (int frame = 0; frame < nframes; ++frame) { v[frame].nframes = nframes; v[frame].frameno = frame; for (size_t x = 0; x < picture::width; ++x) { for (size_t y = 0; y <= picture::height; ++y) { for (size_t color = 0; color < 3; ++color) { const int oldp = begin.a[x][y][color]; const int newp = end.a[x][y][color]; v[frame].a[x][y][color] = oldp + (newp-oldp) * frame/(nframes-1); } } } } } movie& operator<<(movie& m, const putable& p) { for (vector::iterator it = m.v.begin(); it != m.v.end(); ++it){ *it << p; } return m; } void movie::write(ostream& ost, bool direction) const { system("rm -f temp3"); ofstream ofs("temp3"); if (!ofs) { cerr << "couldn't open temp3\n"; exit(EXIT_FAILURE); } if (direction) { //forward for (int frame = 0; frame < nframes; ++frame) { ofs << v[frame]; } } else { //backward for (int frame = nframes - 1; frame >= 0; --frame) { ofs << v[frame]; } } ofs.close(); ostringstream os; os << "gifsicle-1.44/src/gifsicle " //<< "--colors 256 " << "--delay=" << delay << " " << "--multifile " << "--optimize=2 " << "--verbose " << "< temp3 " << "> temp4"; system(os.str().c_str()); ifstream ifs("temp4"); if (!ifs) { cerr << "couldn't open temp4\n"; exit(EXIT_FAILURE); } char c; while (ifs.get(c)) { ost.put(c); } ifs.close(); system("rm temp3 temp4"); } ostream& operator<<(ostream& ost, const movie& m) { m.write(ost, true); return ost; } void movie::write_to_file(const char *outfilename, const char *comment, bool direction) const { { ostringstream os; os << "rm -f " << outfilename; system(os.str().c_str()); } ofstream ofs(outfilename); if (!ofs) { cerr << "couldn't open " << outfilename << "\n"; exit(EXIT_FAILURE); } write(ofs, direction); ofs.close(); if (*comment != '\0') { ostringstream os; os << "gifsicle-1.44/src/gifsicle " << "--batch " //<< "--colors 256 " << "--comment='" << comment << "' " << outfilename; system(os.str().c_str()); } ostringstream os; os << "chmod 444 " << outfilename; system(os.str().c_str()); } void movie::write(const char *outfilename, const char *begin, const char *end) const { write_to_file(outfilename, end, true); string filename = outfilename; const string::size_type pos = filename.rfind(".gif"); if (pos == string::npos) { cerr << "filename " << outfilename << " has no \".gif\".\n"; exit(EXIT_FAILURE); } filename.insert(pos, "_r"); //reverse write_to_file(filename.c_str(), begin, false); }