Subversion Repositories wimsdev

Rev

Rev 18317 | Rev 18320 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17676 bpr 1
/*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
10 reyssat 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
 */
8177 bpr 17
 
18
#include <errno.h>
8102 bpr 19
#include "flydraw.h"
18317 bpr 20
void myGdImageArc(gdImagePtr, int, int, int, int, double, double, int);
21
const double DEG=M_PI/180;
8177 bpr 22
 
17574 bpr 23
/* Tikz things */
17599 bpr 24
 
17608 bpr 25
static char *tikz_color (int the_color)
26
{
27
  static char buf[50];
28
  int r=gdImageRed(image,the_color);
29
  int g=gdImageGreen(image,the_color);
30
  int b=gdImageBlue(image,the_color);
31
  sprintf(buf, "{rgb,255:red,%i;green,%i;blue,%i}",r,g,b);
32
  return buf;
33
}
34
 
17574 bpr 35
static char *tikz_options (int the_color, int the_fill)
36
{
37
  static char buf[100];
38
  if (the_color == gdBrushed)
39
    the_color = tikz_brushColor;
17608 bpr 40
  sprintf (buf, "draw=%s,cap=round",tikz_color(the_color));
17574 bpr 41
  if (the_color != tikz_brushColor) strcat(buf, ",thin");
18125 bpr 42
  if (the_fill == -1 || the_fill == 2) strcat(buf, ",dashed");
43
  if (the_fill == 1 || the_fill == 2)
17608 bpr 44
    sprintf(buf+strlen(buf), ",fill=%s", tikz_color(the_color));
17574 bpr 45
  return buf;
46
}
47
 
17662 bpr 48
#define flip(y) (sizey-1-(y))
17574 bpr 49
 
8010 bpr 50
/* bug in gdImageFillToBorder */
10 reyssat 51
 
8162 bpr 52
void patchgdImageFillToBorder (gdImagePtr im, int x, int y, int border, int color)
8076 bpr 53
{
12473 bpr 54
  if(x>=im->sx || x<0 || y>=im->sy || y<0) return;
55
  gdImageFillToBorder(im,x,y,border,color);
8076 bpr 56
}
57
 
8162 bpr 58
void patchgdImageFill (gdImagePtr im, int x, int y, int color)
8156 bpr 59
{
12473 bpr 60
  if(x>=im->sx || x<0 || y>=im->sy || y<0) return;
61
  gdImageFill(im,x,y,color);
8156 bpr 62
}
8076 bpr 63
 
9651 georgesk 64
#define DASHPIXELS 8
65
 
66
void myDashedLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color){
67
  /**
68
   * emulates gdImageDashedLine with gdImageSetStyle and gdImageLine
69
   * GK: As gdImageDashedLine is slightly broken in latest libgd libraries,
70
   * GK: I implemented an emulation named "myDashedLine"
71
   **/
72
 
73
  int styleDashed[DASHPIXELS],i;
74
  for (i=0; i< DASHPIXELS/2; i++) styleDashed[i]=color;
75
  for (; i< DASHPIXELS; i++) styleDashed[i]=gdTransparent;
76
  gdImageSetStyle(im, styleDashed, DASHPIXELS);
77
  gdImageLine(im, x1, y1, x2, y2, gdStyled);
78
}
79
 
80
 
7675 bpr 81
/* File opening: with security */
10 reyssat 82
FILE *open4read(char *n)
83
{
12473 bpr 84
  char *p, *p1, *p2, namebuf[2048];
85
  int t;
86
  FILE *f;
87
  n=find_word_start(n);
88
  if(*n==0) return NULL;
89
  p=getenv("flydraw_filebase");
90
  p1=n+strlen(n)-4;if(p1<n || strcasecmp(p1,".gif")!=0) t=1; else t=0;
91
  if(p!=NULL && *p!=0) {
92
    char pbuf[MAX_LINELEN+1];
93
    snprintf(pbuf,sizeof(pbuf),"%s",p);
94
    p=find_word_start(pbuf); if(strstr(p,"..")!=NULL) return NULL;
95
    if(*n=='/' || strstr(n,"..")!=NULL) return NULL;
96
    /* prohibit unusual file/dir names */
97
    for(p1=p;*p1;p1++)
98
      if(!isalnum(*p1) && !isspace(*p1) && strchr("~_-/.",*p1)==NULL)
99
        return NULL;
100
    for(p1=n;*p1;p1++)
101
      if(!isalnum(*p1) && !isspace(*p1) && strchr("~_-/.",*p1)==NULL)
102
        return NULL;
103
    f=NULL;
104
    for(p1=p; *p1; p1=find_word_start(p2)) {
105
      p2=find_word_end(p1);
106
      if(*p2) *p2++=0;
107
      snprintf(namebuf,sizeof(namebuf),"%s/%s",p1,n);
108
      f=fopen(namebuf,"r"); if(f!=NULL) goto imgtype;
10 reyssat 109
    }
12473 bpr 110
    p1=getenv("w_wims_session");
111
    if(p1!=NULL && strncmp(n,"insert",6)==0) {
112
      snprintf(namebuf,sizeof(namebuf),"../s2/%s/%s",p1,n);
7675 bpr 113
      f=fopen(namebuf,"r");
10 reyssat 114
    }
12473 bpr 115
  }
116
  else {
117
    snprintf(namebuf,sizeof(namebuf),"%s",n);
118
    f=fopen(namebuf,"r");
119
  }
120
  imgtype:
121
  if(t && f!=NULL) {
122
    char tbuf[1024],sbuf[4096];
123
    fclose(f); f=NULL;
124
    p1=getenv("TMPDIR"); if(p1==NULL || *p1==0) p1=".";
125
    snprintf(tbuf,sizeof(tbuf),"%s/drawfile_.gif",p1);
126
    snprintf(sbuf,sizeof(sbuf),"convert %s %s",namebuf,tbuf);
127
    if (system(sbuf)) fly_error("system_failed");
128
      f=fopen(tbuf,"r");
129
  }
130
  return f;
10 reyssat 131
}
132
 
7675 bpr 133
/* Does nothing; just a comment. */
10 reyssat 134
void obj_comment(objparm *pm)
135
{
12473 bpr 136
  return;
10 reyssat 137
}
138
 
7675 bpr 139
/* define image size */
10 reyssat 140
void obj_size(objparm *pm)
141
{
12473 bpr 142
  sizex=rint(pm->pd[0]); sizey=rint(pm->pd[1]);
143
  if(sizex<0 || sizey<0 || sizex>MAX_SIZE || sizey>MAX_SIZE) {
144
    fly_error("bad_size"); return;
145
  }
146
  if(image!=NULL) {
147
    fly_error("size_already_defined"); return;
148
  }
149
  image=gdImageCreate(sizex,sizey);
17574 bpr 150
  if(tikz_file) fprintf(tikz_file,"\\clip (0,0) rectangle (%i, %i);\n", sizex, sizey);
12473 bpr 151
  if(image==NULL) fly_error("image_creation_failure");
152
  else {
153
    color_white=gdImageColorAllocate(image,255,255,255);
154
    color_black=gdImageColorAllocate(image,0,0,0);
155
    color_bounder=gdImageColorAllocate(image,1,2,3);
156
    color_frame=gdImageColorAllocate(image,254,254,254);
157
  }
10 reyssat 158
}
159
 
7675 bpr 160
/* new image */
10 reyssat 161
void obj_new(objparm *pm)
162
{
12473 bpr 163
  if(image) {
164
    gdImageDestroy(image);image=NULL;
165
  }
166
  if(pm->pcnt>=2) { obj_size(pm); gdImageRectangle(image,0,0,sizex-1,sizey-1,color_frame);}
167
  else sizex=sizey=0;
168
  saved=0;
10 reyssat 169
}
170
 
7675 bpr 171
/* new image */
10 reyssat 172
void obj_existing(objparm *pm)
173
{
12473 bpr 174
  FILE *inf;
175
  char *pp;
7615 bpr 176
 
12473 bpr 177
  if(image) {
178
    gdImageDestroy(image);image=NULL;
179
  }
180
  pp=find_word_start(pm->str);*find_word_end(pp)=0;
181
  inf=open4read(pp);
182
  if(inf==NULL) {
183
    fly_error("file_not_exist"); return;
184
  }
185
  image=gdImageCreateFromGif(inf); fclose(inf);
186
  if(image==NULL) {
187
    fly_error("bad_gif"); return;
188
  }
189
  sizex=image->sx; sizey=image->sy;
190
  saved=0;
10 reyssat 191
}
18066 bpr 192
/* tikzfile */
193
void obj_tikzfile(objparm *pm)
194
{
195
  char *p;
196
  if(tikz_file){
197
    fprintf(tikz_file,"\\end{tikzpicture}\n");
198
    fclose(tikz_file);
199
  }
200
  p=find_word_start(pm->str); *find_word_end(p)=0;
201
  snprintf(tikzfilename,sizeof(tikzfilename),"%s",p);
202
  tikz_file=fopen(tikzfilename,"w");
203
  if(!tikz_file)
204
    fly_error("tikz_nopen");
205
  fprintf(tikz_file,"\\begin{tikzpicture}\[x=0.02cm, y=0.02cm]\n");
206
}
10 reyssat 207
 
18123 bpr 208
/* segment */
10 reyssat 209
void obj_line(objparm *pm)
210
{
12473 bpr 211
  scale(pm->pd,pm->p,2);
18125 bpr 212
  if(pm->fill==-1 || pm->fill==2 )
18123 bpr 213
    myDashedLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],pm->color[0]);
214
  else
215
    gdImageLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],pm->color[0]);
17574 bpr 216
  if (tikz_file)
217
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
218
      tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]));
12473 bpr 219
  if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
10 reyssat 220
}
221
 
1024 bpr 222
void _obj_arrow(objparm *pm, int twoside)
10 reyssat 223
{
12473 bpr 224
  int l,xx,yy;
225
  gdPoint ii[3];
226
  double dx,dy,length,dd[6];
227
  scale(pm->pd,pm->p,2);
228
  xx=ii[0].x=pm->p[2];yy=ii[0].y=pm->p[3];
229
  l=pm->pd[4];if(l<0) l=0; if(l>200) l=200;
230
  scale2(pm->pd[0]-pm->pd[2],pm->pd[1]-pm->pd[3],&dx,&dy);
231
  length=sqrt(dx*dx+dy*dy);
232
  if(length<3 || l<5) goto stem;
233
  dd[0]=l*dx/length; dd[1]=l*dy/length;
234
  #define fat 0.27
235
  dd[2]=dd[0]+dd[1]*fat; dd[3]=dd[1]-dd[0]*fat;
236
  dd[4]=dd[0]-dd[1]*fat; dd[5]=dd[1]+dd[0]*fat;
237
  ii[1].x=rint(dd[2])+ii[0].x; ii[1].y=rint(dd[3])+ii[0].y;
238
  ii[2].x=rint(dd[4])+ii[0].x; ii[2].y=rint(dd[5])+ii[0].y;
239
  gdImageFilledPolygon(image, ii,3,pm->color[0]);
17578 bpr 240
  if(tikz_file)
241
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
242
      tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
243
  if(vimg_enable) vimg_polyline(scale_buf,3,1);
12473 bpr 244
  xx=rint(dd[0])+ii[0].x;yy=rint(dd[1])+ii[0].y;
245
  if(twoside) {
246
    ii[0].x=pm->p[0]; ii[0].y=pm->p[1];
247
    ii[1].x=-rint(dd[2])+ii[0].x; ii[1].y=-rint(dd[3])+ii[0].y;
248
    ii[2].x=-rint(dd[4])+ii[0].x; ii[2].y=-rint(dd[5])+ii[0].y;
8163 bpr 249
    gdImageFilledPolygon(image, ii,3,pm->color[0]);
17578 bpr 250
  if(tikz_file)
251
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
252
      tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
12473 bpr 253
  }
18125 bpr 254
  stem: if(pm->fill==-1 || pm->fill==2)
12473 bpr 255
    myDashedLine(image,pm->p[0],pm->p[1],xx,yy,pm->color[0]);
256
  else
257
    gdImageLine(image,pm->p[0],pm->p[1],xx,yy,pm->color[0]);
17578 bpr 258
  if(tikz_file)
259
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
17584 bpr 260
      tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),xx,flip(yy));
12473 bpr 261
  if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
10 reyssat 262
}
263
 
7675 bpr 264
/* Arrow */
1024 bpr 265
void obj_arrow(objparm *pm)
266
{
12473 bpr 267
  _obj_arrow(pm,0);
1024 bpr 268
}
269
 
