#include <gtk/gtk.h>
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#define part 10
#define chno 26
static int len,count,tm[2][16];
static GdkPixmap *pixmap = NULL;
float statistic_data(char *src, char *des)                      // Real Core 
Code
{
        int i=0,j=0,ax,ay,bx,by,sigx=0,sigy=0;
        float
xbar,ybar,sigu,sigv=0,sigu2=0,sigv2=0,siguv=0,karl_pearson,karl_pearsony;
        float *u,*v,*u2,*v2,*uv;
        FILE *X,*Y;
        X = fopen (src,"r");                                    // open test 
file
        Y = fopen (des,"r");                                    //open template 
file
        while(!feof(X))                                         //until test 
file ends
{
        fscanf(X,"%d %d  ",&ax,&ay);                            // get x y
        sigx=sigx+ax;                                           // calculate 
sum of all x
        fscanf(Y,"%d %d  ",&bx,&by);                            //      "
        sigy=sigy+bx;                                           //              
"
        i++;
}
        rewind(X);                                              // starting 
file pointer
        rewind(Y);                                              //      "
        xbar=sigx/part;                                         //      mean x 
of test
        ybar=sigy/part;                                         //      mean x 
of template
        u = (float*)calloc (part,sizeof(float));                // create u=x- 
x(bar) table
        v = (float*)calloc (part,sizeof(float));                //      "
        u2 = (float*)calloc (part,sizeof(float));               //      u(square
        v2 = (float*)calloc (part,sizeof(float));               //      v"
        uv = (float*)calloc (part,sizeof(float));               //      uv
        for(i=1;i<=part;i++)                                    // upto all 
chunks
        {
                fscanf(X,"%d %d  ",&ax,&ay);                    // get x y of 
test
                fscanf(Y,"%d %d  ",&bx,&by);                    // get x y of 
template
                *(u+i) = ax-xbar;                               // put u = 
x-x(bar)
                sigu = sigu + *(u+i);                           // sumof all u
                *(v+i) = bx-ybar;                               //      "
                sigv = sigv + *(v+i);                           //              
"
                *(u2+i) = (*(u+i)) * (*(u+i));                  // calc 
u(square)
                sigu2 = sigu2 + *(u2+i);                        // sum of 
u(square)
                *(v2+i) = (*(v+i)) * (*(v+i));                  //      "
                sigv2 = sigv2 + *(v2+i);                        //              
"
                *(uv+i) = (*(u+i)) * (*(v+i));                  // calc u*v
                siguv = siguv + *(uv+i);                        //sum of u*v
        }
                karl_pearson = siguv/(sqrt(sigu2) * sqrt(sigv2));       // 
calculate
karlpearson
                printf("\n%f is the karl pearson coefficient",karl_pearson);
        while(!feof(X))                         //similar as above but this is 
for y axis
{
        fscanf(X,"%d %d  ",&ay,&ax);
        sigx=sigx+ax;
        fscanf(Y,"%d %d  ",&by,&bx);
        sigy=sigy+bx;
        i++;
}
        rewind(X);
        rewind(Y);
        xbar=sigx/part;
        ybar=sigy/part;
        u = (float*)calloc (part,sizeof(float));
        v = (float*)calloc (part,sizeof(float));
        u2 = (float*)calloc (part,sizeof(float));
        v2 = (float*)calloc (part,sizeof(float));
        uv = (float*)calloc (part,sizeof(float));
        for(i=1;i<=part;i++)
        {
                fscanf(X,"%d %d  ",&ay,&ax);
                fscanf(Y,"%d %d  ",&by,&bx);
                *(u+i) = ax-xbar;
                sigu = sigu + *(u+i);
                *(v+i) = bx-ybar;
                sigv = sigv + *(v+i);
                *(u2+i) = (*(u+i)) * (*(u+i));
                sigu2 = sigu2 + *(u2+i);
                *(v2+i) = (*(v+i)) * (*(v+i));
                sigv2 = sigv2 + *(v2+i);
                *(uv+i) = (*(u+i)) * (*(v+i));
                siguv = siguv + *(uv+i);
        }
                karl_pearsony = siguv/(sqrt(sigu2) * sqrt(sigv2));
                printf("\n%f is the karl pearson coefficient",karl_pearsony);
        fclose(X);
        fclose(Y);
        free(u);                        // free all pointer
        free(v);
        free(u2);
        free(v2);
        free(uv);
        return karl_pearson+karl_pearsony;      //return totoal karlpearson
}
void cleararea (GtkWidget *saw, gpointer data)          // this is not working
i need to review in more detial

        {
        printf("\nuHUHUHU");
GtkWidget *draw = GTK_WIDGET(data);
gdk_draw_rectangle (pixmap,draw->style->white_gc,FALSE,0,0,draw-
>allocation.width,draw->allocation.height);
                 gtk_widget_queue_draw_area 
(draw,0,0,draw->allocation.width,draw-
>allocation.height);

printf("\n%d",draw->allocation.width);
        }
