Subversion Repositories wimsdev

Rev

Rev 8177 | Rev 8414 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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