7675 bpr 270
/* 2-sided arrow */
1024 bpr 271
void obj_arrow2(objparm *pm)
272
{
12473 bpr 273
   _obj_arrow(pm,1);
1024 bpr 274
}
275
 
18047 bpr 276
void _obj_arrows(objparm *pm, int twoside)
277
{
278
  #define fat 0.27
279
  int i,l,xx,yy;
280
  gdPoint ii[3];
281
  double dx,dy,length,dd[6];
282
  l=pm->pd[0];if(l<0) l=0; if(l>200) l=200;
283
  scale(pm->pd+1,pm->p+1,pm->pcnt/2);
284
  for (i=1;i<pm->pcnt;i+=4){
18119 bpr 285
    xx=ii[0].x=pm->p[i+2];yy=ii[0].y=pm->p[i+3];
286
    scale2(pm->pd[i]-pm->pd[i+2],pm->pd[i+1]-pm->pd[i+3],&dx,&dy);
287
    length=sqrt(dx*dx+dy*dy);
288
    if(length<3 || l<5) goto stem;
289
    dd[0]=l*dx/length; dd[1]=l*dy/length;
290
    dd[2]=dd[0]+dd[1]*fat; dd[3]=dd[1]-dd[0]*fat;
291
    dd[4]=dd[0]-dd[1]*fat; dd[5]=dd[1]+dd[0]*fat;
292
    ii[1].x=rint(dd[2])+ii[0].x; ii[1].y=rint(dd[3])+ii[0].y;
293
    ii[2].x=rint(dd[4])+ii[0].x; ii[2].y=rint(dd[5])+ii[0].y;
294
    gdImageFilledPolygon(image, ii,3,pm->color[0]);
295
    if(tikz_file)
18065 bpr 296
  fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
297
        tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
298
      xx=rint(dd[0])+ii[0].x;yy=rint(dd[1])+ii[0].y;
299
      if(twoside) {
300
  ii[0].x=pm->p[i]; ii[0].y=pm->p[i+1];
301
  ii[1].x=-rint(dd[2])+ii[0].x; ii[1].y=-rint(dd[3])+ii[0].y;
302
  ii[2].x=-rint(dd[4])+ii[0].x; ii[2].y=-rint(dd[5])+ii[0].y;
303
  gdImageFilledPolygon(image, ii,3,pm->color[0]);
304
  if(tikz_file)
305
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
306
        tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
18047 bpr 307
      }
18125 bpr 308
    stem: if(pm->fill==-1 || pm->fill==2)
18065 bpr 309
  myDashedLine(image,pm->p[i],pm->p[i+1],xx,yy,pm->color[0]);
310
      else
311
  gdImageLine(image,pm->p[i],pm->p[i+1],xx,yy,pm->color[0]);
312
      if(tikz_file)
313
  fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
314
    tikz_options(pm->color[0],pm->fill),pm->p[i],flip(pm->p[i+1]),xx,flip(yy));
315
    }
18047 bpr 316
}
317
 
318
/* Arrows */
319
void obj_arrows(objparm *pm)
320
{
321
  _obj_arrows(pm,0);
322
}
323
 
324
/* 2-sided arrows */
325
void obj_arrows2(objparm *pm)
326
{
327
   _obj_arrows(pm,1);
328
}
329
 
7675 bpr 330
/* horizontal line */
10 reyssat 331
void obj_hline(objparm *pm)
332
{
12473 bpr 333
  scale(pm->pd,pm->p,1);
18125 bpr 334
  if(pm->fill==-1 || pm->fill==2)
12473 bpr 335
    myDashedLine(image,0,pm->p[1],sizex,pm->p[1],pm->color[0]);
336
  else
337
    gdImageLine(image,0,pm->p[1],sizex,pm->p[1],pm->color[0]);
17574 bpr 338
  if (tikz_file)
339
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
340
      tikz_options(pm->color[0],pm->fill),0,flip(pm->p[1]),sizex,flip(pm->p[1]));
10 reyssat 341
}
342
 
18119 bpr 343
static void line_extend(int x1, int y1, int x2, int y2, int* xend, int *yend)
344
{
345
  double t1, t2, t3;
346
  if (x2 != x1){t1 = (x2 > x1) ? sizex : 0; t1 = (t1 - x1)/(x2 - x1);}
347
  else t1 = INFINITY;
348
  if (y2 != y1){t2 = (y2 > y1) ? sizey : 0; t2 = (t2 - y1)/(y2 - y1);}
349
  else t2 = INFINITY;
350
  t3 = (t1 < t2) ? t1 : t2;
351
  *xend = x1+t3*(x2-x1);
352
  *yend = y1+t3*(y2-y1);
353
}
18066 bpr 354
 
18065 bpr 355
/* halfline */
356
void obj_halfline(objparm *pm)
357
{
18119 bpr 358
  int xend,yend;
18065 bpr 359
  scale(pm->pd,pm->p,2);
18119 bpr 360
  line_extend(pm->p[0],pm->p[1],pm->p[2],pm->p[3],&xend,&yend);
18125 bpr 361
  if(pm->fill==-1 || pm->fill==2)
18119 bpr 362
    myDashedLine(image,pm->p[0],pm->p[1],xend,yend,pm->color[0]);
18065 bpr 363
  else
18119 bpr 364
    gdImageLine(image,pm->p[0],pm->p[1],xend,yend,pm->color[0]);
18065 bpr 365
  if (tikz_file)
366
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
367
      tikz_options(pm->color[0],pm->fill),
18119 bpr 368
      pm->p[0],flip(pm->p[1]),xend,flip(yend));
18065 bpr 369
}
17574 bpr 370
 
18119 bpr 371
void obj_fullline(objparm *pm)
372
{
373
  int xstart,ystart,xend,yend;
374
  scale(pm->pd,pm->p,2);
375
  line_extend(pm->p[0],pm->p[1],pm->p[2],pm->p[3],&xend,&yend);
376
  line_extend(pm->p[2],pm->p[3],pm->p[0],pm->p[1],&xstart,&ystart);
18125 bpr 377
  if(pm->fill==-1 || pm->fill==2)
18119 bpr 378
    myDashedLine(image,xstart,ystart,xend,yend,pm->color[0]);
379
  else
380
    gdImageLine(image,xstart,ystart,xend,yend,pm->color[0]);
381
  if (tikz_file)
382
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
383
      tikz_options(pm->color[0],pm->fill),xstart,flip(ystart),xend,flip(yend));
384
}
385
 
386
void obj_fulllines(objparm *pm)
387
{
388
  int i,xstart,ystart,xend,yend;
389
  scale(pm->pd,pm->p,pm->pcnt/2);
390
  if (tikz_file)
391
      fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
392
  for (i=0;i<pm->pcnt;i+=4){
393
    line_extend(pm->p[i],pm->p[i+1],pm->p[i+2],pm->p[i+3],&xend,&yend);
394
    line_extend(pm->p[i+2],pm->p[i+3],pm->p[i],pm->p[i+1],&xstart,&ystart);
18125 bpr 395
    if(pm->fill==-1 || pm->fill==2)
18119 bpr 396
      myDashedLine(image,xstart,ystart,xend,yend,pm->color[0]);
397
    else
398
      gdImageLine(image,xstart,ystart,xend,yend,pm->color[0]);
399
    if (tikz_file)
400
      fprintf(tikz_file, "(%i,%i)--(%i, %i)",xstart,flip(ystart),xend,flip(yend));
401
  }
402
  if (tikz_file)
403
      fprintf(tikz_file, ";\n");
404
}
405
 
7675 bpr 406
/* vertical line */
10 reyssat 407
void obj_vline(objparm *pm)
408
{
12473 bpr 409
  scale(pm->pd,pm->p,1);
18125 bpr 410
  if(pm->fill==-1 || pm->fill==2)
12473 bpr 411
    myDashedLine(image,pm->p[0],0,pm->p[0],sizey,pm->color[0]);
412
  else
413
    gdImageLine(image,pm->p[0],0,pm->p[0],sizey,pm->color[0]);
17574 bpr 414
  if (tikz_file)
415
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
416
      tikz_options(pm->color[0],pm->fill),pm->p[0],0,pm->p[0],sizey);
10 reyssat 417
}
418
 
7675 bpr 419
/* dashed line */
10 reyssat 420
void obj_dline(objparm *pm)
421
{
12473 bpr 422
  scale(pm->pd,pm->p,2);
423
  myDashedLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3], pm->color[0]);
17574 bpr 424
  if (tikz_file)
425
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
426
      tikz_options(pm->color[0],-1),
427
      pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]));
10 reyssat 428
}
429
 
7675 bpr 430
/* parallel lines.
431
 * x1,y1,x2,y2,xv,yv,n,color */
10 reyssat 432
void obj_parallel(objparm *pm)
433
{
18123 bpr 434
  int i, n, xi,yi;
435
  double xv,yv;
436
  n=pm->pd[6]; if(n<0) return; if(n>256) n=256;
437
  scale(pm->pd,pm->p,3);
438
  scale2(pm->pd[4],pm->pd[5],&xv,&yv);
439
  if (tikz_file)
440
    fprintf(tikz_file, "\\draw\[%s]", tikz_options(pm->color[0],pm->fill));
441
  for(i=0;i<n;i++) {
442
    xi=rint(i*xv); yi=rint(i*yv);
18125 bpr 443
    if(pm->fill==-1 || pm->fill==2)
18123 bpr 444
      myDashedLine(image,pm->p[0]+xi,pm->p[1]+yi,pm->p[2]+xi,pm->p[3]+yi,
445
        pm->color[0]);
446
    else
447
      gdImageLine(image,pm->p[0]+xi,pm->p[1]+yi,pm->p[2]+xi,pm->p[3]+yi,
448
        pm->color[0]);
449
    if (tikz_file)
450
      fprintf(tikz_file, "(%i,%i)--(%i, %i)",
451
        pm->p[0]+xi,flip(pm->p[1]+yi),pm->p[2]+xi,flip(pm->p[3]+yi));
452
    if(vimg_enable) vimg_line(scale_buf[0]+i*(scale_buf[4]-transx),
453
      scale_buf[1]+i*(scale_buf[5]-transy),
454
      scale_buf[2]+i*(scale_buf[4]-transx),
455
      scale_buf[3]+i*(scale_buf[5]-transy));
456
  }
17914 bpr 457
 if(tikz_file) fprintf(tikz_file,";\n");
10 reyssat 458
}
459
 
7675 bpr 460
/* rectangle */
10 reyssat 461
void obj_rect(objparm *pm)
462
{
12473 bpr 463
  int x1,y1,x2,y2;
464
  scale(pm->pd,pm->p,2);
465
  x1=min(pm->p[0],pm->p[2]); x2=max(pm->p[0],pm->p[2]);
466
  y1=min(pm->p[1],pm->p[3]); y2=max(pm->p[1],pm->p[3]);
18140 bpr 467
  if(pm->fill!=0){
468
    if(pm->fill==-1 || pm->fill==2){
469
      myDashedLine(image,x1,y1,x1,y2,pm->color[0]);
470
      myDashedLine(image,x1,y2,x2,y2,pm->color[0]);
471
      myDashedLine(image,x2,y2,x2,y1,pm->color[0]);
472
      myDashedLine(image,x2,y1,x1,y1,pm->color[0]);
473
    }
474
    if(pm->fill==1 || pm->fill==2)
475
      gdImageFilledRectangle(image,x1,y1,x2,y2,pm->color[0]);
476
  }
12473 bpr 477
  else
478
    gdImageRectangle(image,x1,y1,x2,y2,pm->color[0]);
17584 bpr 479
  if(tikz_file)
480
    fprintf(tikz_file,
481
      "\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
482
      tikz_options(pm->color[0],pm->fill),x1,flip(y1),x2,flip(y2));
12473 bpr 483
  if(vimg_enable) vimg_rect(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
10 reyssat 484
}
485
 
7675 bpr 486
/* square */
10 reyssat 487
void obj_square(objparm *pm)
488
{
12473 bpr 489
  int w,h;
490
  scale(pm->pd,pm->p,1);
491
  w=rint(pm->pd[2]); h=rint(pm->pd[2]);
18142 bpr 492
  if(pm->fill!=0){
493
    if(pm->fill==-1 || pm->fill==2){
494
      myDashedLine(image,pm->p[0],pm->p[1],pm->p[0]+w,pm->p[1],pm->color[0]);
495
      myDashedLine(image,pm->p[0]+w,pm->p[1],pm->p[0]+w,pm->p[1]+h,pm->color[0]);
496
      myDashedLine(image,pm->p[0]+w,pm->p[1]+h,pm->p[0],pm->p[1]+h,pm->color[0]);
497
      myDashedLine(image,pm->p[0],pm->p[1]+h,pm->p[0],pm->p[1],pm->color[0]);
498
    }
499
    if(pm->fill==1 || pm->fill==2)
12473 bpr 500
    gdImageFilledRectangle(image,pm->p[0],pm->p[1],
18123 bpr 501
        pm->p[0]+w,pm->p[1]+h,pm->color[0]);
18142 bpr 502
  }
12473 bpr 503
  else
504
    gdImageRectangle(image,pm->p[0],pm->p[1],
505
               pm->p[0]+w,pm->p[1]+h,pm->color[0]);
17574 bpr 506
  if(tikz_file){
507
    fprintf(tikz_file,
508
      "\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
509
      tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),
510
      pm->p[0]+w,flip(pm->p[1]+h));
511
  }
12473 bpr 512
  if(vimg_enable) vimg_rect(scale_buf[0],scale_buf[1],
513
                      scale_buf[0]+pm->pd[2],scale_buf[1]+pm->pd[2]);
10 reyssat 514
}
515
 
