croppgm.cc

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