Subversion Repositories wimsdev

Rev

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

  1. /*    CopyrOAight (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) strcat(buf, ",dashed");
  41.   if (the_fill == 1)
  42.     sprintf(buf+strlen(buf), ",fill=%s", tikz_color(the_color));
  43.   return buf;
  44. }
  45.  
  46. #define flip(y) (sizey-(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.  
  191. /* solid line */
  192. void obj_line(objparm *pm)
  193. {
  194.   scale(pm->pd,pm->p,2);
  195.   gdImageLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],pm->color[0]);
  196.   if (tikz_file)
  197.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  198.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]));
  199.   if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
  200. }
  201.  
  202. void _obj_arrow(objparm *pm, int twoside)
  203. {
  204.   int l,xx,yy;
  205.   gdPoint ii[3];
  206.   double dx,dy,length,dd[6];
  207.   scale(pm->pd,pm->p,2);
  208.   xx=ii[0].x=pm->p[2];yy=ii[0].y=pm->p[3];
  209.   l=pm->pd[4];if(l<0) l=0; if(l>200) l=200;
  210.   scale2(pm->pd[0]-pm->pd[2],pm->pd[1]-pm->pd[3],&dx,&dy);
  211.   length=sqrt(dx*dx+dy*dy);
  212.   if(length<3 || l<5) goto stem;
  213.   dd[0]=l*dx/length; dd[1]=l*dy/length;
  214.   #define fat 0.27
  215.   dd[2]=dd[0]+dd[1]*fat; dd[3]=dd[1]-dd[0]*fat;
  216.   dd[4]=dd[0]-dd[1]*fat; dd[5]=dd[1]+dd[0]*fat;
  217.   ii[1].x=rint(dd[2])+ii[0].x; ii[1].y=rint(dd[3])+ii[0].y;
  218.   ii[2].x=rint(dd[4])+ii[0].x; ii[2].y=rint(dd[5])+ii[0].y;
  219.   gdImageFilledPolygon(image, ii,3,pm->color[0]);
  220.   if(tikz_file)
  221.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  222.       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));
  223.   if(vimg_enable) vimg_polyline(scale_buf,3,1);
  224.   xx=rint(dd[0])+ii[0].x;yy=rint(dd[1])+ii[0].y;
  225.   if(twoside) {
  226.     ii[0].x=pm->p[0]; ii[0].y=pm->p[1];
  227.     ii[1].x=-rint(dd[2])+ii[0].x; ii[1].y=-rint(dd[3])+ii[0].y;
  228.     ii[2].x=-rint(dd[4])+ii[0].x; ii[2].y=-rint(dd[5])+ii[0].y;
  229.     gdImageFilledPolygon(image, ii,3,pm->color[0]);
  230.   if(tikz_file)
  231.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  232.       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));
  233.   }
  234.   stem: if(pm->fill)
  235.     myDashedLine(image,pm->p[0],pm->p[1],xx,yy,pm->color[0]);
  236.   else
  237.     gdImageLine(image,pm->p[0],pm->p[1],xx,yy,pm->color[0]);
  238.   if(tikz_file)
  239.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  240.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),xx,flip(yy));
  241.   if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
  242. }
  243.  
  244. /* Arrow */
  245. void obj_arrow(objparm *pm)
  246. {
  247.   _obj_arrow(pm,0);
  248. }
  249.  
  250. /* 2-sided arrow */
  251. void obj_arrow2(objparm *pm)
  252. {
  253.    _obj_arrow(pm,1);
  254. }
  255.  
  256. /* horizontal line */
  257. void obj_hline(objparm *pm)
  258. {
  259.   scale(pm->pd,pm->p,1);
  260.   if(pm->fill)
  261.     myDashedLine(image,0,pm->p[1],sizex,pm->p[1],pm->color[0]);
  262.   else
  263.     gdImageLine(image,0,pm->p[1],sizex,pm->p[1],pm->color[0]);
  264.   if (tikz_file)
  265.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  266.       tikz_options(pm->color[0],pm->fill),0,flip(pm->p[1]),sizex,flip(pm->p[1]));
  267. }
  268.  
  269. /* tikzfile */
  270. void obj_tikzfile(objparm *pm)
  271. {
  272.   char *p;
  273.   if(tikz_file){
  274.     fprintf(tikz_file,"\\end{tikzpicture}\n\\end{document}\n");
  275.     fclose(tikz_file);
  276.   }
  277.   p=find_word_start(pm->str); *find_word_end(p)=0;
  278.   snprintf(tikzfilename,sizeof(tikzfilename),"%s",p);
  279.   tikz_file=fopen(tikzfilename,"w");
  280.   if(!tikz_file)
  281.     fly_error("tikz_nopen");
  282.   fprintf(tikz_file,"\\documentclass\[11pt,a5paper]{article}\n\\usepackage{tikz}\\usepackage{geometry}\\geometry{paperheight=320px,\npaperwidth=320px, \ntop=10px, \nleft=2px, \nbottom=2px, \nright=2px,\n}\\pagestyle{empty}\n\\begin{document}\n\\begin{tikzpicture}\[scale=0.0254]\n");
  283. }
  284.  
  285. /* vertical line */
  286. void obj_vline(objparm *pm)
  287. {
  288.   scale(pm->pd,pm->p,1);
  289.   if(pm->fill)
  290.     myDashedLine(image,pm->p[0],0,pm->p[0],sizey,pm->color[0]);
  291.   else
  292.     gdImageLine(image,pm->p[0],0,pm->p[0],sizey,pm->color[0]);
  293.   if (tikz_file)
  294.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  295.       tikz_options(pm->color[0],pm->fill),pm->p[0],0,pm->p[0],sizey);
  296. }
  297.  
  298. /* dashed line */
  299. void obj_dline(objparm *pm)
  300. {
  301.   scale(pm->pd,pm->p,2);
  302.   myDashedLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3], pm->color[0]);
  303.   if (tikz_file)
  304.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  305.       tikz_options(pm->color[0],-1),
  306.       pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]));
  307. }
  308.  
  309. /* parallel lines.
  310.  * x1,y1,x2,y2,xv,yv,n,color */
  311. void obj_parallel(objparm *pm)
  312. {
  313.   int i, n, xi,yi;
  314.   double xv,yv;
  315.   n=pm->pd[6]; if(n<0) return; if(n>256) n=256;
  316.   scale(pm->pd,pm->p,3);
  317.   scale2(pm->pd[4],pm->pd[5],&xv,&yv);
  318.   for(i=0;i<n;i++) {
  319.     xi=rint(i*xv); yi=rint(i*yv);
  320.     gdImageLine(image,pm->p[0]+xi,pm->p[1]+yi,pm->p[2]+xi,pm->p[3]+yi,
  321.               pm->color[0]);
  322.     if (tikz_file)
  323.       fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  324.         tikz_options(pm->color[0],pm->fill),
  325.         pm->p[0]+xi,flip(pm->p[1]+yi),pm->p[2]+xi,flip(pm->p[3]+yi));
  326.     if(vimg_enable) vimg_line(scale_buf[0]+i*(scale_buf[4]-transx),
  327.         scale_buf[1]+i*(scale_buf[5]-transy),
  328.         scale_buf[2]+i*(scale_buf[4]-transx),
  329.         scale_buf[3]+i*(scale_buf[5]-transy));
  330.   }
  331. }
  332.  
  333. /* rectangle */
  334. void obj_rect(objparm *pm)
  335. {
  336.   int x1,y1,x2,y2;
  337.   scale(pm->pd,pm->p,2);
  338.   x1=min(pm->p[0],pm->p[2]); x2=max(pm->p[0],pm->p[2]);
  339.   y1=min(pm->p[1],pm->p[3]); y2=max(pm->p[1],pm->p[3]);
  340.   if(pm->fill)
  341.     gdImageFilledRectangle(image,x1,y1,x2,y2,pm->color[0]);
  342.   else
  343.     gdImageRectangle(image,x1,y1,x2,y2,pm->color[0]);
  344.   if(tikz_file)
  345.     fprintf(tikz_file,
  346.       "\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
  347.       tikz_options(pm->color[0],pm->fill),x1,flip(y1),x2,flip(y2));
  348.   if(vimg_enable) vimg_rect(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
  349. }
  350.  
  351. /* square */
  352. void obj_square(objparm *pm)
  353. {
  354.   int w,h;
  355.   scale(pm->pd,pm->p,1);
  356.   w=rint(pm->pd[2]); h=rint(pm->pd[2]);
  357.   if(pm->fill)
  358.     gdImageFilledRectangle(image,pm->p[0],pm->p[1],
  359.                pm->p[0]+w,pm->p[1]+h,pm->color[0]);
  360.   else
  361.     gdImageRectangle(image,pm->p[0],pm->p[1],
  362.                pm->p[0]+w,pm->p[1]+h,pm->color[0]);
  363.   if(tikz_file){
  364.     fprintf(tikz_file,
  365.       "\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
  366.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),
  367.       pm->p[0]+w,flip(pm->p[1]+h));
  368.   }
  369.   if(vimg_enable) vimg_rect(scale_buf[0],scale_buf[1],
  370.                       scale_buf[0]+pm->pd[2],scale_buf[1]+pm->pd[2]);
  371. }
  372.  
  373. /* triangle */
  374. void obj_triangle(objparm *pm)
  375. {
  376.   scale(pm->pd,pm->p,3);
  377.   if(pm->fill)
  378.     gdImageFilledPolygon(image,(gdPointPtr) pm->p,3,pm->color[0]);
  379.   else
  380.     gdImagePolygon(image,(gdPointPtr) pm->p,3,pm->color[0]);
  381.   if (tikz_file)
  382.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  383.       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]));
  384.   if(vimg_enable) vimg_polyline(scale_buf,3,1);
  385. }
  386.  
  387. /* polygon */
  388. void obj_poly(objparm *pm)
  389. {
  390.   int cnt,i;
  391.   cnt=(pm->pcnt)/2;
  392.   scale(pm->pd,pm->p,cnt);
  393.   if(pm->fill)
  394.     gdImageFilledPolygon(image,(gdPointPtr) pm->p,cnt,pm->color[0]);
  395.   else
  396.     gdImagePolygon(image,(gdPointPtr) pm->p,cnt,pm->color[0]);
  397.     if(tikz_file)
  398.       for (i= 0; i<cnt; i++)
  399.         fprintf(tikz_file,"\\draw\[%s] (%i,%i)--(%i,%i);\n",
  400.         tikz_options(pm->color[0],pm->fill),
  401.         pm->p[2*i],flip(pm->p[2*i+1]),
  402.         pm->p[2*((i+1)%cnt)],flip(pm->p[2*((i+1)%cnt)+1]));
  403.   if(vimg_enable) vimg_polyline(scale_buf,cnt,1);
  404. }
  405.  
  406. /* rays */
  407. void obj_rays(objparm *pm)
  408. {
  409.   int i, n;
  410.   n=(pm->pcnt)/2;
  411.   scale(pm->pd,pm->p,n);
  412.   for(i=2;i<2*n;i+=2) {
  413.     gdImageLine(image,pm->p[0],pm->p[1],pm->p[i],pm->p[i+1],pm->color[0]);
  414.     if (tikz_file)
  415.       fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  416.         tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),pm->p[i],flip(pm->p[i+1]));
  417.     if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],
  418.                         scale_buf[i],scale_buf[i+1]);
  419.   }
  420. }
  421. /* crosshair */
  422. void obj_crosshair(objparm *pm)
  423. {
  424.   scale(pm->pd,pm->p,2);
  425.   gdImageLine(image,pm->p[0]+width2,pm->p[1]+width2,pm->p[0]-width2,pm->p[1]-width2,pm->color[0]);
  426.   gdImageLine(image,pm->p[0]-width2,pm->p[1]+width2,pm->p[0]+width2,pm->p[1]-width2,pm->color[0]);
  427.   if (tikz_file){
  428.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  429.       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));
  430.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  431.       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));
  432.   }
  433. }
  434. /* crosshairs */
  435.  
  436. void obj_crosshairs(objparm *pm)
  437. {
  438.   int i, n;
  439.   n=(pm->pcnt)/2;
  440.   scale(pm->pd,pm->p,n);
  441.   for(i=0;i<2*n;i+=2) {
  442.     gdImageLine(image,pm->p[i]+width2,pm->p[i+1]+width2,pm->p[i]-width2,pm->p[i+1]-width2,pm->color[0]);
  443.     gdImageLine(image,pm->p[i]-width2,pm->p[i+1]+width2,pm->p[i]+width2,pm->p[i+1]-width2,pm->color[0]);
  444.     if (tikz_file){
  445.       fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  446.         tikz_options(pm->color[0],pm->fill),pm->p[i]+width2,flip(pm->p[i+1]+width2),pm->p[i]-width2,flip(pm->p[i+1]-width2));
  447.       fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  448.         tikz_options(pm->color[0],pm->fill),pm->p[i]-width2,flip(pm->p[i+1]+width2),pm->p[i]+width2,flip(pm->p[i+1]-width2));
  449.     }
  450.   }
  451. }
  452.  
  453.  
  454. /* segments */
  455. void obj_lines(objparm *pm)
  456. {
  457.   int i, n;
  458.   n=(pm->pcnt)/2;
  459.   scale(pm->pd,pm->p,n);
  460.   for(i=2;i<2*n;i+=2){
  461.     gdImageLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
  462.     if (tikz_file){
  463.       fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  464.         tikz_options(pm->color[0],pm->fill),pm->p[i-2],flip(pm->p[i-1]),pm->p[i],flip(pm->p[i+1]));
  465.     }
  466.     if(vimg_enable) vimg_polyline(scale_buf,n,0);
  467.   }
  468. }
  469. /*segments from x1,y1 to x2,y2, x3,y3 to x4,y4, etc */
  470. void obj_segments(objparm *pm)
  471. {
  472.   int i, n;
  473.   n=(pm->pcnt)/4;
  474.   scale(pm->pd,pm->p,2*n);
  475.   for(i=0;i<4*n;i+=4){
  476.     gdImageLine(image,pm->p[i],pm->p[i+1],pm->p[i+2],pm->p[i+3],pm->color[0]);
  477.     if (tikz_file){
  478.       fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  479.         tikz_options(pm->color[0],pm->fill),pm->p[i],flip(pm->p[i+1]),pm->p[i+2],flip(pm->p[i+3]));
  480.     }
  481.   }
  482. }
  483. /* segments */
  484. void obj_dlines(objparm *pm)
  485. {
  486.   int i, n;
  487.   n=(pm->pcnt)/2;
  488.   scale(pm->pd,pm->p,n);
  489.   for(i=2;i<2*n;i+=2)
  490.     myDashedLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
  491.     if (tikz_file){
  492.       fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  493.         tikz_options(pm->color[0],pm->fill),pm->p[i-2],flip(pm->p[i-1]),pm->p[i],flip(pm->p[i+1]));
  494.     }
  495.   if(vimg_enable) vimg_polyline(scale_buf,n,0);
  496. }
  497.  
  498. /* points */
  499. void obj_points(objparm *pm)
  500. {
  501.   int i, n;
  502.   n=(pm->pcnt)/2;
  503.   scale(pm->pd,pm->p,n);
  504.   for(i=0;i<2*n;i+=2){
  505.     gdImageSetPixel(image,pm->p[i],pm->p[i+1],pm->color[0]);
  506.     if(tikz_file) fprintf(tikz_file, "\\draw\[%s] (%i, %i) circle (0pt);\n",
  507.       tikz_options(pm->color[0],pm->fill),pm->p[i],flip(pm->p[i+1]));
  508.   }
  509. }
  510.  
  511. /* lattice.
  512.  * x0,y0,xv1,yv1,xv2,yv2,n1,n2,color */
  513. void obj_lattice(objparm *pm)
  514. {
  515.   int n1,n2,i1,i2,xi1,yi1,xi2,yi2;
  516.   double xv1,xv2,yv1,yv2;
  517.   n1=pm->pd[6];n2=pm->pd[7]; if(n1<0 || n2<0) return;
  518.   if(n1>256) n1=256;
  519.   if(n2>256) n2=256;
  520.   scale(pm->pd,pm->p,1);
  521.   scale2(pm->pd[2],pm->pd[3],&xv1,&yv1);
  522.   scale2(pm->pd[4],pm->pd[5],&xv2,&yv2);
  523.   for(i1=0;i1<n1;i1++) {
  524.     xi1=rint(i1*xv1)+pm->p[0]; yi1=rint(i1*yv1)+pm->p[1];
  525.     for(i2=0;i2<n2;i2++) {
  526.       xi2=i2*xv2+xi1;yi2=i2*yv2+yi1;
  527.       gdImageSetPixel(image,xi2,yi2,pm->color[0]);
  528.       if(tikz_file) fprintf(tikz_file,"\\draw[%s] (%i,%i) circle (0pt);\n",
  529.         tikz_options(pm->color[0],0),xi2,flip(yi2));
  530.     }
  531.   }
  532. }
  533.  
  534. /* arc */
  535. void obj_arc(objparm *pm)
  536. {
  537.   scale(pm->pd,pm->p,1);
  538.   pm->p[2]=rint(pm->pd[2]*xscale); pm->p[3]=rint(pm->pd[3]*yscale);
  539.   gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],
  540.            pm->pd[4],pm->pd[5],pm->color[0]);
  541.   if(tikz_file) {
  542.       fprintf(tikz_file,
  543.     "\\draw\[%s, domain=%f:%f] plot ({%i+%f*cos(\\x)}, {%i+%f*sin(\\x)});\n",
  544.     tikz_options(pm->color[0],pm->fill),
  545.       pm->pd[4],pm->pd[5],pm->p[0],pm->p[2]*0.5,flip(pm->p[1]),pm->p[3]*(-0.5));
  546.   }
  547.   /*FIXME echelle mauvaise*/
  548.   if(vimg_enable) vimg_arc(scale_buf[0],scale_buf[1],
  549.                      0.5*pm->pd[2],0.5*pm->pd[3],pm->pd[4],pm->pd[5]);
  550. }
  551.  
  552. /* Ellipse: centre 0,1, width 2, hight 3, color 4,5,6 */
  553. void obj_ellipse(objparm *pm)
  554. {
  555.   scale(pm->pd,pm->p,1);
  556.   pm->p[2]=rint(pm->pd[2]*xscale); pm->p[3]=rint(pm->pd[3]*yscale);
  557.   if(pm->fill) {
  558.     gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
  559.              color_bounder);
  560.     patchgdImageFillToBorder(image,pm->p[0],pm->p[1],
  561.                     color_bounder,pm->color[0]);
  562.   }
  563.   gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,pm->color[0]);
  564.   if(tikz_file) fprintf(tikz_file,"\\draw\[%s] (%i,%i) ellipse (%f and %f);\n\n",
  565.     tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),pm->p[2]/2.,pm->p[3]/2.);
  566.   if(vimg_enable) vimg_ellipse(scale_buf[0],scale_buf[1],0.5*pm->pd[2],0.5*pm->pd[3]);
  567. }
  568.  
  569. /* Circle */
  570. void obj_circle(objparm *pm)
  571. {
  572.   scale(pm->pd,pm->p,1);
  573.   pm->p[2]=rint(pm->pd[2]); pm->p[3]=rint(pm->pd[2]);
  574.   if(pm->fill) {
  575.     gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
  576.              color_bounder);
  577.     patchgdImageFillToBorder(image,pm->p[0],pm->p[1],
  578.                     color_bounder,pm->color[0]);
  579.   }
  580.   gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,pm->color[0]);
  581.   if(tikz_file) fprintf(tikz_file, "\\draw\[%s] (%i, %i) circle (%f);\n",
  582.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),pm->p[2]/2.);
  583. }
  584.  
  585. typedef enum tikz_fill_options {grille, points, hatch, dhatch} tfo;
  586. int compar(const void *a, const void *b) {return *(int *)b - *(int *)a;}
  587.  
  588. void tikz_fill(int x, int y, int nx, int ny, int the_color, tfo opt)
  589. {
  590.   char *to = tikz_options(the_color,1);
  591.   int numpix=sizex*sizey, top = 0, a, cy, lx, rx, seed, nt,ct=0, ct2=0;
  592.   int *stack = xmalloc(numpix*sizeof(int));
  593.   int *stack2 = xmalloc(numpix*sizeof(int));
  594.   if (nx==0) nx=1; if (ny==0) ny=1; nt = abs(nx*ny);
  595.   seed = gdImageGetPixel(image,x,y);
  596.   char *check = xmalloc(numpix);
  597.   while (numpix--) check[numpix]=0;
  598.   a=x+y*sizex;
  599.   check[a]=1;
  600.   stack[top++]=a;
  601.   while(top)
  602.     {
  603.       a = stack[--top];
  604.       x = a%sizex; y = a/sizex;
  605.       if (gdImageGetPixel(image,x,y) != seed) continue;
  606.       if (x>0 && !check[a-1]) {check[a-1]=1; stack[top++]=a-1;}
  607.       if (x+1<sizex && !check[a+1]) {check[a+1]=1; stack[top++]=a+1;}
  608.       if (y>0 && !check[a-sizex]) {check[a-sizex]=1; stack[top++]=a-sizex;}
  609.       if (y+1<sizey && !check[a+sizex]) {check[a+sizex]=1; stack[top++]=a+sizex;}
  610.       switch(opt)
  611.         {
  612.         case grille: if(((x%nx)!=(nx/2))&&((y%ny)!=(ny/2))) continue; break;
  613.         case points: if(x%nx!=nx/2||y%ny!=ny/2) continue; break;
  614.         case hatch: if(abs((ny*x-nx*y)%nt)!=nt/2) continue; break;
  615.         case dhatch: if((ny*x+nx*y)%nt!=nt/2 && abs((ny*x-nx*y)%nt)!=nt/2) continue; break;
  616.         default: break;
  617.         }
  618.       stack2[ct++]=a;
  619.     }
  620.   free(stack);
  621.   free(check);
  622.   if (ct==0) return;
  623.   qsort(stack2, ct, sizeof(int), compar);
  624.   cy=-1;
  625.   while (ct--)
  626.     {
  627.       a = stack2[ct];
  628.       x = a%sizex; y = a/sizex;
  629.       if (y != cy || x != rx+1)
  630.       {
  631.         if (cy >= 0)
  632.           fprintf(tikz_file,
  633.                   "\\draw\[%s] (%.2f,%.2f) rectangle (%.2f,%.2f);\n",
  634.                   to, lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
  635.         cy = y;
  636.         lx = rx = x;
  637.         ct2++;
  638.       }
  639.       else
  640.         rx++;
  641.     }
  642.   fprintf(tikz_file,
  643.           "\\draw\[%s] (%.2f,%.2f) rectangle (%.2f,%.2f);\n",
  644.           to, lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
  645.   free(stack2);
  646. }
  647.  
  648. /* flood fill */
  649. void obj_fill(objparm *pm)
  650. {
  651.   scale(pm->pd,pm->p,1);
  652.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,pm->color[0],grille);
  653.   patchgdImageFill(image,pm->p[0],pm->p[1],pm->color[0]);
  654. }
  655.  
  656. /* flood fill to border*/
  657. void obj_fillb(objparm *pm)
  658. {
  659.   scale(pm->pd,pm->p,1);
  660.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,pm->color[0],grille);
  661.   patchgdImageFillToBorder(image,pm->p[0],pm->p[1],pm->color[0],pm->color[1]);
  662. }
  663.  
  664. gdImagePtr himg;
  665.  
  666. int makehatchimage(int x, int y, int px, int py, int col)
  667. {
  668.   int c1,c2,r,g,b;
  669.   gdImagePtr saveimg;
  670.   himg=gdImageCreate(x,y);
  671.   c1=gdImageGetPixel(image,px,py);
  672.   r=gdImageRed(image,c1); g=gdImageGreen(image,c1); b=gdImageBlue(image,c1);
  673.   if(r>=255) r--; else r++; if(g>=255) g--; else g++; if(b>=255) b--; else b++;
  674.   c1=gdImageColorAllocate(himg,r,g,b);
  675.   r=gdImageRed(image,col); g=gdImageGreen(image,col); b=gdImageBlue(image,col);
  676.   c2=gdImageColorAllocate(himg,r,g,b);
  677.   if(width>1) {
  678.     savew=-1; saveimg=image;
  679.     image=himg; c2=widthcolor(width,c2); image=saveimg;
  680.     c2=gdBrushed; savew=-1;
  681.   }
  682.   return c2;
  683. }
  684.  
  685. /* flood fill with hatching */
  686. void obj_hatchfill(objparm *pm)
  687. {
  688.   int nx,ny,ax,ay, dir, c;
  689.   scale(pm->pd,pm->p,1);
  690.   nx=pm->pd[2]; ny=pm->pd[3]; ax=abs(nx); ay=abs(ny);
  691.   if(nx==0 && ny==0) {fly_error("bad displacement vector"); return;}
  692.   if((nx>0 && ny>0) || (nx<0 && ny<0)) dir=1; else dir=-1;
  693.   if(ax==0) {ax=100; dir=2;}
  694.   if(ay==0) {ay=100; dir=3;}
  695.   c=makehatchimage(ax,ay,pm->p[0],pm->p[1],pm->color[0]);
  696.   switch(dir) {
  697.     case -1: {
  698.       gdImageLine(himg,0,ay-1,ax-1,0,c);
  699.       if(width>1) {
  700.         gdImageLine(himg,-ax,ay-1,-1,0,c);
  701.         gdImageLine(himg,ax,ay-1,2*ax-1,0,c);
  702.         gdImageLine(himg,0,-1,ax-1,-ay,c);
  703.         gdImageLine(himg,0,2*ay-1,ax-1,ay,c);
  704.       }
  705.       break;
  706.     }
  707.     case 1: {
  708.       gdImageLine(himg,0,0,ax-1,ay-1,c);
  709.       if(width>1) {
  710.         gdImageLine(himg,-ax,0,-1,ay-1,c);
  711.         gdImageLine(himg,ax,0,2*ax-1,ay-1,c);
  712.         gdImageLine(himg,0,-ay,ax-1,-1,c);
  713.         gdImageLine(himg,0,ay,ax-1,2*ay-1,c);
  714.       }
  715.       break;
  716.     }
  717.     case 2: gdImageLine(himg,0,ay/2,ax-1,ay/2,c); break;
  718.     case 3: gdImageLine(himg,ax/2,0,ax/2,ay-1,c); break;
  719.   }
  720.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,pm->color[0],hatch);
  721.   gdImageSetTile(image,himg);
  722.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  723.   gdImageDestroy(himg);
  724.   if(tiled) gdImageSetTile(image,tileimg);
  725. }
  726.  
  727. /* flood fill with grid */
  728. void obj_gridfill(objparm *pm)
  729. {
  730.   int nx,ny, c;
  731.   scale(pm->pd,pm->p,1);
  732.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  733.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  734.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  735.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,pm->color[0],grille);
  736.   gdImageLine(himg,0,ny/2,nx-1,ny/2,c); gdImageLine(himg,nx/2,0,nx/2,ny-1,c);
  737.   gdImageSetTile(image,himg);
  738.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  739.   gdImageDestroy(himg);
  740.   if(tiled) gdImageSetTile(image,tileimg);
  741. }
  742.  
  743. /* flood fill with double hatching */
  744. void obj_diafill(objparm *pm)
  745. {
  746.   int nx,ny, c;
  747.   scale(pm->pd,pm->p,1);
  748.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  749.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  750.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  751.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,pm->color[0],dhatch);
  752.   gdImageLine(himg,0,0,nx-1,ny-1,c); gdImageLine(himg,0,ny-1,nx-1,0,c);
  753.   gdImageSetTile(image,himg);
  754.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  755.   gdImageDestroy(himg);
  756.   if(tiled) gdImageSetTile(image,tileimg);
  757. }
  758.  
  759. /* flood fill with dots */
  760. void obj_dotfill(objparm *pm)
  761. {
  762.   int nx,ny, c;
  763.   scale(pm->pd,pm->p,1);
  764.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  765.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  766.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  767.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,pm->color[0],points);
  768.   gdImageSetPixel(himg,nx/2,ny/2,c);
  769.   gdImageSetTile(image,himg);
  770.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  771.   gdImageDestroy(himg);
  772.   if(tiled) gdImageSetTile(image,tileimg);
  773. }
  774.  
  775. struct {
  776.   char *name;
  777.   gdFontPtr *fpt;
  778. } fonttab[]={
  779.   {"tiny",      &gdFontTiny},
  780.   {"small",      &gdFontSmall},
  781.   {"medium",&gdFontMediumBold},
  782.   {"large",      &gdFontLarge},
  783.   {"giant",      &gdFontGiant},
  784.   {"huge",      &gdFontGiant}
  785. };
  786.  
  787. #define fonttab_no (sizeof(fonttab)/sizeof(fonttab[0]))
  788.  
  789. /* string */
  790. void obj_string(objparm *pm)
  791. {
  792.   char *pp, *pe, *p2;
  793.   int i;
  794.   pp=pm->str; pe=strchr(pp,','); if(pe==NULL) {
  795.     fly_error("too_few_parms"); return;
  796.   }
  797.   *pe++=0; pp=find_word_start(pp); *find_word_end(pp)=0;
  798.   pe=find_word_start(pe); strip_trailing_spaces(pe);
  799.   if(*pp) {
  800.     for(i=0;i<fonttab_no && strcmp(pp,fonttab[i].name)!=0; i++);
  801.     if(i>=fonttab_no) i=1;
  802.   }
  803.   else i=1;
  804.   scale(pm->pd,pm->p,1);
  805.   if(*pe=='"') {
  806.     p2=strchr(pe+1,'"');
  807.     if(p2 && *(p2+1)==0) {*p2=0; pe++;}
  808.   }
  809.   if(pm->fill){
  810.     gdImageStringUp(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
  811.         pm->color[0]);
  812.     if(tikz_file) fprintf(tikz_file,"\\draw (%i,%i) node[font=\\%s,rotate=90,color=%s,anchor=north west,inner sep=0pt] {%s};\n",
  813.       pm->p[0],flip(pm->p[1]),fonttab[i].name,tikz_color(pm->color[0]),(unsigned char*) pe);
  814.   }
  815.   else {
  816.     gdImageString(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
  817.         pm->color[0]);
  818.     if(tikz_file) fprintf(tikz_file,"\\draw (%i,%i) node[font=\\%s,color=%s,anchor=north west,inner sep=0pt] {%s};\n",
  819.       pm->p[0],flip(pm->p[1]),fonttab[i].name,tikz_color(pm->color[0]),(unsigned char*) pe);
  820.   }
  821. }
  822. /*FIXME; giant does not exist in tikz and ... samething as huge */
  823. /* point */
  824. void obj_point(objparm *pm)
  825. {
  826.   scale(pm->pd,pm->p,1);
  827.   gdImageSetPixel(image,pm->p[0],pm->p[1],pm->color[0]);
  828.   if(tikz_file) fprintf(tikz_file,"\\draw[%s] (%i,%i) circle (0pt);\n",
  829.     tikz_options(pm->color[0],0),pm->p[0],flip(pm->p[1]));
  830. }
  831.  
  832. /* copy an image file */
  833. void obj_copy(objparm *pm)
  834. {
  835.   char *pp;
  836.   FILE *inf;
  837.   gdImagePtr insimg;
  838.  
  839.   pp=find_word_start(pm->str);*find_word_end(pp)=0;
  840.   inf=open4read(pp);
  841.   if(inf==NULL) {
  842.     fly_error("file_does_not_exist"); return;
  843.   }
  844.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  845.   if(insimg==NULL) {
  846.     fly_error("bad_gif"); return;
  847.   }
  848.   scale(pm->pd,pm->p,1);
  849.   if(pm->pd[2]<0 && pm->pd[3]<0 && pm->pd[4]<0 && pm->pd[5]<0)
  850.     gdImageCopy(image,insimg,pm->p[0],pm->p[1],0,0,
  851.             insimg->sx,insimg->sy);
  852.   else
  853.     gdImageCopy(image,insimg,pm->p[0],pm->p[1],pm->pd[2],pm->pd[3],
  854.             pm->pd[4]-pm->pd[2],pm->pd[5]-pm->pd[3]);
  855.   if(tikz_file) fprintf(tikz_file,
  856.     "\\node at (%i,%i) {\\includegraphics\[width=.05\\linewidth]{%s}};\n",
  857.       pm->p[0],flip(pm->p[1]),pm->str);
  858.     gdImageDestroy(insimg);
  859. /*FIXME position non correcte; on ne peut pas utiliser une image gif */
  860. }
  861.  
  862. /* copy an image file, with resizing */
  863. void obj_copyresize(objparm *pm)
  864. {
  865.   char *pp;
  866.   FILE *inf;
  867.   gdImagePtr insimg;
  868.  
  869.   pp=find_word_start(pm->str);*find_word_end(pp)=0;
  870.   inf=open4read(pp);
  871.   if(inf==NULL) {
  872.     fly_error("file_not_found"); return;
  873.   }
  874.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  875.   if(insimg==NULL) {
  876.     fly_error("bad_gif"); return;
  877.   }
  878.   scale(pm->pd+4,pm->p+4,2);
  879.   if(pm->pd[0]<0 && pm->pd[1]<0 && pm->pd[2]<0 && pm->pd[3]<0)
  880.     gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],0,0,
  881.                  pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
  882.                  insimg->sx,insimg->sy);
  883.   else
  884.     gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],pm->pd[0],pm->pd[1],
  885.                  pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
  886.                  pm->pd[2]-pm->pd[0]+1,pm->pd[3]-pm->pd[1]+1);
  887.   gdImageDestroy(insimg);
  888. }
  889.  
  890. /* set brush or tile */
  891. void obj_setbrush(objparm *pm)
  892. {
  893.   char *pp;
  894.   FILE *inf;
  895.   gdImagePtr insimg;
  896.  
  897.   pp=find_word_start(pm->str); *find_word_end(pp)=0;
  898.   inf=open4read(pp); if(inf==NULL) {
  899.     fly_error("file_not_found"); return;
  900.   }
  901.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  902.   if(insimg==NULL) {
  903.     fly_error("bad_gif"); return;
  904.   }
  905.   if(pm->fill) {
  906.     gdImageSetTile(image,insimg); tiled=1; tileimg=insimg;
  907.   }
  908.   else {
  909.     gdImageSetBrush(image,insimg);brushed=1; brushimg=insimg;
  910.   }
  911. }
  912.  
  913. /* kill brush */
  914. void obj_killbrush(objparm *pm)
  915. {
  916.   if(brushimg) gdImageDestroy(brushimg);
  917.   brushed=0; brushimg=NULL;
  918. }
  919.  
  920. /* kill tile */
  921. void obj_killtile(objparm *pm)
  922. {
  923.   if(tileimg) gdImageDestroy(tileimg);
  924.   tiled=0; tileimg=NULL;
  925. }
  926.  
  927. /* set style */
  928. void obj_setstyle(objparm *pm)
  929. {
  930.   int i,t;
  931.   t=pm->pcnt/3; if(t<=0) {
  932.     fly_error("too_few_parms"); return;
  933.   }
  934.   for(i=0;i<t;i++) {
  935.     if(pm->pd[3*i]<0 || pm->pd[3*i+1]<0 || pm->pd[3*i+2]<0)
  936.       pm->p[i]=gdTransparent;
  937.     else
  938.       pm->p[i]=getcolor(pm->pd[3*i],pm->pd[3*i+1],pm->pd[3*i+2]);
  939.   }
  940.   gdImageSetStyle(image,pm->p,t); styled=1;
  941. }
  942.  
  943. /* kill style */
  944. void obj_killstyle(objparm *pm)
  945. {
  946.   styled=0;
  947. }
  948.  
  949. /* set transparent */
  950. void obj_transp(objparm *pm)
  951. {
  952.   gdImageColorTransparent(image,pm->color[0]);
  953. }
  954.  
  955. /* set interlace */
  956. void obj_interlace(objparm *pm)
  957. {
  958.   gdImageInterlace(image,1);
  959. }
  960.  
  961. /* set linewidth */
  962. void obj_linewidth(objparm *pm)
  963. {
  964.   if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
  965.   width=pm->pd[0];
  966.   if(tikz_file) fprintf(tikz_file,"\\pgfsetlinewidth{%f}\n",width*0.7);
  967. }
  968.  
  969. /* set crosshairsize */
  970. void obj_crosshairsize(objparm *pm)
  971. {
  972.   if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
  973.   else width2=pm->pd[0];
  974. }
  975.  
  976. /* set x range */
  977. void obj_xrange(objparm *pm)
  978. {
  979.   double dd;
  980.   dd=pm->pd[1]-pm->pd[0];
  981.   xstart=pm->pd[0]; xscale=sizex/dd;
  982. }
  983.  
  984. /* set y range */
  985. void obj_yrange(objparm *pm)
  986. {
  987.   double dd;
  988.   dd=pm->pd[1]-pm->pd[0];
  989.   ystart=pm->pd[1]; yscale=-sizey/dd;
  990. }
  991.  
  992. /* set x et y range */
  993. void obj_range(objparm *pm)
  994. {
  995.   double d1,d2;
  996.   d1=pm->pd[1]-pm->pd[0]; d2=pm->pd[3]-pm->pd[2];
  997.   xstart=pm->pd[0]; xscale=(sizex-1)/d1;
  998.   ystart=pm->pd[3]; yscale=-(sizey-1)/d2;
  999. }
  1000.  
  1001. /* set t range */
  1002. void obj_trange(objparm *pm)
  1003. {
  1004.   /*double dd;
  1005.   dd=pm->pd[1]-pm->pd[0];*/
  1006.   tstart=pm->pd[0]; tend=pm->pd[1];
  1007.   tranged=1;
  1008. }
  1009.  
  1010. /* set tstep (plotting step) */
  1011. void obj_tstep(objparm *pm)
  1012. {
  1013.   int dd;
  1014.   dd=pm->pd[0];
  1015.   if(dd<3) {
  1016.     fly_error("bad_step"); return;
  1017.   }
  1018.   if(dd>2000) dd=2000;
  1019.   tstep=dd;
  1020. }
  1021.  
  1022. /* set plotjump (plot jump break threashold) */
  1023. void obj_plotjump(objparm *pm)
  1024. {
  1025.   int dd;
  1026.   dd=pm->pd[0];
  1027.   if(dd<3) dd=3;
  1028.   if(dd>MAX_SIZE) dd=MAX_SIZE;
  1029.   plotjump=dd;
  1030. }
  1031.  
  1032. /* plot a curve, either parametric or explicit */
  1033. void _obj_plot(objparm *pm,int dash)
  1034. {
  1035.   int i,j,n,dist,xx,yy,varpos;
  1036.   char p1[MAX_LINELEN+1], p2[MAX_LINELEN+1];
  1037.   char *varn, *pp;
  1038.   double dc[2],t,v;
  1039.   int ic[2],oc[2];
  1040.  
  1041.   n=itemnum(pm->str);
  1042.   if(n<1) {
  1043.     fly_error("bad_parms"); return;
  1044.   }
  1045.   fnd_item(pm->str,1,p1); fnd_item(pm->str,2,p2);
  1046.   if(n==1 && !tranged) v=sizex/xscale/tstep;
  1047.   else v=(tend-tstart)/tstep;
  1048.   if(n==1) varn="x"; else varn="t";
  1049.   for(pp=varchr(p1,varn); pp; pp=varchr(pp,varn)) {
  1050.     string_modify(p1,pp,pp+strlen(varn),EV_T);
  1051.     pp+=strlen(EV_T);
  1052.   }
  1053.   for(pp=varchr(p2,varn); pp; pp=varchr(pp,varn)) {
  1054.     string_modify(p2,pp,pp+strlen(varn),EV_T);
  1055.       pp+=strlen(EV_T);
  1056.   }
  1057.   varpos=eval_getpos(EV_T);
  1058.   if(varpos<0) return; /* unknown error */
  1059.   evalue_compile(p1); evalue_compile(p2);
  1060.   if(vimg_enable) vimg_plotstart();
  1061.   for(i=j=0;i<=tstep;i++) {
  1062.     if(n==1) {
  1063.       if(tranged) t=tstart+i*v; else t=xstart+i*v;
  1064.       eval_setval(varpos,t); dc[0]=t; dc[1]=strevalue(p1);
  1065.     }
  1066.       else {
  1067.         t=tstart+i*v; eval_setval(varpos,t);
  1068.         dc[0]=strevalue(p1); dc[1]=strevalue(p2);
  1069.     }
  1070.       if(!isfinite(dc[0]) || !isfinite(dc[1])) ic[0]=ic[1]=-BOUND;
  1071.       else scale(dc,ic,1);
  1072.       if(vimg_enable) vimg_plot1 (scale_buf[0],scale_buf[1]);
  1073.       if(j==0) {
  1074.         gdImageSetPixel(image,ic[0],ic[1],pm->color[0]); j++;
  1075.         if (tikz_file)
  1076.           fprintf(tikz_file,
  1077.             "\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
  1078.             tikz_options(pm->color[0],1),ic[0],flip(ic[1]),ic[0]+1,flip(ic[1]+1));
  1079.        }
  1080.       else {
  1081.         xx=ic[0]-oc[0]; yy=ic[1]-oc[1];
  1082.         dist=sqrt(xx*xx+yy*yy);
  1083.         if(dist>0){
  1084.           if(dist>plotjump || dist==1) {
  1085.             gdImageSetPixel(image,ic[0],ic[1],pm->color[0]);
  1086.             if (tikz_file)
  1087.               fprintf(tikz_file,
  1088.                 "\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
  1089.                 tikz_options(pm->color[0],1),ic[0],flip(ic[1]),ic[0]+1,flip(ic[1]+1));
  1090.           }
  1091.           }
  1092.           else {
  1093.             if ( dash==1)
  1094.               gdImageDashedLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
  1095.             else
  1096.               gdImageLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
  1097.             if (tikz_file)
  1098.               fprintf(tikz_file,
  1099.                 "\\draw\[%s] (%i,%i) -- (%i,%i);\n",
  1100.                 tikz_options(pm->color[0],dash),oc[0],flip(oc[1]),ic[0],flip(ic[1]));
  1101.  
  1102.           }
  1103.         }
  1104.     memmove(oc,ic,sizeof(oc));
  1105.   }
  1106.   if(vimg_enable) vimg_plotend();
  1107. }
  1108.  
  1109. void obj_plot(objparm *pm) { _obj_plot( pm,0) ;}
  1110. void obj_dplot(objparm *pm) { _obj_plot( pm,1) ; }
  1111.  
  1112. /* set levelcurve granularity */
  1113. void obj_levelstep(objparm *pm)
  1114. {
  1115.   int dd;
  1116.   dd=pm->pd[0];
  1117.   if(dd<1) return;
  1118.   if(dd>16) dd=16;
  1119.   lstep=dd;
  1120. }
  1121.  
  1122. /* level curve */
  1123. void obj_levelcurve(objparm *pm)
  1124. {
  1125.   char fn[MAX_LINELEN+1],tc[MAX_LINELEN+1];
  1126.   int n,i;
  1127.   double d;
  1128.   leveldata *ld;
  1129.  
  1130.   ld=xmalloc(sizeof(leveldata)+16);
  1131.   ld->xname="x"; ld->yname="y";
  1132.   ld->xsize=sizex; ld->ysize=sizey; ld->datacnt=0;
  1133.   ld->xrange[0]=xstart; ld->xrange[1]=sizex/xscale+xstart;
  1134.   ld->yrange[0]=sizey/yscale+ystart; ld->yrange[1]=ystart;
  1135.   ld->grain=lstep;
  1136.   fnd_item(pm->str,1,fn); ld->fn=fn;
  1137.   n=itemnum(pm->str);
  1138.   if(n<=1) {ld->levelcnt=1; ld->levels[0]=0;}
  1139.   else {
  1140.     if(n>LEVEL_LIM+1) n=LEVEL_LIM+1;
  1141.     for(i=0;i<n-1;i++) {
  1142.       fnd_item(pm->str,i+2,tc);
  1143.       d=strevalue(tc);
  1144.       if(isfinite(d)) ld->levels[i]=d; else ld->levels[i]=0;
  1145.     }
  1146.     ld->levelcnt=n-1;
  1147.   }
  1148.   levelcurve(ld);
  1149.   for(i=0;i<ld->datacnt;i++) {
  1150.     gdImageSetPixel(image,ld->xdata[i],ld->ydata[i],pm->color[0]);
  1151.     if (tikz_file)
  1152.       fprintf(tikz_file,
  1153.         "\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
  1154.         tikz_options(pm->color[0],1),ld->xdata[i],flip(ld->ydata[i]),ld->xdata[i]+1,flip(ld->ydata[i]+1));
  1155.   }
  1156.   free(ld);
  1157. }
  1158.  
  1159. /* set animation step */
  1160. void obj_animstep(objparm *pm)
  1161. {
  1162.   animstep=pm->pd[0];
  1163.   setvar("animstep",pm->pd[0]);
  1164. }
  1165.  
  1166. /* Starts a line counting */
  1167. void obj_linecount(objparm *pm)
  1168. {
  1169.   linecnt=pm->pd[0];
  1170. }
  1171.  
  1172. /* marks end of execution */
  1173. void obj_end(objparm *pm)
  1174. {
  1175.   fly_error("successful_end_of_execution");
  1176. }
  1177.  
  1178. /* output */
  1179. void obj_output(objparm *pm)
  1180. {
  1181.   char *p, namebuf[1024];
  1182.   p=find_word_start(pm->str); *find_word_end(p)=0;
  1183.   snprintf(namebuf,sizeof(namebuf),"%s",imagefilename);
  1184.   snprintf(imagefilename,sizeof(imagefilename),"%s",p);
  1185.   output();
  1186.   snprintf(imagefilename,sizeof(imagefilename),"%s",namebuf);
  1187. }
  1188.  
  1189. /* vimgfile */
  1190. void obj_vimgfile(objparm *pm)
  1191. {
  1192.   char *p;
  1193.   p=find_word_start(pm->str); *find_word_end(p)=0;
  1194.   snprintf(vimgfilename,sizeof(vimgfilename),"%s",p);
  1195.   if(vimg_ready) vimg_close();
  1196. }
  1197.  
  1198. /* vimg enable/disable */
  1199. void obj_vimg(objparm *pm)
  1200. {
  1201.   vimg_enable=pm->pd[0];
  1202.   if(vimg_enable>0 && vimg_ready==0) vimg_init();
  1203. }
  1204.  
  1205. /* Set affine transformation */
  1206. void obj_affine(objparm *pm)
  1207. {
  1208.   int i;
  1209.   for(i=0;i<4;i++) matrix[i]=pm->pd[i];
  1210.   transx=pm->pd[4]; transy=pm->pd[5];
  1211.   transform=1;
  1212. }
  1213.  
  1214. /* Set affine transformation */
  1215. void obj_rotation(objparm *pm)
  1216. {
  1217.   double r;
  1218.   r=M_PI*pm->pd[0]/180;
  1219.   matrix[0]=matrix[3]=cos(r);
  1220.   matrix[1]=-sin(r); matrix[2]=sin(r);
  1221.   transform=1;
  1222. }
  1223.  
  1224. /* Set linear transformation */
  1225. void obj_linear(objparm *pm)
  1226. {
  1227.   int i;
  1228.   for(i=0;i<4;i++) matrix[i]=pm->pd[i];
  1229.   transform=1;
  1230. }
  1231.  
  1232. /* Set translation transformation */
  1233. void obj_translation(objparm *pm)
  1234. {
  1235.   transx=pm->pd[0]; transy=pm->pd[1];
  1236. }
  1237.  
  1238. /* kill affine transformation */
  1239. void obj_killaffine(objparm *pm)
  1240. {
  1241.   matrix[0]=matrix[3]=1;
  1242.   matrix[1]=matrix[2]=transx=transy=0;
  1243.   transform=0;
  1244. }
  1245.  
  1246. /* kill affine transformation */
  1247. void obj_killlinear(objparm *pm)
  1248. {
  1249.   matrix[0]=matrix[3]=1;
  1250.   matrix[1]=matrix[2]=0;
  1251.   transform=0;
  1252. }
  1253.  
  1254. /* kill affine transformation */
  1255. void obj_killtranslation(objparm *pm)
  1256. {
  1257.   transx=transy=0;
  1258. }
  1259.  
  1260. /***** Les modifs de Jean-Christophe Leger Fev 2006 *****/
  1261.  
  1262. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES, une liste de 4 nombres reels pour la matrice */
  1263. void obj_setmatrix(objparm *pm)
  1264. {
  1265.   int nummatrix = (int) (pm->pd[0]);
  1266.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1267.     fly_error("bad_matrix_number");
  1268.     return;
  1269.   }
  1270.   matrices_pavage[nummatrix][0] = pm->pd[1];
  1271.   matrices_pavage[nummatrix][1] = pm->pd[2];
  1272.   matrices_pavage[nummatrix][2] = pm->pd[3];
  1273.   matrices_pavage[nummatrix][3] = pm->pd[4];
  1274. }
  1275.  
  1276. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1277. void obj_resetmatrix(objparm *pm)
  1278. {
  1279.   int nummatrix = (int) (pm->pd[0]);
  1280.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1281.     fly_error("bad_matrix_number");
  1282.     return;
  1283.   }
  1284.   matrices_pavage[nummatrix][0] = 1;
  1285.   matrices_pavage[nummatrix][1] = 0;
  1286.   matrices_pavage[nummatrix][2] = 0;
  1287.   matrices_pavage[nummatrix][3] = 1;
  1288.  
  1289. }
  1290. /* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 2 nombres reels pour le vecteur */
  1291. void obj_setvector(objparm *pm)
  1292. {
  1293.   int nummatrix = (int) (pm->pd[0]);
  1294.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1295.     fly_error("bad_vector_number");
  1296.     return;
  1297.   }
  1298.   vecteurs_pavage[nummatrix][0] = pm->pd[1];
  1299.   vecteurs_pavage[nummatrix][1] = pm->pd[2];
  1300. }
  1301.  
  1302. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1303. void obj_resetvector(objparm *pm)
  1304. {
  1305.   int nummatrix = (int) (pm->pd[0]);
  1306.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1307.     fly_error("bad_vector_number");
  1308.     return;
  1309.   }
  1310.   vecteurs_pavage[nummatrix][0] = 0;
  1311.   vecteurs_pavage[nummatrix][1] = 0;
  1312. }
  1313.  
  1314. /* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 6 nombres reels pour la matrice et le vecteur */
  1315. void obj_settransform(objparm *pm)
  1316. {
  1317.   int nummatrix = (int) (pm->pd[0]);
  1318.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1319.     fly_error("bad_vector_number");
  1320.     return;
  1321.   }
  1322.   matrices_pavage[nummatrix][0] = pm->pd[1];
  1323.   matrices_pavage[nummatrix][1] = pm->pd[2];
  1324.   matrices_pavage[nummatrix][2] = pm->pd[3];
  1325.   matrices_pavage[nummatrix][3] = pm->pd[4];
  1326.   vecteurs_pavage[nummatrix][0] = pm->pd[5];
  1327.   vecteurs_pavage[nummatrix][1] = pm->pd[6];
  1328. }
  1329.  
  1330.  
  1331. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1332. void obj_resettransform(objparm *pm)
  1333. {
  1334.   int nummatrix = (int) (pm->pd[0]);
  1335.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1336.     fly_error("bad_vector_number");
  1337.     return;
  1338.   }
  1339.   vecteurs_pavage[nummatrix][0] = 0;
  1340.   vecteurs_pavage[nummatrix][1] = 0;
  1341.   matrices_pavage[nummatrix][0] = 1;
  1342.   matrices_pavage[nummatrix][1] = 0;
  1343.   matrices_pavage[nummatrix][2] = 0;
  1344.   matrices_pavage[nummatrix][3] = 1;
  1345. }
  1346.  
  1347. /* arguments: une liste de 6 nombres reels pour les coordonnees de l'origine et des vecteurs de base */
  1348. void obj_setparallelogram(objparm *pm)
  1349. {
  1350.   int i;
  1351.   for(i=0;i<6;i++)  parallogram_fonda[i]=pm->pd[i];
  1352. }
  1353.  
  1354. /* arguments :aucun */
  1355. void obj_resetparallelogram(objparm *pm)
  1356. {
  1357.   parallogram_fonda[0]=0;
  1358.   parallogram_fonda[1]=0;
  1359.   parallogram_fonda[2]=100;
  1360.   parallogram_fonda[3]=0;
  1361.   parallogram_fonda[4]=0;
  1362.   parallogram_fonda[5]=100;
  1363. }
  1364.  
  1365. /* argument : la liste des numeros des matrices a faire agir, le nom du fichier de l'image a inclure */
  1366. void obj_multicopy(objparm *pm)
  1367. {
  1368.   char *pp,*pe,*cptr;
  1369.   FILE *inf;
  1370.   gdImagePtr insimg;
  1371.   int i,j;
  1372.   int imax,jmax;
  1373.   int c; /* la couleur reperee */
  1374.   int mat;
  1375.   int nummatrices[JC_NB_MATRICES];
  1376.   double pt[2]; /* les coordonnees math. du point repere dans le parallelogramme */
  1377.   double ptr[2]; /* les coordonnees math. du point transforme */
  1378.   int pti[2]; /* les coordonnees img du point a placer sur le canevas */
  1379.   int t;
  1380.   /* gestion palette de couleur de l'image a inserer */
  1381.   int colorMap[gdMaxColors];
  1382.   int comptmat=0; /* combien de matrices en tout ? */
  1383.  
  1384.   for (i=0; (i<gdMaxColors); i++) {
  1385.     colorMap[i] = (-1);
  1386.   }
  1387.  
  1388.   /* Gestion des parametres la liste est censee finir avec le nom du fichier */
  1389.   t=pm->pcnt-1; /* il faut enlever le nom de fichier ! c'est le nombre de parametres en plus */
  1390.   pp=find_word_start(pm->str);
  1391.   /* visiblement find_word_start n'arrive pas a trouver le dernier mot dans un contexte ou le nombre de parameters est variable
  1392.      * on cherche donc l'emplacement de la derniere virgule:
  1393.      * il y en a une car t>0, on retrouve ensuite le debut de mot
  1394.      */
  1395.   for(pe=pp;*pe!=0;pe++);/* trouve la fin de la chaine */
  1396.   if(t>0){
  1397.     for(cptr = pe; cptr > pp && *cptr != ','; cptr--);
  1398.     pp=find_word_start(cptr+1);
  1399.   }
  1400.   pe=find_word_end(pp);*pe=0; /* strip junk final */
  1401.   //      printf("pp contient %s\n",pp);
  1402.  
  1403.  
  1404.   inf=open4read(pp);
  1405.   if(inf==NULL) {
  1406.     fly_error("file_not_exist"); return;
  1407.   }
  1408.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1409.   if(insimg==NULL) {
  1410.     fly_error("bad_gif"); return;
  1411.   }
  1412.  
  1413.   /* On recupere les numeros des matrices/vecteurs a faire agir,
  1414.     * s'il n'y en a pas, on les fait toutes agir
  1415.   */
  1416.   for(i=0;i<t && i< JC_NB_MATRICES;i++) {
  1417.     if(pm->pd[i]>=1 && pm->pd[i]<=JC_NB_MATRICES){
  1418.       nummatrices[comptmat] = pm->pd[i];
  1419.       comptmat++;
  1420.     }
  1421.   }
  1422.   if(t<=0){
  1423.     for(i=0;i<JC_NB_MATRICES;i++) {
  1424.       nummatrices[i] = i+1;
  1425.     }
  1426.     comptmat=JC_NB_MATRICES;
  1427.   }
  1428.  
  1429.  
  1430.   imax = gdImageSX(insimg);
  1431.   jmax = gdImageSY(insimg);
  1432.  
  1433.   for(i=0;i<imax;i++){
  1434.     for(j=0;j<jmax;j++){
  1435.       int nc;
  1436.       c=gdImageGetPixel(insimg,i,j);
  1437.       /* Le code suivant est une copie du code dans gdImageCopy  Added 7/24/95: support transparent copies */
  1438.       if (gdImageGetTransparent(insimg) != c) {
  1439.         /* Have we established a mapping for this color? */
  1440.         if (colorMap[c] == (-1)) {
  1441.           /* If it's the same image, mapping is trivial, dans notre cas ca n'arrive jamais... */
  1442.           if (image == insimg) {
  1443.             nc = c;
  1444.           } else {
  1445.             /* First look for an exact match */
  1446.             nc = gdImageColorExact(image,
  1447.                    insimg->red[c], insimg->green[c],
  1448.                            insimg->blue[c]);
  1449.           }
  1450.           if (nc == (-1)) {
  1451.           /* No, so try to allocate it */
  1452.             nc = gdImageColorAllocate(image,
  1453.                              insimg->red[c], insimg->green[c],
  1454.                              insimg->blue[c]);
  1455.              /* If we're out of colors, go for the closest color */
  1456.             if (nc == (-1)) {
  1457.               nc = gdImageColorClosest(image,
  1458.                               insimg->red[c], insimg->green[c],
  1459.                               insimg->blue[c]);
  1460.             }
  1461.           }
  1462.           colorMap[c] = nc;
  1463.         }
  1464.  
  1465.         pt[0]= i*(parallogram_fonda[2])/(imax-1)+(jmax-j)*(parallogram_fonda[4])/(jmax-1)+parallogram_fonda[0];
  1466.         pt[1]= i*(parallogram_fonda[3])/(imax-1)+(jmax-j)*(parallogram_fonda[5])/(jmax-1)+parallogram_fonda[1];
  1467.         for(mat=0;mat<comptmat;mat++){
  1468.           double *matricecourante = matrices_pavage[nummatrices[mat]];
  1469.           double *vecteurcourant = vecteurs_pavage[nummatrices[mat]];
  1470.           ptr[0] = matricecourante[0]*pt[0]+matricecourante[1]*pt[1]+vecteurcourant[0];
  1471.           ptr[1] = matricecourante[2]*pt[0]+matricecourante[3]*pt[1]+vecteurcourant[1];
  1472.           scale(ptr,pti,1);
  1473.           gdImageSetPixel(image,pti[0],pti[1],colorMap[c]);
  1474.         }
  1475.       }
  1476.     }
  1477.   }
  1478.   gdImageDestroy(insimg);
  1479. }
  1480.  
  1481. /**** Fin modifs JC Fev 06 ******/
  1482.  
  1483. /* get color from value */
  1484. int calc_color(char *p, struct objtab *o)
  1485. {
  1486.   int k, cc[3];
  1487.   char buf[MAX_LINELEN+1];
  1488.   for(k=0;k<3;k++) {
  1489.     fnd_item(p,k+1,buf);
  1490.     cc[k]=strevalue(buf);
  1491.   }
  1492.   collapse_item(p,3);
  1493.   if(cc[0]==-1 && cc[1]==-1 && cc[2]==-255) {
  1494.  
  1495.     if(brushed && o->subst&1) return gdBrushed;
  1496.     else return color_black;
  1497.   }
  1498.   if(cc[0]==-1 && cc[1]==-255 && cc[2]==-1) {
  1499.     if(styled && o->subst&2)  return gdStyled;
  1500.     else return color_black;
  1501.   }
  1502.   if(cc[0]==-255 && cc[1]==-1 && cc[2]==-1) {
  1503.     if(tiled && o->fill_tag==1)  return gdTiled;
  1504.     else return color_black;
  1505.   }
  1506.   return getcolor(cc[0],cc[1],cc[2]);
  1507. }
  1508.  
  1509. /* Routine to parse parameters */
  1510. int parse_parms(char *p,objparm *pm,struct objtab *o)
  1511. {
  1512.   char buf[MAX_LINELEN+1];
  1513.   char *p1, *p2;
  1514.   int j,t,c,c1,c2;
  1515.  
  1516.   c=o->color_pos;c1=c2=0;
  1517.   pm->color[0]=pm->color[1]=0;
  1518.   if(c>0) c1=c;
  1519.   if(c<0) c2=-c;
  1520.   c=c1+c2;
  1521.   t=itemnum(p);if(t<o->required_parms+3*c) return -1;
  1522.   if(c1>0 && t>o->required_parms+3*c) t=o->required_parms+3*c;
  1523.   pm->pcnt=t-3*c;
  1524.   if(pm->pcnt>MAX_PARMS) pm->pcnt=MAX_PARMS;
  1525.   if(c2>0) {
  1526.     for(j=0;j<2 && j<c2; j++) pm->color[j]=calc_color(p,o);
  1527.   }
  1528.   snprintf(buf,sizeof(buf),"%s",p);
  1529.   for(j=0, p1=buf; j<pm->pcnt; j++, p1=p2) {
  1530.     p2=find_item_end(p1); if(*p2) *p2++=0;
  1531.     p1=find_word_start(p1);
  1532.     if(*p1) pm->pd[j]=strevalue(p1); else pm->pd[j]=0;
  1533.     if(!isfinite(pm->pd[j])) {
  1534.         if(j<o->required_parms) return -1;
  1535.         else {pm->pd[j]=0;break;}
  1536.     }
  1537.   }
  1538.   collapse_item(p,o->required_parms);
  1539.   if(c1>0) {
  1540.     for(j=0;j<c1 && j<2; j++) pm->color[j]=calc_color(p,o);
  1541.   }
  1542.   if(width>1 && o->subst&1 && pm->color[0]!=gdBrushed
  1543.       && pm->color[0]!=gdStyled && pm->color[0]!=gdTiled) {
  1544.     pm->color[1]=pm->color[0];
  1545.     pm->color[0]=widthcolor(width,pm->color[0]);
  1546.   }
  1547.   pm->fill=o->fill_tag;
  1548.   ovlstrcpy(pm->str,p); return 0;
  1549. }
  1550.  
  1551. /* Create the struct objparm pm corresponding to the objparm p
  1552.  * Execute a command. Returns 0 if OK.
  1553.  */
  1554. int obj_main(char *p)
  1555. {
  1556.   int i;
  1557.   char *pp,*name,*pt;
  1558.   char tbuf2[MAX_LINELEN+1];
  1559.   struct objparm pm;
  1560.  
  1561.   p=find_word_start(p);
  1562.   if(*p==exec_prefix_char || *p==0) return 0; /* comment */
  1563.   name=p;
  1564.   pp=find_name_end(p);
  1565.   pt=find_word_start(pp); if(*pt=='=') *pt=' ';
  1566.   if(*pt==':' && *(pt+1)=='=') *pt=*(pt+1)=' ';
  1567.   pp=find_word_end(p);
  1568.   if(*pp!=0) {
  1569.     *(pp++)=0; pp=find_word_start(pp);
  1570.   }
  1571.   if(strlen(p)==1 || (strlen(p)==2 && isdigit(*(p+1)))) {
  1572.     setvar(p,strevalue(pp)); return 0;
  1573.   }
  1574.   /* search the number of the object */
  1575.   i=search_list(objtab,obj_no,sizeof(objtab[0]),name);
  1576.   if(i<0) {
  1577.     fly_error("bad_cmd"); return 1;
  1578.   }
  1579.   if(image==NULL && (objtab[i].color_pos || objtab[i].required_parms>2)) {
  1580.     fly_error("image_not_defined"); return 1;
  1581.   }
  1582.   ovlstrcpy(tbuf2,pp);
  1583.   if(objtab[i].color_pos || objtab[i].routine==obj_setstyle) {
  1584.     substit(tbuf2);
  1585.   }
  1586.   if(parse_parms(tbuf2,&pm,objtab+i)!=0) fly_error("bad_parms");
  1587.   else objtab[i].routine(&pm);
  1588.   return 0;
  1589. }
  1590.