7675 bpr 516
/* triangle */
10 reyssat 517
void obj_triangle(objparm *pm)
518
{
12473 bpr 519
  scale(pm->pd,pm->p,3);
18131 bpr 520
  int i,n=2;
521
  if(pm->fill!=0){
522
    if(pm->fill==-1 || pm->fill==2){
523
      for(i=0;i<n;i+=1)
524
        myDashedLine(image,pm->p[2*i],pm->p[2*i+1],pm->p[2*i+2],pm->p[2*i+3],pm->color[0]);
525
      myDashedLine(image,pm->p[2*n],pm->p[2*n+1],pm->p[0],pm->p[1],pm->color[0]);
526
    }
527
    if(pm->fill==1 || pm->fill==2)
528
      gdImageFilledPolygon(image,(gdPointPtr) pm->p,3,pm->color[0]);
529
  }
12473 bpr 530
  else
531
    gdImagePolygon(image,(gdPointPtr) pm->p,3,pm->color[0]);
17574 bpr 532
  if (tikz_file)
533
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
534
      tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]),pm->p[4],flip(pm->p[5]));
12473 bpr 535
  if(vimg_enable) vimg_polyline(scale_buf,3,1);
10 reyssat 536
}
537
 
7675 bpr 538
/* polygon */
10 reyssat 539
void obj_poly(objparm *pm)
540
{
17574 bpr 541
  int cnt,i;
12473 bpr 542
  cnt=(pm->pcnt)/2;
543
  scale(pm->pd,pm->p,cnt);
18125 bpr 544
  if(pm->fill==1 || pm->fill==2)
12473 bpr 545
    gdImageFilledPolygon(image,(gdPointPtr) pm->p,cnt,pm->color[0]);
546
  else
547
    gdImagePolygon(image,(gdPointPtr) pm->p,cnt,pm->color[0]);
17614 bpr 548
  if(tikz_file){
549
    fprintf(tikz_file,"\\draw\[%s] (%i,%i) --",tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]));
550
    for (i= 1; i<cnt; i++)
551
        fprintf(tikz_file,"(%i,%i)--", pm->p[2*i],flip(pm->p[2*i+1]));
552
    fprintf(tikz_file," cycle;\n");
553
  }
12473 bpr 554
  if(vimg_enable) vimg_polyline(scale_buf,cnt,1);
10 reyssat 555
}
556
 
7675 bpr 557
/* rays */
10 reyssat 558
void obj_rays(objparm *pm)
559
{
12473 bpr 560
  int i, n;
561
  n=(pm->pcnt)/2;
562
  scale(pm->pd,pm->p,n);
17920 bpr 563
  if (tikz_file)
564
    fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
12473 bpr 565
  for(i=2;i<2*n;i+=2) {
18125 bpr 566
    if(pm->fill==-1 || pm->fill==2)
18123 bpr 567
      myDashedLine(image,pm->p[0],pm->p[1],pm->p[i],pm->p[i+1],pm->color[0]);
568
    else
569
      gdImageLine(image,pm->p[0],pm->p[1],pm->p[i],pm->p[i+1],pm->color[0]);
17574 bpr 570
    if (tikz_file)
17920 bpr 571
      fprintf(tikz_file, "(%i,%i) -- (%i,%i)",
572
        pm->p[0],flip(pm->p[1]),pm->p[i],flip(pm->p[i+1]));
12473 bpr 573
    if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],
574
                        scale_buf[i],scale_buf[i+1]);
575
  }
17920 bpr 576
  if (tikz_file) fprintf(tikz_file,";\n");
10 reyssat 577
}
8414 bpr 578
/* crosshair */
579
void obj_crosshair(objparm *pm)
580
{
12473 bpr 581
  scale(pm->pd,pm->p,2);
582
  gdImageLine(image,pm->p[0]+width2,pm->p[1]+width2,pm->p[0]-width2,pm->p[1]-width2,pm->color[0]);
583
  gdImageLine(image,pm->p[0]-width2,pm->p[1]+width2,pm->p[0]+width2,pm->p[1]-width2,pm->color[0]);
17574 bpr 584
  if (tikz_file){
585
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
586
      tikz_options(pm->color[0],pm->fill),pm->p[0]+width2,flip(pm->p[1]+width2),pm->p[0]-width2,flip(pm->p[1]-width2));
587
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
588
      tikz_options(pm->color[0],pm->fill),pm->p[0]-width2,flip(pm->p[1]+width2),pm->p[0]+width2,flip(pm->p[1]-width2));
589
  }
8414 bpr 590
}
591
/* crosshairs */
10 reyssat 592
 
8414 bpr 593
void obj_crosshairs(objparm *pm)
594
{
12473 bpr 595
  int i, n;
596
  n=(pm->pcnt)/2;
597
  scale(pm->pd,pm->p,n);
17921 bpr 598
  if (tikz_file)
599
      fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
12473 bpr 600
  for(i=0;i<2*n;i+=2) {
601
    gdImageLine(image,pm->p[i]+width2,pm->p[i+1]+width2,pm->p[i]-width2,pm->p[i+1]-width2,pm->color[0]);
602
    gdImageLine(image,pm->p[i]-width2,pm->p[i+1]+width2,pm->p[i]+width2,pm->p[i+1]-width2,pm->color[0]);
17574 bpr 603
    if (tikz_file){
17921 bpr 604
      fprintf(tikz_file, "(%i, %i) -- (%i, %i) ",
605
        pm->p[i]+width2,flip(pm->p[i+1]+width2),pm->p[i]-width2,flip(pm->p[i+1]-width2));
606
      fprintf(tikz_file, "(%i, %i) -- (%i, %i)",
607
        pm->p[i]-width2,flip(pm->p[i+1]+width2),pm->p[i]+width2,flip(pm->p[i+1]-width2));
17574 bpr 608
    }
12473 bpr 609
  }
17921 bpr 610
  if (tikz_file) fprintf(tikz_file,";\n");
8414 bpr 611
}
612
 
613
 
7675 bpr 614
/* segments */
10 reyssat 615
void obj_lines(objparm *pm)
616
{
17914 bpr 617
 int i, n;
618
 n=(pm->pcnt)/2;
619
 scale(pm->pd,pm->p,n);
18123 bpr 620
 if (tikz_file){
17914 bpr 621
   fprintf(tikz_file,"\\draw\[%s] (%i,%i)",
622
     tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]));
623
   for(i=2;i<2*n;i+=2)
624
     fprintf(tikz_file, "--(%i,%i)",pm->p[i],flip(pm->p[i+1]));
625
   fprintf(tikz_file, ";\n");
626
 }
627
 for(i=2;i<2*n;i+=2){
18125 bpr 628
  if(pm->fill==-1 || pm->fill==2 )
18123 bpr 629
    myDashedLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
630
  else
631
    gdImageLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
17914 bpr 632
  if(vimg_enable) vimg_polyline(scale_buf,n,0);
17574 bpr 633
  }
10 reyssat 634
}
16344 bpr 635
/*segments from x1,y1 to x2,y2, x3,y3 to x4,y4, etc */
636
void obj_segments(objparm *pm)
637
{
638
  int i, n;
18317 bpr 639
  n=(pm->pcnt)/2;
640
  scale(pm->pd,pm->p,n);
17914 bpr 641
  if (tikz_file)
642
    fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
18317 bpr 643
  for(i=0;i<2*n;i+=4){
18125 bpr 644
    if(pm->fill==-1 || pm->fill==2)
18123 bpr 645
      myDashedLine(image,pm->p[i],pm->p[i+1],pm->p[i+2],pm->p[i+3],pm->color[0]);
646
    else
647
      gdImageLine(image,pm->p[i],pm->p[i+1],pm->p[i+2],pm->p[i+3],pm->color[0]);
17914 bpr 648
    if (tikz_file)
649
      fprintf(tikz_file, "(%i,%i)--(%i,%i)",
650
       pm->p[i],flip(pm->p[i+1]),pm->p[i+2],flip(pm->p[i+3]));
17574 bpr 651
  }
17914 bpr 652
  if(tikz_file) fprintf(tikz_file, ";\n");
16344 bpr 653
}
18317 bpr 654
/* hyperbolic geodesics from x1,y1 to x2,y2, x3,y3 to x4,y4 in Poincaré disk */
655
 
656
void obj_hypgeods(objparm *pm)
657
{
18318 bpr 658
 int i, *qq = pm->p;
18317 bpr 659
 for (i = 0; i < pm->pcnt; i+=4){
660
   double r, *q=pm->pd + i,
661
   nx = -q[0]*q[2]*q[2]+(q[0]*q[0]+q[1]*q[1]+1)*q[2]-q[0]*q[3]*q[3]-q[0],
662
   ny = -q[1]*q[2]*q[2]-q[1]*q[3]*q[3]+(q[0]*q[0]+q[1]*q[1]+1)*q[3]-q[1],
663
   dy = -2*q[1]*q[2]+2*q[0]*q[3];
664
   if (dy*dy * 1e8 < nx*nx+ny*ny){
665
     scale(q,qq,2);
666
     gdImageLine(image,qq[0],qq[1],qq[2],qq[3],pm->color[0]);
667
   }
668
    else {
669
      double rx,ry;
670
      double dc[2];int ic[2];
671
      double
672
        cx = ny/dy, cy=-nx/dy,
673
        a1 = atan2(q[1]-cy, q[0]-cx)/DEG,
674
        a2 = atan2(q[3]-cy, q[2]-cx)/DEG;
675
      if (fabs(a2-a1)>180){if(a1<a2) a1+=360; else a2+=360;};
676
      r = sqrt(cx*cx+cy*cy-1);
677
      scale2(r,r,&rx,&ry);
678
      dc[0]=cx;dc[1]=cy;
679
      scale(dc,ic,1);
680
      if(a1<a2) myGdImageArc(image,ic[0],ic[1],rx,ry,a1,a2,pm->color[0]);
681
      else myGdImageArc(image,ic[0],ic[1],rx,ry,a2,a1,pm->color[0]);
682
    }
683
  }
684
}
7675 bpr 685
/* segments */
10 reyssat 686
void obj_dlines(objparm *pm)
687
{
17914 bpr 688
 int i, n;
689
 n=(pm->pcnt)/2;
690
 scale(pm->pd,pm->p,n);
18123 bpr 691
 if (tikz_file){
17914 bpr 692
   fprintf(tikz_file,"\\draw\[%s] (%i,%i)",
693
     tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]));
694
   for(i=2;i<2*n;i+=2)
695
     fprintf(tikz_file, "--(%i,%i)",pm->p[i],flip(pm->p[i+1]));
696
   fprintf(tikz_file, ";\n");
697
 }
698
 for(i=2;i<2*n;i+=2)
699
   myDashedLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
700
 if(vimg_enable) vimg_polyline(scale_buf,n,0);
10 reyssat 701
}
702
 
