pgmlist2bindata.cc

  1 const char *help = "\
  2 progname: pgmlist2bindata.cc\n\
  3 code2html: This program creates a bindata file from a list of pgm images.\n\
  4 version: Torch3 vision2.0, 2004\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
  6 
  7 #define NBFILES_MAX 5000
  8 
  9 #include "DiskXFile.h"
 10 #include "FileListCmdOption.h"
 11 #include "ImageGray.h"
 12 #include "CmdLine.h"
 13 
 14 using namespace Torch;
 15 
 16 int main(int argc, char *argv[])
 17 {
 18 	char *filename_out;
 19 	bool norm;
 20 	
 21 	FileListCmdOption filelist("file name", "the list files or one data file");
 22         filelist.isArgument(true);
 23 	
 24 	CmdLine cmd;
 25 	cmd.setBOption("write log", false);
 26 
 27 	cmd.info(help);
 28 	cmd.addText("\nArguments:");
 29 	cmd.addCmdOption(&filelist);
 30 	cmd.addSCmdArg("filename out", &filename_out, "output bindata filename");
 31 	cmd.addText("\nOptions:");
 32 	cmd.addBCmdOption("-norm", &norm, false, "normalize images");
 33 	cmd.read(argc, argv);
 34 
 35 	//
 36 	print(" + n_filenames = %d\n", filelist.n_files);
 37 	for(int i = 0 ; i < filelist.n_files ; i++)
 38 		print("   filename[%d] = %s\n", i, filelist.file_names[i]);
 39 
 40   	if(filelist.n_files > NBFILES_MAX)
 41 	{
 42 		error("Cannot merge more than %d files", NBFILES_MAX);
 43 		
 44 		return 1;		
 45 	}
 46         
 47 	ImageGray *grayimage = NULL;
 48 	DiskXFile *fileout = NULL;
 49   
 50   	print("Merging %d files :\n", filelist.n_files);
 51 
 52   	fileout = new DiskXFile(filename_out, "w");
 53   	if(fileout == NULL)
 54 	{
 55       		error("Opening BinData file %s", filename_out);
 56       
 57       		return 1;
 58 	}
 59 
 60   	// Reading headers
 61   	int P = filelist.n_files;
 62 	int n_inputs;
 63 
 64   	for(int i = 0 ; i < filelist.n_files ; i++)
 65 	{		
 66 		grayimage = new ImageGray(filelist.file_names[i]);
 67 
 68 		print("Reading image file (%s)\n", filelist.file_names[i]);
 69 		print("   width = %d\n", grayimage->width);
 70 		print("   height = %d\n", grayimage->height);
 71 
 72 		if(i == 0)
 73 			n_inputs = grayimage->width * grayimage->height;
 74 		else
 75 		{
 76 			if(n_inputs != grayimage->width * grayimage->height)
 77 			{
 78 				delete fileout;
 79 				delete grayimage;
 80 
 81 				error("Files does not have the same size");
 82 
 83 				return 1; 				  
 84 			}
 85 		}		
 86 		
 87 		delete grayimage;
 88 		grayimage = NULL;
 89 	}
 90   
 91 	fileout->write(&P, sizeof(int), 1);
 92 	fileout->write(&n_inputs, sizeof(int), 1);
 93 
 94   	print("\n");  
 95   	print("Writing bindata file :\n");  
 96   	print("   n_inputs : %d\n", n_inputs);
 97   	print("   n_patterns : %d\n", P);  
 98         
 99 	
100   	for(int i = 0 ; i < filelist.n_files ; i++)
101 	{		
102 		grayimage = new ImageGray(filelist.file_names[i]);
103 
104 		if(norm)
105 		{
106 			for(int j = 0 ; j < n_inputs ; j++)
107 			{
108 				real z_ = grayimage->data[j];
109 
110 				z_ /= 255.0;
111 
112 				int a = (int) (z_ * 255.0);
113 				int b = (int) grayimage->data[j];
114 				if(a != b) 
115 					error("%d != %d", a, b);
116 
117 				grayimage->data[j] = z_;
118 			}
119 		}
120 		
121 		fileout->write(grayimage->data, sizeof(real), n_inputs);
122 
123 		delete grayimage;
124 		grayimage = NULL;
125 	}
126 	
127 	delete fileout;
128 
129 	return 0;
130 }
131