Subversion Repositories wimsdev

Rev

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