Subversion Repositories wimsdev

Rev

Rev 10 | Rev 8103 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 2002-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. /* tex 2 gif translator, image manipulating routines */
  19.  
  20. gdImagePtr image=NULL;
  21. int color_white, color_black, color_bounder;
  22.  
  23. int getcolor(int r, int g, int b)
  24. {
  25.     int col;
  26.     if(r>255) r=255; if(r<0) r=0;
  27.     if(g>255) g=255; if(g<0) g=0;
  28.     if(b>255) b=255; if(b<0) b=0;
  29.     col=gdImageColorExact(image, r, g, b);
  30.     if(col==-1) col=gdImageColorAllocate(image,r,g,b);
  31.     return col;
  32. }
  33.  
  34. void createimage(int xsize, int ysize)
  35. {
  36.     if(xsize<=0 || ysize<=0) error("Bad image size.");
  37.     if(xsize*ysize>IMAGE_SIZE_LIMIT) error("Image size too big.");
  38.     image=gdImageCreate(xsize,ysize);
  39.     if(image==NULL) error("Image creation failure");
  40.     color_white=gdImageColorAllocate(image,255,255,255);
  41.     color_black=gdImageColorAllocate(image,0,0,0);
  42.     color_bounder=gdImageColorAllocate(image,1,2,3);
  43.     gdImageColorTransparent(image,color_white);
  44.     printf("Created image: %dx%d\n",xsize,ysize);
  45. }
  46.  
  47. void saveimage()
  48. {
  49.     int xsize, ysize;
  50.     int white2;
  51.     FILE *outf;
  52.     char *ff;
  53.     if(image==NULL) return;
  54.     xsize=image->sx; ysize=image->sy;
  55.     ff=outfile; while(isspace(*ff)) ff++;
  56.     for(; *outfile && *outfile!='\n';outfile++);
  57.     if(*outfile) *outfile++=0;
  58.     outf=fopen(ff,"w"); if(outf==NULL) error("unable to create gif output.");
  59.     printf("Output to %s.\n",ff);
  60.     if(compressratio==1) gdImageGif(image,outf);
  61.     else {
  62.         gdImagePtr image2;
  63.         int x2, y2;
  64.         x2=(xsize+1)/2; y2=(ysize+1)/2;
  65.         image2=gdImageCreate(x2,y2);
  66.         white2=gdImageColorAllocate(image2,255,255,255);
  67.         gdImageColorTransparent(image2,white2);
  68.         gdImageCopyResized(image2,image,0,0,0,0,x2,y2,xsize,ysize);
  69.         gdImageGif(image2,outf); gdImageDestroy(image2);
  70.     }
  71.     fclose(outf);
  72.     gdImageDestroy(image); image=NULL;
  73. }
  74.  
  75. int paintfont(FONT *f, int ch, int x, int y, int color)
  76. {
  77.     int i,j, xlen, ylen, xsize;
  78.     char *buf;
  79.     ch-=f->bc; x+=f->fh[ch].xstart; y+=f->fh[ch].ystart;
  80.     xlen=f->fh[ch].xmax; ylen=f->fh[ch].ymax;
  81.     xsize=(xlen+7)/8; buf=f->data+f->fh[ch].start;
  82.     for(j=0;j<ylen;j++) for(i=0;i<xlen;i++) {
  83.         if((buf[j*xsize+(i>>3)]&(1<<(i&7)))!=0)
  84.             gdImageSetPixel(image,x+i,y+j,color);
  85.     }
  86.     return f->fh[ch].w;
  87. }
  88.  
  89.     /* Make color according to string indication */
  90. void makecolor(char *p)
  91. {
  92.     int t, r,g,b;
  93.     char *p2;
  94.    
  95.     p=find_word_start(p); r=g=b=0;
  96.     p2=strchr(p,',');
  97.     if(p2==NULL) {  /* color name */
  98.         *find_word_end(p)=0;
  99.         t=search_list(colors,colorno,sizeof(colors[0]),p);
  100.         if(t<0) return;
  101.         r=colors[t].r; g=colors[t].g; b=colors[t].b;
  102.     }
  103.     else {  /* rgb values */
  104.         *p2++=0;
  105.         r=atoi(p); p=find_word_start(p2);
  106.         p2=strchr(p,','); if(p2==NULL) p2=p+strlen(p); else *p2++=0;
  107.         g=atoi(p); b=atoi(p2);
  108.     }
  109.     t=getcolor(r,g,b);
  110.     if(t!=-1) currentcolor=t;
  111. }
  112.