Subversion Repositories wimsdev

Rev

Rev 8195 | 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. #include "texgif.h"
  20.  
  21. gdImagePtr image=NULL;
  22. int color_white, color_black, color_bounder;
  23.  
  24. /* same in Flydraw/flylines.c */
  25.  
  26. int getcolor(int r, int g, int b)
  27. {
  28.     int col;
  29.     if(r>255) r=255; if(r<0) r=0;
  30.     if(g>255) g=255; if(g<0) g=0;
  31.     if(b>255) b=255; if(b<0) b=0;
  32.     col=gdImageColorExact(image, r, g, b);
  33.     if(col==-1) col=gdImageColorAllocate(image,r,g,b);
  34.     return col;
  35. }
  36.  
  37. void createimage(int xsize, int ysize)
  38. {
  39.     if(xsize<=0 || ysize<=0) texgif_error("Bad image size.");
  40.     if(xsize*ysize>IMAGE_SIZE_LIMIT) texgif_error("Image size too big.");
  41.     image=gdImageCreate(xsize,ysize);
  42.     if(image==NULL) texgif_error("Image creation failure");
  43.     color_white=gdImageColorAllocate(image,255,255,255);
  44.     color_black=gdImageColorAllocate(image,0,0,0);
  45.     color_bounder=gdImageColorAllocate(image,1,2,3);
  46.     gdImageColorTransparent(image,color_white);
  47.     printf("Created image: %dx%d\n",xsize,ysize);
  48. }
  49.  
  50. void saveimage()
  51. {
  52.     int xsize, ysize;
  53.     int white2;
  54.     FILE *outf;
  55.     char *ff;
  56.     if(image==NULL) return;
  57.     xsize=image->sx; ysize=image->sy;
  58.     ff=outfile; while(isspace(*ff)) ff++;
  59.     for(; *outfile && *outfile!='\n';outfile++);
  60.     if(*outfile) *outfile++=0;
  61.     outf=fopen(ff,"w"); if(outf==NULL) texgif_error("unable to create gif output.");
  62.     printf("Output to %s.\n",ff);
  63.     if(compressratio==1) gdImageGif(image,outf);
  64.     else {
  65.         gdImagePtr image2;
  66.         int x2, y2;
  67.         x2=(xsize+1)/2; y2=(ysize+1)/2;
  68.         image2=gdImageCreate(x2,y2);
  69.         white2=gdImageColorAllocate(image2,255,255,255);
  70.         gdImageColorTransparent(image2,white2);
  71.         gdImageCopyResized(image2,image,0,0,0,0,x2,y2,xsize,ysize);
  72.         gdImageGif(image2,outf); gdImageDestroy(image2);
  73.     }
  74.     fclose(outf);
  75.     gdImageDestroy(image); image=NULL;
  76. }
  77.  
  78. int paintfont(FONT *f, int ch, int x, int y, int color)
  79. {
  80.     int i,j, xlen, ylen, xsize;
  81.     char *buf;
  82.     ch-=f->bc; x+=f->fh[ch].xstart; y+=f->fh[ch].ystart;
  83.     xlen=f->fh[ch].xmax; ylen=f->fh[ch].ymax;
  84.     xsize=(xlen+7)/8; buf=f->data+f->fh[ch].start;
  85.     for(j=0;j<ylen;j++) for(i=0;i<xlen;i++) {
  86.         if((buf[j*xsize+(i>>3)]&(1<<(i&7)))!=0)
  87.             gdImageSetPixel(image,x+i,y+j,color);
  88.     }
  89.     return f->fh[ch].w;
  90. }
  91.  
  92.     /* Make color according to string indication */
  93. void makecolor(char *p)
  94. {
  95.     int t, r,g,b;
  96.     char *p2;
  97.  
  98.     p=find_word_start(p); r=g=b=0;
  99.     p2=strchr(p,',');
  100.     if(p2==NULL) {  /* color name */
  101.         *find_word_end(p)=0;
  102.         t=search_list(colors,colorno,sizeof(colors[0]),p);
  103.         if(t<0) return;
  104.         r=colors[t].r; g=colors[t].g; b=colors[t].b;
  105.     }
  106.     else {  /* rgb values */
  107.         *p2++=0;
  108.         r=atoi(p); p=find_word_start(p2);
  109.         p2=strchr(p,','); if(p2==NULL) p2=p+strlen(p); else *p2++=0;
  110.         g=atoi(p); b=atoi(p2);
  111.     }
  112.     t=getcolor(r,g,b);
  113.     if(t!=-1) currentcolor=t;
  114. }
  115.