ppmdraw.cc

 1 const char *help = "\
 2 progname: ppmdraw.cc\n\
 3 code2html: This program reads a ppm image and draws on it.\n\
 4 version: Torch3 vision2.0, 2004-2005\n\
 5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
 6 
 7 #include "ImageRgb.h"
 8 #include "Rectangle2D.h"
 9 #include "DiskXFile.h"
10 #include "CmdLine.h"
11 
12 using namespace Torch;
13 
14 int main(int argc, char **argv)
15 {
16 	char *image_filename;
17 	char *color_name;
18 	bool verbose;
19   
20 	// Construct the command line
21   	CmdLine cmd;
22 	cmd.setBOption("write log", false);
23 	
24   	// Put the help line at the beginning
25   	cmd.info(help);
26 
27   	cmd.addText("\nArguments:");
28   	cmd.addSCmdArg("image filename", &image_filename, "image filename");
29   	cmd.addText("\nOptions:");
30   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
31   	cmd.addSCmdOption("-colorname", &color_name, "green", "color name");
32 
33 	cmd.read(argc, argv);
34 
35 
36   	DiskXFile *image_file = NULL;
37 	Image *image = NULL;
38 
39 	image = new ImageRgb();
40 	image->setBOption("verbose", verbose);
41 	
42 	image_file = new DiskXFile(image_filename, "r");
43 	image->loadXFile(image_file);
44 	delete image_file;
45 
46 	if(verbose)
47 	{
48 		print("Image info:\n");
49 		print("   width = %d\n", image->width);
50 		print("   height = %d\n", image->height);
51 		print("   format = %s (%d)\n", image->coding, image->n_planes);
52 	}
53 
54 	if((image->width < 100) || (image->height < 100))
55 	{
56 		print("Image too small to draw in it.");
57 
58 		delete image_file;
59 		delete image;
60 
61 		return 1;
62 	}
63 	
64 	//
65 	Point2D A(10,10);
66 	Point2D B(100,10);
67 	Point2D C(100,60);
68 	Point2D D(10,60);
69 
70 	Rectangle2D r1(A, B, C, D);
71 	r1.draw(image, red);
72 
73 	//
74 	A.reset(100,60);
75 	B.reset(200,60);
76 	C.reset(200,100);
77 	D.reset(100,100);
78 	
79 	Rectangle2D r2(A, B, C, D);
80 	r2.draw(image, color_name);
81 	
82 	image_file = new DiskXFile("test.ppm", "w");
83 	image->saveXFile(image_file);
84 	delete image_file;
85 	delete image;
86 
87 	return(0);
88 }