Subversion Repositories wimsdev

Rev

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