Back to index

wims  3.65+svn20090927
image.c
Go to the documentation of this file.
00001 /*    Copyright (C) 2002-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018 /* tex 2 gif translator, image manipulating routines */
00019 
00020 gdImagePtr image=NULL;
00021 int color_white, color_black, color_bounder;
00022 
00023 int getcolor(int r, int g, int b)
00024 {
00025     int col;
00026     if(r>255) r=255; if(r<0) r=0;
00027     if(g>255) g=255; if(g<0) g=0;
00028     if(b>255) b=255; if(b<0) b=0;
00029     col=gdImageColorExact(image, r, g, b);
00030     if(col==-1) col=gdImageColorAllocate(image,r,g,b);
00031     return col;
00032 }
00033 
00034 void createimage(int xsize, int ysize)
00035 {
00036     if(xsize<=0 || ysize<=0) error("Bad image size.");
00037     if(xsize*ysize>IMAGE_SIZE_LIMIT) error("Image size too big.");
00038     image=gdImageCreate(xsize,ysize);
00039     if(image==NULL) error("Image creation failure");
00040     color_white=gdImageColorAllocate(image,255,255,255);
00041     color_black=gdImageColorAllocate(image,0,0,0);
00042     color_bounder=gdImageColorAllocate(image,1,2,3);
00043     gdImageColorTransparent(image,color_white);
00044 printf("Created image: %dx%d\n",xsize,ysize);
00045 }
00046 
00047 void saveimage()
00048 {
00049     int xsize, ysize;
00050     int white2;
00051     FILE *outf;
00052     char *ff;
00053     if(image==NULL) return;
00054     xsize=image->sx; ysize=image->sy;
00055     ff=outfile; while(isspace(*ff)) ff++;
00056     for(; *outfile && *outfile!='\n';outfile++);
00057     if(*outfile) *outfile++=0;
00058     outf=fopen(ff,"w"); if(outf==NULL) error("unable to create gif output.");
00059     printf("Output to %s.\n",ff);
00060     if(compressratio==1) gdImageGif(image,outf);
00061     else {
00062        gdImagePtr image2;
00063        int x2, y2;
00064        x2=(xsize+1)/2; y2=(ysize+1)/2;
00065        image2=gdImageCreate(x2,y2);
00066        white2=gdImageColorAllocate(image2,255,255,255);
00067        gdImageColorTransparent(image2,white2);
00068        gdImageCopyResized(image2,image,0,0,0,0,x2,y2,xsize,ysize);
00069        gdImageGif(image2,outf); gdImageDestroy(image2);
00070     }
00071     fclose(outf);
00072     gdImageDestroy(image); image=NULL;
00073 }
00074 
00075 int paintfont(FONT *f, int ch, int x, int y, int color)
00076 {
00077     int i,j, xlen, ylen, xsize;
00078     char *buf;
00079     ch-=f->bc; x+=f->fh[ch].xstart; y+=f->fh[ch].ystart;
00080     xlen=f->fh[ch].xmax; ylen=f->fh[ch].ymax;
00081     xsize=(xlen+7)/8; buf=f->data+f->fh[ch].start;
00082     for(j=0;j<ylen;j++) for(i=0;i<xlen;i++) {
00083        if((buf[j*xsize+(i>>3)]&(1<<(i&7)))!=0)
00084          gdImageSetPixel(image,x+i,y+j,color);
00085     }
00086     return f->fh[ch].w;
00087 }
00088 
00089        /* Make color according to string indication */
00090 void makecolor(char *p)
00091 {
00092     int t, r,g,b;
00093     char *p2;
00094     
00095     p=find_word_start(p); r=g=b=0;
00096     p2=strchr(p,','); if(p2==NULL) {      /* color name */
00097        *find_word_end(p)=0;
00098        t=search_list(colors,colorno,sizeof(colors[0]),p);
00099        if(t<0) return;
00100        r=colors[t].r; g=colors[t].g; b=colors[t].b;
00101     }
00102     else {    /* rgb values */
00103        *p2++=0;
00104        r=atoi(p); p=find_word_start(p2);
00105        p2=strchr(p,','); if(p2==NULL) p2=p+strlen(p); else *p2++=0;
00106        g=atoi(p); b=atoi(p2);
00107     }
00108     t=getcolor(r,g,b);
00109     if(t!=-1) currentcolor=t;
00110 }