subsamplepgm.cc

 1 const char *help = "\
 2 progname: subsamplepgm.cc\n\
 3 code2html: This program reads a pgm image and subsample it.\n\
 4 version: Torch3 vision2.0, 2004-2005\n\
 5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
 6 
 7 #include "ImageGray.h"
 8 #include "ipSubsample.h"
 9 #include "ipScale_yx.h"
10 #include "DiskXFile.h"
11 #include "CmdLine.h"
12 
13 using namespace Torch;
14 
15 int main(int argc, char **argv)
16 {
17 	char *image_filename;
18 	int width_out;
19 	int height_out;
20 	bool verbose;
21 	bool yx;
22   
23   	
24 	CmdLine cmd;
25 	cmd.setBOption("write log", false);
26   	cmd.info(help);
27   	cmd.addText("\nArguments:");
28   	cmd.addSCmdArg("image filename", &image_filename, "image filename");
29   	cmd.addICmdArg("new width", &width_out, "new width");
30   	cmd.addICmdArg("new height", &height_out, "new height");
31   	cmd.addText("\nOptions:");
32   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
33   	cmd.addBCmdOption("-yx", &yx, false, "use yx subsampling");
34 	cmd.read(argc, argv);
35 
36 
37   	DiskXFile *image_file = NULL;
38 	Image *image_in = NULL;
39 	Image *image_out = NULL;
40 
41 	image_in = new ImageGray();
42 	image_in->setBOption("verbose", verbose);
43 	
44 	image_file = new DiskXFile(image_filename, "r");
45 	image_in->loadXFile(image_file);
46 	delete image_file;
47 
48 	if(verbose)
49 	{
50 		print("Image info:\n");
51 		print("   width = %d\n", image_in->width);
52 		print("   height = %d\n", image_in->height);
53 		print("   format = %s (%d)\n", image_in->coding, image_in->n_planes);
54 	}
55 
56 	ipCore *submachine = NULL;
57 
58 	if(yx) submachine = new ipScale_yx(image_in->width, image_in->height, "gray", width_out, height_out);
59 	else submachine = new ipSubsample(image_in->width, image_in->height, "gray", width_out, height_out);
60 	submachine->setBOption("verbose", verbose);
61 	submachine->process(image_in);
62 	
63 	image_out = new ImageGray();
64 	image_out->setBOption("verbose", verbose);
65 
66 	image_out->copyFrom(width_out, height_out, submachine->seq_out->frames[0], "gray");
67 	
68 	image_file = new DiskXFile("subsample.pgm", "w");
69 	image_out->saveXFile(image_file);
70 	delete image_file;
71 
72 	delete submachine;
73 	delete image_out;
74 	delete image_in;
75 
76 	return(0);
77 }