7675 bpr 703
/* points */
10 reyssat 704
void obj_points(objparm *pm)
705
{
17914 bpr 706
 int i, n;
707
 n=(pm->pcnt)/2;
708
 scale(pm->pd,pm->p,n);
709
 for(i=0;i<2*n;i+=2) gdImageSetPixel(image,pm->p[i],pm->p[i+1],pm->color[0]);
710
 if(tikz_file) {
711
   fprintf(tikz_file, "\\draw\[%s]", tikz_options(pm->color[0],pm->fill));
712
   for(i=0;i<2*n;i+=2)
713
     fprintf(tikz_file,"(%i,%i)circle(1pt)",pm->p[i],flip(pm->p[i+1]));
714
   fprintf(tikz_file,";\n");
715
 }
10 reyssat 716
}
717
 
7675 bpr 718
/* lattice.
719
 * x0,y0,xv1,yv1,xv2,yv2,n1,n2,color */
10 reyssat 720
void obj_lattice(objparm *pm)
721
{
17914 bpr 722
 int n1,n2,i1,i2,xi1,yi1,xi2,yi2;
723
 double xv1,xv2,yv1,yv2;
724
 n1=pm->pd[6];n2=pm->pd[7]; if(n1<0 || n2<0) return;
725
 if(n1>256) n1=256;
726
 if(n2>256) n2=256;
727
 scale(pm->pd,pm->p,1);
728
 scale2(pm->pd[2],pm->pd[3],&xv1,&yv1);
729
 scale2(pm->pd[4],pm->pd[5],&xv2,&yv2);
730
 if(tikz_file)
731
   fprintf(tikz_file,"\\draw[%s]", tikz_options(pm->color[0],0));
732
 for(i1=0;i1<n1;i1++) {
733
   xi1=rint(i1*xv1)+pm->p[0]; yi1=rint(i1*yv1)+pm->p[1];
734
   for(i2=0;i2<n2;i2++) {
735
     xi2=i2*xv2+xi1;yi2=i2*yv2+yi1;
736
     gdImageSetPixel(image,xi2,yi2,pm->color[0]);
737
     if(tikz_file) fprintf(tikz_file,"(%i,%i)circle(1pt)",xi2,flip(yi2));
738
   }
739
 }
740
 if(tikz_file) fprintf(tikz_file,";\n");
10 reyssat 741
}
742
 
7675 bpr 743
/* arc */
18317 bpr 744
/*trouble with gdImageArc as the angles are of type int */
745
void myGdImageArc(gdImagePtr im, int cx, int cy, int rx, int ry, double t1, double t2, int color)
10 reyssat 746
{
18308 bpr 747
  int i,nb;
18317 bpr 748
  double dt, r=rx>ry?rx:ry;
18308 bpr 749
  t2-=t1;
18317 bpr 750
  t2-=360*floor(t2/360);
751
  t1*=DEG;t2*=DEG;
18308 bpr 752
  nb = r*t2/3;
753
  dt = t2/nb;
754
  for (i=0; i<nb; ++i,t1+=dt)
18317 bpr 755
    gdImageLine(im,cx+rx*cos(t1),cy+ry*sin(t1),
756
    cx+rx*cos(t1+dt),cy+ry*sin(t1+dt),color);
757
}
758
 
759
void obj_arc(objparm *pm)
760
{
761
  scale(pm->pd,pm->p,1);
762
  int rx=pm->pd[2]*xscale/2, ry=pm->pd[3]*yscale/2;
763
  myGdImageArc(image, pm->p[0], pm->p[1], rx, ry, pm->pd[4],pm->pd[5],pm->color[0]);
764
  if(tikz_file){
765
    pm->pd[5]-=pm->pd[4];
766
    pm->pd[5]-=360*floor(pm->pd[5]/360);
767
    pm->pd[5]+=pm->pd[4];
17914 bpr 768
    fprintf(tikz_file,
18317 bpr 769
/*    "\\draw\[%s, domain=%f:%f] plot ({%i+%f*cos(\\x)}, {%i+%f*sin(\\x)});\n",*/
770
      "\\draw \[%s] (%i,%i) arc (%f:%f:%i and %i);\n",
771
        tikz_options(pm->color[0],pm->fill),
772
        (int)rint(pm->p[0]+rx*cos(DEG*pm->pd[4])),
773
        flip((int)rint(pm->p[1]+ry*sin(DEG*pm->pd[4]))),
774
        pm->pd[4],pm->pd[5],rx,-ry);
17574 bpr 775
  }
776
  /*FIXME echelle mauvaise*/
12473 bpr 777
  if(vimg_enable) vimg_arc(scale_buf[0],scale_buf[1],
778
                     0.5*pm->pd[2],0.5*pm->pd[3],pm->pd[4],pm->pd[5]);
10 reyssat 779
}
780
 
17676 bpr 781
void obj_angle(objparm *pm)
782
{
783
  const double DEG=M_PI/180;
784
  double dpts[6];
785
  int pts[6], wx=rint(2*pm->pd[2]*xscale), wy=rint(2*pm->pd[2]*yscale);
786
  dpts[0]=pm->pd[0];
787
  dpts[1]=pm->pd[1];
788
  dpts[2]=pm->pd[0]+pm->pd[2]*cos(DEG*pm->pd[3]);
789
  dpts[3]=pm->pd[1]+pm->pd[2]*sin(DEG*pm->pd[3]);
790
  dpts[4]=pm->pd[0]+pm->pd[2]*cos(DEG*pm->pd[4]);
791
  dpts[5]=pm->pd[1]+pm->pd[2]*sin(DEG*pm->pd[4]);
792
  scale(dpts, pts, 3);
793
  gdImageLine(image,pts[0],pts[1],pts[2],pts[3],pm->color[0]);
794
  gdImageLine(image,pts[0],pts[1],pts[4],pts[5],pm->color[0]);
18308 bpr 795
  gdImageArc(image,pts[0],pts[1],wx,wy,pm->pd[3],pm->pd[4],pm->color[0]);
17676 bpr 796
  if(tikz_file) {
797
    fprintf(tikz_file, "\\draw\[%s, domain=%f:%f] plot ({%i+%f*cos(\\x)}, {%i+%f*sin(\\x)});\n",
798
      tikz_options(pm->color[0],pm->fill),pm->pd[3],pm->pd[4],pts[0],wx*0.5,flip(pts[1]),wy*(-0.5));
17950 bpr 799
    fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) (%i, %i) -- (%i, %i);\n",
800
        tikz_options(pm->color[0],pm->fill),pts[0],flip(pts[1]),pts[2],flip(pts[3]),pts[0],flip(pts[1]),pts[4],flip(pts[5]));
17676 bpr 801
  }
802
}
7675 bpr 803
/* Ellipse: centre 0,1, width 2, hight 3, color 4,5,6 */
10 reyssat 804
void obj_ellipse(objparm *pm)
805
{
12473 bpr 806
  scale(pm->pd,pm->p,1);
17676 bpr 807
  pm->p[2]=pm->pd[2]*xscale; pm->p[3]=pm->pd[3]*yscale;
18125 bpr 808
  if(pm->fill==1 || pm->fill==2) {
12473 bpr 809
    gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
810
             color_bounder);
811
    patchgdImageFillToBorder(image,pm->p[0],pm->p[1],
812
                    color_bounder,pm->color[0]);
813
  }
17676 bpr 814
  gdImageArc(image,pm->p[0],pm->p[1],rint(pm->p[2]),rint(pm->p[3]),0,360,pm->color[0]);
815
  if(tikz_file) fprintf(tikz_file,"\\draw\[%s] (%i,%i) ellipse (%i and %i);\n\n",
17912 bpr 816
    tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),(int) rint(0.5*pm->p[2]),(int) rint(0.5*pm->p[3]));
17574 bpr 817
  if(vimg_enable) vimg_ellipse(scale_buf[0],scale_buf[1],0.5*pm->pd[2],0.5*pm->pd[3]);
10 reyssat 818
}
819
 
17656 bpr 820
/* Circle radius in pixels*/
10 reyssat 821
void obj_circle(objparm *pm)
822
{
12473 bpr 823
  scale(pm->pd,pm->p,1);
824
  pm->p[2]=rint(pm->pd[2]); pm->p[3]=rint(pm->pd[2]);
18125 bpr 825
  if(pm->fill==1 || pm->fill==2) {
12473 bpr 826
    gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
827
             color_bounder);
828
    patchgdImageFillToBorder(image,pm->p[0],pm->p[1],
829
                    color_bounder,pm->color[0]);
830
  }
831
  gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,pm->color[0]);
17676 bpr 832
  if(tikz_file) fprintf(tikz_file, "\\draw\[%s] (%i, %i) circle (%i);\n",
833
      tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),(int) rint(pm->pd[2]/2.));
