Subversion Repositories wimsdev

Rev

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