//------------------------------------------------------ // module : TP5-IFT2425-II2c.c // author : // date : // version : 1.0 // language: C++ //------------------------------------------------------ // //------------------------------------------------ // FICHIERS INCLUS ------------------------------- //------------------------------------------------ #include #include #include #include #include //------------------------------------------------ // DEFINITIONS ----------------------------------- //------------------------------------------------ #define SIZE 512 #define OUTPUT_FILE "Fractal" #define CARRE(X) ((X)*(X)) //------------------------- //--- Windows ------------- //------------------------- #include Display *display; int screen_num; int depth; Window root; Visual* visual; GC gc; /************************************************************************/ /* OPEN_DISPLAY() */ /************************************************************************/ int open_display() { if ((display=XOpenDisplay(NULL))==NULL) { printf("Connection impossible\n"); return(-1); } else { screen_num=DefaultScreen(display); visual=DefaultVisual(display,screen_num); depth=DefaultDepth(display,screen_num); root=RootWindow(display,screen_num); return 0; } } /************************************************************************/ /* FABRIQUE_WINDOW() */ /* Cette fonction crée une fenetre X et l'affiche à l'écran. */ /************************************************************************/ Window fabrique_window(char *nom_fen,int x,int y,int width,int height,int zoom) { Window win; XSizeHints size_hints; XWMHints wm_hints; XClassHint class_hints; XTextProperty windowName, iconName; char *name=nom_fen; if(zoom<0) { width/=-zoom; height/=-zoom; } if(zoom>0) { width*=zoom; height*=zoom; } win=XCreateSimpleWindow(display,root,x,y,width,height,1,0,255); size_hints.flags=PPosition|PSize|PMinSize; size_hints.min_width=width; size_hints.min_height=height; XStringListToTextProperty(&name,1,&windowName); XStringListToTextProperty(&name,1,&iconName); wm_hints.initial_state=NormalState; wm_hints.input=True; wm_hints.flags=StateHint|InputHint; class_hints.res_name=nom_fen; class_hints.res_class=nom_fen; XSetWMProperties(display,win,&windowName,&iconName, NULL,0,&size_hints,&wm_hints,&class_hints); gc=XCreateGC(display,win,0,NULL); XSelectInput(display,win,ExposureMask|KeyPressMask|ButtonPressMask| ButtonReleaseMask|ButtonMotionMask|PointerMotionHintMask| StructureNotifyMask); XMapWindow(display,win); return(win); } /****************************************************************************/ /* CREE_XIMAGE() */ /* Crée une XImage à partir d'un tableau de float */ /* L'image peut subir un zoom. */ /****************************************************************************/ XImage* cree_Ximage(float** mat,int z,int length,int width) { int lgth,wdth,lig,col,zoom_col,zoom_lig; float somme; unsigned char pix; unsigned char* dat; XImage* imageX; /*Zoom positiv*/ /*------------*/ if (z>0) { lgth=length*z; wdth=width*z; dat=(unsigned char*)malloc(lgth*(wdth*4)*sizeof(unsigned char)); if (dat==NULL) { printf("Impossible d'allouer de la memoire."); exit(-1); } for(lig=0;lig au format pgm //---------------------------------------------------------- void SaveImagePgm(char* name,float** mat,int lgth,int wdth) { int i,j; char buff[300]; FILE* fic; //--extension-- strcpy(buff,name); strcat(buff,".pgm"); //--ouverture fichier-- fic=fopen(buff,"wb"); if (fic==NULL) { printf("Probleme dans la sauvegarde de %s",buff); exit(-1); } printf("\n Sauvegarde de %s au format pgm\n",buff); //--sauvegarde de l'entete-- fprintf(fic,"P5"); fprintf(fic,"\n# IMG Module"); fprintf(fic,"\n%d %d",wdth,lgth); fprintf(fic,"\n255\n"); //--enregistrement-- for(i=0;imax) max=mat[i][j]; //Recalibre la matrice*/ for(i=0;iRecal_&_Save Recal(Graph2D,length,width); SaveImagePgm((char*)OUTPUT_FILE,Graph2D,length,width); //-------------------------------------------------------------------------------- //---------------- visu sous XWINDOW --------------------------------------------- //-------------------------------------------------------------------------------- if (flag_graph) { //ouverture session graphique if (open_display()<0) printf(" Impossible d'ouvrir une session graphique"); sprintf(nomfen_ppicture,"Graphe :"); win_ppicture=fabrique_window(nomfen_ppicture,10,10,width,length,zoom); x_ppicture=cree_Ximage(Graph2D,zoom,length,width); printf("\n\n Pour quitter,appuyer sur la barre d'espace"); fflush(stdout); //boucle d'evenements for(;;) { XNextEvent(display,&ev); switch(ev.type) { case Expose: XPutImage(display,win_ppicture,gc,x_ppicture,0,0,0,0,x_ppicture->width,x_ppicture->height); break; case KeyPress: XDestroyImage(x_ppicture); XFreeGC(display,gc); XCloseDisplay(display); flag_graph=0; break; } if (!flag_graph) break; } } //retour sans probleme printf("\n Fini... \n\n\n"); return 0; }