10 reyssat 834
}
17656 bpr 835
/* Circle, radius in xrange */
836
void obj_circles(objparm *pm)
837
{
838
  int i, n;
839
  n=(pm->pcnt)/3;
840
  for (i=0; i<n; ++i) scale(pm->pd+3*i, pm->p+3*i, 1);
17942 bpr 841
  if (tikz_file) fprintf(tikz_file,"\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
17656 bpr 842
  for(i=0;i<3*n;i+=3){
18125 bpr 843
    if(pm->fill==1 || pm->fill==2) {
18118 bpr 844
      gdImageArc(image,pm->p[i],pm->p[i+1],
845
        rint(2*pm->pd[i+2]*xscale),rint(2*pm->pd[i+2]*xscale),0,360,
846
          color_bounder);
847
      patchgdImageFillToBorder(image,pm->p[i],pm->p[i+1],
848
                    color_bounder,pm->color[0]);
849
    }
17656 bpr 850
    gdImageArc(image,pm->p[i],pm->p[i+1],
18118 bpr 851
        rint(2*pm->pd[i+2]*xscale),rint(2*pm->pd[i+2]*xscale),0,360,pm->color[0]);
17656 bpr 852
    if (tikz_file){
17942 bpr 853
      fprintf(tikz_file, "(%i, %i) circle (%i and %i)",
854
        pm->p[i],flip(pm->p[i+1]),(int) rint(pm->pd[i+2]*xscale),(int) rint(pm->pd[i+2]*xscale));
17656 bpr 855
    }
856
  }
17942 bpr 857
  if (tikz_file) fprintf(tikz_file,";\n");
17656 bpr 858
}
17676 bpr 859
/* ellipse, width in xrange,height in yrange */
17656 bpr 860
void obj_ellipses(objparm *pm)
861
{
862
  int i, n;
863
  n=(pm->pcnt)/4;
864
  for (i=0; i<n; ++i) scale(pm->pd+4*i, pm->p+4*i, 1);
17943 bpr 865
  if (tikz_file) fprintf(tikz_file,"\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
17656 bpr 866
  for(i=0;i<4*n;i+=4){
18125 bpr 867
    if(pm->fill==1 || pm->fill==2){
18123 bpr 868
      gdImageArc(image,pm->p[i],pm->p[i+1],
869
        rint(pm->pd[i+2]*xscale),rint(pm->pd[i+2]*xscale),0,360,
870
        color_bounder);
871
      patchgdImageFillToBorder(image,pm->p[i],pm->p[i+1],
872
        color_bounder,pm->color[0]);
873
    } else
874
      gdImageArc(image,pm->p[i],pm->p[i+1],rint(pm->pd[i+2]*xscale),rint(pm->pd[i+3]*yscale),0,360,pm->color[0]);
17656 bpr 875
    if (tikz_file){
17943 bpr 876
      fprintf(tikz_file, "(%i, %i) circle (%i and %i)",
877
        pm->p[i],flip(pm->p[i+1]),(int) rint(pm->pd[i+2]*xscale/2),(int) rint(pm->pd[i+3]*yscale/2));
17656 bpr 878
    }
879
  }
17943 bpr 880
  if (tikz_file) fprintf(tikz_file,";\n");
17656 bpr 881
}
17940 bpr 882
typedef enum tikz_fill_options {grid, dot, hatch, diamond} tfo;
17599 bpr 883
int compar(const void *a, const void *b) {return *(int *)b - *(int *)a;}
884
 
17662 bpr 885
static void tikz_fill(int x, int y, int nx, int ny, int wall, int the_color, tfo opt)
17599 bpr 886
{
17662 bpr 887
  int numpix=sizex*sizey, top = 0, a, c, cy, lx=0, rx=0, seed, nt,ct=0;
17599 bpr 888
  int *stack = xmalloc(numpix*sizeof(int));
889
  int *stack2 = xmalloc(numpix*sizeof(int));
18132 bpr 890
  if (nx==0) nx=1;
891
  if (ny==0) ny=1;
892
  nt = abs(nx*ny);
17599 bpr 893
  seed = gdImageGetPixel(image,x,y);
894
  char *check = xmalloc(numpix);
895
  while (numpix--) check[numpix]=0;
896
  a=x+y*sizex;
897
  check[a]=1;
898
  stack[top++]=a;
899
  while(top)
900
    {
901
      a = stack[--top];
902
      x = a%sizex; y = a/sizex;
17662 bpr 903
      c = gdImageGetPixel(image,x,y);
904
      if ((wall && (c == wall))||(!wall && c!=seed))
905
        continue;
17599 bpr 906
      if (x>0 && !check[a-1]) {check[a-1]=1; stack[top++]=a-1;}
907
      if (x+1<sizex && !check[a+1]) {check[a+1]=1; stack[top++]=a+1;}
908
      if (y>0 && !check[a-sizex]) {check[a-sizex]=1; stack[top++]=a-sizex;}
909
      if (y+1<sizey && !check[a+sizex]) {check[a+sizex]=1; stack[top++]=a+sizex;}
910
      switch(opt)
17621 bpr 911
  {
17940 bpr 912
  case grid: if(((x%nx)!=(nx/2))&&((y%ny)!=(ny/2))) continue; break;
913
  case dot: if(x%nx!=nx/2||y%ny!=ny/2) continue; break;
17939 bpr 914
  case hatch: if((ny*x-nx*y+nt)%nt) continue; break;
17940 bpr 915
  case diamond: if((ny*x+nx*y)%nt && (ny*x-nx*y+nt)%nt)continue;
17621 bpr 916
  default: break;
917
  }
17599 bpr 918
      stack2[ct++]=a;
919
    }
920
  free(stack);
921
  free(check);
922
  if (ct==0) return;
17938 bpr 923
  fprintf(tikz_file,"\\draw\[%s]", tikz_options(the_color,1));
17599 bpr 924
  qsort(stack2, ct, sizeof(int), compar);
925
  cy=-1;
926
  while (ct--)
927
    {
928
      a = stack2[ct];
929
      x = a%sizex; y = a/sizex;
930
      if (y != cy || x != rx+1)
931
      {
17621 bpr 932
  if (cy >= 0)
933
    fprintf(tikz_file,
17933 bpr 934
      "(%.2f,%.2f)rectangle(%.2f,%.2f)",
935
      lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
17621 bpr 936
  cy = y;
937
  lx = rx = x;
17599 bpr 938
      }
939
      else
17621 bpr 940
  rx++;
17599 bpr 941
    }
942
  fprintf(tikz_file,
17933 bpr 943
    "(%.2f,%.2f)rectangle(%.2f,%.2f);\n",
944
     lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
17599 bpr 945
  free(stack2);
946
}
18125 bpr 947
void obj_dashed(objparm *pm){ dashed=1;}
948
void obj_filled(objparm *pm){ filled=1;}
18159 bpr 949
void obj_noreset(objparm *pm){ noreset=1;}
950
void obj_reset(objparm *pm){ noreset=0;dashed=0;filled=0;}
17599 bpr 951
 
7675 bpr 952
/* flood fill */
10 reyssat 953
void obj_fill(objparm *pm)
954
{
12473 bpr 955
  scale(pm->pd,pm->p,1);
17940 bpr 956
  if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,0,pm->color[0],grid);
12473 bpr 957
  patchgdImageFill(image,pm->p[0],pm->p[1],pm->color[0]);
10 reyssat 958
}
959
 
7675 bpr 960
/* flood fill to border*/
10 reyssat 961
void obj_fillb(objparm *pm)
962
{
12473 bpr 963
  scale(pm->pd,pm->p,1);
17940 bpr 964
  if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,pm->color[0],pm->color[1],grid);
12473 bpr 965
  patchgdImageFillToBorder(image,pm->p[0],pm->p[1],pm->color[0],pm->color[1]);
10 reyssat 966
}
967
 
968
gdImagePtr himg;
969
 
970
int makehatchimage(int x, int y, int px, int py, int col)
971
{
12473 bpr 972
  int c1,c2,r,g,b;
973
  gdImagePtr saveimg;
974
  himg=gdImageCreate(x,y);
975
  c1=gdImageGetPixel(image,px,py);
976
  r=gdImageRed(image,c1); g=gdImageGreen(image,c1); b=gdImageBlue(image,c1);
977
  if(r>=255) r--; else r++; if(g>=255) g--; else g++; if(b>=255) b--; else b++;
978
  c1=gdImageColorAllocate(himg,r,g,b);
979
  r=gdImageRed(image,col); g=gdImageGreen(image,col); b=gdImageBlue(image,col);
980
  c2=gdImageColorAllocate(himg,r,g,b);
981
  if(width>1) {
982
    savew=-1; saveimg=image;
983
    image=himg; c2=widthcolor(width,c2); image=saveimg;
984
    c2=gdBrushed; savew=-1;
985
  }
986
  return c2;
10 reyssat 987
}
988
 
7675 bpr 989
/* flood fill with hatching */
10 reyssat 990
void obj_hatchfill(objparm *pm)
991
{
12473 bpr 992
  int nx,ny,ax,ay, dir, c;
993
  scale(pm->pd,pm->p,1);
994
  nx=pm->pd[2]; ny=pm->pd[3]; ax=abs(nx); ay=abs(ny);
995
  if(nx==0 && ny==0) {fly_error("bad displacement vector"); return;}
996
  if((nx>0 && ny>0) || (nx<0 && ny<0)) dir=1; else dir=-1;
997
  if(ax==0) {ax=100; dir=2;}
998
  if(ay==0) {ay=100; dir=3;}
999
  c=makehatchimage(ax,ay,pm->p[0],pm->p[1],pm->color[0]);
1000
  switch(dir) {
1001
    case -1: {
1002
      gdImageLine(himg,0,ay-1,ax-1,0,c);
1003
      if(width>1) {
1004
        gdImageLine(himg,-ax,ay-1,-1,0,c);
1005
        gdImageLine(himg,ax,ay-1,2*ax-1,0,c);
1006
        gdImageLine(himg,0,-1,ax-1,-ay,c);
1007
        gdImageLine(himg,0,2*ay-1,ax-1,ay,c);
7675 bpr 1008
      }
12473 bpr 1009
      break;
1010
    }
1011
    case 1: {
1012
      gdImageLine(himg,0,0,ax-1,ay-1,c);
1013
      if(width>1) {
1014
        gdImageLine(himg,-ax,0,-1,ay-1,c);
1015
        gdImageLine(himg,ax,0,2*ax-1,ay-1,c);
1016
        gdImageLine(himg,0,-ay,ax-1,-1,c);
1017
        gdImageLine(himg,0,ay,ax-1,2*ay-1,c);
7675 bpr 1018
      }
12473 bpr 1019
      break;
1020
    }
1021
    case 2: gdImageLine(himg,0,ay/2,ax-1,ay/2,c); break;
17574 bpr 1022
    case 3: gdImageLine(himg,ax/2,0,ax/2,ay-1,c); break;
12473 bpr 1023
  }
17662 bpr 1024
  if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],hatch);
12473 bpr 1025
  gdImageSetTile(image,himg);
1026
  patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
1027
  gdImageDestroy(himg);
1028
  if(tiled) gdImageSetTile(image,tileimg);
10 reyssat 1029
}
1030
 
7675 bpr 1031
/* flood fill with grid */
10 reyssat 1032
void obj_gridfill(objparm *pm)
1033
{
12473 bpr 1034
  int nx,ny, c;
1035
  scale(pm->pd,pm->p,1);
1036
  nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
1037
  if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
1038
  c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
17940 bpr 1039
  if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],grid);
12473 bpr 1040
  gdImageLine(himg,0,ny/2,nx-1,ny/2,c); gdImageLine(himg,nx/2,0,nx/2,ny-1,c);
1041
  gdImageSetTile(image,himg);
1042
  patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
1043
  gdImageDestroy(himg);
1044
  if(tiled) gdImageSetTile(image,tileimg);
10 reyssat 1045
}
1046
 
7675 bpr 1047
/* flood fill with double hatching */
10 reyssat 1048
void obj_diafill(objparm *pm)
1049
{
12473 bpr 1050
  int nx,ny, c;
1051
  scale(pm->pd,pm->p,1);
1052
  nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
1053
  if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
1054
  c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
17940 bpr 1055
  if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],diamond);
12473 bpr 1056
  gdImageLine(himg,0,0,nx-1,ny-1,c); gdImageLine(himg,0,ny-1,nx-1,0,c);
1057
  gdImageSetTile(image,himg);
1058
  patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
1059
  gdImageDestroy(himg);
1060
  if(tiled) gdImageSetTile(image,tileimg);
10 reyssat 1061
}
1062
 
17599 bpr 1063
/* flood fill with dots */
10 reyssat 1064
void obj_dotfill(objparm *pm)
1065
{
12473 bpr 1066
  int nx,ny, c;
1067
  scale(pm->pd,pm->p,1);
1068
  nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
1069
  if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
1070
  c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
17940 bpr 1071
  if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],dot);
12473 bpr 1072
  gdImageSetPixel(himg,nx/2,ny/2,c);
1073
  gdImageSetTile(image,himg);
1074
  patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
1075
  gdImageDestroy(himg);
1076
  if(tiled) gdImageSetTile(image,tileimg);
10 reyssat 1077
}
1078
 
1079
struct {
12473 bpr 1080
  char *name;
1081
  gdFontPtr *fpt;
17964 bpr 1082
  char *ftikz;
10 reyssat 1083
} fonttab[]={
17964 bpr 1084
  {"tiny",  &gdFontTiny,"0.3"},
1085
  {"small", &gdFontSmall,"0.5"},
1086
  {"medium",&gdFontMediumBold,"0.7"},
1087
  {"large", &gdFontLarge,"0.9"},
1088
  {"giant", &gdFontGiant,"1"},
1089
  {"huge",  &gdFontGiant,"1"}
10 reyssat 1090
};
1091
 
1092
#define fonttab_no (sizeof(fonttab)/sizeof(fonttab[0]))
1093
 
7675 bpr 1094
/* string */
10 reyssat 1095
void obj_string(objparm *pm)
1096
{
12473 bpr 1097
  char *pp, *pe, *p2;
1098
  int i;
1099
  pp=pm->str; pe=strchr(pp,','); if(pe==NULL) {
1100
    fly_error("too_few_parms"); return;
1101
  }
1102
  *pe++=0; pp=find_word_start(pp); *find_word_end(pp)=0;
1103
  pe=find_word_start(pe); strip_trailing_spaces(pe);
1104
  if(*pp) {
1105
    for(i=0;i<fonttab_no && strcmp(pp,fonttab[i].name)!=0; i++);
1106
    if(i>=fonttab_no) i=1;
1107
  }
1108
  else i=1;
1109
  scale(pm->pd,pm->p,1);
1110
  if(*pe=='"') {
1111
    p2=strchr(pe+1,'"');
1112
    if(p2 && *(p2+1)==0) {*p2=0; pe++;}
1113
  }
17585 bpr 1114
  if(pm->fill){
12473 bpr 1115
    gdImageStringUp(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
1116
        pm->color[0]);
17964 bpr 1117
    if(tikz_file) fprintf(tikz_file,"\\draw (%i,%i) node[scale=%s,rotate=90,color=%s,anchor=north west,inner sep=0pt] {\\(%s\\)};\n",
1118
      pm->p[0],flip(pm->p[1]),fonttab[i].ftikz,tikz_color(pm->color[0]),(unsigned char*) pe);
17585 bpr 1119
  }
1120
  else {
12473 bpr 1121
    gdImageString(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
1122
        pm->color[0]);
17964 bpr 1123
    if(tikz_file) fprintf(tikz_file,"\\draw (%i,%i) node[scale=%s,color=%s,anchor=north west,inner sep=0pt] {\\(%s\\)};\n",
1124
      pm->p[0],flip(pm->p[1]),fonttab[i].ftikz,tikz_color(pm->color[0]),(unsigned char*) pe);
17585 bpr 1125
  }
10 reyssat 1126
}
17585 bpr 1127
/*FIXME; giant does not exist in tikz and ... samething as huge */
7675 bpr 1128
/* point */
10 reyssat 1129
void obj_point(objparm *pm)
1130
{
12473 bpr 1131
  scale(pm->pd,pm->p,1);
1132
  gdImageSetPixel(image,pm->p[0],pm->p[1],pm->color[0]);
17917 bpr 1133
  if(tikz_file) fprintf(tikz_file,"\\draw[%s] (%i,%i) circle (1pt);\n",
17574 bpr 1134
    tikz_options(pm->color[0],0),pm->p[0],flip(pm->p[1]));
10 reyssat 1135
}
1136
 
