Subversion Repositories wimsdev

Rev

Rev 3718 | Rev 6578 | 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)
  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.                   gdImageLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
  797.                
  798.             }
  799.         }
  800.         memmove(oc,ic,sizeof(oc));
  801.     }
  802.     if(vimg_enable) vimg_plotend();
  803. }
  804.  
  805.         /* set levelcurve granularity */
  806. void obj_levelstep(objparm *pm)
  807. {
  808.     int dd;
  809.     dd=pm->pd[0];
  810.     if(dd<1) return; if(dd>16) dd=16;
  811.     lstep=dd;
  812. }
  813.  
  814.         /* level curve */
  815. void obj_levelcurve(objparm *pm)
  816. {
  817.     char fn[MAX_LINELEN+1],tc[MAX_LINELEN+1];
  818.     int n,i;
  819.     double d;
  820.     leveldata *ld;
  821.    
  822.     ld=xmalloc(sizeof(leveldata)+16);
  823.     ld->xname="x"; ld->yname="y";
  824.     ld->xsize=sizex; ld->ysize=sizey; ld->datacnt=0;
  825.     ld->xrange[0]=xstart; ld->xrange[1]=sizex/xscale+xstart;
  826.     ld->yrange[0]=sizey/yscale+ystart; ld->yrange[1]=ystart;
  827.     ld->grain=lstep;
  828.     fnd_item(pm->str,1,fn); ld->fn=fn;
  829.     n=itemnum(pm->str);
  830.     if(n<=1) {ld->levelcnt=1; ld->levels[0]=0;}
  831.     else {
  832.         if(n>LEVEL_LIM+1) n=LEVEL_LIM+1;
  833.         for(i=0;i<n-1;i++) {
  834.             fnd_item(pm->str,i+2,tc);
  835.             d=evalue(tc);
  836.             if(finite(d)) ld->levels[i]=d; else ld->levels[i]=0;
  837.         }
  838.         ld->levelcnt=n-1;
  839.     }
  840.     levelcurve(ld);
  841.     for(i=0;i<ld->datacnt;i++) {
  842.         gdImageSetPixel(image,ld->xdata[i],ld->ydata[i],pm->color[0]);
  843.     }
  844.     free(ld);
  845. }
  846.  
  847.         /* set animation step */
  848. void obj_animstep(objparm *pm)
  849. {
  850.     animstep=pm->pd[0];
  851.     setvar("animstep",pm->pd[0]);
  852. }
  853.  
  854.         /* Starts a line counting */
  855. void obj_linecount(objparm *pm)
  856. {
  857.     linecnt=pm->pd[0];
  858. }
  859.  
  860.         /* marks end of execution */
  861. void obj_end(objparm *pm)
  862. {
  863.     error("successful_end_of_execution");
  864. }
  865.  
  866.         /* output */
  867. void obj_output(objparm *pm)
  868. {
  869.     char *p, namebuf[1024];
  870.     p=find_word_start(pm->str); *find_word_end(p)=0;
  871.     snprintf(namebuf,sizeof(namebuf),"%s",imagefilename);
  872.     snprintf(imagefilename,sizeof(imagefilename),"%s",p);
  873.     output();
  874.     snprintf(imagefilename,sizeof(imagefilename),"%s",namebuf);
  875. }
  876.  
  877.         /* vimgfile */
  878. void obj_vimgfile(objparm *pm)
  879. {
  880.     char *p;
  881.     p=find_word_start(pm->str); *find_word_end(p)=0;
  882.     snprintf(vimgfilename,sizeof(vimgfilename),"%s",p);
  883.     if(vimg_ready) vimg_close();
  884. }
  885.  
  886.         /* vimg enable/disable */
  887. void obj_vimg(objparm *pm)
  888. {
  889.     vimg_enable=pm->pd[0];
  890.     if(vimg_enable>0 && vimg_ready==0) {
  891.         vimg_init();
  892.     }
  893. }
  894.  
  895.         /* Set affine transformation */
  896. void obj_affine(objparm *pm)
  897. {
  898.     int i;
  899.     for(i=0;i<4;i++) matrix[i]=pm->pd[i];
  900.     transx=pm->pd[4]; transy=pm->pd[5];
  901.     transform=1;
  902. }
  903.  
  904.         /* Set affine transformation */
  905. void obj_rotation(objparm *pm)
  906. {
  907.     double r;
  908.     r=M_PI*pm->pd[0]/180;
  909.     matrix[0]=matrix[3]=cos(r);
  910.     matrix[1]=-sin(r); matrix[2]=sin(r);
  911.     transform=1;
  912. }
  913.  
  914.         /* Set affine transformation */
  915. void obj_linear(objparm *pm)
  916. {
  917.     int i;
  918.     for(i=0;i<4;i++) matrix[i]=pm->pd[i];
  919.     transform=1;
  920. }
  921.  
  922.         /* Set affine transformation */
  923. void obj_translation(objparm *pm)
  924. {
  925.     transx=pm->pd[0]; transy=pm->pd[1];
  926. }
  927.  
  928.         /* Set affine transformation */
  929. void obj_killaffine(objparm *pm)
  930. {
  931.     matrix[0]=matrix[3]=1;
  932.     matrix[1]=matrix[2]=transx=transy=0;
  933.     transform=0;
  934. }
  935.  
  936.         /* Set affine transformation */
  937. void obj_killlinear(objparm *pm)
  938. {
  939.     matrix[0]=matrix[3]=1;
  940.     matrix[1]=matrix[2]=0;
  941.     transform=0;
  942. }
  943.  
  944.         /* Set affine transformation */
  945. void obj_killtranslation(objparm *pm)
  946. {
  947.     transx=transy=0;
  948. }
  949.  
  950. /***** Les modifs de Jean-Christophe Leger Fev 2006 *****/
  951.  
  952. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES, une liste de 4 nombres reels pour la matrice */
  953. void obj_setmatrix(objparm *pm)
  954. {
  955.   int nummatrix = (int) (pm->pd[0]);
  956.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  957.     error("bad_matrix_number");
  958.     return;
  959.   }
  960.   matrices_pavage[nummatrix][0] = pm->pd[1];
  961.   matrices_pavage[nummatrix][1] = pm->pd[2];
  962.   matrices_pavage[nummatrix][2] = pm->pd[3];
  963.   matrices_pavage[nummatrix][3] = pm->pd[4];
  964. }
  965.  
  966. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  967. void obj_resetmatrix(objparm *pm)
  968. {
  969.   int nummatrix = (int) (pm->pd[0]);
  970.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  971.     error("bad_matrix_number");
  972.     return;
  973.   }
  974.   matrices_pavage[nummatrix][0] = 1;
  975.   matrices_pavage[nummatrix][1] = 0;
  976.   matrices_pavage[nummatrix][2] = 0;
  977.   matrices_pavage[nummatrix][3] = 1;
  978.  
  979. }
  980. /* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 2 nombres reels pour le vecteur */
  981. void obj_setvector(objparm *pm)
  982. {
  983.   int nummatrix = (int) (pm->pd[0]);
  984.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  985.     error("bad_vector_number");
  986.     return;
  987.   }
  988.   vecteurs_pavage[nummatrix][0] = pm->pd[1];
  989.   vecteurs_pavage[nummatrix][1] = pm->pd[2];
  990. }
  991.  
  992. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  993. void obj_resetvector(objparm *pm)
  994. {
  995.   int nummatrix = (int) (pm->pd[0]);
  996.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  997.     error("bad_vector_number");
  998.     return;
  999.   }
  1000.   vecteurs_pavage[nummatrix][0] = 0;
  1001.   vecteurs_pavage[nummatrix][1] = 0;
  1002. }
  1003.  
  1004. /* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 6 nombres reels pour la matrice et le vecteur */
  1005. void obj_settransform(objparm *pm)
  1006. {
  1007.   int nummatrix = (int) (pm->pd[0]);
  1008.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1009.     error("bad_vector_number");
  1010.     return;
  1011.   }
  1012.   matrices_pavage[nummatrix][0] = pm->pd[1];
  1013.   matrices_pavage[nummatrix][1] = pm->pd[2];
  1014.   matrices_pavage[nummatrix][2] = pm->pd[3];
  1015.   matrices_pavage[nummatrix][3] = pm->pd[4];
  1016.   vecteurs_pavage[nummatrix][0] = pm->pd[5];
  1017.   vecteurs_pavage[nummatrix][1] = pm->pd[6];
  1018. }
  1019.  
  1020.  
  1021. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1022. void obj_resettransform(objparm *pm)
  1023. {
  1024.   int nummatrix = (int) (pm->pd[0]);
  1025.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1026.     error("bad_vector_number");
  1027.     return;
  1028.   }
  1029.   vecteurs_pavage[nummatrix][0] = 0;
  1030.   vecteurs_pavage[nummatrix][1] = 0;
  1031.   matrices_pavage[nummatrix][0] = 1;
  1032.   matrices_pavage[nummatrix][1] = 0;
  1033.   matrices_pavage[nummatrix][2] = 0;
  1034.   matrices_pavage[nummatrix][3] = 1;
  1035. }
  1036.  
  1037. /* arguments: une liste de 6 nombres reels pour les coordonnees de l'origine et des vecteurs de base */
  1038. void obj_setparallelogram(objparm *pm)
  1039. {
  1040.   int i;
  1041.   for(i=0;i<6;i++)  parallogram_fonda[i]=pm->pd[i];
  1042. }
  1043.  
  1044. /* arguments :aucun */
  1045. void obj_resetparallelogram(objparm *pm)
  1046. {
  1047.   parallogram_fonda[0]=0;
  1048.   parallogram_fonda[1]=0;
  1049.   parallogram_fonda[2]=100;
  1050.   parallogram_fonda[3]=0;
  1051.   parallogram_fonda[4]=0;
  1052.   parallogram_fonda[5]=100;
  1053. }
  1054.  
  1055. /* argument : la liste des numeros des matrices a faire agir, le nom du fichier de l'image a inclure */
  1056. void obj_multicopy(objparm *pm)
  1057. {
  1058.     char *pp,*pe,*cptr;
  1059.     FILE *inf;
  1060.     gdImagePtr  insimg;
  1061.     int i,j;
  1062.     int imax,jmax;
  1063.     int c; /* la couleur reperee */
  1064.     int mat;
  1065.     int nummatrices[JC_NB_MATRICES];
  1066.     double pt[2]; /* les coordonnees math. du point repere dans le parallelogramme */
  1067.     double ptr[2]; /* les coordonnees math. du point transforme */
  1068.     int pti[2]; /* les coordonnees img du point a placer sur le canevas */
  1069.     int t;
  1070.     /* gestion palette de couleur de l'image a inserer */
  1071.     int colorMap[gdMaxColors];
  1072.     int comptmat=0; /* combien de matrices en tout ? */
  1073.    
  1074.     for (i=0; (i<gdMaxColors); i++) {
  1075.       colorMap[i] = (-1);
  1076.     }
  1077.  
  1078.     /* Gestion des parametres la liste est censee finir avec le nom du fichier */
  1079.     t=pm->pcnt-1; /* il faut enlever le nom de fichier ! c'est le nombre de parametres en plus */
  1080.     pp=find_word_start(pm->str);
  1081.     /* visiblement find_word_start n'arrive pas a trouver le dernier mot dans un contexte ou le nombre de parameters est variable
  1082.      * on cherche donc l'emplacement de la derniere virgule:
  1083.      * il y en a une car t>0, on retrouve ensuite le debut de mot
  1084.      */
  1085.     for(pe=pp;*pe!=0;pe++);/* trouve la fin de la chaine */
  1086.     if(t>0){
  1087.       for(cptr = pe; cptr > pp && *cptr != ','; cptr--);
  1088.       pp=find_word_start(cptr+1);
  1089.     }
  1090.     pe=find_word_end(pp);*pe=0; /* strip junk final */
  1091.     //      printf("pp contient %s\n",pp);
  1092.  
  1093.  
  1094.     inf=open4read(pp);
  1095.     if(inf==NULL) {
  1096.         error("file_not_exist"); return;
  1097.     }
  1098.     insimg=gdImageCreateFromGif(inf); fclose(inf);
  1099.     if(insimg==NULL) {
  1100.         error("bad_gif"); return;
  1101.     }
  1102.  
  1103.     /* On recupere les numeros des matrices/vecteurs a faire agir,
  1104.      * s'il n'y en a pas, on les fait toutes agir
  1105.      */
  1106.     for(i=0;i<t && i< JC_NB_MATRICES;i++) {
  1107.       if(pm->pd[i]>=1 && pm->pd[i]<=JC_NB_MATRICES){
  1108.           nummatrices[comptmat] = pm->pd[i];
  1109.           comptmat++;
  1110.       }
  1111.     }
  1112.     if(t<=0){
  1113.       for(i=0;i<JC_NB_MATRICES;i++) {
  1114.         nummatrices[i] = i+1;
  1115.       }
  1116.       comptmat=JC_NB_MATRICES;
  1117.     }
  1118.  
  1119.  
  1120.     imax = gdImageSX(insimg);
  1121.     jmax = gdImageSY(insimg);
  1122.  
  1123.     for(i=0;i<imax;i++){
  1124.       for(j=0;j<jmax;j++){
  1125.         int nc;
  1126.         c=gdImageGetPixel(insimg,i,j);
  1127.         /* Le code suivant est une copie du code dans gdImageCopy  Added 7/24/95: support transparent copies */
  1128.         if (gdImageGetTransparent(insimg) != c) {
  1129.           /* Have we established a mapping for this color? */
  1130.           if (colorMap[c] == (-1)) {
  1131.             /* If it's the same image, mapping is trivial, dans notre cas ca n'arrive jamais... */
  1132.             if (image == insimg) {
  1133.               nc = c;
  1134.             } else {
  1135.               /* First look for an exact match */
  1136.               nc = gdImageColorExact(image,
  1137.                                      insimg->red[c], insimg->green[c],
  1138.                                      insimg->blue[c]);
  1139.             }  
  1140.             if (nc == (-1)) {
  1141.               /* No, so try to allocate it */
  1142.               nc = gdImageColorAllocate(image,
  1143.                                         insimg->red[c], insimg->green[c],
  1144.                                         insimg->blue[c]);
  1145.               /* If we're out of colors, go for the
  1146.                  closest color */
  1147.               if (nc == (-1)) {
  1148.                 nc = gdImageColorClosest(image,
  1149.                                          insimg->red[c], insimg->green[c],
  1150.                                          insimg->blue[c]);
  1151.               }
  1152.             }
  1153.             colorMap[c] = nc;
  1154.           }
  1155.  
  1156.           pt[0]= i*(parallogram_fonda[2])/(imax-1)+(jmax-j)*(parallogram_fonda[4])/(jmax-1)+parallogram_fonda[0];
  1157.           pt[1]= i*(parallogram_fonda[3])/(imax-1)+(jmax-j)*(parallogram_fonda[5])/(jmax-1)+parallogram_fonda[1];
  1158.           for(mat=0;mat<comptmat;mat++){
  1159.             double *matricecourante = matrices_pavage[nummatrices[mat]];
  1160.             double *vecteurcourant = vecteurs_pavage[nummatrices[mat]];
  1161.             ptr[0] = matricecourante[0]*pt[0]+matricecourante[1]*pt[1]+vecteurcourant[0];
  1162.             ptr[1] = matricecourante[2]*pt[0]+matricecourante[3]*pt[1]+vecteurcourant[1];
  1163.             scale(ptr,pti,1);
  1164.             gdImageSetPixel(image,pti[0],pti[1],colorMap[c]);
  1165.           }
  1166.         }
  1167.       }
  1168.     }
  1169.     gdImageDestroy(insimg);
  1170. }
  1171.  
  1172. /**** Fin modifs JC Fev 06 ******/
  1173.  
  1174.         /* get color from value */
  1175. int calc_color(char *p, struct objtab *o)
  1176. {
  1177.     int k, cc[3];
  1178.     char buf[MAX_LINELEN+1];
  1179.     for(k=0;k<3;k++) {
  1180.         fnd_item(p,k+1,buf);
  1181.         cc[k]=evalue(buf);
  1182.     }
  1183.     collapse_item(p,3);
  1184.     if(cc[0]==-1 && cc[1]==-1 && cc[2]==-255) {
  1185.  
  1186.         if(brushed && o->subst&1) return gdBrushed;
  1187.         else return color_black;
  1188.     }
  1189.     if(cc[0]==-1 && cc[1]==-255 && cc[2]==-1) {
  1190.         if(styled && o->subst&2)  return gdStyled;
  1191.         else return color_black;
  1192.     }
  1193.     if(cc[0]==-255 && cc[1]==-1 && cc[2]==-1) {
  1194.         if(tiled && o->fill_tag==1)  return gdTiled;
  1195.         else return color_black;
  1196.     }
  1197.     return getcolor(cc[0],cc[1],cc[2]);
  1198. }
  1199.  
  1200.         /* Routine to parse parameters */
  1201. int parse_parms(char *p,objparm *pm,struct objtab *o)
  1202. {
  1203.     char buf[MAX_LINELEN+1];
  1204.     char *p1, *p2;
  1205.     int j,t,c,c1,c2;
  1206.    
  1207.     c=o->color_pos;c1=c2=0;
  1208.     pm->color[0]=pm->color[1]=0;
  1209.     if(c>0) c1=c; if(c<0) c2=-c; c=c1+c2;
  1210.     t=itemnum(p);if(t<o->required_parms+3*c) return -1;
  1211.     if(c1>0 && t>o->required_parms+3*c) t=o->required_parms+3*c;
  1212.     pm->pcnt=t-3*c;
  1213.     if(pm->pcnt>MAX_PARMS) pm->pcnt=MAX_PARMS;
  1214.     if(c2>0) {
  1215.         for(j=0;j<2 && j<c2; j++) pm->color[j]=calc_color(p,o);
  1216.     }
  1217.     snprintf(buf,sizeof(buf),"%s",p);
  1218.     for(j=0, p1=buf; j<pm->pcnt; j++, p1=p2) {
  1219.         p2=find_item_end(p1); if(*p2) *p2++=0;
  1220.         p1=find_word_start(p1);
  1221.         if(*p1) pm->pd[j]=evalue(p1); else pm->pd[j]=0;
  1222.         if(!finite(pm->pd[j])) {
  1223.             if(j<o->required_parms) return -1;
  1224.             else {pm->pd[j]=0;break;}
  1225.         }
  1226.     }
  1227.     collapse_item(p,o->required_parms);
  1228.     if(c1>0) {
  1229.         for(j=0;j<c1 && j<2; j++) pm->color[j]=calc_color(p,o);
  1230.     }
  1231.     if(width>1 && o->subst&1 && pm->color[0]!=gdBrushed
  1232.        && pm->color[0]!=gdStyled && pm->color[0]!=gdTiled) {
  1233.         pm->color[1]=pm->color[0];
  1234.         pm->color[0]=widthcolor(width,pm->color[0]);
  1235.     }
  1236.     pm->fill=o->fill_tag;
  1237.     ovlstrcpy(pm->str,p); return 0;
  1238. }
  1239.  
  1240.         /* Execute a command. Returns 0 if OK. */
  1241. int obj_main(char *p)
  1242. {
  1243.     int i;
  1244.     char *pp,*name,*pt;
  1245.     char tbuf2[MAX_LINELEN+1];
  1246.     struct objparm pm;
  1247.  
  1248.     p=find_word_start(p);
  1249.     if(*p==exec_prefix_char || *p==0) return 0; /* comment */
  1250.     name=p;
  1251.     pp=find_name_end(p);
  1252.     pt=find_word_start(pp); if(*pt=='=') *pt=' ';
  1253.     if(*pt==':' && *(pt+1)=='=') *pt=*(pt+1)=' ';
  1254.     pp=find_word_end(p);
  1255.     if(*pp!=0) {
  1256.         *(pp++)=0; pp=find_word_start(pp);
  1257.     }
  1258.     if(strlen(p)==1 || (strlen(p)==2 && isdigit(*(p+1)))) {
  1259.         setvar(p,evalue(pp)); return 0;
  1260.     }
  1261.     i=search_list(objtab,obj_no,sizeof(objtab[0]),name);
  1262.     if(i<0) {
  1263.         error("bad_cmd"); return 1;
  1264.     }
  1265.     if(image==NULL && (objtab[i].color_pos || objtab[i].required_parms>2)) {
  1266.         error("image_not_defined"); return 1;
  1267.     }
  1268.     ovlstrcpy(tbuf2,pp);
  1269.     if(objtab[i].color_pos || objtab[i].routine==obj_setstyle) {
  1270.         substit(tbuf2);
  1271.     }
  1272.     if(parse_parms(tbuf2,&pm,objtab+i)!=0) error("bad_parms");
  1273.     else objtab[i].routine(&pm);
  1274.     return 0;
  1275. }
  1276.  
  1277.