Source C du chargement et sauvegarde des images




/****************************************************************************/
/*                                                                          */
/*      Visualisation d'une image sous X11 par utilisation des seules       */
/*      primitives de la Xlib.                                              */
/*                                                                          */
/*      Patrick Horain (ENST/IMA), le 5 juin 1995                           */
/*                                                                          */
/****************************************************************************/


/****************************************************************************/

#include "imdisplay.h"

int display (char* nom_fich)
{
    char                * display_name=NULL, *nom_base ;
    char                * data ;
    int                 ima_larg, ima_haut, ima_bits ;

    Display             * display ;
    Screen              * screen ;
    Window              window ;
    GC                  gc ;
    Visual              * visual ;
    XImage              * ximage ;
    Pixmap              pixmap ;
    XEvent              event;
    XTextProperty       winName,iconName;
    XWMHints            wmhints;



    if  ( ! ( display = XOpenDisplay (display_name) ) )
                abandon ("Echec lors de la connexion au serveur")

    screen = XDefaultScreenOfDisplay ( display ) ;
    gc     = DefaultGCOfScreen (screen) ;

/* Lecture de l'image:
   *******************/
    data = get_image ( nom_fich, "r", &ima_larg, &ima_haut, &ima_bits ) ;
    if  ( ! data )      abandon ("get_image") ;
 
    visual = DefaultVisualOfScreen(screen) ;

    ximage = XCreateImage ( display, visual, (unsigned int) ima_bits, 
                            ZPixmap, OFFSET, data, ima_larg, ima_haut,
                            BITMAP_PAD,
                            (ima_bits+BITMAP_PAD-1) / BITMAP_PAD * ima_larg ) ;
    if  ( ! ximage )    abandon ("XCreateImage") ;

/* Création de la fenêtre:
   ***********************/
    window = XCreateSimpleWindow ( display, RootWindowOfScreen(screen),
                                   H_POS, V_POS,
                                   ximage->width, ximage->height, 2, 
                                   WhitePixelOfScreen ( screen ), 
                                   BlackPixelOfScreen ( screen ) ) ;

    if       ( nom_base = strrchr ( nom_fich,ENDDIR) )  nom_base ++;
    else        /* Image file in current directory */   nom_base = nom_fich ;

    if (        ! XStringListToTextProperty (&nom_fich, 1, &winName)
             || ! XStringListToTextProperty (&nom_base, 1, &iconName)   )
        abandon ("Structure allocation failed in XStringListToTextProperty") ;

    wmhints.initial_state       = NormalState ; /* Do not start iconified */
    wmhints.input               = True ;        /* Need keyboard input    */
    wmhints.flags               = StateHint | InputHint ;

    XSetWMProperties (display, window, &winName, &iconName, NULL,
                                                0, NULL, &wmhints, NULL) ;

/* Chargement de l'image:
   **********************/
    pixmap = XCreatePixmap ( display, window,
                                ximage->width, ximage->height, ximage->depth ) ;

    XPutImage ( display, pixmap, gc, ximage, 0, 0, 0, 0,
                                        ximage->width, ximage->height ) ;

/* Installation de la table de couleurs:
   *************************************/
    if ( ! xcmap_ima ( display, window, nom_fich, visual ) )
                                                abandon ( "xcmap_ima" ) ;

/* Attente des evenements:
   ***********************/
    XSelectInput ( display, window, ExposureMask | KeyPressMask ) ;
    XMapWindow   ( display, window ) ;

    while(1)
    {   XNextEvent (display, &event) ;
/*      (void) fprintf (stderr, "%s: evenement: %d\n", nom_base, event.type); */

        switch(event.type)

        { case Expose:
                /* Optimisation possible: voir XPM (O'Reilly V1), ex 3.15 */
                if  ( event.xexpose.count == 0 )
                    XCopyArea ( display, pixmap, window, gc,
                                0, 0, ximage->width, ximage->height, 0, 0 ) ;
                break ;

          case KeyPress:
                /* Evenements non vus avec Open Windows */
                { char key[1] ;

                  XLookupString(&event,key,1,NULL,NULL);
                  if    ( (*key=='q') || (*key=='Q') )  exit(EXIT_SUCCESS) ;
                  break ;
                }

          default:      break ;
        }
    }
}





---------

Problèmes, commentaires...