7675 bpr 1137
/* copy an image file */
10 reyssat 1138
void obj_copy(objparm *pm)
1139
{
12473 bpr 1140
  char *pp;
1141
  FILE *inf;
1142
  gdImagePtr insimg;
7615 bpr 1143
 
12473 bpr 1144
  pp=find_word_start(pm->str);*find_word_end(pp)=0;
1145
  inf=open4read(pp);
1146
  if(inf==NULL) {
17599 bpr 1147
    fly_error("file_does_not_exist"); return;
12473 bpr 1148
  }
1149
  insimg=gdImageCreateFromGif(inf); fclose(inf);
1150
  if(insimg==NULL) {
1151
    fly_error("bad_gif"); return;
1152
  }
1153
  scale(pm->pd,pm->p,1);
1154
  if(pm->pd[2]<0 && pm->pd[3]<0 && pm->pd[4]<0 && pm->pd[5]<0)
1155
    gdImageCopy(image,insimg,pm->p[0],pm->p[1],0,0,
1156
            insimg->sx,insimg->sy);
1157
  else
1158
    gdImageCopy(image,insimg,pm->p[0],pm->p[1],pm->pd[2],pm->pd[3],
1159
            pm->pd[4]-pm->pd[2],pm->pd[5]-pm->pd[3]);
17587 bpr 1160
  if(tikz_file) fprintf(tikz_file,
17618 bpr 1161
    "\\node[anchor=north west,inner sep=0pt] at (%i,%i) {\\includegraphics\[width=.05\\linewidth]{%s}};\n",
17587 bpr 1162
      pm->p[0],flip(pm->p[1]),pm->str);
1163
    gdImageDestroy(insimg);
1164
/*FIXME position non correcte; on ne peut pas utiliser une image gif */
10 reyssat 1165
}
1166
 
7675 bpr 1167
/* copy an image file, with resizing */
10 reyssat 1168
void obj_copyresize(objparm *pm)
1169
{
12473 bpr 1170
  char *pp;
1171
  FILE *inf;
1172
  gdImagePtr insimg;
7615 bpr 1173
 
12473 bpr 1174
  pp=find_word_start(pm->str);*find_word_end(pp)=0;
1175
  inf=open4read(pp);
1176
  if(inf==NULL) {
1177
    fly_error("file_not_found"); return;
1178
  }
1179
  insimg=gdImageCreateFromGif(inf); fclose(inf);
1180
  if(insimg==NULL) {
1181
    fly_error("bad_gif"); return;
1182
  }
1183
  scale(pm->pd+4,pm->p+4,2);
1184
  if(pm->pd[0]<0 && pm->pd[1]<0 && pm->pd[2]<0 && pm->pd[3]<0)
1185
    gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],0,0,
1186
                 pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
1187
                 insimg->sx,insimg->sy);
1188
  else
1189
    gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],pm->pd[0],pm->pd[1],
1190
                 pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
1191
                 pm->pd[2]-pm->pd[0]+1,pm->pd[3]-pm->pd[1]+1);
1192
  gdImageDestroy(insimg);
10 reyssat 1193
}
1194
 
7675 bpr 1195
/* set brush or tile */
10 reyssat 1196
void obj_setbrush(objparm *pm)
1197
{
12473 bpr 1198
  char *pp;
1199
  FILE *inf;
1200
  gdImagePtr insimg;
10 reyssat 1201
 
12473 bpr 1202
  pp=find_word_start(pm->str); *find_word_end(pp)=0;
1203
  inf=open4read(pp); if(inf==NULL) {
1204
    fly_error("file_not_found"); return;
1205
  }
1206
  insimg=gdImageCreateFromGif(inf); fclose(inf);
1207
  if(insimg==NULL) {
1208
    fly_error("bad_gif"); return;
1209
  }
1210
  if(pm->fill) {
1211
    gdImageSetTile(image,insimg); tiled=1; tileimg=insimg;
1212
  }
1213
  else {
1214
    gdImageSetBrush(image,insimg);brushed=1; brushimg=insimg;
1215
  }
10 reyssat 1216
}
1217
 
7675 bpr 1218
/* kill brush */
10 reyssat 1219
void obj_killbrush(objparm *pm)
1220
{
12473 bpr 1221
  if(brushimg) gdImageDestroy(brushimg);
1222
  brushed=0; brushimg=NULL;
10 reyssat 1223
}
1224
 
7675 bpr 1225
/* kill tile */
10 reyssat 1226
void obj_killtile(objparm *pm)
1227
{
12473 bpr 1228
  if(tileimg) gdImageDestroy(tileimg);
1229
  tiled=0; tileimg=NULL;
10 reyssat 1230
}
1231
 
7675 bpr 1232
/* set style */
10 reyssat 1233
void obj_setstyle(objparm *pm)
1234
{
12473 bpr 1235
  int i,t;
1236
  t=pm->pcnt/3; if(t<=0) {
1237
    fly_error("too_few_parms"); return;
1238
  }
1239
  for(i=0;i<t;i++) {
1240
    if(pm->pd[3*i]<0 || pm->pd[3*i+1]<0 || pm->pd[3*i+2]<0)
1241
      pm->p[i]=gdTransparent;
1242
    else
1243
      pm->p[i]=getcolor(pm->pd[3*i],pm->pd[3*i+1],pm->pd[3*i+2]);
1244
  }
1245
  gdImageSetStyle(image,pm->p,t); styled=1;
10 reyssat 1246
}
1247
 
7675 bpr 1248
/* kill style */
10 reyssat 1249
void obj_killstyle(objparm *pm)
1250
{
12473 bpr 1251
  styled=0;
10 reyssat 1252
}
1253
 
7675 bpr 1254
/* set transparent */
10 reyssat 1255
void obj_transp(objparm *pm)
1256
{
12473 bpr 1257
  gdImageColorTransparent(image,pm->color[0]);
10 reyssat 1258
}
1259
 
7675 bpr 1260
/* set interlace */
10 reyssat 1261
void obj_interlace(objparm *pm)
1262
{
12473 bpr 1263
  gdImageInterlace(image,1);
10 reyssat 1264
}
1265
 
7675 bpr 1266
/* set linewidth */
10 reyssat 1267
void obj_linewidth(objparm *pm)
1268
{
12473 bpr 1269
  if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
17574 bpr 1270
  width=pm->pd[0];
17612 bpr 1271
  if(tikz_file) fprintf(tikz_file,"\\pgfsetlinewidth{%fpt}\n",width*0.7);
10 reyssat 1272
}
1273
 
8414 bpr 1274
/* set crosshairsize */
1275
void obj_crosshairsize(objparm *pm)
1276
{
12473 bpr 1277
  if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
1278
  else width2=pm->pd[0];
8414 bpr 1279
}
1280
 
7675 bpr 1281
/* set x range */
10 reyssat 1282
void obj_xrange(objparm *pm)
1283
{
12473 bpr 1284
  double dd;
1285
  dd=pm->pd[1]-pm->pd[0];
1286
  xstart=pm->pd[0]; xscale=sizex/dd;
10 reyssat 1287
}
1288
 
7675 bpr 1289
/* set y range */
10 reyssat 1290
void obj_yrange(objparm *pm)
1291
{
12473 bpr 1292
  double dd;
1293
  dd=pm->pd[1]-pm->pd[0];
1294
  ystart=pm->pd[1]; yscale=-sizey/dd;
10 reyssat 1295
}
1296
 
7675 bpr 1297
/* set x et y range */
10 reyssat 1298
void obj_range(objparm *pm)
1299
{
12473 bpr 1300
  double d1,d2;
1301
  d1=pm->pd[1]-pm->pd[0]; d2=pm->pd[3]-pm->pd[2];
1302
  xstart=pm->pd[0]; xscale=(sizex-1)/d1;
1303
  ystart=pm->pd[3]; yscale=-(sizey-1)/d2;
10 reyssat 1304
}
1305
 
7675 bpr 1306
/* set t range */
10 reyssat 1307
void obj_trange(objparm *pm)
1308
{
12473 bpr 1309
  /*double dd;
1310
  dd=pm->pd[1]-pm->pd[0];*/
1311
  tstart=pm->pd[0]; tend=pm->pd[1];
1312
  tranged=1;
10 reyssat 1313
}
1314
 
7675 bpr 1315
/* set tstep (plotting step) */
10 reyssat 1316
void obj_tstep(objparm *pm)
1317
{
12473 bpr 1318
  int dd;
1319
  dd=pm->pd[0];
1320
  if(dd<3) {
1321
    fly_error("bad_step"); return;
1322
  }
1323
  if(dd>2000) dd=2000;
1324
  tstep=dd;
10 reyssat 1325
}
1326
 
7675 bpr 1327
/* set plotjump (plot jump break threashold) */
10 reyssat 1328
void obj_plotjump(objparm *pm)
1329
{
12473 bpr 1330
  int dd;
1331
  dd=pm->pd[0];
1332
  if(dd<3) dd=3;
1333
  if(dd>MAX_SIZE) dd=MAX_SIZE;
1334
  plotjump=dd;
10 reyssat 1335
}
1336
 
7675 bpr 1337
/* plot a curve, either parametric or explicit */
6578 bpr 1338
void _obj_plot(objparm *pm,int dash)
10 reyssat 1339
{
12473 bpr 1340
  int i,j,n,dist,xx,yy,varpos;
1341
  char p1[MAX_LINELEN+1], p2[MAX_LINELEN+1];
1342
  char *varn, *pp;
1343
  double dc[2],t,v;
1344
  int ic[2],oc[2];
10 reyssat 1345
 
12473 bpr 1346
  n=itemnum(pm->str);
1347
  if(n<1) {
1348
    fly_error("bad_parms"); return;
1349
  }
1350
  fnd_item(pm->str,1,p1); fnd_item(pm->str,2,p2);
1351
  if(n==1 && !tranged) v=sizex/xscale/tstep;
1352
  else v=(tend-tstart)/tstep;
1353
  if(n==1) varn="x"; else varn="t";
1354
  for(pp=varchr(p1,varn); pp; pp=varchr(pp,varn)) {
1355
    string_modify(p1,pp,pp+strlen(varn),EV_T);
1356
    pp+=strlen(EV_T);
1357
  }
1358
  for(pp=varchr(p2,varn); pp; pp=varchr(pp,varn)) {
1359
    string_modify(p2,pp,pp+strlen(varn),EV_T);
7675 bpr 1360
      pp+=strlen(EV_T);
12473 bpr 1361
  }
1362
  varpos=eval_getpos(EV_T);
1363
  if(varpos<0) return; /* unknown error */
1364
  evalue_compile(p1); evalue_compile(p2);
1365
  if(vimg_enable) vimg_plotstart();
17933 bpr 1366
  if (tikz_file) fprintf(tikz_file,"\%\%begin plot\n\\draw\[%s]",
1367
    tikz_options(pm->color[0],-dash));
12473 bpr 1368
  for(i=j=0;i<=tstep;i++) {
1369
    if(n==1) {
1370
      if(tranged) t=tstart+i*v; else t=xstart+i*v;
1371
      eval_setval(varpos,t); dc[0]=t; dc[1]=strevalue(p1);
10 reyssat 1372
    }
17621 bpr 1373
    else {
1374
      t=tstart+i*v; eval_setval(varpos,t);
1375
      dc[0]=strevalue(p1); dc[1]=strevalue(p2);
10 reyssat 1376
    }
17621 bpr 1377
    if(!isfinite(dc[0]) || !isfinite(dc[1])) ic[0]=ic[1]=-BOUND;
1378
    else scale(dc,ic,1);
1379
    if(vimg_enable) vimg_plot1 (scale_buf[0],scale_buf[1]);
18019 bpr 1380
    if(j==0) {
1381
      gdImageSetPixel(image,ic[0],ic[1],pm->color[0]); j++;
1382
      if (tikz_file)
1383
        fprintf(tikz_file,"(%i,%i)rectangle(%i,%i)",
1384
          ic[0],flip(ic[1]),ic[0]+1,flip(ic[1]+1));
1385
    }
1386
    else {
1387
      xx=ic[0]-oc[0]; yy=ic[1]-oc[1];
1388
      dist=sqrt(xx*xx+yy*yy);
1389
      if(dist>0){
1390
        if(dist>plotjump || dist==1) {
1391
          gdImageSetPixel(image,ic[0],ic[1],pm->color[0]);
1392
          if (tikz_file)
1393
            fprintf(tikz_file,"(%i,%i)rectangle(%i,%i)",
1394
              ic[0],flip(ic[1]),ic[0]+1,flip(ic[1]+1));
1395
        }
1396
        else {
1397
          if (dash==1)
18105 georgesk 1398
            myDashedLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
18019 bpr 1399
          else
1400
            gdImageLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
1401
          if (tikz_file)
1402
            fprintf(tikz_file, "(%i,%i)--(%i,%i)",
17932 bpr 1403
                oc[0],flip(oc[1]),ic[0],flip(ic[1]));
18019 bpr 1404
        }
17621 bpr 1405
      }
1406
    }
18019 bpr 1407
    memmove(oc,ic,sizeof(oc));
12473 bpr 1408
  }
17932 bpr 1409
  if (tikz_file) fprintf(tikz_file,";\n\%\%end plot\n");
12473 bpr 1410
  if(vimg_enable) vimg_plotend();
10 reyssat 1411
}
1412
 
