Subversion Repositories wimsdev

Rev

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