home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Enlightenment / imlib_DR-0.2.tar.gz / imlib_DR-0.2.tar / imlib / misc.c < prev    next >
C/C++ Source or Header  |  1997-06-19  |  6KB  |  255 lines

  1. #include "imlib.h"
  2.  
  3. int ImlibGetRenderType(ImlibData *id)
  4. {
  5.    if (id) return id->render_type;
  6.    else
  7.      {    
  8.     fprintf(stderr,"ImLib ERROR: ImlibData struct not initialised\n");
  9.     return -1;
  10.      }
  11. }
  12.  
  13. int ImlibSetRenderType(ImlibData *id, int rend_type)
  14. {
  15.    if (id)
  16.      {
  17.     if (id->x.depth>8) id->render_type=rend_type;
  18.     else
  19.       {
  20.          if (rend_type==RT_PLAIN_TRUECOL)
  21.            id->render_type=RT_DITHER_PALETTE_FAST;
  22.          else
  23.            id->render_type=rend_type;
  24.       }
  25.     return 1;
  26.      }
  27.    else
  28.      {
  29.     fprintf(stderr,"ImLib ERROR: ImlibData struct not initialised\n");
  30.     return 0;
  31.      }
  32. }
  33.  
  34. ImlibData *ImlibInit(Display *disp)
  35. {
  36.    ImlibData *id;
  37.    XWindowAttributes xwa;
  38.    XVisualInfo xvi,*xvir;
  39.    char *homedir;
  40.    char file[4096];
  41.    char s[4096],s1[1024],s2[1024];
  42.    FILE *f;
  43.    int override=0;
  44.    int dither=0;
  45.    int remap=1;
  46.    int num;
  47.    int i,max,maxn;
  48.    int clas;
  49.    
  50.    id=(ImlibData *)malloc(sizeof(ImlibData));
  51.    if (!id)
  52.      {
  53.     fprintf(stderr,"ImLib ERROR: Cannot alloc RAM for Initial data struct\n");
  54.     return NULL;
  55.      }
  56.    id->x.disp=disp;
  57.    id->x.screen=DefaultScreen(disp); /* the screen number */
  58.    id->x.root=DefaultRootWindow(disp); /* the root window id */
  59.    id->x.visual=DefaultVisual(disp,id->x.screen); /* the visual type */
  60.    id->x.depth=DefaultDepth(disp,id->x.screen); /* the depth of the screen in bpp */
  61.    if (XShmQueryExtension(id->x.disp)) 
  62.      {
  63.     id->x.shm=1;
  64.     id->x.shm_event=XShmGetEventBase(id->x.disp)+ShmCompletion;
  65.     id->x.last_xim=NULL;
  66.     id->x.last_sxim=NULL;
  67.     id->max_shm=0x7fffffff;
  68.      }
  69.    else id->x.shm=0;
  70.    xvi.visual=id->x.visual;
  71.    xvi.visualid=XVisualIDFromVisual(id->x.visual);
  72.    xvi.screen=id->x.screen;
  73.    xvir=XGetVisualInfo(disp,VisualScreenMask,&xvi,&num);
  74.    if (xvir) 
  75.      {
  76.     max=0;
  77.     maxn=0;
  78.     clas=-1;
  79.     for (i=0;i<num;i++)
  80.       {
  81.          if (xvir[i].depth>=max)
  82.            {
  83.           if (xvir[i].depth<8)
  84.             {
  85.                if ((xvir[i].class>clas)&&(xvir[i].class!=DirectColor)&&(xvir[i].class!=TrueColor)&&(xvir[i].class!=PseudoColor))
  86.              {
  87.                 clas=xvir[i].class;
  88.                 maxn=i;
  89.                 max=xvir[i].depth>max;
  90.              }
  91.             }
  92.           else if (xvir[i].depth==8)
  93.             {
  94.                if ((xvir[i].class>clas)&&(xvir[i].class!=DirectColor)&&(xvir[i].class!=TrueColor))
  95.              {
  96.                 clas=xvir[i].class;
  97.                 maxn=i;
  98.                 max=xvir[i].depth>max;
  99.              }
  100.             }
  101.           else
  102.             {
  103.                if ((xvir[i].class>clas)&&(xvir[i].class!=DirectColor))
  104.              {
  105.                 clas=xvir[i].class;
  106.                 maxn=i;
  107.                 max=xvir[i].depth>max;
  108.              }
  109.             }
  110.            }
  111.       }
  112.     id->x.depth=xvir[maxn].depth;
  113.     id->x.visual=xvir[maxn].visual;
  114.     printf("<deubug> IMLIB: Chose the %i Bit depth Visual, ID 0x%x, to work with\n",id->x.depth,XVisualIDFromVisual(id->x.visual));
  115.     XFree(xvir);
  116.      }
  117.    if (id->x.depth==16)
  118.      {
  119.     xvi.visual=id->x.visual;
  120.     xvi.visualid=XVisualIDFromVisual(id->x.visual);
  121.     xvir=XGetVisualInfo(disp,VisualIDMask,&xvi,&num);
  122.     if (xvir) 
  123.       {
  124.          if (xvir->red_mask!=0xf800) id->x.depth=15;
  125.          XFree(xvir);
  126.       }
  127.      }
  128.    if (XGetWindowAttributes(disp,id->x.root,&xwa))
  129.      {
  130.     if (xwa.colormap) id->x.root_cmap=xwa.colormap;
  131.     else id->x.root_cmap=0;
  132.      }
  133.    else id->x.root_cmap=0;
  134.    id->num_colors=0;
  135.    homedir=getenv("HOME");
  136.    sprintf(file,"%s/.imrc",homedir);
  137.    f=fopen(file,"r");
  138.    if (!f) f=fopen(SYSTEM_IMRC,"r");
  139.    if (f)
  140.      {
  141.     while (fgets(s,4096,f))
  142.       {
  143.          if (s[0]=='#') continue;
  144.          sscanf(s,"%s %s\n",s1,s2);
  145.          if (!strcasecmp("PaletteFile",s1))
  146.            {
  147.           ImlibLoadColors(id,s2);
  148.            }
  149.          if (!strcasecmp("PaletteOverride",s1))
  150.            {
  151.           if (!strcasecmp("yes",s2)) override=1;
  152.           else override=0;
  153.            }
  154.          if (!strcasecmp("Dither",s1))
  155.            {
  156.           if (!strcasecmp("yes",s2)) dither=1;
  157.           else dither=0;
  158.            }
  159.          if (!strcasecmp("Remap",s1))
  160.            {
  161.           if (!strcasecmp("fast",s2)) 
  162.             remap=1;
  163.           else 
  164.             remap=0;
  165.            }
  166.          if (!strcasecmp("Mit-Shm",s1))
  167.            {
  168.           if (!strcasecmp("off",s2)) id->x.shm=0;
  169.            }
  170.          if (!strcasecmp("Shm_Max_Size",s1))
  171.            {
  172.           sscanf(s,"%s %i",s1,&num);
  173.           id->max_shm=num;
  174.            }
  175.       }
  176.     fclose(f);
  177.      }
  178.    if ((id->x.depth<=8)||(override==1)) 
  179.      {
  180.     if (dither==1)
  181.       {
  182.          if (remap==1) id->render_type=RT_DITHER_PALETTE_FAST;
  183.          else id->render_type=RT_DITHER_PALETTE;
  184.       }
  185.     else
  186.       {
  187.          if (remap==1) id->render_type=RT_PLAIN_PALETTE_FAST;
  188.          else id->render_type=RT_PLAIN_PALETTE;
  189.       }
  190.      }
  191.    else 
  192.      {
  193.     id->render_type=RT_PLAIN_TRUECOL;
  194.      }
  195.    return id;
  196. }
  197.  
  198. Pixmap ImlibCopyImageToPixmap(ImlibData *id, Image *im)
  199. {
  200.    Pixmap p;
  201.    GC tgc;
  202.    XGCValues gcv;
  203.    
  204.    if (!im->pixmap) return 0;
  205.    p=XCreatePixmap(id->x.disp,id->x.root,im->width,im->height,id->x.depth);
  206.    tgc=XCreateGC(id->x.disp,im->pixmap,(unsigned long)0,&gcv);
  207.    XCopyArea(id->x.disp,im->pixmap,p,tgc,0,0,im->width,im->height,0,0);
  208.    XFreeGC(id->x.disp,tgc);
  209.    return p;
  210. }
  211.  
  212. Pixmap ImlibMoveImageToPixmap(ImlibData *id, Image *im)
  213. {
  214.    Pixmap p;
  215.    
  216.    p=im->pixmap;
  217.    im->pixmap=0;
  218.    return p;
  219. }
  220.  
  221. Pixmap ImlibCopyMaskToPixmap(ImlibData *id, Image *im)
  222. {
  223.    Pixmap p;
  224.    GC tgc;
  225.    XGCValues gcv;
  226.    
  227.    if (!im->shape_mask) return 0;
  228.    p=XCreatePixmap(id->x.disp,id->x.root,im->width,im->height,1);
  229.    tgc=XCreateGC(id->x.disp,im->shape_mask,(unsigned long)0,&gcv);
  230.    XCopyArea(id->x.disp,im->shape_mask,p,tgc,0,0,im->width,im->height,0,0);
  231.    XFreeGC(id->x.disp,tgc);
  232.    return p;
  233. }
  234.  
  235. Pixmap ImlibMoveMaskToPixmap(ImlibData *id, Image *im)
  236. {
  237.    Pixmap p;
  238.    
  239.    p=im->shape_mask;
  240.    im->shape_mask=0;
  241.    return p;
  242. }
  243.  
  244. void ImlibDestroyImage(ImlibData *id, Image *im)
  245. {
  246.    if (im)
  247.      {
  248.     if (im->pixmap) XFreePixmap(id->x.disp,im->pixmap);
  249.     if (im->shape_mask) XFreePixmap(id->x.disp,im->shape_mask);
  250.     if (im->rgb_data) free(im->rgb_data);
  251.     free(im);
  252.      }
  253. }
  254.  
  255.