gboolean configure_pixmap (GtkWidget *widget, gpointer data)                    
//
create pixmap and adjust to drawing area
{
        len = 0;
        count=0;
if (pixmap)
    g_object_unref(pixmap);
    pixmap = gdk_pixmap_new(widget->window,widget-
>allocation.width,widget->allocation.height,-1);
  gdk_draw_rectangle (pixmap,widget->style->white_gc,TRUE,0,0,widget-
>allocation.width,widget->allocation.height);
  return TRUE;
}
gboolean draw_update(GtkWidget *widget)                                         
// update drawing in
pixmap
{
        gdk_draw_drawable (widget->window,widget->style-
>fg_gc[GTK_WIDGET_STATE (widget)],pixmap,0,0,0,0,widget-
>allocation.width, widget->allocation.height);
        return TRUE;
}
gboolean getxy (GtkWidget *area, gpointer data)                 // show the
recognition comparision
{
        FILE *fp,*np;                                           // two 
filepointer of all data and divided data
        char ch,cha[2];
        char *src = "ujwal.txt";
        char *des[26] =
{"A.txt","B.txt","C.txt","D.txt","E.txt","F.txt","G.txt","H.txt","I.txt","J.txt","K.txt","L.txt","M.txt","N.txt","O.txt","P.txt","Q.txt","R.txt","S.txt","T.txt","U.txt","V.txt","W.txt","X.txt","Y.txt","Z.txt"};
                                              //
list of files to compare
        int x,n,c=0,i=1,y;
        float cc,aa,bb,kar[chno];
        static int a;
        fp=fopen("tmp.txt","r");                                // open all 
data file
        np=fopen("ujwal.txt","w+");                             // open new 
data file
GtkWidget *label = GTK_WIDGET(data);                            // label to 
print recognised
character
        c=0;
        if(len == 0)
        {
        printf("error in writing and gaining data");
        }
        else
        {
        printf("\n%d is the length\n",len);
        n=len/part;                                             //divide 10 
equal parts
        while(!feof(fp))                                        //until file 
ends
                {
                        fscanf(fp,"%d %d  ",&x,&y);             //get xy from 
file tmp.txt
                        if(n * i == c)                          //if that value 
is exact 10 parts save in
        {
                        fprintf(np,"%d %d  ",x,y);              // ujwal.txt
                        i++;
        }
                c++;
                }
        }
                fclose(fp);                                     //close files
                fclose(np);
        kar[0]=statistic_data(src,des[0]);
                aa = kar[0];
                ch = 65;
        for (i = 1; i < chno;i++)
        {
                kar[i] = statistic_data(src,des[i]);
                if (aa < kar[i])
                {
                aa = kar[i];
                ch = 65 + i;
                }
}
                cha[0]=ch;
                cha[1]='\0';
gtk_label_set_text (GTK_LABEL (label),cha);
printf(" %c is recognised\n\n\n ",ch);
return TRUE;
}
void draw (GtkWidget *draw,GdkEventButton *butto)                               
//function to
draw on pixmap
{
        FILE *tmp;                              //create a file pointer tmp
    int x,y;                                    // x y interger variable
        tmp=fopen("tmp.txt","a+");              // open tmp.txt in append mode +
   GdkModifierType stat;                        // A GDK modifier mask
                gdk_window_get_pointer (draw->window, &x, &y, &stat);   //get
pointer x and y with mouse status
        if (stat & GDK_BUTTON1_MASK)                    //check if button 
pressed
        {
        //g_printf("%d and %d\t",x,y);
                len++;                                  // inc len var
                fprintf(tmp,"%d %d  ",x,y);
        gdk_draw_rectangle (pixmap,draw->style-
>black_gc,TRUE,x-5,y-5,10,10);  //draw small rectangle
                 gtk_widget_queue_draw_area (draw,x-5,y-5,10,10);               
// query to draw
in pixmap
                }
        fclose(tmp);                                                    //close 
file tmp.txt
}
void killall (GtkWidget *window,gpointer data)                  //function to 
kill
window and all
{
    gtk_main_quit();
}
int main( int   argc,char *argv[] )
{
    GtkWidget *window;                                  //create a pointer 
window
    GtkWidget *frame;                                   //      "
    GtkWidget *frame1;                                  //      "
    GtkWidget *button;                                  //      "
        GtkWidget *clrbut;                              //      "
    GtkWidget *box;                                     //      "
    GtkWidget *box1;                                    //      "
    GtkWidget *hbox;                                    //      "
    GtkWidget *drawing;                                 //      "
    GtkWidget *label;                                   //      "
        FILE *tmp;                                      // file pointer tmp
        tmp=fopen("tmp.txt","w+");                      // open file tmp.txt in 
write mod if
exists or not
        gtk_init (&argc, &argv);                        // program argument to 
gtk
initiating function
     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);     // create a new
