Subversion Repositories wimsdev

Rev

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