Subversion Repositories wimsdev

Rev

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

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