duoppm2avi.cc

  1 const char *help = "\
  2 progname: duoppm2avi.cc\n\
  3 code2html: This program makes a video from 2 sequence of ppm images.\n\
  4 version: Torch3 vision2.0, 2004-2005\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
  6 
  7 #include "ImageRgb.h"
  8 #include "ffmpegVideoFile.h"
  9 #include "rgbRawVideoFile.h"
 10 #include "DiskXFile.h"
 11 #include "FileListCmdOption.h"
 12 #include "CmdLine.h"
 13 
 14 using namespace Torch;
 15 
 16 void compose(int width, int height, unsigned char *pixmap1, unsigned char *pixmap2, unsigned char *imagecomposite)
 17 {
 18 	unsigned char *test = imagecomposite;
 19 	
 20 	for (int h=0;h<height;h++) 
 21 	{
 22 		for (int l=0;l<width*3;l++) 
 23 			*test++ = *pixmap1++;
 24 		for (int l=0;l<width*3;l++)
 25 			*test++ = *pixmap2++;
 26 	}
 27 }
 28 
 29 int main(int argc, char **argv)
 30 {
 31 	int width;
 32 	int height;
 33 	bool verbose;
 34 	int fps;
 35 	int bitrate;
 36 	char *output_filename;
 37 
 38 	FileListCmdOption input_file_list1("filelist 1", "the first list of input files");
 39 	input_file_list1.isArgument(true);
 40 
 41 	FileListCmdOption input_file_list2("filelist 2", "the second list of input files");
 42 	input_file_list2.isArgument(true);
 43 
 44 	// Construct the command line
 45   	CmdLine cmd;
 46 	cmd.setBOption("write log", false);
 47 	
 48   	// Put the help line at the beginning
 49   	cmd.info(help);
 50 
 51   	cmd.addText("\nArguments:");
 52 	cmd.addCmdOption(&input_file_list1);
 53 	cmd.addCmdOption(&input_file_list2);
 54   	cmd.addICmdArg("width", &width, "width");
 55   	cmd.addICmdArg("height", &height, "height");
 56   	cmd.addText("\nOptions:");
 57   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 58   	cmd.addICmdOption("-fps", &fps, 25, "verbose");
 59   	cmd.addSCmdOption("-o", &output_filename, "output.avi", "verbose");
 60   	cmd.addICmdOption("-bitrate", &bitrate, 1000000, "bitrate for encoding");
 61 
 62 	cmd.read(argc, argv);
 63 
 64 	if(input_file_list1.n_files != input_file_list2.n_files) 
 65 		error("List of files should have the same number of lines.");
 66 
 67 	int nframes = input_file_list1.n_files;
 68 
 69 	VideoFile *output = NULL;
 70 	output = new ffmpegVideoFile();
 71 	output->open(output_filename, "w");
 72 	output->setIOption("width", 2 * width);
 73 	output->setIOption("height", height);
 74 	output->setROption("framerate", fps);
 75 	output->setIOption("bitrate", bitrate);
 76 
 77 	Image *imagecomposite = new ImageRgb(2 * width, height);
 78 	Image *image1 = new ImageRgb(width, height);
 79 	Image *image2 = new ImageRgb(width, height);
 80 
 81 	for(int i = 0 ; i < nframes ; i++)
 82 	{   	
 83 		//
 84 		image1->load(input_file_list1.file_names[i]);
 85 		if((image1->width != width) || (image1->height != height))
 86 		{
 87 			warning("error incorrect image size.");
 88 			
 89 			delete image1;
 90 			delete image2;
 91 			delete imagecomposite;
 92 			delete output;
 93 
 94 			return 1;
 95 		}
 96 		 
 97 		//
 98 		image2->load(input_file_list2.file_names[i]);
 99 		if((image2->width != width) || (image2->height != height))
100 		{
101 			warning("error incorrect image size.");
102 			
103 			delete image1;
104 			delete image2;
105 			delete imagecomposite;
106 			delete output;
107 
108 			return 1;
109 		}
110 
111 		//
112 	    	if(verbose) print("r");
113 	
114 		//
115 		compose(width, height, image1->pixmap, image2->pixmap, imagecomposite->pixmap);
116 		if(verbose) print("c");
117 	
118 		//
119 		output->write(imagecomposite->pixmap);
120 		if(verbose) print("w");
121 
122 		real percent = 100.0 * (i+1) / nframes;
123 		print("%03d%\r", (int) percent);
124 	}
125 
126 	print("\n");
127 	
128 	output->close();
129 	delete output;
130 
131 	delete image1;
132 	delete image2;
133 	delete imagecomposite;
134 
135 	return(0);
136 }