Move the sources to trunk
[opencv] / apps / vmdemotk / NewTclCommands.cpp
1 #include "VMDemoTk.h"
2 #include "NewTclCommands.h"
3 #include "DataandMorphing.h"
4 #include "Globals.h"
5 #include "CommandConcatenation.h"
6 #include <highgui.h>
7
8
9 int TKVMDemo_Setposition( ClientData cd, Tcl_Interp *interp,
10                           int argc, char **argv)
11 {
12     //Tk_Window win1 , win2 , win3;
13     Tk_Window mainWindow = Tk_MainWindow(interp);
14     Tk_MoveToplevelWindow(mainWindow,300,300);
15     return 0;
16 }
17
18 int TKVMDemo_fileopen( ClientData cd, Tcl_Interp *interp,
19                       int argc, char **argv)
20 {
21     //Open file dialog and sets path to interp.result
22     Tcl_Eval(interp,"tk_getOpenFile");
23     if (!(*(interp->result))) return 2;
24     HaveMorph   = false;
25     
26     //Initialising dynamic strings for storing data
27     Tcl_DString s, dir, inifiletoken, file;
28     Tcl_DStringInit(&s);
29     Tcl_DStringInit(&dir);
30     Tcl_DStringInit(&inifiletoken);
31     Tcl_DStringInit(&file);
32     
33     //Saving path to the file
34     Tcl_DStringGetResult(interp, &file);
35     
36     //Saving directory path
37     Tcl_Eval(interp, CommandConcatenation(&s,"%s%s", "file dirname ", file.string));
38     Tcl_DStringGetResult(interp, &dir);
39     
40     // Running package for parsing .ini files
41     Tcl_EvalFile (interp, "iniparse.tcl");
42     
43     
44     //Removing scanlines
45     Tcl_Eval(interp, ".bottom.cb1 deselect");
46     Tcl_Eval(interp, "CShowScanlines");
47     
48     // disable slider and checkbutton
49     Tcl_Eval(interp, ".bottom.cb1 configure -state disabled");
50     Tcl_Eval(interp, ".s configure -state disabled");
51     
52     // Opens project file
53     Tcl_Eval (interp, CommandConcatenation(&s,"%s%s", "iniparse:openfile ", 
54         file.string));
55     
56     // Saving opend file Tcl token
57     Tcl_DStringAppend(&inifiletoken , (*interp).result,-1);
58     
59     // Saving token for opened ini file to a Tcl variable
60     CommandConcatenation(&s,"%s%s", "set var ", inifiletoken.string);
61     Tcl_Eval (interp, s.string);
62     Tcl_DStringFree(&s);
63     
64     //Reading 1-st image from a file
65     CommandConcatenation(&s,"%s%s%s", "readini ", inifiletoken.string,
66         " Pair1 Image1" );
67     Tcl_Eval (interp, s.string);
68     if (!(*(interp->result))) return 2;
69     
70     CommandConcatenation(&s,
71         "%s%s%s", 
72         dir.string,"/", (*interp).result);
73     char temp[1000];
74     sprintf(temp, "%s", s.string);
75     while( temp[strlen(temp) - 1] <= ' ' && temp[strlen(temp) - 1] > 0 )
76         temp[strlen(temp) - 1] = 0;
77     if( datal )
78         cvReleaseImage( &datal );
79     datal =  cvvLoadImage( temp );
80     
81     Tcl_DStringFree(&s);
82     if( !datal ) return 2;
83     
84     //Reading 2-d image from a file
85     CommandConcatenation(&s,"%s%s%s", "readini ", inifiletoken.string,
86         " Pair1 Image2" );
87     Tcl_Eval (interp, s.string);
88     if (!(*(interp->result))) return 2;
89     
90     CommandConcatenation(&s,
91         "%s%s%s", 
92         dir.string,"/", (*interp).result);
93     
94     sprintf(temp, "%s", s.string);
95     while( temp[strlen(temp) - 1] <= ' ' && temp[strlen(temp) - 1] > 0 )
96         temp[strlen(temp) - 1] = 0;
97     if( datar )
98         cvReleaseImage( &datar );
99     datar =  cvvLoadImage( temp );
100     
101     Tcl_DStringFree(&s);
102     if( !datar ) return 2;
103     
104     // Loading images data and fundanental matrix 
105     FreeMemory();
106     if(loaddata( interp)) return 2;
107     
108     MemoryAllocation();
109     
110     Tcl_Eval(interp, "CSetpos");
111     
112     //Displaying source images
113     cvvShowImage( "left", datal );
114     cvvShowImage( "right", datar );
115     
116     Tcl_Eval(interp, ".s configure -state active");
117     Tcl_Eval(interp, ".s set 0.0");
118     Tcl_Eval(interp, ".bottom.cb1 configure -state normal");
119     Tcl_Eval (interp, CommandConcatenation(&s,"%s%d%s%d%s", 
120         ".top.l2 configure -text {",
121         imgsize.width,"X ", imgsize.height,
122         "}"));
123     return 0;
124 }
125
126
127 int TKVMDemo_scale(ClientData cd, Tcl_Interp *interp,
128                    int argc, char **argv)
129 {
130     Tcl_Eval(interp, ".s get");
131     float sk = (float)atof((*interp).result);
132     static float previosposition = 0.0;
133     
134     if ((sk-previosposition)*(sk-previosposition)>= 0.01)
135     {
136         OnMakeMorphing(sk, interp);
137         
138         previosposition=sk;
139         Tcl_DString setposition;
140         Tcl_DStringInit(&setposition);
141         char position = (char)sk;
142         Tcl_DStringAppend(&setposition,".s set",-1);
143         Tcl_DStringAppend(&setposition,&position,-1);
144         Tcl_Eval(interp, setposition.string);
145     }
146     
147     return 0;
148 }
149
150
151 int TKVMDemo_ShowScanlines(ClientData cd, Tcl_Interp *interp,
152                            int argc, char **argv)
153 {
154     Tcl_Eval(interp, "set chb");
155     int chb = atoi(interp->result);
156     Tcl_DString drawlinecommand;
157     Tcl_DStringInit(&drawlinecommand);
158     int     n;
159     int     x1,y1,x2,y2;
160     
161     if (numScanlines>0 && chb ) 
162     {
163         for (n = 0; n < numScanlines; n +=10) 
164         {
165             x1 = (scanlines_2)[n * 4    ];
166             y1 = (scanlines_2)[n * 4 + 1];
167             x2 = (scanlines_2)[n * 4 + 2];
168             y2 = (scanlines_2)[n * 4 + 3];
169             
170             CommandConcatenation(&drawlinecommand,"%s%s%d%d%d%d%s",
171                 "set numlines1 [ ",
172                 ".win1.c create line ",
173                 x1, y1, x2, y2,
174                 "]"); 
175             Tcl_Eval(interp, drawlinecommand.string);
176             Tcl_DStringFree(&drawlinecommand);
177             
178             x1 = (scanlines_1)[n * 4    ];
179             y1 = (scanlines_1)[n * 4 + 1];
180             x2 = (scanlines_1)[n * 4 + 2];
181             y2 = (scanlines_1)[n * 4 + 3];
182             
183             CommandConcatenation(&drawlinecommand,"%s%d%d%d%d",
184                 ".win2.c create line ",
185                 x1, y1, x2, y2
186                 ); 
187             
188             Tcl_Eval(interp, drawlinecommand.string);
189             Tcl_DStringFree(&drawlinecommand);
190             
191             x1 = (scanlines_a)[n * 4    ];
192             y1 = (scanlines_a)[n * 4 + 1];
193             x2 = (scanlines_a)[n * 4 + 2];
194             y2 = (scanlines_a)[n * 4 + 3];
195             CommandConcatenation(&drawlinecommand,"%s%d%d%d%d",
196                 ".win3.c create line ",
197                 x1, y1, x2, y2
198                 ); 
199             
200             Tcl_Eval(interp, drawlinecommand.string);
201             
202             Tcl_DStringFree(&drawlinecommand);
203         }
204         return 0;
205     }
206     
207     if (numScanlines>0 && !chb ) 
208     {
209         Tcl_Eval(interp,"set numlines1");
210         int numlines1 = atoi(interp->result);
211         
212         Tcl_DString deletelinecommand;
213         Tcl_DStringInit(&deletelinecommand);
214         
215         for (n = 0; n < numScanlines; n +=10) 
216         {
217             int m = n/10;
218             CommandConcatenation(&deletelinecommand,"%s%d%d%d%d",
219                 ".win1.c delete ",
220                 numlines1 - m
221                 ); 
222             
223             Tcl_Eval(interp, deletelinecommand.string);
224             Tcl_DStringFree(&deletelinecommand);
225             
226             CommandConcatenation(&deletelinecommand,"%s%d%d%d%d",
227                 ".win2.c delete ",
228                 numlines1 - m
229                 ); 
230             
231             Tcl_Eval(interp, deletelinecommand.string);
232             Tcl_DStringFree(&deletelinecommand);
233             
234             CommandConcatenation(&deletelinecommand,"%s%d%d%d%d",
235                 ".win3.c delete ",
236                 numlines1 - m
237                 ); 
238             
239             Tcl_Eval(interp, deletelinecommand.string);
240             Tcl_DStringFree(&deletelinecommand);
241         }
242     }
243     
244     return 0;
245 }
246
247 ////////////////////////////////////////////////////////////////////////////
248 void    OnExit(ClientData clientData)
249 {
250     FreeMemory();
251 }
252
253