readbindata.cc

 1 const char *help = "\
 2 progname: readbindata.cc\n\
 3 code2html: This program reads a bindata file (Torch binary dataset format).\n\
 4 version: Torch3 vision2.0, 2003-2005\n\
 5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
 6 
 7 #include "DiskXFile.h"
 8 #include "CmdLine.h"
 9 
10 using namespace Torch;
11 
12 int main(int argc, char *argv[])
13 {
14 	char *filename;
15 	bool verbose;
16 	bool brackets;
17 
18 	CmdLine cmd;
19 	cmd.setBOption("write log", false);
20 
21 	//
22 	cmd.info(help);
23 	cmd.addText("\nArguments:");
24 	cmd.addSCmdArg("filename", &filename, "image file in");
25 	cmd.addText("\nOptions:");
26 	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
27 	cmd.addBCmdOption("-brackets", &brackets, false, "print brackets");
28 	cmd.read(argc, argv);
29 	
30 	//
31 	int n_inputs;
32 	int n_patterns;
33 	DiskXFile *pf_in;
34 
35 	pf_in = new DiskXFile(filename, "r");
36 	if((pf_in == NULL) || (pf_in->is_opened == false))
37 	{
38       		error("Opening bindata file %s", filename);
39       
40       		return 0;
41 	}
42 	
43 	pf_in->read(&n_patterns, sizeof(int), 1);
44 	pf_in->read(&n_inputs, sizeof(int), 1);
45 
46 	if(!verbose)
47 	{
48 		print("Reading bindata file (%s)\n", filename);
49 		print("   n_inputs = %d\n", n_inputs);
50 		print("   n_patterns = %d\n", n_patterns);  
51 	}
52 
53 	real data;
54 
55 	if(verbose)
56 	{
57 		for(int j = 0 ; j <  n_patterns ; j++)
58 		{
59 			if(brackets) print("[%06d] = [ ", j);
60 		
61 			for(int i = 0 ; i <  n_inputs ; i++)
62 			{
63 				pf_in->read(&data, sizeof(real), 1);   
64 				print("%g ", data);
65 			}
66 	
67 			if(brackets) print("]\n");
68 			else print("\n");
69 		}	
70 		delete pf_in;
71 	}
72 
73 	return 0;
74 }