Subversion Repositories wimsdev

Rev

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