6578 bpr 1413
void obj_plot(objparm *pm) { _obj_plot( pm,0) ;}
1414
void obj_dplot(objparm *pm) { _obj_plot( pm,1) ; }
1415
 
7675 bpr 1416
/* set levelcurve granularity */
10 reyssat 1417
void obj_levelstep(objparm *pm)
1418
{
12473 bpr 1419
  int dd;
1420
  dd=pm->pd[0];
1421
  if(dd<1) return;
1422
  if(dd>16) dd=16;
1423
  lstep=dd;
10 reyssat 1424
}
1425
 
7675 bpr 1426
/* level curve */
10 reyssat 1427
void obj_levelcurve(objparm *pm)
1428
{
12473 bpr 1429
  char fn[MAX_LINELEN+1],tc[MAX_LINELEN+1];
1430
  int n,i;
1431
  double d;
1432
  leveldata *ld;
7615 bpr 1433
 
12473 bpr 1434
  ld=xmalloc(sizeof(leveldata)+16);
1435
  ld->xname="x"; ld->yname="y";
1436
  ld->xsize=sizex; ld->ysize=sizey; ld->datacnt=0;
1437
  ld->xrange[0]=xstart; ld->xrange[1]=sizex/xscale+xstart;
1438
  ld->yrange[0]=sizey/yscale+ystart; ld->yrange[1]=ystart;
1439
  ld->grain=lstep;
1440
  fnd_item(pm->str,1,fn); ld->fn=fn;
1441
  n=itemnum(pm->str);
1442
  if(n<=1) {ld->levelcnt=1; ld->levels[0]=0;}
1443
  else {
1444
    if(n>LEVEL_LIM+1) n=LEVEL_LIM+1;
1445
    for(i=0;i<n-1;i++) {
1446
      fnd_item(pm->str,i+2,tc);
1447
      d=strevalue(tc);
1448
      if(isfinite(d)) ld->levels[i]=d; else ld->levels[i]=0;
10 reyssat 1449
    }
12473 bpr 1450
    ld->levelcnt=n-1;
1451
  }
1452
  levelcurve(ld);
17932 bpr 1453
  if (tikz_file)
1454
    fprintf(tikz_file,"\%\%begin levelcurve\n\\draw\[%s]"
18065 bpr 1455
      ,tikz_options(pm->color[0],1));
12473 bpr 1456
  for(i=0;i<ld->datacnt;i++) {
1457
    gdImageSetPixel(image,ld->xdata[i],ld->ydata[i],pm->color[0]);
17584 bpr 1458
    if (tikz_file)
1459
      fprintf(tikz_file,
17933 bpr 1460
        "(%i,%i)rectangle(%i,%i)",
1461
        ld->xdata[i],flip(ld->ydata[i]),ld->xdata[i]+1,flip(ld->ydata[i]+1));
12473 bpr 1462
  }
17932 bpr 1463
  if (tikz_file) fprintf(tikz_file,";\n\%\%end levelcurve\n");
12473 bpr 1464
  free(ld);
10 reyssat 1465
}
1466
 
7675 bpr 1467
/* set animation step */
10 reyssat 1468
void obj_animstep(objparm *pm)
1469
{
12473 bpr 1470
  animstep=pm->pd[0];
1471
  setvar("animstep",pm->pd[0]);
10 reyssat 1472
}
1473
 
7675 bpr 1474
/* Starts a line counting */
10 reyssat 1475
void obj_linecount(objparm *pm)
1476
{
12473 bpr 1477
  linecnt=pm->pd[0];
10 reyssat 1478
}
1479
 
7675 bpr 1480
/* marks end of execution */
10 reyssat 1481
void obj_end(objparm *pm)
1482
{
12473 bpr 1483
  fly_error("successful_end_of_execution");
10 reyssat 1484
}
1485
 
7675 bpr 1486
/* output */
10 reyssat 1487
void obj_output(objparm *pm)
1488
{
12473 bpr 1489
  char *p, namebuf[1024];
1490
  p=find_word_start(pm->str); *find_word_end(p)=0;
1491
  snprintf(namebuf,sizeof(namebuf),"%s",imagefilename);
1492
  snprintf(imagefilename,sizeof(imagefilename),"%s",p);
1493
  output();
1494
  snprintf(imagefilename,sizeof(imagefilename),"%s",namebuf);
10 reyssat 1495
}
1496
 
7675 bpr 1497
/* vimgfile */
1024 bpr 1498
void obj_vimgfile(objparm *pm)
1499
{
12473 bpr 1500
  char *p;
1501
  p=find_word_start(pm->str); *find_word_end(p)=0;
1502
  snprintf(vimgfilename,sizeof(vimgfilename),"%s",p);
1503
  if(vimg_ready) vimg_close();
1024 bpr 1504
}
1505
 
7675 bpr 1506
/* vimg enable/disable */
1024 bpr 1507
void obj_vimg(objparm *pm)
1508
{
12473 bpr 1509
  vimg_enable=pm->pd[0];
1510
  if(vimg_enable>0 && vimg_ready==0) vimg_init();
1024 bpr 1511
}
1512
 
7675 bpr 1513
/* Set affine transformation */
10 reyssat 1514
void obj_affine(objparm *pm)
1515
{
12473 bpr 1516
  int i;
1517
  for(i=0;i<4;i++) matrix[i]=pm->pd[i];
1518
  transx=pm->pd[4]; transy=pm->pd[5];
1519
  transform=1;
10 reyssat 1520
}
1521
 
7675 bpr 1522
/* Set affine transformation */
10 reyssat 1523
void obj_rotation(objparm *pm)
1524
{
12473 bpr 1525
  double r;
1526
  r=M_PI*pm->pd[0]/180;
1527
  matrix[0]=matrix[3]=cos(r);
1528
  matrix[1]=-sin(r); matrix[2]=sin(r);
1529
  transform=1;
10 reyssat 1530
}
1531
 
8419 bpr 1532
/* Set linear transformation */
10 reyssat 1533
void obj_linear(objparm *pm)
1534
{
12473 bpr 1535
  int i;
1536
  for(i=0;i<4;i++) matrix[i]=pm->pd[i];
1537
  transform=1;
10 reyssat 1538
}
1539
 
8419 bpr 1540
/* Set translation transformation */
10 reyssat 1541
void obj_translation(objparm *pm)
1542
{
12473 bpr 1543
  transx=pm->pd[0]; transy=pm->pd[1];
10 reyssat 1544
}
1545
 
8419 bpr 1546
/* kill affine transformation */
10 reyssat 1547
void obj_killaffine(objparm *pm)
1548
{
12473 bpr 1549
  matrix[0]=matrix[3]=1;
1550
  matrix[1]=matrix[2]=transx=transy=0;
1551
  transform=0;
10 reyssat 1552
}
1553
 
8419 bpr 1554
/* kill affine transformation */
10 reyssat 1555
void obj_killlinear(objparm *pm)
1556
{
12473 bpr 1557
  matrix[0]=matrix[3]=1;
1558
  matrix[1]=matrix[2]=0;
1559
  transform=0;
10 reyssat 1560
}
1561
 
8419 bpr 1562
/* kill affine transformation */
10 reyssat 1563
void obj_killtranslation(objparm *pm)
1564
{
12473 bpr 1565
  transx=transy=0;
10 reyssat 1566
}
1567
 
1568
/***** Les modifs de Jean-Christophe Leger Fev 2006 *****/
1569
 
7615 bpr 1570
/* arguments: un numero de matrice entre 1 et JC_NB_MATRICES, une liste de 4 nombres reels pour la matrice */
10 reyssat 1571
void obj_setmatrix(objparm *pm)
1572
{
1573
  int nummatrix = (int) (pm->pd[0]);
1574
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
8195 bpr 1575
    fly_error("bad_matrix_number");
10 reyssat 1576
    return;
1577
  }
1578
  matrices_pavage[nummatrix][0] = pm->pd[1];
1579
  matrices_pavage[nummatrix][1] = pm->pd[2];
1580
  matrices_pavage[nummatrix][2] = pm->pd[3];
1581
  matrices_pavage[nummatrix][3] = pm->pd[4];
1582
}
1583
 
7615 bpr 1584
/* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
10 reyssat 1585
void obj_resetmatrix(objparm *pm)
1586
{
1587
  int nummatrix = (int) (pm->pd[0]);
1588
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
8195 bpr 1589
    fly_error("bad_matrix_number");
10 reyssat 1590
    return;
1591
  }
1592
  matrices_pavage[nummatrix][0] = 1;
1593
  matrices_pavage[nummatrix][1] = 0;
1594
  matrices_pavage[nummatrix][2] = 0;
1595
  matrices_pavage[nummatrix][3] = 1;
1596
 
1597
}
7615 bpr 1598
/* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 2 nombres reels pour le vecteur */
10 reyssat 1599
void obj_setvector(objparm *pm)
1600
{
1601
  int nummatrix = (int) (pm->pd[0]);
1602
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
8195 bpr 1603
    fly_error("bad_vector_number");
10 reyssat 1604
    return;
1605
  }
1606
  vecteurs_pavage[nummatrix][0] = pm->pd[1];
1607
  vecteurs_pavage[nummatrix][1] = pm->pd[2];
1608
}
1609
 
7615 bpr 1610
/* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
10 reyssat 1611
void obj_resetvector(objparm *pm)
1612
{
1613
  int nummatrix = (int) (pm->pd[0]);
1614
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
8195 bpr 1615
    fly_error("bad_vector_number");
10 reyssat 1616
    return;
1617
  }
1618
  vecteurs_pavage[nummatrix][0] = 0;
1619
  vecteurs_pavage[nummatrix][1] = 0;
1620
}
1621
 
7615 bpr 1622
/* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 6 nombres reels pour la matrice et le vecteur */
10 reyssat 1623
void obj_settransform(objparm *pm)
1624
{
1625
  int nummatrix = (int) (pm->pd[0]);
1626
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
8195 bpr 1627
    fly_error("bad_vector_number");
10 reyssat 1628
    return;
1629
  }
1630
  matrices_pavage[nummatrix][0] = pm->pd[1];
1631
  matrices_pavage[nummatrix][1] = pm->pd[2];
1632
  matrices_pavage[nummatrix][2] = pm->pd[3];
1633
  matrices_pavage[nummatrix][3] = pm->pd[4];
1634
  vecteurs_pavage[nummatrix][0] = pm->pd[5];
1635
  vecteurs_pavage[nummatrix][1] = pm->pd[6];
1636
}
1637
 
1638
 
7615 bpr 1639
/* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
10 reyssat 1640
void obj_resettransform(objparm *pm)
1641
{
1642
  int nummatrix = (int) (pm->pd[0]);
1643
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
8195 bpr 1644
    fly_error("bad_vector_number");
10 reyssat 1645
    return;
1646
  }
1647
  vecteurs_pavage[nummatrix][0] = 0;
1648
  vecteurs_pavage[nummatrix][1] = 0;
1649
  matrices_pavage[nummatrix][0] = 1;
1650
  matrices_pavage[nummatrix][1] = 0;
1651
  matrices_pavage[nummatrix][2] = 0;
1652
  matrices_pavage[nummatrix][3] = 1;
1653
}
1654
 
7615 bpr 1655
/* arguments: une liste de 6 nombres reels pour les coordonnees de l'origine et des vecteurs de base */
10 reyssat 1656
void obj_setparallelogram(objparm *pm)
1657
{
1658
  int i;
1659
  for(i=0;i<6;i++)  parallogram_fonda[i]=pm->pd[i];
1660
}
1661
 
