Subversion Repositories wimsdev

Rev

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