Subversion Repositories wimsdev

Rev

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