window pointed by window pointer
    box = gtk_vbox_new (FALSE, 0);                      //      "
    gtk_container_add (GTK_CONTAINER (window), box);    //      add box to
window
    button = gtk_button_new_with_label ("Recognise");   // create a
button with label recognize
    clrbut = gtk_button_new_with_label ("Clear");       //      "
    box1 = gtk_vbox_new (TRUE, 0);                      //      "
    hbox = gtk_hbox_new (TRUE,0);                       //      "
    label = gtk_label_new ("ujwal");                    //      "
    drawing = gtk_drawing_area_new ();                  //      "
        fclose(tmp);                                    // close file   
(PURPOSE JUST CREATE A FILE)
    gtk_window_set_title (GTK_WINDOW (window), "Handwriting
recognition");  //set title
    gtk_widget_set_size_request (window, 300, 300);                             
// set size
    gtk_widget_set_size_request (drawing, 100, 100);                            
//      "
    frame = gtk_frame_new (NULL);                                               
// create new frame
    frame1 = gtk_frame_new (NULL);                                              
//
    gtk_box_pack_start (GTK_BOX (box), hbox, TRUE, TRUE, 0);                    
// pack
hbox in box
    gtk_box_pack_start (GTK_BOX (hbox), frame ,TRUE,TRUE, 0);                   
//      "
    gtk_box_pack_start (GTK_BOX (hbox), frame1 ,TRUE,TRUE, 0);                  
//      "
    gtk_container_add (GTK_CONTAINER (box1), button);                           
// add button
to box
    gtk_container_add (GTK_CONTAINER (box1), clrbut);                           
//      "
    gtk_container_add (GTK_CONTAINER (frame1),label);                           
//      "
    gtk_box_pack_start (GTK_BOX (box), box1, TRUE, TRUE,0);                     
//      "
    gtk_container_add (GTK_CONTAINER (frame), drawing);                         
//      "
    gtk_frame_set_label (GTK_FRAME (frame), "Main area");                       
// set
frame name
    gtk_frame_set_label (GTK_FRAME (frame1), "Recog");                          
//      "
        g_signal_connect (G_OBJECT(drawing),
"expose_event",G_CALLBACK(draw_update),NULL);   //connect window expose
to draw_update function
    g_signal_connect (G_OBJECT (drawing), "configure_event",G_CALLBACK
(configure_pixmap), NULL);      //connect initial configuration to
configure_pixmap function
    g_signal_connect (G_OBJECT (button), "clicked",G_CALLBACK (getxy),
label);         // RECOGnize button clicked - start recognising
  g_signal_connect (G_OBJECT (clrbut), "clicked",G_CALLBACK
(cleararea), drawing);  // clear drawing area
    g_signal_connect (G_OBJECT (drawing), "motion_notify_event",
G_CALLBACK (draw), NULL);       // mouse in motion connect to draw function
    g_signal_connect (G_OBJECT(window),"destroy",G_CALLBACK
(killall),NULL);        // close button in window
    gtk_widget_set_events (drawing,GDK_EXPOSURE_MASK
             | GDK_LEAVE_NOTIFY_MASK
             | GDK_BUTTON_PRESS_MASK
             | GDK_POINTER_MOTION_MASK
             | GDK_POINTER_MOTION_HINT_MASK);                                   
                // mask for the
drawing area
    gtk_widget_show (label);                            // show label
    gtk_widget_show (frame);                            //      "
    gtk_widget_show (frame1);                           //      "
    gtk_widget_show (button);                           //      "
        gtk_widget_show (clrbut);                       //      "
    gtk_widget_show (box);                              //      "
    gtk_widget_show (hbox);                             //      "
    gtk_widget_show (drawing);                          //      "
    gtk_widget_show (box1);                             //      "
        gtk_widget_show (window);                       //      "
           gtk_main ();                                 // run gtk all events 
and guis
        return 0;                                       // function end return 
value
}

use gcc  -o <exec_name> <file_name.c> `pkg-config --cflags --libs gtk
+-2.0`


--~--~---------~--~----~------------~-------~--~----~
FOSS Nepal mailing list
[email protected]
http://groups.google.com/group/foss-nepal

Community website: http://www.fossnepal.org/
-~----------~----~----~----~------~----~------~--~---

Reply via email to