1662
/* arguments :aucun */
1663
void obj_resetparallelogram(objparm *pm)
1664
{
1665
  parallogram_fonda[0]=0;
1666
  parallogram_fonda[1]=0;
1667
  parallogram_fonda[2]=100;
1668
  parallogram_fonda[3]=0;
1669
  parallogram_fonda[4]=0;
1670
  parallogram_fonda[5]=100;
1671
}
1672
 
1673
/* argument : la liste des numeros des matrices a faire agir, le nom du fichier de l'image a inclure */
1674
void obj_multicopy(objparm *pm)
1675
{
12473 bpr 1676
  char *pp,*pe,*cptr;
1677
  FILE *inf;
1678
  gdImagePtr insimg;
1679
  int i,j;
1680
  int imax,jmax;
1681
  int c; /* la couleur reperee */
1682
  int mat;
1683
  int nummatrices[JC_NB_MATRICES];
1684
  double pt[2]; /* les coordonnees math. du point repere dans le parallelogramme */
1685
  double ptr[2]; /* les coordonnees math. du point transforme */
1686
  int pti[2]; /* les coordonnees img du point a placer sur le canevas */
1687
  int t;
1688
  /* gestion palette de couleur de l'image a inserer */
1689
  int colorMap[gdMaxColors];
1690
  int comptmat=0; /* combien de matrices en tout ? */
7615 bpr 1691
 
12473 bpr 1692
  for (i=0; (i<gdMaxColors); i++) {
1693
    colorMap[i] = (-1);
1694
  }
10 reyssat 1695
 
12473 bpr 1696
  /* Gestion des parametres la liste est censee finir avec le nom du fichier */
1697
  t=pm->pcnt-1; /* il faut enlever le nom de fichier ! c'est le nombre de parametres en plus */
1698
  pp=find_word_start(pm->str);
1699
  /* visiblement find_word_start n'arrive pas a trouver le dernier mot dans un contexte ou le nombre de parameters est variable
10 reyssat 1700
     * on cherche donc l'emplacement de la derniere virgule:
1701
     * il y en a une car t>0, on retrouve ensuite le debut de mot
1702
     */
12473 bpr 1703
  for(pe=pp;*pe!=0;pe++);/* trouve la fin de la chaine */
1704
  if(t>0){
1705
    for(cptr = pe; cptr > pp && *cptr != ','; cptr--);
1706
    pp=find_word_start(cptr+1);
1707
  }
1708
  pe=find_word_end(pp);*pe=0; /* strip junk final */
1709
  //      printf("pp contient %s\n",pp);
10 reyssat 1710
 
1711
 
12473 bpr 1712
  inf=open4read(pp);
1713
  if(inf==NULL) {
1714
    fly_error("file_not_exist"); return;
1715
  }
1716
  insimg=gdImageCreateFromGif(inf); fclose(inf);
1717
  if(insimg==NULL) {
1718
    fly_error("bad_gif"); return;
1719
  }
10 reyssat 1720
 
12473 bpr 1721
  /* On recupere les numeros des matrices/vecteurs a faire agir,
1722
    * s'il n'y en a pas, on les fait toutes agir
1723
  */
1724
  for(i=0;i<t && i< JC_NB_MATRICES;i++) {
1725
    if(pm->pd[i]>=1 && pm->pd[i]<=JC_NB_MATRICES){
1726
      nummatrices[comptmat] = pm->pd[i];
1727
      comptmat++;
10 reyssat 1728
    }
12473 bpr 1729
  }
1730
  if(t<=0){
1731
    for(i=0;i<JC_NB_MATRICES;i++) {
1732
      nummatrices[i] = i+1;
10 reyssat 1733
    }
12473 bpr 1734
    comptmat=JC_NB_MATRICES;
1735
  }
10 reyssat 1736
 
7615 bpr 1737
 
12473 bpr 1738
  imax = gdImageSX(insimg);
1739
  jmax = gdImageSY(insimg);
10 reyssat 1740
 
12473 bpr 1741
  for(i=0;i<imax;i++){
1742
    for(j=0;j<jmax;j++){
1743
      int nc;
1744
      c=gdImageGetPixel(insimg,i,j);
1745
      /* Le code suivant est une copie du code dans gdImageCopy  Added 7/24/95: support transparent copies */
1746
      if (gdImageGetTransparent(insimg) != c) {
1747
        /* Have we established a mapping for this color? */
1748
        if (colorMap[c] == (-1)) {
1749
          /* If it's the same image, mapping is trivial, dans notre cas ca n'arrive jamais... */
1750
          if (image == insimg) {
1751
            nc = c;
1752
          } else {
1753
            /* First look for an exact match */
1754
            nc = gdImageColorExact(image,
1755
                   insimg->red[c], insimg->green[c],
1756
                           insimg->blue[c]);
1757
          }
1758
          if (nc == (-1)) {
1759
          /* No, so try to allocate it */
1760
            nc = gdImageColorAllocate(image,
1761
                             insimg->red[c], insimg->green[c],
7675 bpr 1762
                             insimg->blue[c]);
12473 bpr 1763
             /* If we're out of colors, go for the closest color */
1764
            if (nc == (-1)) {
1765
              nc = gdImageColorClosest(image,
7675 bpr 1766
                              insimg->red[c], insimg->green[c],
1767
                              insimg->blue[c]);
12473 bpr 1768
            }
1769
          }
1770
          colorMap[c] = nc;
1771
        }
10 reyssat 1772
 
12473 bpr 1773
        pt[0]= i*(parallogram_fonda[2])/(imax-1)+(jmax-j)*(parallogram_fonda[4])/(jmax-1)+parallogram_fonda[0];
1774
        pt[1]= i*(parallogram_fonda[3])/(imax-1)+(jmax-j)*(parallogram_fonda[5])/(jmax-1)+parallogram_fonda[1];
1775
        for(mat=0;mat<comptmat;mat++){
7675 bpr 1776
          double *matricecourante = matrices_pavage[nummatrices[mat]];
1777
          double *vecteurcourant = vecteurs_pavage[nummatrices[mat]];
1778
          ptr[0] = matricecourante[0]*pt[0]+matricecourante[1]*pt[1]+vecteurcourant[0];
1779
          ptr[1] = matricecourante[2]*pt[0]+matricecourante[3]*pt[1]+vecteurcourant[1];
1780
          scale(ptr,pti,1);
1781
          gdImageSetPixel(image,pti[0],pti[1],colorMap[c]);
12473 bpr 1782
        }
10 reyssat 1783
      }
1784
    }
12473 bpr 1785
  }
1786
  gdImageDestroy(insimg);
10 reyssat 1787
}
1788
 
1789
/**** Fin modifs JC Fev 06 ******/
1790
 
7675 bpr 1791
/* get color from value */
10 reyssat 1792
int calc_color(char *p, struct objtab *o)
1793
{
12473 bpr 1794
  int k, cc[3];
1795
  char buf[MAX_LINELEN+1];
1796
  for(k=0;k<3;k++) {
1797
    fnd_item(p,k+1,buf);
1798
    cc[k]=strevalue(buf);
1799
  }
1800
  collapse_item(p,3);
1801
  if(cc[0]==-1 && cc[1]==-1 && cc[2]==-255) {
10 reyssat 1802
 
12473 bpr 1803
    if(brushed && o->subst&1) return gdBrushed;
1804
    else return color_black;
1805
  }
1806
  if(cc[0]==-1 && cc[1]==-255 && cc[2]==-1) {
1807
    if(styled && o->subst&2)  return gdStyled;
1808
    else return color_black;
1809
  }
1810
  if(cc[0]==-255 && cc[1]==-1 && cc[2]==-1) {
1811
    if(tiled && o->fill_tag==1)  return gdTiled;
1812
    else return color_black;
1813
  }
1814
  return getcolor(cc[0],cc[1],cc[2]);
10 reyssat 1815
}
1816
 
7675 bpr 1817
/* Routine to parse parameters */
10 reyssat 1818
int parse_parms(char *p,objparm *pm,struct objtab *o)
1819
{
12473 bpr 1820
  char buf[MAX_LINELEN+1];
1821
  char *p1, *p2;
1822
  int j,t,c,c1,c2;
7615 bpr 1823
 
12473 bpr 1824
  c=o->color_pos;c1=c2=0;
1825
  pm->color[0]=pm->color[1]=0;
1826
  if(c>0) c1=c;
1827
  if(c<0) c2=-c;
1828
  c=c1+c2;
1829
  t=itemnum(p);if(t<o->required_parms+3*c) return -1;
1830
  if(c1>0 && t>o->required_parms+3*c) t=o->required_parms+3*c;
1831
  pm->pcnt=t-3*c;
1832
  if(pm->pcnt>MAX_PARMS) pm->pcnt=MAX_PARMS;
1833
  if(c2>0) {
1834
    for(j=0;j<2 && j<c2; j++) pm->color[j]=calc_color(p,o);
1835
  }
1836
  snprintf(buf,sizeof(buf),"%s",p);
1837
  for(j=0, p1=buf; j<pm->pcnt; j++, p1=p2) {
1838
    p2=find_item_end(p1); if(*p2) *p2++=0;
1839
    p1=find_word_start(p1);
1840
    if(*p1) pm->pd[j]=strevalue(p1); else pm->pd[j]=0;
1841
    if(!isfinite(pm->pd[j])) {
1842
        if(j<o->required_parms) return -1;
1843
        else {pm->pd[j]=0;break;}
10 reyssat 1844
    }
12473 bpr 1845
  }
1846
  collapse_item(p,o->required_parms);
1847
  if(c1>0) {
1848
    for(j=0;j<c1 && j<2; j++) pm->color[j]=calc_color(p,o);
1849
  }
1850
  if(width>1 && o->subst&1 && pm->color[0]!=gdBrushed
1851
      && pm->color[0]!=gdStyled && pm->color[0]!=gdTiled) {
1852
    pm->color[1]=pm->color[0];
1853
    pm->color[0]=widthcolor(width,pm->color[0]);
1854
  }
1855
  pm->fill=o->fill_tag;
18131 bpr 1856
  if(o->required_parms!=0){
1857
    if(dashed) {
1858
      if(pm->fill==1) pm->fill=2;
1859
      if(pm->fill==0) pm->fill=-1;
18159 bpr 1860
       if(noreset==0) dashed=0;
18131 bpr 1861
    }
1862
    if(filled) {
1863
      if(pm->fill==-1) pm->fill=2;
1864
      if(pm->fill==0) pm->fill=1;
18159 bpr 1865
      if(noreset==0) filled=0;
18131 bpr 1866
    }
1867
  }
12473 bpr 1868
  ovlstrcpy(pm->str,p); return 0;
10 reyssat 1869
}
1870
 
8068 bpr 1871
/* Create the struct objparm pm corresponding to the objparm p
1872
 * Execute a command. Returns 0 if OK.
1873
 */
10 reyssat 1874
int obj_main(char *p)
1875
{
12473 bpr 1876
  int i;
1877
  char *pp,*name,*pt;
1878
  char tbuf2[MAX_LINELEN+1];
1879
  struct objparm pm;
10 reyssat 1880
 
12473 bpr 1881
  p=find_word_start(p);
1882
  if(*p==exec_prefix_char || *p==0) return 0; /* comment */
1883
  name=p;
1884
  pp=find_name_end(p);
1885
  pt=find_word_start(pp); if(*pt=='=') *pt=' ';
1886
  if(*pt==':' && *(pt+1)=='=') *pt=*(pt+1)=' ';
1887
  pp=find_word_end(p);
1888
  if(*pp!=0) {
1889
    *(pp++)=0; pp=find_word_start(pp);
1890
  }
1891
  if(strlen(p)==1 || (strlen(p)==2 && isdigit(*(p+1)))) {
1892
    setvar(p,strevalue(pp)); return 0;
1893
  }
1894
  /* search the number of the object */
1895
  i=search_list(objtab,obj_no,sizeof(objtab[0]),name);
1896
  if(i<0) {
1897
    fly_error("bad_cmd"); return 1;
1898
  }
1899
  if(image==NULL && (objtab[i].color_pos || objtab[i].required_parms>2)) {
1900
    fly_error("image_not_defined"); return 1;
1901
  }
1902
  ovlstrcpy(tbuf2,pp);
1903
  if(objtab[i].color_pos || objtab[i].routine==obj_setstyle) {
1904
    substit(tbuf2);
1905
  }
1906
  if(parse_parms(tbuf2,&pm,objtab+i)!=0) fly_error("bad_parms");
1907
  else objtab[i].routine(&pm);
1908
  return 0;
10 reyssat 1909
}