home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / xfedor.zip / XFree86 / src / xfedor / myrast.c < prev    next >
C/C++ Source or Header  |  1999-03-02  |  8KB  |  300 lines

  1. /* Copyright 1989 GROUPE BULL -- See licence conditions in file COPYRIGHT */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "clientimage.h"    /*  ClientImage .. */
  6.  
  7. #include <X11/Xlib.h>
  8. #include <X11/Xutil.h>
  9.  
  10. extern Display * Dpl ;
  11. extern Window root ;
  12. extern int screen ;
  13. extern int depth ;
  14. extern int ImaForm ;
  15. static ClientImage * backcol ;
  16.  
  17.  
  18.   
  19. void Rast_Free(climag)
  20. /***************/
  21.     ClientImage * * climag ;
  22. {
  23.   XDestroyImage(*climag) ;
  24.   *climag = NULL ;
  25. }
  26.  
  27. void Rast_Init(climag,w,h,flag)
  28. /************************/
  29. /* alloue et initialise une image :
  30.      si flag = 0 : depth 1 pad 8 au format XYBitmap 
  31.      si flag = 1 : depth n pad 8 au format ImaForm 
  32. */
  33.     ClientImage * * climag ;
  34.     int w,h ;
  35.         int flag ; /* 0 ou 1 */
  36. {    
  37.   char * data ;
  38.   int vidwidth ;
  39.  
  40.   vidwidth = ((w+7)>>3) ;       /* pad 8 */
  41.   data = (char *) calloc (1,vidwidth*h*((flag)?depth:1)) ;   /* tout a 0 */
  42.  
  43.    * climag = (ClientImage *) XCreateImage(Dpl,DefaultVisual(Dpl,screen),
  44.             (flag)?depth:1,
  45.                     (flag)?ImaForm:XYBitmap,
  46.             0,data,w,h,8,0) ;
  47.   if (flag == 0) {
  48.   /* il semble qu'il suffise d'ecraser ces valeurs pour changer
  49.    le format de l'image cree */
  50.      ((XImage *)*climag)->byte_order = MSBFirst ;
  51.      ((XImage *)*climag)->bitmap_unit = 8 ;
  52.      ((XImage *)*climag)->bitmap_bit_order = MSBFirst ;
  53.   /* cette image sera manipuler par XPutPixel, XGetPixel, XPutImage
  54.      qui garderont son format tel quel */
  55.   }
  56.  
  57. }
  58.  
  59.  
  60. void Rast_Off(climag,color)
  61. /********************/ 
  62. /* 128x128 only , format MSB/pad8 only */
  63.     ClientImage * * climag ;
  64.         int color ;
  65. {
  66.    bcopy(((XImage *)backcol)->data,((XImage *)*climag)->data,128/8*128*depth);
  67. }
  68.  
  69.  
  70. void Rast_Copy(sourceimage,destimage)
  71. /******************************/
  72. /* 128x128 only , format MSB/pad8 only */
  73.     ClientImage * sourceimage, * * destimage ;
  74. {
  75.    bcopy(((XImage *)sourceimage)->data,((XImage *)*destimage)->data,
  76.      128/8*128*depth);
  77. }
  78.  
  79.  
  80. void Rast_Put(climag,data,w,h)
  81. /***********************/
  82. /* data pointe sur un plan bitmap w*h pad=8 MSB */
  83. /* on construit une XImage monochrome depth 1 */
  84.     ClientImage * * climag ;
  85.         char * data ;
  86.     int w,h ;
  87. {
  88.  
  89.   *climag = (ClientImage *) XCreateImage(Dpl,DefaultVisual(Dpl,screen),
  90.             1,XYBitmap,0,data,w,h,8,0) ;
  91.   ((XImage *)*climag)->byte_order = MSBFirst ;
  92.   ((XImage *)*climag)->bitmap_unit = 8 ;
  93.   ((XImage *)*climag)->bitmap_bit_order = MSBFirst ;
  94.  
  95. }
  96.  
  97.  
  98. void Rast_Mem(climap,w,h,pmem,pwidth)
  99. /******************************/
  100. /* rend le pointeur sur la memoire image et la largeur en bytes */
  101. /* on saura que c'est en pad8 et MSB */
  102.     ClientImage * climap ;
  103.         int w,h ;
  104.     char * * pmem ;
  105.     int * pwidth ;
  106. {
  107.   *pmem = climap->data ;
  108.   *pwidth = climap->bytes_per_line ;
  109. }
  110.  
  111.  
  112. int Rast_Inq(climag,x,y)
  113. /*******************/
  114. /* return the pixel value */
  115.     ClientImage * climag ;
  116.     int x,y ;
  117. {
  118.   return XGetPixel(climag,x,y) ;
  119. }
  120.  
  121.  
  122. void Rast_Pix(climag,x,y,color)
  123. /************************/
  124.     ClientImage* climag ;
  125.     int x,y ;
  126.     int color ;
  127. {
  128.   XPutPixel(climag,x,y,color) ;
  129. }
  130.  
  131.  
  132. int Ligne_color(climag,y,x1,x2,color)
  133. /*******************************/
  134.     ClientImage * climag ;
  135.     int y,x1,x2 ;
  136.     int color ;
  137. /* rends 0 si au moins un pixel de climag entre x1
  138.    et x2, avec y en ordonnee est de la couleur color */
  139. {
  140.     int i ;
  141.  
  142.     for ( i=x1; i<x2 ; i++) 
  143.         if (Rast_Inq(climag,i,y)!=color) return 0 ;
  144.     return 1 ;
  145. }    
  146.  
  147.  
  148. int Colonne_color(climag,x,y1,y2,color)
  149. /*********************************/
  150.     ClientImage * climag ;
  151.     int x,y1,y2 ;
  152.     int color ;
  153. /* rends 0 si au moins un pixel de climag entre y1
  154.    et y2, avec x en ordonnee est de la couleur color */
  155. {
  156.     int j ;
  157.  
  158.     for ( j=y1; j<y2; j++) 
  159.         if (Rast_Inq(climag,x,j)!=color) return 0 ;
  160.     return 1 ;
  161. }    
  162.  
  163. void Rast_Op(sourceimage,destimage, x1,y1,x2,y2,w,h, mode, color)
  164. /**********************************************************/
  165.     ClientImage * sourceimage, * * destimage ;
  166.     int x1,y1,x2,y2 ;
  167.     int w,h ;
  168.     int mode ;
  169.         int color ;
  170. {
  171.   register int i,j ;
  172.   register int dx, dy, lx, ly;
  173.   register int p,q ;
  174.   
  175.   dx = x2 - x1 ;
  176.   dy = y2 - y1 ;
  177.   lx = x1+w ;
  178.   ly = y1+h ;
  179.   if (mode == VIDSTR ) {
  180.     for (i=x1 ; i<lx ; i++)
  181.       for (j=y1 ; j<ly ; j++) {
  182.     p = XGetPixel(sourceimage,i,j) ;
  183.     XPutPixel(*destimage,i+dx,j+dy,p) ;
  184.       }
  185.   } else
  186.   if (mode == VIDOR ) {
  187.     for (i=x1 ; i<lx ; i++)
  188.       for (j=y1 ; j<ly ; j++) {
  189.     p = XGetPixel(sourceimage,i,j) ;
  190.     if (p == color) continue ;
  191.     if (XGetPixel(*destimage,i+dx,j+dy) != color) continue ;
  192.     XPutPixel(*destimage,i+dx,j+dy,p) ;
  193.       }
  194.   } else
  195.   if (mode == VIDAND ) {
  196.     for (i=x1 ; i<lx ; i++)
  197.       for (j=y1 ; j<ly ; j++) {
  198.     p = XGetPixel(sourceimage,i,j) ;
  199.     q = XGetPixel(*destimage,i+dx,j+dy) ;
  200.     if ((p == color) || (q == color))
  201.       XPutPixel(*destimage,i+dx,j+dy,color) ; else continue ;
  202.       }
  203.   } else
  204.   if (mode == VIDXOR ) {
  205.     for (i=x1 ; i<lx ; i++)
  206.       for (j=y1 ; j<ly ; j++) {
  207.     p = XGetPixel(sourceimage,i,j) ;
  208.     q = XGetPixel(*destimage,i+dx,j+dy) ;
  209.     if ((p != color) && (q == color))
  210.       XPutPixel(*destimage,i+dx,j+dy,p) ; else
  211.     if ((p != color) && (q != color))
  212.       XPutPixel(*destimage,i+dx,j+dy,color) ; else continue ;
  213.       }
  214.   }
  215.  
  216. }
  217.  
  218. void Rast_Op_1n(sourceimage,destimage, x1,y1,x2,y2,w,h, mode, colorcur)
  219. /****************************************************************/
  220. /* copy d'image de depth = 1 sur depth = n */
  221. /* on ne fait que mettre des pixels colorcur sur destimage */
  222.     ClientImage * sourceimage, * * destimage ;
  223.     int x1,y1,x2,y2 ;
  224.     int w,h ;
  225.     int mode ;
  226.         int colorcur ;
  227. {
  228.   register int i,j ;
  229.   register int dx, dy, lx, ly;
  230.   
  231.   dx = x2 - x1 ;
  232.   dy = y2 - y1 ;
  233.   lx = x1+w ;
  234.   ly = y1+h ;
  235.   for (i=x1 ; i<lx ; i++)
  236.     for (j=y1 ; j<ly ; j++) 
  237.       if (XGetPixel(sourceimage,i,j) == 1)
  238.     XPutPixel(*destimage,i+dx,j+dy,colorcur) ;
  239. }
  240.  
  241. void Rast_Op_n1(sourceimage,destimage, x1,y1,x2,y2,w,h, mode, colorback)
  242. /*****************************************************************/
  243. /* copy d'image depth = n vers depth = 1 */
  244. /* on met des pixel a 1 dans le bitmap depth = 1 */
  245.     ClientImage * sourceimage, * * destimage ;
  246.     int x1,y1,x2,y2 ;
  247.     int w,h ;
  248.     int mode ;
  249.         int colorback ;
  250. {
  251.   register int i,j ;
  252.   register int dx, dy, lx, ly;
  253.   
  254.   dx = x2 - x1 ;
  255.   dy = y2 - y1 ;
  256.   lx = x1+w ;
  257.   ly = y1+h ;
  258.   for (i=x1 ; i<lx ; i++)
  259.     for (j=y1 ; j<ly ; j++) 
  260.       if (XGetPixel(sourceimage,i,j) != colorback)
  261.     XPutPixel(*destimage,i+dx,j+dy,1) ;
  262. }
  263.  
  264. void Init_Rast(color)
  265. /**************/
  266.     int color ;
  267. {
  268.    int i,j ;
  269.  
  270.    /* the first time, we create an ClientImage fill in the color(one!)
  271.     *  it has the same content structure as the climag to come for bcopy
  272.     * 128x128 depth, ImaForm and MSB */
  273.    Rast_Init(&backcol,128,128,1);
  274.    for (i=0 ; i<128; i++) {
  275.       for (j=0 ; j<128; j++) {
  276.     Rast_Pix(backcol,i,j,color);
  277.      }
  278.    }
  279. }
  280.  
  281. /*********
  282.   Une XImage est caracterisee par 
  283.       sa taille, son format, son pad, sa depth,
  284.   qui sont donnees a la creation par l'utilisateur de XCreateImage et par
  285.       son byte_order, bitmap_unit, bitmap_bit_order et bits_per_pixel
  286.   qui sont mises a jour a partir des caracteristiques du serveur X courant.
  287.       (hors controle de l'utilisateur...sauf hack)
  288.  
  289.   Ensuite les primitives XGetPixel et XPutPixel "normalize" en LSBFirst 
  290.   les bitmap_unit (pas l'image complete) de l'image avant de faire le boulot. 
  291.  
  292.  
  293.   HORS, je prefere que toutes mes XImages soient en MSBFirst, unit=8, pad=8 : 
  294.        - celles de la fonte car je les lit et les ecrit ainsi rapidos.
  295.        - celles des autres images car on fait des copies avec bcopy en
  296.          connaissant ainsi le format.
  297.  
  298.  **********/
  299.  
  300.