Subversion Repositories wimsdev

Rev

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

  1. /*    Copyright (C) 1998-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. #include <errno.h>
  19. #include "flydraw.h"
  20.  
  21. /* Tikz things */
  22.  
  23. static char *tikz_color (int the_color)
  24. {
  25.   static char buf[50];
  26.   int r=gdImageRed(image,the_color);
  27.   int g=gdImageGreen(image,the_color);
  28.   int b=gdImageBlue(image,the_color);
  29.   sprintf(buf, "{rgb,255:red,%i;green,%i;blue,%i}",r,g,b);
  30.   return buf;
  31. }
  32.  
  33. static char *tikz_options (int the_color, int the_fill)
  34. {
  35.   static char buf[100];
  36.   if (the_color == gdBrushed)
  37.     the_color = tikz_brushColor;
  38.   sprintf (buf, "draw=%s,cap=round",tikz_color(the_color));
  39.   if (the_color != tikz_brushColor) strcat(buf, ",thin");
  40.   if (the_fill == -1 || the_fill == 2) strcat(buf, ",dashed");
  41.   if (the_fill == 1 || the_fill == 2)
  42.     sprintf(buf+strlen(buf), ",fill=%s", tikz_color(the_color));
  43.   return buf;
  44. }
  45.  
  46. #define flip(y) (sizey-1-(y))
  47.  
  48. /* bug in gdImageFillToBorder */
  49.  
  50. void patchgdImageFillToBorder (gdImagePtr im, int x, int y, int border, int color)
  51. {
  52.   if(x>=im->sx || x<0 || y>=im->sy || y<0) return;
  53.   gdImageFillToBorder(im,x,y,border,color);
  54. }
  55.  
  56. void patchgdImageFill (gdImagePtr im, int x, int y, int color)
  57. {
  58.   if(x>=im->sx || x<0 || y>=im->sy || y<0) return;
  59.   gdImageFill(im,x,y,color);
  60. }
  61.  
  62. #define DASHPIXELS 8
  63.  
  64. void myDashedLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color){
  65.   /**
  66.    * emulates gdImageDashedLine with gdImageSetStyle and gdImageLine
  67.    * GK: As gdImageDashedLine is slightly broken in latest libgd libraries,
  68.    * GK: I implemented an emulation named "myDashedLine"
  69.    **/
  70.  
  71.   int styleDashed[DASHPIXELS],i;
  72.   for (i=0; i< DASHPIXELS/2; i++) styleDashed[i]=color;
  73.   for (; i< DASHPIXELS; i++) styleDashed[i]=gdTransparent;
  74.   gdImageSetStyle(im, styleDashed, DASHPIXELS);
  75.   gdImageLine(im, x1, y1, x2, y2, gdStyled);
  76. }
  77.  
  78.  
  79. /* File opening: with security */
  80. FILE *open4read(char *n)
  81. {
  82.   char *p, *p1, *p2, namebuf[2048];
  83.   int t;
  84.   FILE *f;
  85.   n=find_word_start(n);
  86.   if(*n==0) return NULL;
  87.   p=getenv("flydraw_filebase");
  88.   p1=n+strlen(n)-4;if(p1<n || strcasecmp(p1,".gif")!=0) t=1; else t=0;
  89.   if(p!=NULL && *p!=0) {
  90.     char pbuf[MAX_LINELEN+1];
  91.     snprintf(pbuf,sizeof(pbuf),"%s",p);
  92.     p=find_word_start(pbuf); if(strstr(p,"..")!=NULL) return NULL;
  93.     if(*n=='/' || strstr(n,"..")!=NULL) return NULL;
  94.     /* prohibit unusual file/dir names */
  95.     for(p1=p;*p1;p1++)
  96.       if(!isalnum(*p1) && !isspace(*p1) && strchr("~_-/.",*p1)==NULL)
  97.         return NULL;
  98.     for(p1=n;*p1;p1++)
  99.       if(!isalnum(*p1) && !isspace(*p1) && strchr("~_-/.",*p1)==NULL)
  100.         return NULL;
  101.     f=NULL;
  102.     for(p1=p; *p1; p1=find_word_start(p2)) {
  103.       p2=find_word_end(p1);
  104.       if(*p2) *p2++=0;
  105.       snprintf(namebuf,sizeof(namebuf),"%s/%s",p1,n);
  106.       f=fopen(namebuf,"r"); if(f!=NULL) goto imgtype;
  107.     }
  108.     p1=getenv("w_wims_session");
  109.     if(p1!=NULL && strncmp(n,"insert",6)==0) {
  110.       snprintf(namebuf,sizeof(namebuf),"../s2/%s/%s",p1,n);
  111.       f=fopen(namebuf,"r");
  112.     }
  113.   }
  114.   else {
  115.     snprintf(namebuf,sizeof(namebuf),"%s",n);
  116.     f=fopen(namebuf,"r");
  117.   }
  118.   imgtype:
  119.   if(t && f!=NULL) {
  120.     char tbuf[1024],sbuf[4096];
  121.     fclose(f); f=NULL;
  122.     p1=getenv("TMPDIR"); if(p1==NULL || *p1==0) p1=".";
  123.     snprintf(tbuf,sizeof(tbuf),"%s/drawfile_.gif",p1);
  124.     snprintf(sbuf,sizeof(sbuf),"convert %s %s",namebuf,tbuf);
  125.     if (system(sbuf)) fly_error("system_failed");
  126.       f=fopen(tbuf,"r");
  127.   }
  128.   return f;
  129. }
  130.  
  131. /* Does nothing; just a comment. */
  132. void obj_comment(objparm *pm)
  133. {
  134.   return;
  135. }
  136.  
  137. /* define image size */
  138. void obj_size(objparm *pm)
  139. {
  140.   sizex=rint(pm->pd[0]); sizey=rint(pm->pd[1]);
  141.   if(sizex<0 || sizey<0 || sizex>MAX_SIZE || sizey>MAX_SIZE) {
  142.     fly_error("bad_size"); return;
  143.   }
  144.   if(image!=NULL) {
  145.     fly_error("size_already_defined"); return;
  146.   }
  147.   image=gdImageCreate(sizex,sizey);
  148.   if(tikz_file) fprintf(tikz_file,"\\clip (0,0) rectangle (%i, %i);\n", sizex, sizey);
  149.   if(image==NULL) fly_error("image_creation_failure");
  150.   else {
  151.     color_white=gdImageColorAllocate(image,255,255,255);
  152.     color_black=gdImageColorAllocate(image,0,0,0);
  153.     color_bounder=gdImageColorAllocate(image,1,2,3);
  154.     color_frame=gdImageColorAllocate(image,254,254,254);
  155.   }
  156. }
  157.  
  158. /* new image */
  159. void obj_new(objparm *pm)
  160. {
  161.   if(image) {
  162.     gdImageDestroy(image);image=NULL;
  163.   }
  164.   if(pm->pcnt>=2) { obj_size(pm); gdImageRectangle(image,0,0,sizex-1,sizey-1,color_frame);}
  165.   else sizex=sizey=0;
  166.   saved=0;
  167. }
  168.  
  169. /* new image */
  170. void obj_existing(objparm *pm)
  171. {
  172.   FILE *inf;
  173.   char *pp;
  174.  
  175.   if(image) {
  176.     gdImageDestroy(image);image=NULL;
  177.   }
  178.   pp=find_word_start(pm->str);*find_word_end(pp)=0;
  179.   inf=open4read(pp);
  180.   if(inf==NULL) {
  181.     fly_error("file_not_exist"); return;
  182.   }
  183.   image=gdImageCreateFromGif(inf); fclose(inf);
  184.   if(image==NULL) {
  185.     fly_error("bad_gif"); return;
  186.   }
  187.   sizex=image->sx; sizey=image->sy;
  188.   saved=0;
  189. }
  190. /* tikzfile */
  191. void obj_tikzfile(objparm *pm)
  192. {
  193.   char *p;
  194.   if(tikz_file){
  195.     fprintf(tikz_file,"\\end{tikzpicture}\n");
  196.     fclose(tikz_file);
  197.   }
  198.   p=find_word_start(pm->str); *find_word_end(p)=0;
  199.   snprintf(tikzfilename,sizeof(tikzfilename),"%s",p);
  200.   tikz_file=fopen(tikzfilename,"w");
  201.   if(!tikz_file)
  202.     fly_error("tikz_nopen");
  203.   fprintf(tikz_file,"\\begin{tikzpicture}\[x=0.02cm, y=0.02cm]\n");
  204. }
  205.  
  206. /* segment */
  207. void obj_line(objparm *pm)
  208. {
  209.   scale(pm->pd,pm->p,2);
  210.   if(pm->fill==-1 || pm->fill==2 )
  211.     myDashedLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],pm->color[0]);
  212.   else
  213.     gdImageLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],pm->color[0]);
  214.   if (tikz_file)
  215.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  216.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]));
  217.   if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
  218. }
  219.  
  220. void _obj_arrow(objparm *pm, int twoside)
  221. {
  222.   int l,xx,yy;
  223.   gdPoint ii[3];
  224.   double dx,dy,length,dd[6];
  225.   scale(pm->pd,pm->p,2);
  226.   xx=ii[0].x=pm->p[2];yy=ii[0].y=pm->p[3];
  227.   l=pm->pd[4];if(l<0) l=0; if(l>200) l=200;
  228.   scale2(pm->pd[0]-pm->pd[2],pm->pd[1]-pm->pd[3],&dx,&dy);
  229.   length=sqrt(dx*dx+dy*dy);
  230.   if(length<3 || l<5) goto stem;
  231.   dd[0]=l*dx/length; dd[1]=l*dy/length;
  232.   #define fat 0.27
  233.   dd[2]=dd[0]+dd[1]*fat; dd[3]=dd[1]-dd[0]*fat;
  234.   dd[4]=dd[0]-dd[1]*fat; dd[5]=dd[1]+dd[0]*fat;
  235.   ii[1].x=rint(dd[2])+ii[0].x; ii[1].y=rint(dd[3])+ii[0].y;
  236.   ii[2].x=rint(dd[4])+ii[0].x; ii[2].y=rint(dd[5])+ii[0].y;
  237.   gdImageFilledPolygon(image, ii,3,pm->color[0]);
  238.   if(tikz_file)
  239.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  240.       tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
  241.   if(vimg_enable) vimg_polyline(scale_buf,3,1);
  242.   xx=rint(dd[0])+ii[0].x;yy=rint(dd[1])+ii[0].y;
  243.   if(twoside) {
  244.     ii[0].x=pm->p[0]; ii[0].y=pm->p[1];
  245.     ii[1].x=-rint(dd[2])+ii[0].x; ii[1].y=-rint(dd[3])+ii[0].y;
  246.     ii[2].x=-rint(dd[4])+ii[0].x; ii[2].y=-rint(dd[5])+ii[0].y;
  247.     gdImageFilledPolygon(image, ii,3,pm->color[0]);
  248.   if(tikz_file)
  249.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  250.       tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
  251.   }
  252.   stem: if(pm->fill==-1 || pm->fill==2)
  253.     myDashedLine(image,pm->p[0],pm->p[1],xx,yy,pm->color[0]);
  254.   else
  255.     gdImageLine(image,pm->p[0],pm->p[1],xx,yy,pm->color[0]);
  256.   if(tikz_file)
  257.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  258.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),xx,flip(yy));
  259.   if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
  260. }
  261.  
  262. /* Arrow */
  263. void obj_arrow(objparm *pm)
  264. {
  265.   _obj_arrow(pm,0);
  266. }
  267.  
  268. /* 2-sided arrow */
  269. void obj_arrow2(objparm *pm)
  270. {
  271.    _obj_arrow(pm,1);
  272. }
  273.  
  274. void _obj_arrows(objparm *pm, int twoside)
  275. {
  276.   #define fat 0.27
  277.   int i,l,xx,yy;
  278.   gdPoint ii[3];
  279.   double dx,dy,length,dd[6];
  280.   l=pm->pd[0];if(l<0) l=0; if(l>200) l=200;
  281.   scale(pm->pd+1,pm->p+1,pm->pcnt/2);
  282.   for (i=1;i<pm->pcnt;i+=4){
  283.     xx=ii[0].x=pm->p[i+2];yy=ii[0].y=pm->p[i+3];
  284.     scale2(pm->pd[i]-pm->pd[i+2],pm->pd[i+1]-pm->pd[i+3],&dx,&dy);
  285.     length=sqrt(dx*dx+dy*dy);
  286.     if(length<3 || l<5) goto stem;
  287.     dd[0]=l*dx/length; dd[1]=l*dy/length;
  288.     dd[2]=dd[0]+dd[1]*fat; dd[3]=dd[1]-dd[0]*fat;
  289.     dd[4]=dd[0]-dd[1]*fat; dd[5]=dd[1]+dd[0]*fat;
  290.     ii[1].x=rint(dd[2])+ii[0].x; ii[1].y=rint(dd[3])+ii[0].y;
  291.     ii[2].x=rint(dd[4])+ii[0].x; ii[2].y=rint(dd[5])+ii[0].y;
  292.     gdImageFilledPolygon(image, ii,3,pm->color[0]);
  293.     if(tikz_file)
  294.   fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  295.         tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
  296.       xx=rint(dd[0])+ii[0].x;yy=rint(dd[1])+ii[0].y;
  297.       if(twoside) {
  298.   ii[0].x=pm->p[i]; ii[0].y=pm->p[i+1];
  299.   ii[1].x=-rint(dd[2])+ii[0].x; ii[1].y=-rint(dd[3])+ii[0].y;
  300.   ii[2].x=-rint(dd[4])+ii[0].x; ii[2].y=-rint(dd[5])+ii[0].y;
  301.   gdImageFilledPolygon(image, ii,3,pm->color[0]);
  302.   if(tikz_file)
  303.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  304.         tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
  305.       }
  306.     stem: if(pm->fill==-1 || pm->fill==2)
  307.   myDashedLine(image,pm->p[i],pm->p[i+1],xx,yy,pm->color[0]);
  308.       else
  309.   gdImageLine(image,pm->p[i],pm->p[i+1],xx,yy,pm->color[0]);
  310.       if(tikz_file)
  311.   fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  312.     tikz_options(pm->color[0],pm->fill),pm->p[i],flip(pm->p[i+1]),xx,flip(yy));
  313.     }
  314. }
  315.  
  316. /* Arrows */
  317. void obj_arrows(objparm *pm)
  318. {
  319.   _obj_arrows(pm,0);
  320. }
  321.  
  322. /* 2-sided arrows */
  323. void obj_arrows2(objparm *pm)
  324. {
  325.    _obj_arrows(pm,1);
  326. }
  327.  
  328. /* horizontal line */
  329. void obj_hline(objparm *pm)
  330. {
  331.   scale(pm->pd,pm->p,1);
  332.   if(pm->fill==-1 || pm->fill==2)
  333.     myDashedLine(image,0,pm->p[1],sizex,pm->p[1],pm->color[0]);
  334.   else
  335.     gdImageLine(image,0,pm->p[1],sizex,pm->p[1],pm->color[0]);
  336.   if (tikz_file)
  337.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  338.       tikz_options(pm->color[0],pm->fill),0,flip(pm->p[1]),sizex,flip(pm->p[1]));
  339. }
  340.  
  341. static void line_extend(int x1, int y1, int x2, int y2, int* xend, int *yend)
  342. {
  343.   double t1, t2, t3;
  344.   if (x2 != x1){t1 = (x2 > x1) ? sizex : 0; t1 = (t1 - x1)/(x2 - x1);}
  345.   else t1 = INFINITY;
  346.   if (y2 != y1){t2 = (y2 > y1) ? sizey : 0; t2 = (t2 - y1)/(y2 - y1);}
  347.   else t2 = INFINITY;
  348.   t3 = (t1 < t2) ? t1 : t2;
  349.   *xend = x1+t3*(x2-x1);
  350.   *yend = y1+t3*(y2-y1);
  351. }
  352.  
  353. /* halfline */
  354. void obj_halfline(objparm *pm)
  355. {
  356.   int xend,yend;
  357.   scale(pm->pd,pm->p,2);
  358.   line_extend(pm->p[0],pm->p[1],pm->p[2],pm->p[3],&xend,&yend);
  359.   if(pm->fill==-1 || pm->fill==2)
  360.     myDashedLine(image,pm->p[0],pm->p[1],xend,yend,pm->color[0]);
  361.   else
  362.     gdImageLine(image,pm->p[0],pm->p[1],xend,yend,pm->color[0]);
  363.   if (tikz_file)
  364.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  365.       tikz_options(pm->color[0],pm->fill),
  366.       pm->p[0],flip(pm->p[1]),xend,flip(yend));
  367. }
  368.  
  369. void obj_fullline(objparm *pm)
  370. {
  371.   int xstart,ystart,xend,yend;
  372.   scale(pm->pd,pm->p,2);
  373.   line_extend(pm->p[0],pm->p[1],pm->p[2],pm->p[3],&xend,&yend);
  374.   line_extend(pm->p[2],pm->p[3],pm->p[0],pm->p[1],&xstart,&ystart);
  375.   if(pm->fill==-1 || pm->fill==2)
  376.     myDashedLine(image,xstart,ystart,xend,yend,pm->color[0]);
  377.   else
  378.     gdImageLine(image,xstart,ystart,xend,yend,pm->color[0]);
  379.   if (tikz_file)
  380.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  381.       tikz_options(pm->color[0],pm->fill),xstart,flip(ystart),xend,flip(yend));
  382. }
  383.  
  384. void obj_fulllines(objparm *pm)
  385. {
  386.   int i,xstart,ystart,xend,yend;
  387.   scale(pm->pd,pm->p,pm->pcnt/2);
  388.   if (tikz_file)
  389.       fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  390.   for (i=0;i<pm->pcnt;i+=4){
  391.     line_extend(pm->p[i],pm->p[i+1],pm->p[i+2],pm->p[i+3],&xend,&yend);
  392.     line_extend(pm->p[i+2],pm->p[i+3],pm->p[i],pm->p[i+1],&xstart,&ystart);
  393.     if(pm->fill==-1 || pm->fill==2)
  394.       myDashedLine(image,xstart,ystart,xend,yend,pm->color[0]);
  395.     else
  396.       gdImageLine(image,xstart,ystart,xend,yend,pm->color[0]);
  397.     if (tikz_file)
  398.       fprintf(tikz_file, "(%i,%i)--(%i, %i)",xstart,flip(ystart),xend,flip(yend));
  399.   }
  400.   if (tikz_file)
  401.       fprintf(tikz_file, ";\n");
  402. }
  403.  
  404. /* vertical line */
  405. void obj_vline(objparm *pm)
  406. {
  407.   scale(pm->pd,pm->p,1);
  408.   if(pm->fill==-1 || pm->fill==2)
  409.     myDashedLine(image,pm->p[0],0,pm->p[0],sizey,pm->color[0]);
  410.   else
  411.     gdImageLine(image,pm->p[0],0,pm->p[0],sizey,pm->color[0]);
  412.   if (tikz_file)
  413.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  414.       tikz_options(pm->color[0],pm->fill),pm->p[0],0,pm->p[0],sizey);
  415. }
  416.  
  417. /* dashed line */
  418. void obj_dline(objparm *pm)
  419. {
  420.   scale(pm->pd,pm->p,2);
  421.   myDashedLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3], pm->color[0]);
  422.   if (tikz_file)
  423.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  424.       tikz_options(pm->color[0],-1),
  425.       pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]));
  426. }
  427.  
  428. /* parallel lines.
  429.  * x1,y1,x2,y2,xv,yv,n,color */
  430. void obj_parallel(objparm *pm)
  431. {
  432.   int i, n, xi,yi;
  433.   double xv,yv;
  434.   n=pm->pd[6]; if(n<0) return; if(n>256) n=256;
  435.   scale(pm->pd,pm->p,3);
  436.   scale2(pm->pd[4],pm->pd[5],&xv,&yv);
  437.   if (tikz_file)
  438.     fprintf(tikz_file, "\\draw\[%s]", tikz_options(pm->color[0],pm->fill));
  439.   for(i=0;i<n;i++) {
  440.     xi=rint(i*xv); yi=rint(i*yv);
  441.     if(pm->fill==-1 || pm->fill==2)
  442.       myDashedLine(image,pm->p[0]+xi,pm->p[1]+yi,pm->p[2]+xi,pm->p[3]+yi,
  443.         pm->color[0]);
  444.     else
  445.       gdImageLine(image,pm->p[0]+xi,pm->p[1]+yi,pm->p[2]+xi,pm->p[3]+yi,
  446.         pm->color[0]);
  447.     if (tikz_file)
  448.       fprintf(tikz_file, "(%i,%i)--(%i, %i)",
  449.         pm->p[0]+xi,flip(pm->p[1]+yi),pm->p[2]+xi,flip(pm->p[3]+yi));
  450.     if(vimg_enable) vimg_line(scale_buf[0]+i*(scale_buf[4]-transx),
  451.       scale_buf[1]+i*(scale_buf[5]-transy),
  452.       scale_buf[2]+i*(scale_buf[4]-transx),
  453.       scale_buf[3]+i*(scale_buf[5]-transy));
  454.   }
  455.  if(tikz_file) fprintf(tikz_file,";\n");
  456. }
  457.  
  458. /* rectangle */
  459. void obj_rect(objparm *pm)
  460. {
  461.   int x1,y1,x2,y2;
  462.   scale(pm->pd,pm->p,2);
  463.   x1=min(pm->p[0],pm->p[2]); x2=max(pm->p[0],pm->p[2]);
  464.   y1=min(pm->p[1],pm->p[3]); y2=max(pm->p[1],pm->p[3]);
  465.   if(pm->fill==1 || pm->fill==2)
  466.     gdImageFilledRectangle(image,x1,y1,x2,y2,pm->color[0]);
  467.   else
  468.     gdImageRectangle(image,x1,y1,x2,y2,pm->color[0]);
  469.   if(tikz_file)
  470.     fprintf(tikz_file,
  471.       "\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
  472.       tikz_options(pm->color[0],pm->fill),x1,flip(y1),x2,flip(y2));
  473.   if(vimg_enable) vimg_rect(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
  474. }
  475.  
  476. /* square */
  477. void obj_square(objparm *pm)
  478. {
  479.   int w,h;
  480.   scale(pm->pd,pm->p,1);
  481.   w=rint(pm->pd[2]); h=rint(pm->pd[2]);
  482.   if(pm->fill==1 || pm->fill==2)
  483.     gdImageFilledRectangle(image,pm->p[0],pm->p[1],
  484.         pm->p[0]+w,pm->p[1]+h,pm->color[0]);
  485.   else
  486.     gdImageRectangle(image,pm->p[0],pm->p[1],
  487.                pm->p[0]+w,pm->p[1]+h,pm->color[0]);
  488.   if(tikz_file){
  489.     fprintf(tikz_file,
  490.       "\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
  491.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),
  492.       pm->p[0]+w,flip(pm->p[1]+h));
  493.   }
  494.   if(vimg_enable) vimg_rect(scale_buf[0],scale_buf[1],
  495.                       scale_buf[0]+pm->pd[2],scale_buf[1]+pm->pd[2]);
  496. }
  497.  
  498. /* triangle */
  499. void obj_triangle(objparm *pm)
  500. {
  501.   scale(pm->pd,pm->p,3);
  502.   int i,n=2;
  503.   if(pm->fill!=0){
  504.     if(pm->fill==-1 || pm->fill==2){
  505.       for(i=0;i<n;i+=1)
  506.         myDashedLine(image,pm->p[2*i],pm->p[2*i+1],pm->p[2*i+2],pm->p[2*i+3],pm->color[0]);
  507.       myDashedLine(image,pm->p[2*n],pm->p[2*n+1],pm->p[0],pm->p[1],pm->color[0]);
  508.     }
  509.     if(pm->fill==1 || pm->fill==2)
  510.       gdImageFilledPolygon(image,(gdPointPtr) pm->p,3,pm->color[0]);
  511.   }
  512.   else
  513.     gdImagePolygon(image,(gdPointPtr) pm->p,3,pm->color[0]);
  514.   if (tikz_file)
  515.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  516.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]),pm->p[4],flip(pm->p[5]));
  517.   if(vimg_enable) vimg_polyline(scale_buf,3,1);
  518. }
  519.  
  520. /* polygon */
  521. void obj_poly(objparm *pm)
  522. {
  523.   int cnt,i;
  524.   cnt=(pm->pcnt)/2;
  525.   scale(pm->pd,pm->p,cnt);
  526.   if(pm->fill==1 || pm->fill==2)
  527.     gdImageFilledPolygon(image,(gdPointPtr) pm->p,cnt,pm->color[0]);
  528.   else
  529.     gdImagePolygon(image,(gdPointPtr) pm->p,cnt,pm->color[0]);
  530.   if(tikz_file){
  531.     fprintf(tikz_file,"\\draw\[%s] (%i,%i) --",tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]));
  532.     for (i= 1; i<cnt; i++)
  533.         fprintf(tikz_file,"(%i,%i)--", pm->p[2*i],flip(pm->p[2*i+1]));
  534.     fprintf(tikz_file," cycle;\n");
  535.   }
  536.   if(vimg_enable) vimg_polyline(scale_buf,cnt,1);
  537. }
  538.  
  539. /* rays */
  540. void obj_rays(objparm *pm)
  541. {
  542.   int i, n;
  543.   n=(pm->pcnt)/2;
  544.   scale(pm->pd,pm->p,n);
  545.   if (tikz_file)
  546.     fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  547.   for(i=2;i<2*n;i+=2) {
  548.     if(pm->fill==-1 || pm->fill==2)
  549.       myDashedLine(image,pm->p[0],pm->p[1],pm->p[i],pm->p[i+1],pm->color[0]);
  550.     else
  551.       gdImageLine(image,pm->p[0],pm->p[1],pm->p[i],pm->p[i+1],pm->color[0]);
  552.     if (tikz_file)
  553.       fprintf(tikz_file, "(%i,%i) -- (%i,%i)",
  554.         pm->p[0],flip(pm->p[1]),pm->p[i],flip(pm->p[i+1]));
  555.     if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],
  556.                         scale_buf[i],scale_buf[i+1]);
  557.   }
  558.   if (tikz_file) fprintf(tikz_file,";\n");
  559. }
  560. /* crosshair */
  561. void obj_crosshair(objparm *pm)
  562. {
  563.   scale(pm->pd,pm->p,2);
  564.   gdImageLine(image,pm->p[0]+width2,pm->p[1]+width2,pm->p[0]-width2,pm->p[1]-width2,pm->color[0]);
  565.   gdImageLine(image,pm->p[0]-width2,pm->p[1]+width2,pm->p[0]+width2,pm->p[1]-width2,pm->color[0]);
  566.   if (tikz_file){
  567.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  568.       tikz_options(pm->color[0],pm->fill),pm->p[0]+width2,flip(pm->p[1]+width2),pm->p[0]-width2,flip(pm->p[1]-width2));
  569.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  570.       tikz_options(pm->color[0],pm->fill),pm->p[0]-width2,flip(pm->p[1]+width2),pm->p[0]+width2,flip(pm->p[1]-width2));
  571.   }
  572. }
  573. /* crosshairs */
  574.  
  575. void obj_crosshairs(objparm *pm)
  576. {
  577.   int i, n;
  578.   n=(pm->pcnt)/2;
  579.   scale(pm->pd,pm->p,n);
  580.   if (tikz_file)
  581.       fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  582.   for(i=0;i<2*n;i+=2) {
  583.     gdImageLine(image,pm->p[i]+width2,pm->p[i+1]+width2,pm->p[i]-width2,pm->p[i+1]-width2,pm->color[0]);
  584.     gdImageLine(image,pm->p[i]-width2,pm->p[i+1]+width2,pm->p[i]+width2,pm->p[i+1]-width2,pm->color[0]);
  585.     if (tikz_file){
  586.       fprintf(tikz_file, "(%i, %i) -- (%i, %i) ",
  587.         pm->p[i]+width2,flip(pm->p[i+1]+width2),pm->p[i]-width2,flip(pm->p[i+1]-width2));
  588.       fprintf(tikz_file, "(%i, %i) -- (%i, %i)",
  589.         pm->p[i]-width2,flip(pm->p[i+1]+width2),pm->p[i]+width2,flip(pm->p[i+1]-width2));
  590.     }
  591.   }
  592.   if (tikz_file) fprintf(tikz_file,";\n");
  593. }
  594.  
  595.  
  596. /* segments */
  597. void obj_lines(objparm *pm)
  598. {
  599.  int i, n;
  600.  n=(pm->pcnt)/2;
  601.  scale(pm->pd,pm->p,n);
  602.  if (tikz_file){
  603.    fprintf(tikz_file,"\\draw\[%s] (%i,%i)",
  604.      tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]));
  605.    for(i=2;i<2*n;i+=2)
  606.      fprintf(tikz_file, "--(%i,%i)",pm->p[i],flip(pm->p[i+1]));
  607.    fprintf(tikz_file, ";\n");
  608.  }
  609.  for(i=2;i<2*n;i+=2){
  610.   if(pm->fill==-1 || pm->fill==2 )
  611.     myDashedLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
  612.   else
  613.     gdImageLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
  614.   if(vimg_enable) vimg_polyline(scale_buf,n,0);
  615.   }
  616. }
  617. /*segments from x1,y1 to x2,y2, x3,y3 to x4,y4, etc */
  618. void obj_segments(objparm *pm)
  619. {
  620.   int i, n;
  621.   n=(pm->pcnt)/4;
  622.   scale(pm->pd,pm->p,2*n);
  623.   if (tikz_file)
  624.     fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  625.   for(i=0;i<4*n;i+=4){
  626.     if(pm->fill==-1 || pm->fill==2)
  627.       myDashedLine(image,pm->p[i],pm->p[i+1],pm->p[i+2],pm->p[i+3],pm->color[0]);
  628.     else
  629.       gdImageLine(image,pm->p[i],pm->p[i+1],pm->p[i+2],pm->p[i+3],pm->color[0]);
  630.     if (tikz_file)
  631.       fprintf(tikz_file, "(%i,%i)--(%i,%i)",
  632.        pm->p[i],flip(pm->p[i+1]),pm->p[i+2],flip(pm->p[i+3]));
  633.   }
  634.   if(tikz_file) fprintf(tikz_file, ";\n");
  635. }
  636. /* segments */
  637. void obj_dlines(objparm *pm)
  638. {
  639.  int i, n;
  640.  n=(pm->pcnt)/2;
  641.  scale(pm->pd,pm->p,n);
  642.  if (tikz_file){
  643.    fprintf(tikz_file,"\\draw\[%s] (%i,%i)",
  644.      tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]));
  645.    for(i=2;i<2*n;i+=2)
  646.      fprintf(tikz_file, "--(%i,%i)",pm->p[i],flip(pm->p[i+1]));
  647.    fprintf(tikz_file, ";\n");
  648.  }
  649.  for(i=2;i<2*n;i+=2)
  650.    myDashedLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
  651.  if(vimg_enable) vimg_polyline(scale_buf,n,0);
  652. }
  653.  
  654. /* points */
  655. void obj_points(objparm *pm)
  656. {
  657.  int i, n;
  658.  n=(pm->pcnt)/2;
  659.  scale(pm->pd,pm->p,n);
  660.  for(i=0;i<2*n;i+=2) gdImageSetPixel(image,pm->p[i],pm->p[i+1],pm->color[0]);
  661.  if(tikz_file) {
  662.    fprintf(tikz_file, "\\draw\[%s]", tikz_options(pm->color[0],pm->fill));
  663.    for(i=0;i<2*n;i+=2)
  664.      fprintf(tikz_file,"(%i,%i)circle(1pt)",pm->p[i],flip(pm->p[i+1]));
  665.    fprintf(tikz_file,";\n");
  666.  }
  667. }
  668.  
  669. /* lattice.
  670.  * x0,y0,xv1,yv1,xv2,yv2,n1,n2,color */
  671. void obj_lattice(objparm *pm)
  672. {
  673.  int n1,n2,i1,i2,xi1,yi1,xi2,yi2;
  674.  double xv1,xv2,yv1,yv2;
  675.  n1=pm->pd[6];n2=pm->pd[7]; if(n1<0 || n2<0) return;
  676.  if(n1>256) n1=256;
  677.  if(n2>256) n2=256;
  678.  scale(pm->pd,pm->p,1);
  679.  scale2(pm->pd[2],pm->pd[3],&xv1,&yv1);
  680.  scale2(pm->pd[4],pm->pd[5],&xv2,&yv2);
  681.  if(tikz_file)
  682.    fprintf(tikz_file,"\\draw[%s]", tikz_options(pm->color[0],0));
  683.  for(i1=0;i1<n1;i1++) {
  684.    xi1=rint(i1*xv1)+pm->p[0]; yi1=rint(i1*yv1)+pm->p[1];
  685.    for(i2=0;i2<n2;i2++) {
  686.      xi2=i2*xv2+xi1;yi2=i2*yv2+yi1;
  687.      gdImageSetPixel(image,xi2,yi2,pm->color[0]);
  688.      if(tikz_file) fprintf(tikz_file,"(%i,%i)circle(1pt)",xi2,flip(yi2));
  689.    }
  690.  }
  691.  if(tikz_file) fprintf(tikz_file,";\n");
  692. }
  693.  
  694. /* arc */
  695. void obj_arc(objparm *pm)
  696. {
  697.   scale(pm->pd,pm->p,1);
  698.   pm->p[2]=rint(pm->pd[2]*xscale); pm->p[3]=rint(pm->pd[3]*yscale);
  699.   gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],
  700.            pm->pd[4],pm->pd[5],pm->color[0]);
  701.   if(tikz_file) {
  702.     pm->pd[4]-=360*rint(pm->pd[4]/360);
  703.     pm->pd[5]-=360*rint(pm->pd[5]/360);
  704.     if(pm->pd[4]>pm->pd[5]) pm->pd[5]+=360;
  705.     fprintf(tikz_file,
  706.     "\\draw\[%s, domain=%f:%f] plot ({%i+%f*cos(\\x)}, {%i+%f*sin(\\x)});\n",
  707.     tikz_options(pm->color[0],pm->fill),
  708.       pm->pd[4],pm->pd[5],pm->p[0],pm->p[2]*0.5,flip(pm->p[1]),pm->p[3]*(-0.5));
  709.   }
  710.   /*FIXME echelle mauvaise*/
  711.   if(vimg_enable) vimg_arc(scale_buf[0],scale_buf[1],
  712.                      0.5*pm->pd[2],0.5*pm->pd[3],pm->pd[4],pm->pd[5]);
  713. }
  714.  
  715. void obj_angle(objparm *pm)
  716. {
  717.   const double DEG=M_PI/180;
  718.   double dpts[6];
  719.   int pts[6], wx=rint(2*pm->pd[2]*xscale), wy=rint(2*pm->pd[2]*yscale);
  720.   dpts[0]=pm->pd[0];
  721.   dpts[1]=pm->pd[1];
  722.   dpts[2]=pm->pd[0]+pm->pd[2]*cos(DEG*pm->pd[3]);
  723.   dpts[3]=pm->pd[1]+pm->pd[2]*sin(DEG*pm->pd[3]);
  724.   dpts[4]=pm->pd[0]+pm->pd[2]*cos(DEG*pm->pd[4]);
  725.   dpts[5]=pm->pd[1]+pm->pd[2]*sin(DEG*pm->pd[4]);
  726.   scale(dpts, pts, 3);
  727.   gdImageLine(image,pts[0],pts[1],pts[2],pts[3],pm->color[0]);
  728.   gdImageLine(image,pts[0],pts[1],pts[4],pts[5],pm->color[0]);
  729.   gdImageArc(image,pts[0],pts[1],wx,wy,pm->pd[3],pm->pd[4],pm->color[0]);
  730.   if(tikz_file) {
  731.     fprintf(tikz_file, "\\draw\[%s, domain=%f:%f] plot ({%i+%f*cos(\\x)}, {%i+%f*sin(\\x)});\n",
  732.       tikz_options(pm->color[0],pm->fill),pm->pd[3],pm->pd[4],pts[0],wx*0.5,flip(pts[1]),wy*(-0.5));
  733.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) (%i, %i) -- (%i, %i);\n",
  734.         tikz_options(pm->color[0],pm->fill),pts[0],flip(pts[1]),pts[2],flip(pts[3]),pts[0],flip(pts[1]),pts[4],flip(pts[5]));
  735.   }
  736. }
  737. /* Ellipse: centre 0,1, width 2, hight 3, color 4,5,6 */
  738. void obj_ellipse(objparm *pm)
  739. {
  740.   scale(pm->pd,pm->p,1);
  741.   pm->p[2]=pm->pd[2]*xscale; pm->p[3]=pm->pd[3]*yscale;
  742.   if(pm->fill==1 || pm->fill==2) {
  743.     gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
  744.              color_bounder);
  745.     patchgdImageFillToBorder(image,pm->p[0],pm->p[1],
  746.                     color_bounder,pm->color[0]);
  747.   }
  748.   gdImageArc(image,pm->p[0],pm->p[1],rint(pm->p[2]),rint(pm->p[3]),0,360,pm->color[0]);
  749.   if(tikz_file) fprintf(tikz_file,"\\draw\[%s] (%i,%i) ellipse (%i and %i);\n\n",
  750.     tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),(int) rint(0.5*pm->p[2]),(int) rint(0.5*pm->p[3]));
  751.   if(vimg_enable) vimg_ellipse(scale_buf[0],scale_buf[1],0.5*pm->pd[2],0.5*pm->pd[3]);
  752. }
  753.  
  754. /* Circle radius in pixels*/
  755. void obj_circle(objparm *pm)
  756. {
  757.   scale(pm->pd,pm->p,1);
  758.   pm->p[2]=rint(pm->pd[2]); pm->p[3]=rint(pm->pd[2]);
  759.   if(pm->fill==1 || pm->fill==2) {
  760.     gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
  761.              color_bounder);
  762.     patchgdImageFillToBorder(image,pm->p[0],pm->p[1],
  763.                     color_bounder,pm->color[0]);
  764.   }
  765.   gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,pm->color[0]);
  766.   if(tikz_file) fprintf(tikz_file, "\\draw\[%s] (%i, %i) circle (%i);\n",
  767.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),(int) rint(pm->pd[2]/2.));
  768. }
  769. /* Circle, radius in xrange */
  770. void obj_circles(objparm *pm)
  771. {
  772.   int i, n;
  773.   n=(pm->pcnt)/3;
  774.   for (i=0; i<n; ++i) scale(pm->pd+3*i, pm->p+3*i, 1);
  775.   if (tikz_file) fprintf(tikz_file,"\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  776.   for(i=0;i<3*n;i+=3){
  777.     if(pm->fill==1 || pm->fill==2) {
  778.       gdImageArc(image,pm->p[i],pm->p[i+1],
  779.         rint(2*pm->pd[i+2]*xscale),rint(2*pm->pd[i+2]*xscale),0,360,
  780.           color_bounder);
  781.       patchgdImageFillToBorder(image,pm->p[i],pm->p[i+1],
  782.                     color_bounder,pm->color[0]);
  783.     }
  784.     gdImageArc(image,pm->p[i],pm->p[i+1],
  785.         rint(2*pm->pd[i+2]*xscale),rint(2*pm->pd[i+2]*xscale),0,360,pm->color[0]);
  786.     if (tikz_file){
  787.       fprintf(tikz_file, "(%i, %i) circle (%i and %i)",
  788.         pm->p[i],flip(pm->p[i+1]),(int) rint(pm->pd[i+2]*xscale),(int) rint(pm->pd[i+2]*xscale));
  789.     }
  790.   }
  791.   if (tikz_file) fprintf(tikz_file,";\n");
  792. }
  793. /* ellipse, width in xrange,height in yrange */
  794. void obj_ellipses(objparm *pm)
  795. {
  796.   int i, n;
  797.   n=(pm->pcnt)/4;
  798.   for (i=0; i<n; ++i) scale(pm->pd+4*i, pm->p+4*i, 1);
  799.   if (tikz_file) fprintf(tikz_file,"\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  800.   for(i=0;i<4*n;i+=4){
  801.     if(pm->fill==1 || pm->fill==2){
  802.       gdImageArc(image,pm->p[i],pm->p[i+1],
  803.         rint(pm->pd[i+2]*xscale),rint(pm->pd[i+2]*xscale),0,360,
  804.         color_bounder);
  805.       patchgdImageFillToBorder(image,pm->p[i],pm->p[i+1],
  806.         color_bounder,pm->color[0]);
  807.     } else
  808.       gdImageArc(image,pm->p[i],pm->p[i+1],rint(pm->pd[i+2]*xscale),rint(pm->pd[i+3]*yscale),0,360,pm->color[0]);
  809.     if (tikz_file){
  810.       fprintf(tikz_file, "(%i, %i) circle (%i and %i)",
  811.         pm->p[i],flip(pm->p[i+1]),(int) rint(pm->pd[i+2]*xscale/2),(int) rint(pm->pd[i+3]*yscale/2));
  812.     }
  813.   }
  814.   if (tikz_file) fprintf(tikz_file,";\n");
  815. }
  816. typedef enum tikz_fill_options {grid, dot, hatch, diamond} tfo;
  817. int compar(const void *a, const void *b) {return *(int *)b - *(int *)a;}
  818.  
  819. static void tikz_fill(int x, int y, int nx, int ny, int wall, int the_color, tfo opt)
  820. {
  821.   int numpix=sizex*sizey, top = 0, a, c, cy, lx=0, rx=0, seed, nt,ct=0;
  822.   int *stack = xmalloc(numpix*sizeof(int));
  823.   int *stack2 = xmalloc(numpix*sizeof(int));
  824.   if (nx==0) nx=1;
  825.   if (ny==0) ny=1;
  826.   nt = abs(nx*ny);
  827.   seed = gdImageGetPixel(image,x,y);
  828.   char *check = xmalloc(numpix);
  829.   while (numpix--) check[numpix]=0;
  830.   a=x+y*sizex;
  831.   check[a]=1;
  832.   stack[top++]=a;
  833.   while(top)
  834.     {
  835.       a = stack[--top];
  836.       x = a%sizex; y = a/sizex;
  837.       c = gdImageGetPixel(image,x,y);
  838.       if ((wall && (c == wall))||(!wall && c!=seed))
  839.         continue;
  840.       if (x>0 && !check[a-1]) {check[a-1]=1; stack[top++]=a-1;}
  841.       if (x+1<sizex && !check[a+1]) {check[a+1]=1; stack[top++]=a+1;}
  842.       if (y>0 && !check[a-sizex]) {check[a-sizex]=1; stack[top++]=a-sizex;}
  843.       if (y+1<sizey && !check[a+sizex]) {check[a+sizex]=1; stack[top++]=a+sizex;}
  844.       switch(opt)
  845.   {
  846.   case grid: if(((x%nx)!=(nx/2))&&((y%ny)!=(ny/2))) continue; break;
  847.   case dot: if(x%nx!=nx/2||y%ny!=ny/2) continue; break;
  848.   case hatch: if((ny*x-nx*y+nt)%nt) continue; break;
  849.   case diamond: if((ny*x+nx*y)%nt && (ny*x-nx*y+nt)%nt)continue;
  850.   default: break;
  851.   }
  852.       stack2[ct++]=a;
  853.     }
  854.   free(stack);
  855.   free(check);
  856.   if (ct==0) return;
  857.   fprintf(tikz_file,"\\draw\[%s]", tikz_options(the_color,1));
  858.   qsort(stack2, ct, sizeof(int), compar);
  859.   cy=-1;
  860.   while (ct--)
  861.     {
  862.       a = stack2[ct];
  863.       x = a%sizex; y = a/sizex;
  864.       if (y != cy || x != rx+1)
  865.       {
  866.   if (cy >= 0)
  867.     fprintf(tikz_file,
  868.       "(%.2f,%.2f)rectangle(%.2f,%.2f)",
  869.       lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
  870.   cy = y;
  871.   lx = rx = x;
  872.       }
  873.       else
  874.   rx++;
  875.     }
  876.   fprintf(tikz_file,
  877.     "(%.2f,%.2f)rectangle(%.2f,%.2f);\n",
  878.      lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
  879.   free(stack2);
  880. }
  881. void obj_dashed(objparm *pm){ dashed=1;}
  882. void obj_filled(objparm *pm){ filled=1;}
  883.  
  884. /* flood fill */
  885. void obj_fill(objparm *pm)
  886. {
  887.   scale(pm->pd,pm->p,1);
  888.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,0,pm->color[0],grid);
  889.   patchgdImageFill(image,pm->p[0],pm->p[1],pm->color[0]);
  890. }
  891.  
  892. /* flood fill to border*/
  893. void obj_fillb(objparm *pm)
  894. {
  895.   scale(pm->pd,pm->p,1);
  896.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,pm->color[0],pm->color[1],grid);
  897.   patchgdImageFillToBorder(image,pm->p[0],pm->p[1],pm->color[0],pm->color[1]);
  898. }
  899.  
  900. gdImagePtr himg;
  901.  
  902. int makehatchimage(int x, int y, int px, int py, int col)
  903. {
  904.   int c1,c2,r,g,b;
  905.   gdImagePtr saveimg;
  906.   himg=gdImageCreate(x,y);
  907.   c1=gdImageGetPixel(image,px,py);
  908.   r=gdImageRed(image,c1); g=gdImageGreen(image,c1); b=gdImageBlue(image,c1);
  909.   if(r>=255) r--; else r++; if(g>=255) g--; else g++; if(b>=255) b--; else b++;
  910.   c1=gdImageColorAllocate(himg,r,g,b);
  911.   r=gdImageRed(image,col); g=gdImageGreen(image,col); b=gdImageBlue(image,col);
  912.   c2=gdImageColorAllocate(himg,r,g,b);
  913.   if(width>1) {
  914.     savew=-1; saveimg=image;
  915.     image=himg; c2=widthcolor(width,c2); image=saveimg;
  916.     c2=gdBrushed; savew=-1;
  917.   }
  918.   return c2;
  919. }
  920.  
  921. /* flood fill with hatching */
  922. void obj_hatchfill(objparm *pm)
  923. {
  924.   int nx,ny,ax,ay, dir, c;
  925.   scale(pm->pd,pm->p,1);
  926.   nx=pm->pd[2]; ny=pm->pd[3]; ax=abs(nx); ay=abs(ny);
  927.   if(nx==0 && ny==0) {fly_error("bad displacement vector"); return;}
  928.   if((nx>0 && ny>0) || (nx<0 && ny<0)) dir=1; else dir=-1;
  929.   if(ax==0) {ax=100; dir=2;}
  930.   if(ay==0) {ay=100; dir=3;}
  931.   c=makehatchimage(ax,ay,pm->p[0],pm->p[1],pm->color[0]);
  932.   switch(dir) {
  933.     case -1: {
  934.       gdImageLine(himg,0,ay-1,ax-1,0,c);
  935.       if(width>1) {
  936.         gdImageLine(himg,-ax,ay-1,-1,0,c);
  937.         gdImageLine(himg,ax,ay-1,2*ax-1,0,c);
  938.         gdImageLine(himg,0,-1,ax-1,-ay,c);
  939.         gdImageLine(himg,0,2*ay-1,ax-1,ay,c);
  940.       }
  941.       break;
  942.     }
  943.     case 1: {
  944.       gdImageLine(himg,0,0,ax-1,ay-1,c);
  945.       if(width>1) {
  946.         gdImageLine(himg,-ax,0,-1,ay-1,c);
  947.         gdImageLine(himg,ax,0,2*ax-1,ay-1,c);
  948.         gdImageLine(himg,0,-ay,ax-1,-1,c);
  949.         gdImageLine(himg,0,ay,ax-1,2*ay-1,c);
  950.       }
  951.       break;
  952.     }
  953.     case 2: gdImageLine(himg,0,ay/2,ax-1,ay/2,c); break;
  954.     case 3: gdImageLine(himg,ax/2,0,ax/2,ay-1,c); break;
  955.   }
  956.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],hatch);
  957.   gdImageSetTile(image,himg);
  958.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  959.   gdImageDestroy(himg);
  960.   if(tiled) gdImageSetTile(image,tileimg);
  961. }
  962.  
  963. /* flood fill with grid */
  964. void obj_gridfill(objparm *pm)
  965. {
  966.   int nx,ny, c;
  967.   scale(pm->pd,pm->p,1);
  968.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  969.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  970.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  971.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],grid);
  972.   gdImageLine(himg,0,ny/2,nx-1,ny/2,c); gdImageLine(himg,nx/2,0,nx/2,ny-1,c);
  973.   gdImageSetTile(image,himg);
  974.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  975.   gdImageDestroy(himg);
  976.   if(tiled) gdImageSetTile(image,tileimg);
  977. }
  978.  
  979. /* flood fill with double hatching */
  980. void obj_diafill(objparm *pm)
  981. {
  982.   int nx,ny, c;
  983.   scale(pm->pd,pm->p,1);
  984.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  985.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  986.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  987.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],diamond);
  988.   gdImageLine(himg,0,0,nx-1,ny-1,c); gdImageLine(himg,0,ny-1,nx-1,0,c);
  989.   gdImageSetTile(image,himg);
  990.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  991.   gdImageDestroy(himg);
  992.   if(tiled) gdImageSetTile(image,tileimg);
  993. }
  994.  
  995. /* flood fill with dots */
  996. void obj_dotfill(objparm *pm)
  997. {
  998.   int nx,ny, c;
  999.   scale(pm->pd,pm->p,1);
  1000.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  1001.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  1002.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  1003.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],dot);
  1004.   gdImageSetPixel(himg,nx/2,ny/2,c);
  1005.   gdImageSetTile(image,himg);
  1006.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  1007.   gdImageDestroy(himg);
  1008.   if(tiled) gdImageSetTile(image,tileimg);
  1009. }
  1010.  
  1011. struct {
  1012.   char *name;
  1013.   gdFontPtr *fpt;
  1014.   char *ftikz;
  1015. } fonttab[]={
  1016.   {"tiny",  &gdFontTiny,"0.3"},
  1017.   {"small", &gdFontSmall,"0.5"},
  1018.   {"medium",&gdFontMediumBold,"0.7"},
  1019.   {"large", &gdFontLarge,"0.9"},
  1020.   {"giant", &gdFontGiant,"1"},
  1021.   {"huge",  &gdFontGiant,"1"}
  1022. };
  1023.  
  1024. #define fonttab_no (sizeof(fonttab)/sizeof(fonttab[0]))
  1025.  
  1026. /* string */
  1027. void obj_string(objparm *pm)
  1028. {
  1029.   char *pp, *pe, *p2;
  1030.   int i;
  1031.   pp=pm->str; pe=strchr(pp,','); if(pe==NULL) {
  1032.     fly_error("too_few_parms"); return;
  1033.   }
  1034.   *pe++=0; pp=find_word_start(pp); *find_word_end(pp)=0;
  1035.   pe=find_word_start(pe); strip_trailing_spaces(pe);
  1036.   if(*pp) {
  1037.     for(i=0;i<fonttab_no && strcmp(pp,fonttab[i].name)!=0; i++);
  1038.     if(i>=fonttab_no) i=1;
  1039.   }
  1040.   else i=1;
  1041.   scale(pm->pd,pm->p,1);
  1042.   if(*pe=='"') {
  1043.     p2=strchr(pe+1,'"');
  1044.     if(p2 && *(p2+1)==0) {*p2=0; pe++;}
  1045.   }
  1046.   if(pm->fill){
  1047.     gdImageStringUp(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
  1048.         pm->color[0]);
  1049.     if(tikz_file) fprintf(tikz_file,"\\draw (%i,%i) node[scale=%s,rotate=90,color=%s,anchor=north west,inner sep=0pt] {\\(%s\\)};\n",
  1050.       pm->p[0],flip(pm->p[1]),fonttab[i].ftikz,tikz_color(pm->color[0]),(unsigned char*) pe);
  1051.   }
  1052.   else {
  1053.     gdImageString(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
  1054.         pm->color[0]);
  1055.     if(tikz_file) fprintf(tikz_file,"\\draw (%i,%i) node[scale=%s,color=%s,anchor=north west,inner sep=0pt] {\\(%s\\)};\n",
  1056.       pm->p[0],flip(pm->p[1]),fonttab[i].ftikz,tikz_color(pm->color[0]),(unsigned char*) pe);
  1057.   }
  1058. }
  1059. /*FIXME; giant does not exist in tikz and ... samething as huge */
  1060. /* point */
  1061. void obj_point(objparm *pm)
  1062. {
  1063.   scale(pm->pd,pm->p,1);
  1064.   gdImageSetPixel(image,pm->p[0],pm->p[1],pm->color[0]);
  1065.   if(tikz_file) fprintf(tikz_file,"\\draw[%s] (%i,%i) circle (1pt);\n",
  1066.     tikz_options(pm->color[0],0),pm->p[0],flip(pm->p[1]));
  1067. }
  1068.  
  1069. /* copy an image file */
  1070. void obj_copy(objparm *pm)
  1071. {
  1072.   char *pp;
  1073.   FILE *inf;
  1074.   gdImagePtr insimg;
  1075.  
  1076.   pp=find_word_start(pm->str);*find_word_end(pp)=0;
  1077.   inf=open4read(pp);
  1078.   if(inf==NULL) {
  1079.     fly_error("file_does_not_exist"); return;
  1080.   }
  1081.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1082.   if(insimg==NULL) {
  1083.     fly_error("bad_gif"); return;
  1084.   }
  1085.   scale(pm->pd,pm->p,1);
  1086.   if(pm->pd[2]<0 && pm->pd[3]<0 && pm->pd[4]<0 && pm->pd[5]<0)
  1087.     gdImageCopy(image,insimg,pm->p[0],pm->p[1],0,0,
  1088.             insimg->sx,insimg->sy);
  1089.   else
  1090.     gdImageCopy(image,insimg,pm->p[0],pm->p[1],pm->pd[2],pm->pd[3],
  1091.             pm->pd[4]-pm->pd[2],pm->pd[5]-pm->pd[3]);
  1092.   if(tikz_file) fprintf(tikz_file,
  1093.     "\\node[anchor=north west,inner sep=0pt] at (%i,%i) {\\includegraphics\[width=.05\\linewidth]{%s}};\n",
  1094.       pm->p[0],flip(pm->p[1]),pm->str);
  1095.     gdImageDestroy(insimg);
  1096. /*FIXME position non correcte; on ne peut pas utiliser une image gif */
  1097. }
  1098.  
  1099. /* copy an image file, with resizing */
  1100. void obj_copyresize(objparm *pm)
  1101. {
  1102.   char *pp;
  1103.   FILE *inf;
  1104.   gdImagePtr insimg;
  1105.  
  1106.   pp=find_word_start(pm->str);*find_word_end(pp)=0;
  1107.   inf=open4read(pp);
  1108.   if(inf==NULL) {
  1109.     fly_error("file_not_found"); return;
  1110.   }
  1111.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1112.   if(insimg==NULL) {
  1113.     fly_error("bad_gif"); return;
  1114.   }
  1115.   scale(pm->pd+4,pm->p+4,2);
  1116.   if(pm->pd[0]<0 && pm->pd[1]<0 && pm->pd[2]<0 && pm->pd[3]<0)
  1117.     gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],0,0,
  1118.                  pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
  1119.                  insimg->sx,insimg->sy);
  1120.   else
  1121.     gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],pm->pd[0],pm->pd[1],
  1122.                  pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
  1123.                  pm->pd[2]-pm->pd[0]+1,pm->pd[3]-pm->pd[1]+1);
  1124.   gdImageDestroy(insimg);
  1125. }
  1126.  
  1127. /* set brush or tile */
  1128. void obj_setbrush(objparm *pm)
  1129. {
  1130.   char *pp;
  1131.   FILE *inf;
  1132.   gdImagePtr insimg;
  1133.  
  1134.   pp=find_word_start(pm->str); *find_word_end(pp)=0;
  1135.   inf=open4read(pp); if(inf==NULL) {
  1136.     fly_error("file_not_found"); return;
  1137.   }
  1138.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1139.   if(insimg==NULL) {
  1140.     fly_error("bad_gif"); return;
  1141.   }
  1142.   if(pm->fill) {
  1143.     gdImageSetTile(image,insimg); tiled=1; tileimg=insimg;
  1144.   }
  1145.   else {
  1146.     gdImageSetBrush(image,insimg);brushed=1; brushimg=insimg;
  1147.   }
  1148. }
  1149.  
  1150. /* kill brush */
  1151. void obj_killbrush(objparm *pm)
  1152. {
  1153.   if(brushimg) gdImageDestroy(brushimg);
  1154.   brushed=0; brushimg=NULL;
  1155. }
  1156.  
  1157. /* kill tile */
  1158. void obj_killtile(objparm *pm)
  1159. {
  1160.   if(tileimg) gdImageDestroy(tileimg);
  1161.   tiled=0; tileimg=NULL;
  1162. }
  1163.  
  1164. /* set style */
  1165. void obj_setstyle(objparm *pm)
  1166. {
  1167.   int i,t;
  1168.   t=pm->pcnt/3; if(t<=0) {
  1169.     fly_error("too_few_parms"); return;
  1170.   }
  1171.   for(i=0;i<t;i++) {
  1172.     if(pm->pd[3*i]<0 || pm->pd[3*i+1]<0 || pm->pd[3*i+2]<0)
  1173.       pm->p[i]=gdTransparent;
  1174.     else
  1175.       pm->p[i]=getcolor(pm->pd[3*i],pm->pd[3*i+1],pm->pd[3*i+2]);
  1176.   }
  1177.   gdImageSetStyle(image,pm->p,t); styled=1;
  1178. }
  1179.  
  1180. /* kill style */
  1181. void obj_killstyle(objparm *pm)
  1182. {
  1183.   styled=0;
  1184. }
  1185.  
  1186. /* set transparent */
  1187. void obj_transp(objparm *pm)
  1188. {
  1189.   gdImageColorTransparent(image,pm->color[0]);
  1190. }
  1191.  
  1192. /* set interlace */
  1193. void obj_interlace(objparm *pm)
  1194. {
  1195.   gdImageInterlace(image,1);
  1196. }
  1197.  
  1198. /* set linewidth */
  1199. void obj_linewidth(objparm *pm)
  1200. {
  1201.   if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
  1202.   width=pm->pd[0];
  1203.   if(tikz_file) fprintf(tikz_file,"\\pgfsetlinewidth{%fpt}\n",width*0.7);
  1204. }
  1205.  
  1206. /* set crosshairsize */
  1207. void obj_crosshairsize(objparm *pm)
  1208. {
  1209.   if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
  1210.   else width2=pm->pd[0];
  1211. }
  1212.  
  1213. /* set x range */
  1214. void obj_xrange(objparm *pm)
  1215. {
  1216.   double dd;
  1217.   dd=pm->pd[1]-pm->pd[0];
  1218.   xstart=pm->pd[0]; xscale=sizex/dd;
  1219. }
  1220.  
  1221. /* set y range */
  1222. void obj_yrange(objparm *pm)
  1223. {
  1224.   double dd;
  1225.   dd=pm->pd[1]-pm->pd[0];
  1226.   ystart=pm->pd[1]; yscale=-sizey/dd;
  1227. }
  1228.  
  1229. /* set x et y range */
  1230. void obj_range(objparm *pm)
  1231. {
  1232.   double d1,d2;
  1233.   d1=pm->pd[1]-pm->pd[0]; d2=pm->pd[3]-pm->pd[2];
  1234.   xstart=pm->pd[0]; xscale=(sizex-1)/d1;
  1235.   ystart=pm->pd[3]; yscale=-(sizey-1)/d2;
  1236. }
  1237.  
  1238. /* set t range */
  1239. void obj_trange(objparm *pm)
  1240. {
  1241.   /*double dd;
  1242.   dd=pm->pd[1]-pm->pd[0];*/
  1243.   tstart=pm->pd[0]; tend=pm->pd[1];
  1244.   tranged=1;
  1245. }
  1246.  
  1247. /* set tstep (plotting step) */
  1248. void obj_tstep(objparm *pm)
  1249. {
  1250.   int dd;
  1251.   dd=pm->pd[0];
  1252.   if(dd<3) {
  1253.     fly_error("bad_step"); return;
  1254.   }
  1255.   if(dd>2000) dd=2000;
  1256.   tstep=dd;
  1257. }
  1258.  
  1259. /* set plotjump (plot jump break threashold) */
  1260. void obj_plotjump(objparm *pm)
  1261. {
  1262.   int dd;
  1263.   dd=pm->pd[0];
  1264.   if(dd<3) dd=3;
  1265.   if(dd>MAX_SIZE) dd=MAX_SIZE;
  1266.   plotjump=dd;
  1267. }
  1268.  
  1269. /* plot a curve, either parametric or explicit */
  1270. void _obj_plot(objparm *pm,int dash)
  1271. {
  1272.   int i,j,n,dist,xx,yy,varpos;
  1273.   char p1[MAX_LINELEN+1], p2[MAX_LINELEN+1];
  1274.   char *varn, *pp;
  1275.   double dc[2],t,v;
  1276.   int ic[2],oc[2];
  1277.  
  1278.   n=itemnum(pm->str);
  1279.   if(n<1) {
  1280.     fly_error("bad_parms"); return;
  1281.   }
  1282.   fnd_item(pm->str,1,p1); fnd_item(pm->str,2,p2);
  1283.   if(n==1 && !tranged) v=sizex/xscale/tstep;
  1284.   else v=(tend-tstart)/tstep;
  1285.   if(n==1) varn="x"; else varn="t";
  1286.   for(pp=varchr(p1,varn); pp; pp=varchr(pp,varn)) {
  1287.     string_modify(p1,pp,pp+strlen(varn),EV_T);
  1288.     pp+=strlen(EV_T);
  1289.   }
  1290.   for(pp=varchr(p2,varn); pp; pp=varchr(pp,varn)) {
  1291.     string_modify(p2,pp,pp+strlen(varn),EV_T);
  1292.       pp+=strlen(EV_T);
  1293.   }
  1294.   varpos=eval_getpos(EV_T);
  1295.   if(varpos<0) return; /* unknown error */
  1296.   evalue_compile(p1); evalue_compile(p2);
  1297.   if(vimg_enable) vimg_plotstart();
  1298.   if (tikz_file) fprintf(tikz_file,"\%\%begin plot\n\\draw\[%s]",
  1299.     tikz_options(pm->color[0],-dash));
  1300.   for(i=j=0;i<=tstep;i++) {
  1301.     if(n==1) {
  1302.       if(tranged) t=tstart+i*v; else t=xstart+i*v;
  1303.       eval_setval(varpos,t); dc[0]=t; dc[1]=strevalue(p1);
  1304.     }
  1305.     else {
  1306.       t=tstart+i*v; eval_setval(varpos,t);
  1307.       dc[0]=strevalue(p1); dc[1]=strevalue(p2);
  1308.     }
  1309.     if(!isfinite(dc[0]) || !isfinite(dc[1])) ic[0]=ic[1]=-BOUND;
  1310.     else scale(dc,ic,1);
  1311.     if(vimg_enable) vimg_plot1 (scale_buf[0],scale_buf[1]);
  1312.     if(j==0) {
  1313.       gdImageSetPixel(image,ic[0],ic[1],pm->color[0]); j++;
  1314.       if (tikz_file)
  1315.         fprintf(tikz_file,"(%i,%i)rectangle(%i,%i)",
  1316.           ic[0],flip(ic[1]),ic[0]+1,flip(ic[1]+1));
  1317.     }
  1318.     else {
  1319.       xx=ic[0]-oc[0]; yy=ic[1]-oc[1];
  1320.       dist=sqrt(xx*xx+yy*yy);
  1321.       if(dist>0){
  1322.         if(dist>plotjump || dist==1) {
  1323.           gdImageSetPixel(image,ic[0],ic[1],pm->color[0]);
  1324.           if (tikz_file)
  1325.             fprintf(tikz_file,"(%i,%i)rectangle(%i,%i)",
  1326.               ic[0],flip(ic[1]),ic[0]+1,flip(ic[1]+1));
  1327.         }
  1328.         else {
  1329.           if (dash==1)
  1330.             myDashedLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
  1331.           else
  1332.             gdImageLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
  1333.           if (tikz_file)
  1334.             fprintf(tikz_file, "(%i,%i)--(%i,%i)",
  1335.                 oc[0],flip(oc[1]),ic[0],flip(ic[1]));
  1336.         }
  1337.       }
  1338.     }
  1339.     memmove(oc,ic,sizeof(oc));
  1340.   }
  1341.   if (tikz_file) fprintf(tikz_file,";\n\%\%end plot\n");
  1342.   if(vimg_enable) vimg_plotend();
  1343. }
  1344.  
  1345. void obj_plot(objparm *pm) { _obj_plot( pm,0) ;}
  1346. void obj_dplot(objparm *pm) { _obj_plot( pm,1) ; }
  1347.  
  1348. /* set levelcurve granularity */
  1349. void obj_levelstep(objparm *pm)
  1350. {
  1351.   int dd;
  1352.   dd=pm->pd[0];
  1353.   if(dd<1) return;
  1354.   if(dd>16) dd=16;
  1355.   lstep=dd;
  1356. }
  1357.  
  1358. /* level curve */
  1359. void obj_levelcurve(objparm *pm)
  1360. {
  1361.   char fn[MAX_LINELEN+1],tc[MAX_LINELEN+1];
  1362.   int n,i;
  1363.   double d;
  1364.   leveldata *ld;
  1365.  
  1366.   ld=xmalloc(sizeof(leveldata)+16);
  1367.   ld->xname="x"; ld->yname="y";
  1368.   ld->xsize=sizex; ld->ysize=sizey; ld->datacnt=0;
  1369.   ld->xrange[0]=xstart; ld->xrange[1]=sizex/xscale+xstart;
  1370.   ld->yrange[0]=sizey/yscale+ystart; ld->yrange[1]=ystart;
  1371.   ld->grain=lstep;
  1372.   fnd_item(pm->str,1,fn); ld->fn=fn;
  1373.   n=itemnum(pm->str);
  1374.   if(n<=1) {ld->levelcnt=1; ld->levels[0]=0;}
  1375.   else {
  1376.     if(n>LEVEL_LIM+1) n=LEVEL_LIM+1;
  1377.     for(i=0;i<n-1;i++) {
  1378.       fnd_item(pm->str,i+2,tc);
  1379.       d=strevalue(tc);
  1380.       if(isfinite(d)) ld->levels[i]=d; else ld->levels[i]=0;
  1381.     }
  1382.     ld->levelcnt=n-1;
  1383.   }
  1384.   levelcurve(ld);
  1385.   if (tikz_file)
  1386.     fprintf(tikz_file,"\%\%begin levelcurve\n\\draw\[%s]"
  1387.       ,tikz_options(pm->color[0],1));
  1388.   for(i=0;i<ld->datacnt;i++) {
  1389.     gdImageSetPixel(image,ld->xdata[i],ld->ydata[i],pm->color[0]);
  1390.     if (tikz_file)
  1391.       fprintf(tikz_file,
  1392.         "(%i,%i)rectangle(%i,%i)",
  1393.         ld->xdata[i],flip(ld->ydata[i]),ld->xdata[i]+1,flip(ld->ydata[i]+1));
  1394.   }
  1395.   if (tikz_file) fprintf(tikz_file,";\n\%\%end levelcurve\n");
  1396.   free(ld);
  1397. }
  1398.  
  1399. /* set animation step */
  1400. void obj_animstep(objparm *pm)
  1401. {
  1402.   animstep=pm->pd[0];
  1403.   setvar("animstep",pm->pd[0]);
  1404. }
  1405.  
  1406. /* Starts a line counting */
  1407. void obj_linecount(objparm *pm)
  1408. {
  1409.   linecnt=pm->pd[0];
  1410. }
  1411.  
  1412. /* marks end of execution */
  1413. void obj_end(objparm *pm)
  1414. {
  1415.   fly_error("successful_end_of_execution");
  1416. }
  1417.  
  1418. /* output */
  1419. void obj_output(objparm *pm)
  1420. {
  1421.   char *p, namebuf[1024];
  1422.   p=find_word_start(pm->str); *find_word_end(p)=0;
  1423.   snprintf(namebuf,sizeof(namebuf),"%s",imagefilename);
  1424.   snprintf(imagefilename,sizeof(imagefilename),"%s",p);
  1425.   output();
  1426.   snprintf(imagefilename,sizeof(imagefilename),"%s",namebuf);
  1427. }
  1428.  
  1429. /* vimgfile */
  1430. void obj_vimgfile(objparm *pm)
  1431. {
  1432.   char *p;
  1433.   p=find_word_start(pm->str); *find_word_end(p)=0;
  1434.   snprintf(vimgfilename,sizeof(vimgfilename),"%s",p);
  1435.   if(vimg_ready) vimg_close();
  1436. }
  1437.  
  1438. /* vimg enable/disable */
  1439. void obj_vimg(objparm *pm)
  1440. {
  1441.   vimg_enable=pm->pd[0];
  1442.   if(vimg_enable>0 && vimg_ready==0) vimg_init();
  1443. }
  1444.  
  1445. /* Set affine transformation */
  1446. void obj_affine(objparm *pm)
  1447. {
  1448.   int i;
  1449.   for(i=0;i<4;i++) matrix[i]=pm->pd[i];
  1450.   transx=pm->pd[4]; transy=pm->pd[5];
  1451.   transform=1;
  1452. }
  1453.  
  1454. /* Set affine transformation */
  1455. void obj_rotation(objparm *pm)
  1456. {
  1457.   double r;
  1458.   r=M_PI*pm->pd[0]/180;
  1459.   matrix[0]=matrix[3]=cos(r);
  1460.   matrix[1]=-sin(r); matrix[2]=sin(r);
  1461.   transform=1;
  1462. }
  1463.  
  1464. /* Set linear transformation */
  1465. void obj_linear(objparm *pm)
  1466. {
  1467.   int i;
  1468.   for(i=0;i<4;i++) matrix[i]=pm->pd[i];
  1469.   transform=1;
  1470. }
  1471.  
  1472. /* Set translation transformation */
  1473. void obj_translation(objparm *pm)
  1474. {
  1475.   transx=pm->pd[0]; transy=pm->pd[1];
  1476. }
  1477.  
  1478. /* kill affine transformation */
  1479. void obj_killaffine(objparm *pm)
  1480. {
  1481.   matrix[0]=matrix[3]=1;
  1482.   matrix[1]=matrix[2]=transx=transy=0;
  1483.   transform=0;
  1484. }
  1485.  
  1486. /* kill affine transformation */
  1487. void obj_killlinear(objparm *pm)
  1488. {
  1489.   matrix[0]=matrix[3]=1;
  1490.   matrix[1]=matrix[2]=0;
  1491.   transform=0;
  1492. }
  1493.  
  1494. /* kill affine transformation */
  1495. void obj_killtranslation(objparm *pm)
  1496. {
  1497.   transx=transy=0;
  1498. }
  1499.  
  1500. /***** Les modifs de Jean-Christophe Leger Fev 2006 *****/
  1501.  
  1502. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES, une liste de 4 nombres reels pour la matrice */
  1503. void obj_setmatrix(objparm *pm)
  1504. {
  1505.   int nummatrix = (int) (pm->pd[0]);
  1506.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1507.     fly_error("bad_matrix_number");
  1508.     return;
  1509.   }
  1510.   matrices_pavage[nummatrix][0] = pm->pd[1];
  1511.   matrices_pavage[nummatrix][1] = pm->pd[2];
  1512.   matrices_pavage[nummatrix][2] = pm->pd[3];
  1513.   matrices_pavage[nummatrix][3] = pm->pd[4];
  1514. }
  1515.  
  1516. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1517. void obj_resetmatrix(objparm *pm)
  1518. {
  1519.   int nummatrix = (int) (pm->pd[0]);
  1520.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1521.     fly_error("bad_matrix_number");
  1522.     return;
  1523.   }
  1524.   matrices_pavage[nummatrix][0] = 1;
  1525.   matrices_pavage[nummatrix][1] = 0;
  1526.   matrices_pavage[nummatrix][2] = 0;
  1527.   matrices_pavage[nummatrix][3] = 1;
  1528.  
  1529. }
  1530. /* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 2 nombres reels pour le vecteur */
  1531. void obj_setvector(objparm *pm)
  1532. {
  1533.   int nummatrix = (int) (pm->pd[0]);
  1534.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1535.     fly_error("bad_vector_number");
  1536.     return;
  1537.   }
  1538.   vecteurs_pavage[nummatrix][0] = pm->pd[1];
  1539.   vecteurs_pavage[nummatrix][1] = pm->pd[2];
  1540. }
  1541.  
  1542. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1543. void obj_resetvector(objparm *pm)
  1544. {
  1545.   int nummatrix = (int) (pm->pd[0]);
  1546.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1547.     fly_error("bad_vector_number");
  1548.     return;
  1549.   }
  1550.   vecteurs_pavage[nummatrix][0] = 0;
  1551.   vecteurs_pavage[nummatrix][1] = 0;
  1552. }
  1553.  
  1554. /* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 6 nombres reels pour la matrice et le vecteur */
  1555. void obj_settransform(objparm *pm)
  1556. {
  1557.   int nummatrix = (int) (pm->pd[0]);
  1558.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1559.     fly_error("bad_vector_number");
  1560.     return;
  1561.   }
  1562.   matrices_pavage[nummatrix][0] = pm->pd[1];
  1563.   matrices_pavage[nummatrix][1] = pm->pd[2];
  1564.   matrices_pavage[nummatrix][2] = pm->pd[3];
  1565.   matrices_pavage[nummatrix][3] = pm->pd[4];
  1566.   vecteurs_pavage[nummatrix][0] = pm->pd[5];
  1567.   vecteurs_pavage[nummatrix][1] = pm->pd[6];
  1568. }
  1569.  
  1570.  
  1571. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1572. void obj_resettransform(objparm *pm)
  1573. {
  1574.   int nummatrix = (int) (pm->pd[0]);
  1575.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1576.     fly_error("bad_vector_number");
  1577.     return;
  1578.   }
  1579.   vecteurs_pavage[nummatrix][0] = 0;
  1580.   vecteurs_pavage[nummatrix][1] = 0;
  1581.   matrices_pavage[nummatrix][0] = 1;
  1582.   matrices_pavage[nummatrix][1] = 0;
  1583.   matrices_pavage[nummatrix][2] = 0;
  1584.   matrices_pavage[nummatrix][3] = 1;
  1585. }
  1586.  
  1587. /* arguments: une liste de 6 nombres reels pour les coordonnees de l'origine et des vecteurs de base */
  1588. void obj_setparallelogram(objparm *pm)
  1589. {
  1590.   int i;
  1591.   for(i=0;i<6;i++)  parallogram_fonda[i]=pm->pd[i];
  1592. }
  1593.  
  1594. /* arguments :aucun */
  1595. void obj_resetparallelogram(objparm *pm)
  1596. {
  1597.   parallogram_fonda[0]=0;
  1598.   parallogram_fonda[1]=0;
  1599.   parallogram_fonda[2]=100;
  1600.   parallogram_fonda[3]=0;
  1601.   parallogram_fonda[4]=0;
  1602.   parallogram_fonda[5]=100;
  1603. }
  1604.  
  1605. /* argument : la liste des numeros des matrices a faire agir, le nom du fichier de l'image a inclure */
  1606. void obj_multicopy(objparm *pm)
  1607. {
  1608.   char *pp,*pe,*cptr;
  1609.   FILE *inf;
  1610.   gdImagePtr insimg;
  1611.   int i,j;
  1612.   int imax,jmax;
  1613.   int c; /* la couleur reperee */
  1614.   int mat;
  1615.   int nummatrices[JC_NB_MATRICES];
  1616.   double pt[2]; /* les coordonnees math. du point repere dans le parallelogramme */
  1617.   double ptr[2]; /* les coordonnees math. du point transforme */
  1618.   int pti[2]; /* les coordonnees img du point a placer sur le canevas */
  1619.   int t;
  1620.   /* gestion palette de couleur de l'image a inserer */
  1621.   int colorMap[gdMaxColors];
  1622.   int comptmat=0; /* combien de matrices en tout ? */
  1623.  
  1624.   for (i=0; (i<gdMaxColors); i++) {
  1625.     colorMap[i] = (-1);
  1626.   }
  1627.  
  1628.   /* Gestion des parametres la liste est censee finir avec le nom du fichier */
  1629.   t=pm->pcnt-1; /* il faut enlever le nom de fichier ! c'est le nombre de parametres en plus */
  1630.   pp=find_word_start(pm->str);
  1631.   /* visiblement find_word_start n'arrive pas a trouver le dernier mot dans un contexte ou le nombre de parameters est variable
  1632.      * on cherche donc l'emplacement de la derniere virgule:
  1633.      * il y en a une car t>0, on retrouve ensuite le debut de mot
  1634.      */
  1635.   for(pe=pp;*pe!=0;pe++);/* trouve la fin de la chaine */
  1636.   if(t>0){
  1637.     for(cptr = pe; cptr > pp && *cptr != ','; cptr--);
  1638.     pp=find_word_start(cptr+1);
  1639.   }
  1640.   pe=find_word_end(pp);*pe=0; /* strip junk final */
  1641.   //      printf("pp contient %s\n",pp);
  1642.  
  1643.  
  1644.   inf=open4read(pp);
  1645.   if(inf==NULL) {
  1646.     fly_error("file_not_exist"); return;
  1647.   }
  1648.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1649.   if(insimg==NULL) {
  1650.     fly_error("bad_gif"); return;
  1651.   }
  1652.  
  1653.   /* On recupere les numeros des matrices/vecteurs a faire agir,
  1654.     * s'il n'y en a pas, on les fait toutes agir
  1655.   */
  1656.   for(i=0;i<t && i< JC_NB_MATRICES;i++) {
  1657.     if(pm->pd[i]>=1 && pm->pd[i]<=JC_NB_MATRICES){
  1658.       nummatrices[comptmat] = pm->pd[i];
  1659.       comptmat++;
  1660.     }
  1661.   }
  1662.   if(t<=0){
  1663.     for(i=0;i<JC_NB_MATRICES;i++) {
  1664.       nummatrices[i] = i+1;
  1665.     }
  1666.     comptmat=JC_NB_MATRICES;
  1667.   }
  1668.  
  1669.  
  1670.   imax = gdImageSX(insimg);
  1671.   jmax = gdImageSY(insimg);
  1672.  
  1673.   for(i=0;i<imax;i++){
  1674.     for(j=0;j<jmax;j++){
  1675.       int nc;
  1676.       c=gdImageGetPixel(insimg,i,j);
  1677.       /* Le code suivant est une copie du code dans gdImageCopy  Added 7/24/95: support transparent copies */
  1678.       if (gdImageGetTransparent(insimg) != c) {
  1679.         /* Have we established a mapping for this color? */
  1680.         if (colorMap[c] == (-1)) {
  1681.           /* If it's the same image, mapping is trivial, dans notre cas ca n'arrive jamais... */
  1682.           if (image == insimg) {
  1683.             nc = c;
  1684.           } else {
  1685.             /* First look for an exact match */
  1686.             nc = gdImageColorExact(image,
  1687.                    insimg->red[c], insimg->green[c],
  1688.                            insimg->blue[c]);
  1689.           }
  1690.           if (nc == (-1)) {
  1691.           /* No, so try to allocate it */
  1692.             nc = gdImageColorAllocate(image,
  1693.                              insimg->red[c], insimg->green[c],
  1694.                              insimg->blue[c]);
  1695.              /* If we're out of colors, go for the closest color */
  1696.             if (nc == (-1)) {
  1697.               nc = gdImageColorClosest(image,
  1698.                               insimg->red[c], insimg->green[c],
  1699.                               insimg->blue[c]);
  1700.             }
  1701.           }
  1702.           colorMap[c] = nc;
  1703.         }
  1704.  
  1705.         pt[0]= i*(parallogram_fonda[2])/(imax-1)+(jmax-j)*(parallogram_fonda[4])/(jmax-1)+parallogram_fonda[0];
  1706.         pt[1]= i*(parallogram_fonda[3])/(imax-1)+(jmax-j)*(parallogram_fonda[5])/(jmax-1)+parallogram_fonda[1];
  1707.         for(mat=0;mat<comptmat;mat++){
  1708.           double *matricecourante = matrices_pavage[nummatrices[mat]];
  1709.           double *vecteurcourant = vecteurs_pavage[nummatrices[mat]];
  1710.           ptr[0] = matricecourante[0]*pt[0]+matricecourante[1]*pt[1]+vecteurcourant[0];
  1711.           ptr[1] = matricecourante[2]*pt[0]+matricecourante[3]*pt[1]+vecteurcourant[1];
  1712.           scale(ptr,pti,1);
  1713.           gdImageSetPixel(image,pti[0],pti[1],colorMap[c]);
  1714.         }
  1715.       }
  1716.     }
  1717.   }
  1718.   gdImageDestroy(insimg);
  1719. }
  1720.  
  1721. /**** Fin modifs JC Fev 06 ******/
  1722.  
  1723. /* get color from value */
  1724. int calc_color(char *p, struct objtab *o)
  1725. {
  1726.   int k, cc[3];
  1727.   char buf[MAX_LINELEN+1];
  1728.   for(k=0;k<3;k++) {
  1729.     fnd_item(p,k+1,buf);
  1730.     cc[k]=strevalue(buf);
  1731.   }
  1732.   collapse_item(p,3);
  1733.   if(cc[0]==-1 && cc[1]==-1 && cc[2]==-255) {
  1734.  
  1735.     if(brushed && o->subst&1) return gdBrushed;
  1736.     else return color_black;
  1737.   }
  1738.   if(cc[0]==-1 && cc[1]==-255 && cc[2]==-1) {
  1739.     if(styled && o->subst&2)  return gdStyled;
  1740.     else return color_black;
  1741.   }
  1742.   if(cc[0]==-255 && cc[1]==-1 && cc[2]==-1) {
  1743.     if(tiled && o->fill_tag==1)  return gdTiled;
  1744.     else return color_black;
  1745.   }
  1746.   return getcolor(cc[0],cc[1],cc[2]);
  1747. }
  1748.  
  1749. /* Routine to parse parameters */
  1750. int parse_parms(char *p,objparm *pm,struct objtab *o)
  1751. {
  1752.   char buf[MAX_LINELEN+1];
  1753.   char *p1, *p2;
  1754.   int j,t,c,c1,c2;
  1755.  
  1756.   c=o->color_pos;c1=c2=0;
  1757.   pm->color[0]=pm->color[1]=0;
  1758.   if(c>0) c1=c;
  1759.   if(c<0) c2=-c;
  1760.   c=c1+c2;
  1761.   t=itemnum(p);if(t<o->required_parms+3*c) return -1;
  1762.   if(c1>0 && t>o->required_parms+3*c) t=o->required_parms+3*c;
  1763.   pm->pcnt=t-3*c;
  1764.   if(pm->pcnt>MAX_PARMS) pm->pcnt=MAX_PARMS;
  1765.   if(c2>0) {
  1766.     for(j=0;j<2 && j<c2; j++) pm->color[j]=calc_color(p,o);
  1767.   }
  1768.   snprintf(buf,sizeof(buf),"%s",p);
  1769.   for(j=0, p1=buf; j<pm->pcnt; j++, p1=p2) {
  1770.     p2=find_item_end(p1); if(*p2) *p2++=0;
  1771.     p1=find_word_start(p1);
  1772.     if(*p1) pm->pd[j]=strevalue(p1); else pm->pd[j]=0;
  1773.     if(!isfinite(pm->pd[j])) {
  1774.         if(j<o->required_parms) return -1;
  1775.         else {pm->pd[j]=0;break;}
  1776.     }
  1777.   }
  1778.   collapse_item(p,o->required_parms);
  1779.   if(c1>0) {
  1780.     for(j=0;j<c1 && j<2; j++) pm->color[j]=calc_color(p,o);
  1781.   }
  1782.   if(width>1 && o->subst&1 && pm->color[0]!=gdBrushed
  1783.       && pm->color[0]!=gdStyled && pm->color[0]!=gdTiled) {
  1784.     pm->color[1]=pm->color[0];
  1785.     pm->color[0]=widthcolor(width,pm->color[0]);
  1786.   }
  1787.   pm->fill=o->fill_tag;
  1788.   if(o->required_parms!=0){
  1789.     if(dashed) {
  1790.       if(pm->fill==1) pm->fill=2;
  1791.       if(pm->fill==0) pm->fill=-1;
  1792.        dashed=0;
  1793.     }
  1794.     if(filled) {
  1795.       if(pm->fill==-1) pm->fill=2;
  1796.       if(pm->fill==0) pm->fill=1;
  1797.       filled=0;
  1798.     }
  1799.   }
  1800.   ovlstrcpy(pm->str,p); return 0;
  1801. }
  1802.  
  1803. /* Create the struct objparm pm corresponding to the objparm p
  1804.  * Execute a command. Returns 0 if OK.
  1805.  */
  1806. int obj_main(char *p)
  1807. {
  1808.   int i;
  1809.   char *pp,*name,*pt;
  1810.   char tbuf2[MAX_LINELEN+1];
  1811.   struct objparm pm;
  1812.  
  1813.   p=find_word_start(p);
  1814.   if(*p==exec_prefix_char || *p==0) return 0; /* comment */
  1815.   name=p;
  1816.   pp=find_name_end(p);
  1817.   pt=find_word_start(pp); if(*pt=='=') *pt=' ';
  1818.   if(*pt==':' && *(pt+1)=='=') *pt=*(pt+1)=' ';
  1819.   pp=find_word_end(p);
  1820.   if(*pp!=0) {
  1821.     *(pp++)=0; pp=find_word_start(pp);
  1822.   }
  1823.   if(strlen(p)==1 || (strlen(p)==2 && isdigit(*(p+1)))) {
  1824.     setvar(p,strevalue(pp)); return 0;
  1825.   }
  1826.   /* search the number of the object */
  1827.   i=search_list(objtab,obj_no,sizeof(objtab[0]),name);
  1828.   if(i<0) {
  1829.     fly_error("bad_cmd"); return 1;
  1830.   }
  1831.   if(image==NULL && (objtab[i].color_pos || objtab[i].required_parms>2)) {
  1832.     fly_error("image_not_defined"); return 1;
  1833.   }
  1834.   ovlstrcpy(tbuf2,pp);
  1835.   if(objtab[i].color_pos || objtab[i].routine==obj_setstyle) {
  1836.     substit(tbuf2);
  1837.   }
  1838.   if(parse_parms(tbuf2,&pm,objtab+i)!=0) fly_error("bad_parms");
  1839.   else objtab[i].routine(&pm);
  1840.   return 0;
  1841. }
  1842.