Subversion Repositories wimsdev

Rev

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

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