Subversion Repositories wimsdev

Rev

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