bindata2pgm.cc

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