Subversion Repositories wimsdev

Rev

Rev 17676 | Rev 17914 | 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) 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-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.  
  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");
  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,"\\begin{tikzpicture}\[scale=0.01]\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.     fprintf(tikz_file,"\\draw\[%s] (%i,%i) --",tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]));
  399.     for (i= 1; i<cnt; i++)
  400.         fprintf(tikz_file,"(%i,%i)--", pm->p[2*i],flip(pm->p[2*i+1]));
  401.     fprintf(tikz_file," cycle;\n");
  402.   }
  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. void obj_angle(objparm *pm)
  553. {
  554.   const double DEG=M_PI/180;
  555.   double dpts[6];
  556.   int pts[6], wx=rint(2*pm->pd[2]*xscale), wy=rint(2*pm->pd[2]*yscale);
  557.   dpts[0]=pm->pd[0];
  558.   dpts[1]=pm->pd[1];
  559.   dpts[2]=pm->pd[0]+pm->pd[2]*cos(DEG*pm->pd[3]);
  560.   dpts[3]=pm->pd[1]+pm->pd[2]*sin(DEG*pm->pd[3]);
  561.   dpts[4]=pm->pd[0]+pm->pd[2]*cos(DEG*pm->pd[4]);
  562.   dpts[5]=pm->pd[1]+pm->pd[2]*sin(DEG*pm->pd[4]);
  563.   scale(dpts, pts, 3);
  564.   gdImageLine(image,pts[0],pts[1],pts[2],pts[3],pm->color[0]);
  565.   gdImageLine(image,pts[0],pts[1],pts[4],pts[5],pm->color[0]);
  566.   gdImageArc(image,pts[0],pts[1],wx,wy,pm->pd[3],pm->pd[4],pm->color[0]);
  567.   if(tikz_file) {
  568.     fprintf(tikz_file, "\\draw\[%s, domain=%f:%f] plot ({%i+%f*cos(\\x)}, {%i+%f*sin(\\x)});\n",
  569.       tikz_options(pm->color[0],pm->fill),pm->pd[3],pm->pd[4],pts[0],wx*0.5,flip(pts[1]),wy*(-0.5));
  570.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  571.         tikz_options(pm->color[0],pm->fill),pts[0],flip(pts[1]),pts[2],flip(pts[3]));
  572.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  573.         tikz_options(pm->color[0],pm->fill),pts[0],flip(pts[1]),pts[4],flip(pts[5]));
  574.   }
  575. }
  576. /* Ellipse: centre 0,1, width 2, hight 3, color 4,5,6 */
  577. void obj_ellipse(objparm *pm)
  578. {
  579.   scale(pm->pd,pm->p,1);
  580.   pm->p[2]=pm->pd[2]*xscale; pm->p[3]=pm->pd[3]*yscale;
  581.   if(pm->fill) {
  582.     gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
  583.              color_bounder);
  584.     patchgdImageFillToBorder(image,pm->p[0],pm->p[1],
  585.                     color_bounder,pm->color[0]);
  586.   }
  587.   gdImageArc(image,pm->p[0],pm->p[1],rint(pm->p[2]),rint(pm->p[3]),0,360,pm->color[0]);
  588.   if(tikz_file) fprintf(tikz_file,"\\draw\[%s] (%i,%i) ellipse (%i and %i);\n\n",
  589.     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[2]));
  590.   if(vimg_enable) vimg_ellipse(scale_buf[0],scale_buf[1],0.5*pm->pd[2],0.5*pm->pd[3]);
  591. }
  592.  
  593. /* Circle radius in pixels*/
  594. void obj_circle(objparm *pm)
  595. {
  596.   scale(pm->pd,pm->p,1);
  597.   pm->p[2]=rint(pm->pd[2]); pm->p[3]=rint(pm->pd[2]);
  598.   if(pm->fill) {
  599.     gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
  600.              color_bounder);
  601.     patchgdImageFillToBorder(image,pm->p[0],pm->p[1],
  602.                     color_bounder,pm->color[0]);
  603.   }
  604.   gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,pm->color[0]);
  605.   if(tikz_file) fprintf(tikz_file, "\\draw\[%s] (%i, %i) circle (%i);\n",
  606.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),(int) rint(pm->pd[2]/2.));
  607. }
  608. /* Circle, radius in xrange */
  609. void obj_circles(objparm *pm)
  610. {
  611.   int i, n;
  612.   n=(pm->pcnt)/3;
  613.   for (i=0; i<n; ++i) scale(pm->pd+3*i, pm->p+3*i, 1);
  614.   for(i=0;i<3*n;i+=3){
  615.     gdImageArc(image,pm->p[i],pm->p[i+1],
  616.       rint(2*pm->pd[i+2]*xscale),rint(2*pm->pd[i+2]*xscale),0,360,pm->color[0]);
  617.     if (tikz_file){
  618.       fprintf(tikz_file, "\\draw\[%s] (%i, %i) circle (%i and %i);\n",
  619.         tikz_options(pm->color[0],pm->fill),pm->p[i],flip(pm->p[i+1]),(int) rint(pm->pd[i+2]*xscale),(int) rint(pm->pd[i+2]*yscale));
  620.     }
  621.   }
  622. }
  623. /* ellipse, width in xrange,height in yrange */
  624. void obj_ellipses(objparm *pm)
  625. {
  626.   int i, n;
  627.   n=(pm->pcnt)/4;
  628.   for (i=0; i<n; ++i) scale(pm->pd+4*i, pm->p+4*i, 1);
  629.   for(i=0;i<4*n;i+=4){
  630.     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]);
  631.     if (tikz_file){
  632.       fprintf(tikz_file, "\\draw\[%s] (%i, %i) circle (%i and %i);\n",
  633.         tikz_options(pm->color[0],pm->fill),pm->p[i],flip(pm->p[i+1]),(int) rint(pm->pd[i+2]*xscale/2),(int) rint(pm->pd[i+3]*yscale/2));
  634.     }
  635.   }
  636. }
  637. typedef enum tikz_fill_options {grille, points, hatch, dhatch} tfo;
  638. int compar(const void *a, const void *b) {return *(int *)b - *(int *)a;}
  639.  
  640. static void tikz_fill(int x, int y, int nx, int ny, int wall, int the_color, tfo opt)
  641. {
  642.   char *to = tikz_options(the_color,1);
  643.   int numpix=sizex*sizey, top = 0, a, c, cy, lx=0, rx=0, seed, nt,ct=0;
  644.   int *stack = xmalloc(numpix*sizeof(int));
  645.   int *stack2 = xmalloc(numpix*sizeof(int));
  646.   if (nx==0) nx=1; if (ny==0) ny=1; nt = abs(nx*ny);
  647.   seed = gdImageGetPixel(image,x,y);
  648.   char *check = xmalloc(numpix);
  649.   while (numpix--) check[numpix]=0;
  650.   a=x+y*sizex;
  651.   check[a]=1;
  652.   stack[top++]=a;
  653.   while(top)
  654.     {
  655.       a = stack[--top];
  656.       x = a%sizex; y = a/sizex;
  657.       c = gdImageGetPixel(image,x,y);
  658.       if ((wall && (c == wall))||(!wall && c!=seed))
  659.         continue;
  660.       if (x>0 && !check[a-1]) {check[a-1]=1; stack[top++]=a-1;}
  661.       if (x+1<sizex && !check[a+1]) {check[a+1]=1; stack[top++]=a+1;}
  662.       if (y>0 && !check[a-sizex]) {check[a-sizex]=1; stack[top++]=a-sizex;}
  663.       if (y+1<sizey && !check[a+sizex]) {check[a+sizex]=1; stack[top++]=a+sizex;}
  664.       switch(opt)
  665.   {
  666.   case grille: if(((x%nx)!=(nx/2))&&((y%ny)!=(ny/2))) continue; break;
  667.   case points: if(x%nx!=nx/2||y%ny!=ny/2) continue; break;
  668.   case hatch: if(abs((ny*x-nx*y)%nt)!=nt/2) continue; break;
  669.   case dhatch: if((ny*x+nx*y)%nt!=nt/2 && abs((ny*x-nx*y)%nt)!=nt/2) continue; break;
  670.   default: break;
  671.   }
  672.       stack2[ct++]=a;
  673.     }
  674.   free(stack);
  675.   free(check);
  676.   if (ct==0) return;
  677.   qsort(stack2, ct, sizeof(int), compar);
  678.   cy=-1;
  679.   while (ct--)
  680.     {
  681.       a = stack2[ct];
  682.       x = a%sizex; y = a/sizex;
  683.       if (y != cy || x != rx+1)
  684.       {
  685.   if (cy >= 0)
  686.     fprintf(tikz_file,
  687.       "\\draw\[%s] (%.2f,%.2f) rectangle (%.2f,%.2f);\n",
  688.       to, lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
  689.   cy = y;
  690.   lx = rx = x;
  691.       }
  692.       else
  693.   rx++;
  694.     }
  695.   fprintf(tikz_file,
  696.     "\\draw\[%s] (%.2f,%.2f) rectangle (%.2f,%.2f);\n",
  697.     to, lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
  698.   free(stack2);
  699. }
  700.  
  701. /* flood fill */
  702. void obj_fill(objparm *pm)
  703. {
  704.   scale(pm->pd,pm->p,1);
  705.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,0,pm->color[0],grille);
  706.   patchgdImageFill(image,pm->p[0],pm->p[1],pm->color[0]);
  707. }
  708.  
  709. /* flood fill to border*/
  710. void obj_fillb(objparm *pm)
  711. {
  712.   scale(pm->pd,pm->p,1);
  713.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,pm->color[0],pm->color[1],grille);
  714.   patchgdImageFillToBorder(image,pm->p[0],pm->p[1],pm->color[0],pm->color[1]);
  715. }
  716.  
  717. gdImagePtr himg;
  718.  
  719. int makehatchimage(int x, int y, int px, int py, int col)
  720. {
  721.   int c1,c2,r,g,b;
  722.   gdImagePtr saveimg;
  723.   himg=gdImageCreate(x,y);
  724.   c1=gdImageGetPixel(image,px,py);
  725.   r=gdImageRed(image,c1); g=gdImageGreen(image,c1); b=gdImageBlue(image,c1);
  726.   if(r>=255) r--; else r++; if(g>=255) g--; else g++; if(b>=255) b--; else b++;
  727.   c1=gdImageColorAllocate(himg,r,g,b);
  728.   r=gdImageRed(image,col); g=gdImageGreen(image,col); b=gdImageBlue(image,col);
  729.   c2=gdImageColorAllocate(himg,r,g,b);
  730.   if(width>1) {
  731.     savew=-1; saveimg=image;
  732.     image=himg; c2=widthcolor(width,c2); image=saveimg;
  733.     c2=gdBrushed; savew=-1;
  734.   }
  735.   return c2;
  736. }
  737.  
  738. /* flood fill with hatching */
  739. void obj_hatchfill(objparm *pm)
  740. {
  741.   int nx,ny,ax,ay, dir, c;
  742.   scale(pm->pd,pm->p,1);
  743.   nx=pm->pd[2]; ny=pm->pd[3]; ax=abs(nx); ay=abs(ny);
  744.   if(nx==0 && ny==0) {fly_error("bad displacement vector"); return;}
  745.   if((nx>0 && ny>0) || (nx<0 && ny<0)) dir=1; else dir=-1;
  746.   if(ax==0) {ax=100; dir=2;}
  747.   if(ay==0) {ay=100; dir=3;}
  748.   c=makehatchimage(ax,ay,pm->p[0],pm->p[1],pm->color[0]);
  749.   switch(dir) {
  750.     case -1: {
  751.       gdImageLine(himg,0,ay-1,ax-1,0,c);
  752.       if(width>1) {
  753.         gdImageLine(himg,-ax,ay-1,-1,0,c);
  754.         gdImageLine(himg,ax,ay-1,2*ax-1,0,c);
  755.         gdImageLine(himg,0,-1,ax-1,-ay,c);
  756.         gdImageLine(himg,0,2*ay-1,ax-1,ay,c);
  757.       }
  758.       break;
  759.     }
  760.     case 1: {
  761.       gdImageLine(himg,0,0,ax-1,ay-1,c);
  762.       if(width>1) {
  763.         gdImageLine(himg,-ax,0,-1,ay-1,c);
  764.         gdImageLine(himg,ax,0,2*ax-1,ay-1,c);
  765.         gdImageLine(himg,0,-ay,ax-1,-1,c);
  766.         gdImageLine(himg,0,ay,ax-1,2*ay-1,c);
  767.       }
  768.       break;
  769.     }
  770.     case 2: gdImageLine(himg,0,ay/2,ax-1,ay/2,c); break;
  771.     case 3: gdImageLine(himg,ax/2,0,ax/2,ay-1,c); break;
  772.   }
  773.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],hatch);
  774.   gdImageSetTile(image,himg);
  775.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  776.   gdImageDestroy(himg);
  777.   if(tiled) gdImageSetTile(image,tileimg);
  778. }
  779.  
  780. /* flood fill with grid */
  781. void obj_gridfill(objparm *pm)
  782. {
  783.   int nx,ny, c;
  784.   scale(pm->pd,pm->p,1);
  785.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  786.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  787.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  788.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],grille);
  789.   gdImageLine(himg,0,ny/2,nx-1,ny/2,c); gdImageLine(himg,nx/2,0,nx/2,ny-1,c);
  790.   gdImageSetTile(image,himg);
  791.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  792.   gdImageDestroy(himg);
  793.   if(tiled) gdImageSetTile(image,tileimg);
  794. }
  795.  
  796. /* flood fill with double hatching */
  797. void obj_diafill(objparm *pm)
  798. {
  799.   int nx,ny, c;
  800.   scale(pm->pd,pm->p,1);
  801.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  802.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  803.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  804.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],dhatch);
  805.   gdImageLine(himg,0,0,nx-1,ny-1,c); gdImageLine(himg,0,ny-1,nx-1,0,c);
  806.   gdImageSetTile(image,himg);
  807.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  808.   gdImageDestroy(himg);
  809.   if(tiled) gdImageSetTile(image,tileimg);
  810. }
  811.  
  812. /* flood fill with dots */
  813. void obj_dotfill(objparm *pm)
  814. {
  815.   int nx,ny, c;
  816.   scale(pm->pd,pm->p,1);
  817.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  818.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  819.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  820.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],points);
  821.   gdImageSetPixel(himg,nx/2,ny/2,c);
  822.   gdImageSetTile(image,himg);
  823.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  824.   gdImageDestroy(himg);
  825.   if(tiled) gdImageSetTile(image,tileimg);
  826. }
  827.  
  828. struct {
  829.   char *name;
  830.   gdFontPtr *fpt;
  831. } fonttab[]={
  832.   {"tiny",      &gdFontTiny},
  833.   {"small",      &gdFontSmall},
  834.   {"medium",&gdFontMediumBold},
  835.   {"large",      &gdFontLarge},
  836.   {"giant",      &gdFontGiant},
  837.   {"huge",      &gdFontGiant}
  838. };
  839.  
  840. #define fonttab_no (sizeof(fonttab)/sizeof(fonttab[0]))
  841.  
  842. /* string */
  843. void obj_string(objparm *pm)
  844. {
  845.   char *pp, *pe, *p2;
  846.   int i;
  847.   pp=pm->str; pe=strchr(pp,','); if(pe==NULL) {
  848.     fly_error("too_few_parms"); return;
  849.   }
  850.   *pe++=0; pp=find_word_start(pp); *find_word_end(pp)=0;
  851.   pe=find_word_start(pe); strip_trailing_spaces(pe);
  852.   if(*pp) {
  853.     for(i=0;i<fonttab_no && strcmp(pp,fonttab[i].name)!=0; i++);
  854.     if(i>=fonttab_no) i=1;
  855.   }
  856.   else i=1;
  857.   scale(pm->pd,pm->p,1);
  858.   if(*pe=='"') {
  859.     p2=strchr(pe+1,'"');
  860.     if(p2 && *(p2+1)==0) {*p2=0; pe++;}
  861.   }
  862.   if(pm->fill){
  863.     gdImageStringUp(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
  864.         pm->color[0]);
  865.     if(tikz_file) fprintf(tikz_file,"\\draw (%i,%i) node[font=\\%s,rotate=90,color=%s,anchor=north west,inner sep=0pt] {%s};\n",
  866.       pm->p[0],flip(pm->p[1]),fonttab[i].name,tikz_color(pm->color[0]),(unsigned char*) pe);
  867.   }
  868.   else {
  869.     gdImageString(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
  870.         pm->color[0]);
  871.     if(tikz_file) fprintf(tikz_file,"\\draw (%i,%i) node[font=\\%s,color=%s,anchor=north west,inner sep=0pt] {%s};\n",
  872.       pm->p[0],flip(pm->p[1]),fonttab[i].name,tikz_color(pm->color[0]),(unsigned char*) pe);
  873.   }
  874. }
  875. /*FIXME; giant does not exist in tikz and ... samething as huge */
  876. /* point */
  877. void obj_point(objparm *pm)
  878. {
  879.   scale(pm->pd,pm->p,1);
  880.   gdImageSetPixel(image,pm->p[0],pm->p[1],pm->color[0]);
  881.   if(tikz_file) fprintf(tikz_file,"\\draw[%s] (%i,%i) circle (0pt);\n",
  882.     tikz_options(pm->color[0],0),pm->p[0],flip(pm->p[1]));
  883. }
  884.  
  885. /* copy an image file */
  886. void obj_copy(objparm *pm)
  887. {
  888.   char *pp;
  889.   FILE *inf;
  890.   gdImagePtr insimg;
  891.  
  892.   pp=find_word_start(pm->str);*find_word_end(pp)=0;
  893.   inf=open4read(pp);
  894.   if(inf==NULL) {
  895.     fly_error("file_does_not_exist"); return;
  896.   }
  897.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  898.   if(insimg==NULL) {
  899.     fly_error("bad_gif"); return;
  900.   }
  901.   scale(pm->pd,pm->p,1);
  902.   if(pm->pd[2]<0 && pm->pd[3]<0 && pm->pd[4]<0 && pm->pd[5]<0)
  903.     gdImageCopy(image,insimg,pm->p[0],pm->p[1],0,0,
  904.             insimg->sx,insimg->sy);
  905.   else
  906.     gdImageCopy(image,insimg,pm->p[0],pm->p[1],pm->pd[2],pm->pd[3],
  907.             pm->pd[4]-pm->pd[2],pm->pd[5]-pm->pd[3]);
  908.   if(tikz_file) fprintf(tikz_file,
  909.     "\\node[anchor=north west,inner sep=0pt] at (%i,%i) {\\includegraphics\[width=.05\\linewidth]{%s}};\n",
  910.       pm->p[0],flip(pm->p[1]),pm->str);
  911.     gdImageDestroy(insimg);
  912. /*FIXME position non correcte; on ne peut pas utiliser une image gif */
  913. }
  914.  
  915. /* copy an image file, with resizing */
  916. void obj_copyresize(objparm *pm)
  917. {
  918.   char *pp;
  919.   FILE *inf;
  920.   gdImagePtr insimg;
  921.  
  922.   pp=find_word_start(pm->str);*find_word_end(pp)=0;
  923.   inf=open4read(pp);
  924.   if(inf==NULL) {
  925.     fly_error("file_not_found"); return;
  926.   }
  927.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  928.   if(insimg==NULL) {
  929.     fly_error("bad_gif"); return;
  930.   }
  931.   scale(pm->pd+4,pm->p+4,2);
  932.   if(pm->pd[0]<0 && pm->pd[1]<0 && pm->pd[2]<0 && pm->pd[3]<0)
  933.     gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],0,0,
  934.                  pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
  935.                  insimg->sx,insimg->sy);
  936.   else
  937.     gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],pm->pd[0],pm->pd[1],
  938.                  pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
  939.                  pm->pd[2]-pm->pd[0]+1,pm->pd[3]-pm->pd[1]+1);
  940.   gdImageDestroy(insimg);
  941. }
  942.  
  943. /* set brush or tile */
  944. void obj_setbrush(objparm *pm)
  945. {
  946.   char *pp;
  947.   FILE *inf;
  948.   gdImagePtr insimg;
  949.  
  950.   pp=find_word_start(pm->str); *find_word_end(pp)=0;
  951.   inf=open4read(pp); if(inf==NULL) {
  952.     fly_error("file_not_found"); return;
  953.   }
  954.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  955.   if(insimg==NULL) {
  956.     fly_error("bad_gif"); return;
  957.   }
  958.   if(pm->fill) {
  959.     gdImageSetTile(image,insimg); tiled=1; tileimg=insimg;
  960.   }
  961.   else {
  962.     gdImageSetBrush(image,insimg);brushed=1; brushimg=insimg;
  963.   }
  964. }
  965.  
  966. /* kill brush */
  967. void obj_killbrush(objparm *pm)
  968. {
  969.   if(brushimg) gdImageDestroy(brushimg);
  970.   brushed=0; brushimg=NULL;
  971. }
  972.  
  973. /* kill tile */
  974. void obj_killtile(objparm *pm)
  975. {
  976.   if(tileimg) gdImageDestroy(tileimg);
  977.   tiled=0; tileimg=NULL;
  978. }
  979.  
  980. /* set style */
  981. void obj_setstyle(objparm *pm)
  982. {
  983.   int i,t;
  984.   t=pm->pcnt/3; if(t<=0) {
  985.     fly_error("too_few_parms"); return;
  986.   }
  987.   for(i=0;i<t;i++) {
  988.     if(pm->pd[3*i]<0 || pm->pd[3*i+1]<0 || pm->pd[3*i+2]<0)
  989.       pm->p[i]=gdTransparent;
  990.     else
  991.       pm->p[i]=getcolor(pm->pd[3*i],pm->pd[3*i+1],pm->pd[3*i+2]);
  992.   }
  993.   gdImageSetStyle(image,pm->p,t); styled=1;
  994. }
  995.  
  996. /* kill style */
  997. void obj_killstyle(objparm *pm)
  998. {
  999.   styled=0;
  1000. }
  1001.  
  1002. /* set transparent */
  1003. void obj_transp(objparm *pm)
  1004. {
  1005.   gdImageColorTransparent(image,pm->color[0]);
  1006. }
  1007.  
  1008. /* set interlace */
  1009. void obj_interlace(objparm *pm)
  1010. {
  1011.   gdImageInterlace(image,1);
  1012. }
  1013.  
  1014. /* set linewidth */
  1015. void obj_linewidth(objparm *pm)
  1016. {
  1017.   if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
  1018.   width=pm->pd[0];
  1019.   if(tikz_file) fprintf(tikz_file,"\\pgfsetlinewidth{%fpt}\n",width*0.7);
  1020. }
  1021.  
  1022. /* set crosshairsize */
  1023. void obj_crosshairsize(objparm *pm)
  1024. {
  1025.   if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
  1026.   else width2=pm->pd[0];
  1027. }
  1028.  
  1029. /* set x range */
  1030. void obj_xrange(objparm *pm)
  1031. {
  1032.   double dd;
  1033.   dd=pm->pd[1]-pm->pd[0];
  1034.   xstart=pm->pd[0]; xscale=sizex/dd;
  1035. }
  1036.  
  1037. /* set y range */
  1038. void obj_yrange(objparm *pm)
  1039. {
  1040.   double dd;
  1041.   dd=pm->pd[1]-pm->pd[0];
  1042.   ystart=pm->pd[1]; yscale=-sizey/dd;
  1043. }
  1044.  
  1045. /* set x et y range */
  1046. void obj_range(objparm *pm)
  1047. {
  1048.   double d1,d2;
  1049.   d1=pm->pd[1]-pm->pd[0]; d2=pm->pd[3]-pm->pd[2];
  1050.   xstart=pm->pd[0]; xscale=(sizex-1)/d1;
  1051.   ystart=pm->pd[3]; yscale=-(sizey-1)/d2;
  1052. }
  1053.  
  1054. /* set t range */
  1055. void obj_trange(objparm *pm)
  1056. {
  1057.   /*double dd;
  1058.   dd=pm->pd[1]-pm->pd[0];*/
  1059.   tstart=pm->pd[0]; tend=pm->pd[1];
  1060.   tranged=1;
  1061. }
  1062.  
  1063. /* set tstep (plotting step) */
  1064. void obj_tstep(objparm *pm)
  1065. {
  1066.   int dd;
  1067.   dd=pm->pd[0];
  1068.   if(dd<3) {
  1069.     fly_error("bad_step"); return;
  1070.   }
  1071.   if(dd>2000) dd=2000;
  1072.   tstep=dd;
  1073. }
  1074.  
  1075. /* set plotjump (plot jump break threashold) */
  1076. void obj_plotjump(objparm *pm)
  1077. {
  1078.   int dd;
  1079.   dd=pm->pd[0];
  1080.   if(dd<3) dd=3;
  1081.   if(dd>MAX_SIZE) dd=MAX_SIZE;
  1082.   plotjump=dd;
  1083. }
  1084.  
  1085. /* plot a curve, either parametric or explicit */
  1086. void _obj_plot(objparm *pm,int dash)
  1087. {
  1088.   int i,j,n,dist,xx,yy,varpos;
  1089.   char p1[MAX_LINELEN+1], p2[MAX_LINELEN+1];
  1090.   char *varn, *pp;
  1091.   double dc[2],t,v;
  1092.   int ic[2],oc[2];
  1093.  
  1094.   n=itemnum(pm->str);
  1095.   if(n<1) {
  1096.     fly_error("bad_parms"); return;
  1097.   }
  1098.   if (tikz_file) fprintf(tikz_file,"\%\%begin plot\n");
  1099.   fnd_item(pm->str,1,p1); fnd_item(pm->str,2,p2);
  1100.   if(n==1 && !tranged) v=sizex/xscale/tstep;
  1101.   else v=(tend-tstart)/tstep;
  1102.   if(n==1) varn="x"; else varn="t";
  1103.   for(pp=varchr(p1,varn); pp; pp=varchr(pp,varn)) {
  1104.     string_modify(p1,pp,pp+strlen(varn),EV_T);
  1105.     pp+=strlen(EV_T);
  1106.   }
  1107.   for(pp=varchr(p2,varn); pp; pp=varchr(pp,varn)) {
  1108.     string_modify(p2,pp,pp+strlen(varn),EV_T);
  1109.       pp+=strlen(EV_T);
  1110.   }
  1111.   varpos=eval_getpos(EV_T);
  1112.   if(varpos<0) return; /* unknown error */
  1113.   evalue_compile(p1); evalue_compile(p2);
  1114.   if(vimg_enable) vimg_plotstart();
  1115.   for(i=j=0;i<=tstep;i++) {
  1116.     if(n==1) {
  1117.       if(tranged) t=tstart+i*v; else t=xstart+i*v;
  1118.       eval_setval(varpos,t); dc[0]=t; dc[1]=strevalue(p1);
  1119.     }
  1120.     else {
  1121.       t=tstart+i*v; eval_setval(varpos,t);
  1122.       dc[0]=strevalue(p1); dc[1]=strevalue(p2);
  1123.     }
  1124.     if(!isfinite(dc[0]) || !isfinite(dc[1])) ic[0]=ic[1]=-BOUND;
  1125.     else scale(dc,ic,1);
  1126.     if(vimg_enable) vimg_plot1 (scale_buf[0],scale_buf[1]);
  1127.     if(ic[1]<sizey && ic[1]>0){
  1128.       if(j==0) {
  1129.         gdImageSetPixel(image,ic[0],ic[1],pm->color[0]); j++;
  1130.         if (tikz_file)
  1131.           fprintf(tikz_file,"\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
  1132.             tikz_options(pm->color[0],1),ic[0],flip(ic[1]),ic[0]+1,flip(ic[1]+1));
  1133.       }
  1134.       else {
  1135.         xx=ic[0]-oc[0]; yy=ic[1]-oc[1];
  1136.         dist=sqrt(xx*xx+yy*yy);
  1137.         if(dist>0){
  1138.           if(dist>plotjump || dist==1) {
  1139.             gdImageSetPixel(image,ic[0],ic[1],pm->color[0]);
  1140.             if (tikz_file)
  1141.               fprintf(tikz_file,"\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
  1142.                 tikz_options(pm->color[0],1),ic[0],flip(ic[1]),ic[0]+1,flip(ic[1]+1));
  1143.           }
  1144.           else {
  1145.             if ( dash==1)
  1146.               gdImageDashedLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
  1147.             else
  1148.               gdImageLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
  1149.             if (tikz_file)
  1150.               fprintf(tikz_file, "\\draw\[%s] (%i,%i) -- (%i,%i);\n",
  1151.                 tikz_options(pm->color[0],dash),oc[0],flip(oc[1]),ic[0],flip(ic[1]));
  1152.           }
  1153.         }
  1154.       }
  1155.     }
  1156.     memmove(oc,ic,sizeof(oc));
  1157.   }
  1158.   if (tikz_file) fprintf(tikz_file,"\%\%end plot\n");
  1159.   if(vimg_enable) vimg_plotend();
  1160. }
  1161.  
  1162. void obj_plot(objparm *pm) { _obj_plot( pm,0) ;}
  1163. void obj_dplot(objparm *pm) { _obj_plot( pm,1) ; }
  1164.  
  1165. /* set levelcurve granularity */
  1166. void obj_levelstep(objparm *pm)
  1167. {
  1168.   int dd;
  1169.   dd=pm->pd[0];
  1170.   if(dd<1) return;
  1171.   if(dd>16) dd=16;
  1172.   lstep=dd;
  1173. }
  1174.  
  1175. /* level curve */
  1176. void obj_levelcurve(objparm *pm)
  1177. {
  1178.   char fn[MAX_LINELEN+1],tc[MAX_LINELEN+1];
  1179.   int n,i;
  1180.   double d;
  1181.   leveldata *ld;
  1182.  
  1183.   ld=xmalloc(sizeof(leveldata)+16);
  1184.   ld->xname="x"; ld->yname="y";
  1185.   ld->xsize=sizex; ld->ysize=sizey; ld->datacnt=0;
  1186.   ld->xrange[0]=xstart; ld->xrange[1]=sizex/xscale+xstart;
  1187.   ld->yrange[0]=sizey/yscale+ystart; ld->yrange[1]=ystart;
  1188.   ld->grain=lstep;
  1189.   fnd_item(pm->str,1,fn); ld->fn=fn;
  1190.   n=itemnum(pm->str);
  1191.   if(n<=1) {ld->levelcnt=1; ld->levels[0]=0;}
  1192.   else {
  1193.     if(n>LEVEL_LIM+1) n=LEVEL_LIM+1;
  1194.     for(i=0;i<n-1;i++) {
  1195.       fnd_item(pm->str,i+2,tc);
  1196.       d=strevalue(tc);
  1197.       if(isfinite(d)) ld->levels[i]=d; else ld->levels[i]=0;
  1198.     }
  1199.     ld->levelcnt=n-1;
  1200.   }
  1201.   levelcurve(ld);
  1202.   if (tikz_file) fprintf(tikz_file,"\%\%begin levelcurve\n");
  1203.   for(i=0;i<ld->datacnt;i++) {
  1204.     gdImageSetPixel(image,ld->xdata[i],ld->ydata[i],pm->color[0]);
  1205.     if (tikz_file)
  1206.       fprintf(tikz_file,
  1207.         "\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
  1208.         tikz_options(pm->color[0],1),ld->xdata[i],flip(ld->ydata[i]),ld->xdata[i]+1,flip(ld->ydata[i]+1));
  1209.   }
  1210.   if (tikz_file) fprintf(tikz_file,"\%\%end levelcurve\n");
  1211.   free(ld);
  1212. }
  1213.  
  1214. /* set animation step */
  1215. void obj_animstep(objparm *pm)
  1216. {
  1217.   animstep=pm->pd[0];
  1218.   setvar("animstep",pm->pd[0]);
  1219. }
  1220.  
  1221. /* Starts a line counting */
  1222. void obj_linecount(objparm *pm)
  1223. {
  1224.   linecnt=pm->pd[0];
  1225. }
  1226.  
  1227. /* marks end of execution */
  1228. void obj_end(objparm *pm)
  1229. {
  1230.   fly_error("successful_end_of_execution");
  1231. }
  1232.  
  1233. /* output */
  1234. void obj_output(objparm *pm)
  1235. {
  1236.   char *p, namebuf[1024];
  1237.   p=find_word_start(pm->str); *find_word_end(p)=0;
  1238.   snprintf(namebuf,sizeof(namebuf),"%s",imagefilename);
  1239.   snprintf(imagefilename,sizeof(imagefilename),"%s",p);
  1240.   output();
  1241.   snprintf(imagefilename,sizeof(imagefilename),"%s",namebuf);
  1242. }
  1243.  
  1244. /* vimgfile */
  1245. void obj_vimgfile(objparm *pm)
  1246. {
  1247.   char *p;
  1248.   p=find_word_start(pm->str); *find_word_end(p)=0;
  1249.   snprintf(vimgfilename,sizeof(vimgfilename),"%s",p);
  1250.   if(vimg_ready) vimg_close();
  1251. }
  1252.  
  1253. /* vimg enable/disable */
  1254. void obj_vimg(objparm *pm)
  1255. {
  1256.   vimg_enable=pm->pd[0];
  1257.   if(vimg_enable>0 && vimg_ready==0) vimg_init();
  1258. }
  1259.  
  1260. /* Set affine transformation */
  1261. void obj_affine(objparm *pm)
  1262. {
  1263.   int i;
  1264.   for(i=0;i<4;i++) matrix[i]=pm->pd[i];
  1265.   transx=pm->pd[4]; transy=pm->pd[5];
  1266.   transform=1;
  1267. }
  1268.  
  1269. /* Set affine transformation */
  1270. void obj_rotation(objparm *pm)
  1271. {
  1272.   double r;
  1273.   r=M_PI*pm->pd[0]/180;
  1274.   matrix[0]=matrix[3]=cos(r);
  1275.   matrix[1]=-sin(r); matrix[2]=sin(r);
  1276.   transform=1;
  1277. }
  1278.  
  1279. /* Set linear transformation */
  1280. void obj_linear(objparm *pm)
  1281. {
  1282.   int i;
  1283.   for(i=0;i<4;i++) matrix[i]=pm->pd[i];
  1284.   transform=1;
  1285. }
  1286.  
  1287. /* Set translation transformation */
  1288. void obj_translation(objparm *pm)
  1289. {
  1290.   transx=pm->pd[0]; transy=pm->pd[1];
  1291. }
  1292.  
  1293. /* kill affine transformation */
  1294. void obj_killaffine(objparm *pm)
  1295. {
  1296.   matrix[0]=matrix[3]=1;
  1297.   matrix[1]=matrix[2]=transx=transy=0;
  1298.   transform=0;
  1299. }
  1300.  
  1301. /* kill affine transformation */
  1302. void obj_killlinear(objparm *pm)
  1303. {
  1304.   matrix[0]=matrix[3]=1;
  1305.   matrix[1]=matrix[2]=0;
  1306.   transform=0;
  1307. }
  1308.  
  1309. /* kill affine transformation */
  1310. void obj_killtranslation(objparm *pm)
  1311. {
  1312.   transx=transy=0;
  1313. }
  1314.  
  1315. /***** Les modifs de Jean-Christophe Leger Fev 2006 *****/
  1316.  
  1317. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES, une liste de 4 nombres reels pour la matrice */
  1318. void obj_setmatrix(objparm *pm)
  1319. {
  1320.   int nummatrix = (int) (pm->pd[0]);
  1321.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1322.     fly_error("bad_matrix_number");
  1323.     return;
  1324.   }
  1325.   matrices_pavage[nummatrix][0] = pm->pd[1];
  1326.   matrices_pavage[nummatrix][1] = pm->pd[2];
  1327.   matrices_pavage[nummatrix][2] = pm->pd[3];
  1328.   matrices_pavage[nummatrix][3] = pm->pd[4];
  1329. }
  1330.  
  1331. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1332. void obj_resetmatrix(objparm *pm)
  1333. {
  1334.   int nummatrix = (int) (pm->pd[0]);
  1335.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1336.     fly_error("bad_matrix_number");
  1337.     return;
  1338.   }
  1339.   matrices_pavage[nummatrix][0] = 1;
  1340.   matrices_pavage[nummatrix][1] = 0;
  1341.   matrices_pavage[nummatrix][2] = 0;
  1342.   matrices_pavage[nummatrix][3] = 1;
  1343.  
  1344. }
  1345. /* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 2 nombres reels pour le vecteur */
  1346. void obj_setvector(objparm *pm)
  1347. {
  1348.   int nummatrix = (int) (pm->pd[0]);
  1349.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1350.     fly_error("bad_vector_number");
  1351.     return;
  1352.   }
  1353.   vecteurs_pavage[nummatrix][0] = pm->pd[1];
  1354.   vecteurs_pavage[nummatrix][1] = pm->pd[2];
  1355. }
  1356.  
  1357. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1358. void obj_resetvector(objparm *pm)
  1359. {
  1360.   int nummatrix = (int) (pm->pd[0]);
  1361.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1362.     fly_error("bad_vector_number");
  1363.     return;
  1364.   }
  1365.   vecteurs_pavage[nummatrix][0] = 0;
  1366.   vecteurs_pavage[nummatrix][1] = 0;
  1367. }
  1368.  
  1369. /* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 6 nombres reels pour la matrice et le vecteur */
  1370. void obj_settransform(objparm *pm)
  1371. {
  1372.   int nummatrix = (int) (pm->pd[0]);
  1373.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1374.     fly_error("bad_vector_number");
  1375.     return;
  1376.   }
  1377.   matrices_pavage[nummatrix][0] = pm->pd[1];
  1378.   matrices_pavage[nummatrix][1] = pm->pd[2];
  1379.   matrices_pavage[nummatrix][2] = pm->pd[3];
  1380.   matrices_pavage[nummatrix][3] = pm->pd[4];
  1381.   vecteurs_pavage[nummatrix][0] = pm->pd[5];
  1382.   vecteurs_pavage[nummatrix][1] = pm->pd[6];
  1383. }
  1384.  
  1385.  
  1386. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1387. void obj_resettransform(objparm *pm)
  1388. {
  1389.   int nummatrix = (int) (pm->pd[0]);
  1390.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1391.     fly_error("bad_vector_number");
  1392.     return;
  1393.   }
  1394.   vecteurs_pavage[nummatrix][0] = 0;
  1395.   vecteurs_pavage[nummatrix][1] = 0;
  1396.   matrices_pavage[nummatrix][0] = 1;
  1397.   matrices_pavage[nummatrix][1] = 0;
  1398.   matrices_pavage[nummatrix][2] = 0;
  1399.   matrices_pavage[nummatrix][3] = 1;
  1400. }
  1401.  
  1402. /* arguments: une liste de 6 nombres reels pour les coordonnees de l'origine et des vecteurs de base */
  1403. void obj_setparallelogram(objparm *pm)
  1404. {
  1405.   int i;
  1406.   for(i=0;i<6;i++)  parallogram_fonda[i]=pm->pd[i];
  1407. }
  1408.  
  1409. /* arguments :aucun */
  1410. void obj_resetparallelogram(objparm *pm)
  1411. {
  1412.   parallogram_fonda[0]=0;
  1413.   parallogram_fonda[1]=0;
  1414.   parallogram_fonda[2]=100;
  1415.   parallogram_fonda[3]=0;
  1416.   parallogram_fonda[4]=0;
  1417.   parallogram_fonda[5]=100;
  1418. }
  1419.  
  1420. /* argument : la liste des numeros des matrices a faire agir, le nom du fichier de l'image a inclure */
  1421. void obj_multicopy(objparm *pm)
  1422. {
  1423.   char *pp,*pe,*cptr;
  1424.   FILE *inf;
  1425.   gdImagePtr insimg;
  1426.   int i,j;
  1427.   int imax,jmax;
  1428.   int c; /* la couleur reperee */
  1429.   int mat;
  1430.   int nummatrices[JC_NB_MATRICES];
  1431.   double pt[2]; /* les coordonnees math. du point repere dans le parallelogramme */
  1432.   double ptr[2]; /* les coordonnees math. du point transforme */
  1433.   int pti[2]; /* les coordonnees img du point a placer sur le canevas */
  1434.   int t;
  1435.   /* gestion palette de couleur de l'image a inserer */
  1436.   int colorMap[gdMaxColors];
  1437.   int comptmat=0; /* combien de matrices en tout ? */
  1438.  
  1439.   for (i=0; (i<gdMaxColors); i++) {
  1440.     colorMap[i] = (-1);
  1441.   }
  1442.  
  1443.   /* Gestion des parametres la liste est censee finir avec le nom du fichier */
  1444.   t=pm->pcnt-1; /* il faut enlever le nom de fichier ! c'est le nombre de parametres en plus */
  1445.   pp=find_word_start(pm->str);
  1446.   /* visiblement find_word_start n'arrive pas a trouver le dernier mot dans un contexte ou le nombre de parameters est variable
  1447.      * on cherche donc l'emplacement de la derniere virgule:
  1448.      * il y en a une car t>0, on retrouve ensuite le debut de mot
  1449.      */
  1450.   for(pe=pp;*pe!=0;pe++);/* trouve la fin de la chaine */
  1451.   if(t>0){
  1452.     for(cptr = pe; cptr > pp && *cptr != ','; cptr--);
  1453.     pp=find_word_start(cptr+1);
  1454.   }
  1455.   pe=find_word_end(pp);*pe=0; /* strip junk final */
  1456.   //      printf("pp contient %s\n",pp);
  1457.  
  1458.  
  1459.   inf=open4read(pp);
  1460.   if(inf==NULL) {
  1461.     fly_error("file_not_exist"); return;
  1462.   }
  1463.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1464.   if(insimg==NULL) {
  1465.     fly_error("bad_gif"); return;
  1466.   }
  1467.  
  1468.   /* On recupere les numeros des matrices/vecteurs a faire agir,
  1469.     * s'il n'y en a pas, on les fait toutes agir
  1470.   */
  1471.   for(i=0;i<t && i< JC_NB_MATRICES;i++) {
  1472.     if(pm->pd[i]>=1 && pm->pd[i]<=JC_NB_MATRICES){
  1473.       nummatrices[comptmat] = pm->pd[i];
  1474.       comptmat++;
  1475.     }
  1476.   }
  1477.   if(t<=0){
  1478.     for(i=0;i<JC_NB_MATRICES;i++) {
  1479.       nummatrices[i] = i+1;
  1480.     }
  1481.     comptmat=JC_NB_MATRICES;
  1482.   }
  1483.  
  1484.  
  1485.   imax = gdImageSX(insimg);
  1486.   jmax = gdImageSY(insimg);
  1487.  
  1488.   for(i=0;i<imax;i++){
  1489.     for(j=0;j<jmax;j++){
  1490.       int nc;
  1491.       c=gdImageGetPixel(insimg,i,j);
  1492.       /* Le code suivant est une copie du code dans gdImageCopy  Added 7/24/95: support transparent copies */
  1493.       if (gdImageGetTransparent(insimg) != c) {
  1494.         /* Have we established a mapping for this color? */
  1495.         if (colorMap[c] == (-1)) {
  1496.           /* If it's the same image, mapping is trivial, dans notre cas ca n'arrive jamais... */
  1497.           if (image == insimg) {
  1498.             nc = c;
  1499.           } else {
  1500.             /* First look for an exact match */
  1501.             nc = gdImageColorExact(image,
  1502.                    insimg->red[c], insimg->green[c],
  1503.                            insimg->blue[c]);
  1504.           }
  1505.           if (nc == (-1)) {
  1506.           /* No, so try to allocate it */
  1507.             nc = gdImageColorAllocate(image,
  1508.                              insimg->red[c], insimg->green[c],
  1509.                              insimg->blue[c]);
  1510.              /* If we're out of colors, go for the closest color */
  1511.             if (nc == (-1)) {
  1512.               nc = gdImageColorClosest(image,
  1513.                               insimg->red[c], insimg->green[c],
  1514.                               insimg->blue[c]);
  1515.             }
  1516.           }
  1517.           colorMap[c] = nc;
  1518.         }
  1519.  
  1520.         pt[0]= i*(parallogram_fonda[2])/(imax-1)+(jmax-j)*(parallogram_fonda[4])/(jmax-1)+parallogram_fonda[0];
  1521.         pt[1]= i*(parallogram_fonda[3])/(imax-1)+(jmax-j)*(parallogram_fonda[5])/(jmax-1)+parallogram_fonda[1];
  1522.         for(mat=0;mat<comptmat;mat++){
  1523.           double *matricecourante = matrices_pavage[nummatrices[mat]];
  1524.           double *vecteurcourant = vecteurs_pavage[nummatrices[mat]];
  1525.           ptr[0] = matricecourante[0]*pt[0]+matricecourante[1]*pt[1]+vecteurcourant[0];
  1526.           ptr[1] = matricecourante[2]*pt[0]+matricecourante[3]*pt[1]+vecteurcourant[1];
  1527.           scale(ptr,pti,1);
  1528.           gdImageSetPixel(image,pti[0],pti[1],colorMap[c]);
  1529.         }
  1530.       }
  1531.     }
  1532.   }
  1533.   gdImageDestroy(insimg);
  1534. }
  1535.  
  1536. /**** Fin modifs JC Fev 06 ******/
  1537.  
  1538. /* get color from value */
  1539. int calc_color(char *p, struct objtab *o)
  1540. {
  1541.   int k, cc[3];
  1542.   char buf[MAX_LINELEN+1];
  1543.   for(k=0;k<3;k++) {
  1544.     fnd_item(p,k+1,buf);
  1545.     cc[k]=strevalue(buf);
  1546.   }
  1547.   collapse_item(p,3);
  1548.   if(cc[0]==-1 && cc[1]==-1 && cc[2]==-255) {
  1549.  
  1550.     if(brushed && o->subst&1) return gdBrushed;
  1551.     else return color_black;
  1552.   }
  1553.   if(cc[0]==-1 && cc[1]==-255 && cc[2]==-1) {
  1554.     if(styled && o->subst&2)  return gdStyled;
  1555.     else return color_black;
  1556.   }
  1557.   if(cc[0]==-255 && cc[1]==-1 && cc[2]==-1) {
  1558.     if(tiled && o->fill_tag==1)  return gdTiled;
  1559.     else return color_black;
  1560.   }
  1561.   return getcolor(cc[0],cc[1],cc[2]);
  1562. }
  1563.  
  1564. /* Routine to parse parameters */
  1565. int parse_parms(char *p,objparm *pm,struct objtab *o)
  1566. {
  1567.   char buf[MAX_LINELEN+1];
  1568.   char *p1, *p2;
  1569.   int j,t,c,c1,c2;
  1570.  
  1571.   c=o->color_pos;c1=c2=0;
  1572.   pm->color[0]=pm->color[1]=0;
  1573.   if(c>0) c1=c;
  1574.   if(c<0) c2=-c;
  1575.   c=c1+c2;
  1576.   t=itemnum(p);if(t<o->required_parms+3*c) return -1;
  1577.   if(c1>0 && t>o->required_parms+3*c) t=o->required_parms+3*c;
  1578.   pm->pcnt=t-3*c;
  1579.   if(pm->pcnt>MAX_PARMS) pm->pcnt=MAX_PARMS;
  1580.   if(c2>0) {
  1581.     for(j=0;j<2 && j<c2; j++) pm->color[j]=calc_color(p,o);
  1582.   }
  1583.   snprintf(buf,sizeof(buf),"%s",p);
  1584.   for(j=0, p1=buf; j<pm->pcnt; j++, p1=p2) {
  1585.     p2=find_item_end(p1); if(*p2) *p2++=0;
  1586.     p1=find_word_start(p1);
  1587.     if(*p1) pm->pd[j]=strevalue(p1); else pm->pd[j]=0;
  1588.     if(!isfinite(pm->pd[j])) {
  1589.         if(j<o->required_parms) return -1;
  1590.         else {pm->pd[j]=0;break;}
  1591.     }
  1592.   }
  1593.   collapse_item(p,o->required_parms);
  1594.   if(c1>0) {
  1595.     for(j=0;j<c1 && j<2; j++) pm->color[j]=calc_color(p,o);
  1596.   }
  1597.   if(width>1 && o->subst&1 && pm->color[0]!=gdBrushed
  1598.       && pm->color[0]!=gdStyled && pm->color[0]!=gdTiled) {
  1599.     pm->color[1]=pm->color[0];
  1600.     pm->color[0]=widthcolor(width,pm->color[0]);
  1601.   }
  1602.   pm->fill=o->fill_tag;
  1603.   ovlstrcpy(pm->str,p); return 0;
  1604. }
  1605.  
  1606. /* Create the struct objparm pm corresponding to the objparm p
  1607.  * Execute a command. Returns 0 if OK.
  1608.  */
  1609. int obj_main(char *p)
  1610. {
  1611.   int i;
  1612.   char *pp,*name,*pt;
  1613.   char tbuf2[MAX_LINELEN+1];
  1614.   struct objparm pm;
  1615.  
  1616.   p=find_word_start(p);
  1617.   if(*p==exec_prefix_char || *p==0) return 0; /* comment */
  1618.   name=p;
  1619.   pp=find_name_end(p);
  1620.   pt=find_word_start(pp); if(*pt=='=') *pt=' ';
  1621.   if(*pt==':' && *(pt+1)=='=') *pt=*(pt+1)=' ';
  1622.   pp=find_word_end(p);
  1623.   if(*pp!=0) {
  1624.     *(pp++)=0; pp=find_word_start(pp);
  1625.   }
  1626.   if(strlen(p)==1 || (strlen(p)==2 && isdigit(*(p+1)))) {
  1627.     setvar(p,strevalue(pp)); return 0;
  1628.   }
  1629.   /* search the number of the object */
  1630.   i=search_list(objtab,obj_no,sizeof(objtab[0]),name);
  1631.   if(i<0) {
  1632.     fly_error("bad_cmd"); return 1;
  1633.   }
  1634.   if(image==NULL && (objtab[i].color_pos || objtab[i].required_parms>2)) {
  1635.     fly_error("image_not_defined"); return 1;
  1636.   }
  1637.   ovlstrcpy(tbuf2,pp);
  1638.   if(objtab[i].color_pos || objtab[i].routine==obj_setstyle) {
  1639.     substit(tbuf2);
  1640.   }
  1641.   if(parse_parms(tbuf2,&pm,objtab+i)!=0) fly_error("bad_parms");
  1642.   else objtab[i].routine(&pm);
  1643.   return 0;
  1644. }
  1645.