Subversion Repositories wimsdev

Rev

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