Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is to exceed 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - graph - plotport.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23694-b3ccec097) Lines: 1169 1340 87.2 %
Date: 2019-03-20 05:44:21 Functions: 108 125 86.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*                         PLOT ROUTINES                           */
      17             : /*                                                                 */
      18             : /*******************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : #include "rect.h"
      22             : 
      23             : static void (*pari_get_plot)(PARI_plot *);
      24             : 
      25             : /* no need for THREAD: OK to share this */
      26             : static hashtable *rgb_colors = NULL;
      27             : 
      28             : THREAD PariRect rectgraph[18]; /*NUMRECT*/
      29             : static THREAD long current_color[18]; /*NUMRECT*/
      30             : 
      31             : static long plotpoint_itype = 0, rectline_itype  = 0;
      32             : 
      33             : const long NUMRECT = 18;
      34             : const long RECUR_MAXDEPTH = 10;
      35             : const double RECUR_PREC = 0.001;
      36             : const long DEFAULT_COLOR = 1, AXIS_COLOR = 2;
      37             : 
      38             : enum {
      39             :   ROt_MV = 1, /* Move */
      40             :   ROt_PT,  /* Point */
      41             :   ROt_LN,  /* Line */
      42             :   ROt_BX,  /* Box */
      43             :   ROt_FBX, /* Filled Box */
      44             :   ROt_MP,  /* Multiple point */
      45             :   ROt_ML,  /* Multiple lines */
      46             :   ROt_ST,  /* String */
      47             :   ROt_PTT,  /* Point type change */
      48             :   ROt_LNT,  /* Line type change */
      49             :   ROt_PTS,  /* Point size change */
      50             :   ROt_NULL, /* To be the start of the chain */
      51             : };
      52             : 
      53             : /* string justification */
      54             : #define RoSTdirLEFT       0x00
      55             : #define RoSTdirRIGHT      0x02
      56             : #define RoSTdirHPOS_mask  0x03
      57             : #define RoSTdirBOTTOM     0x00
      58             : #define RoSTdirTOP        0x08
      59             : #define RoSTdirVPOS_mask  0x0c
      60             : #define RoSTdirHGAP       0x10
      61             : #define RoSTdirVGAP       0x20
      62             : 
      63             : /* ploth flags */
      64             : #define PLOT_PARAMETRIC   0x00001
      65             : #define PLOT_RECURSIVE    0x00002
      66             : #define PLOT_NO_RESCALE   0x00004
      67             : #define PLOT_NO_AXE_X     0x00008
      68             : #define PLOT_NO_AXE_Y     0x00010
      69             : #define PLOT_NO_FRAME     0x00020
      70             : #define PLOT_POINTS       0x00040
      71             : #define PLOT_POINTS_LINES 0x00080
      72             : #define PLOT_SPLINES      0x00100
      73             : #define PLOT_NO_TICK_X    0x00200
      74             : #define PLOT_NO_TICK_Y    0x00400
      75             : #define PLOT_NODOUBLETICK 0x00800
      76             : #define PLOT_COMPLEX      0x01000
      77             : #define PLOT_PARA         0x02000
      78             : 
      79             : INLINE long
      80       70719 : DTOL(double t) { return (long)(t + 0.5); }
      81             : 
      82             : static const long PS_WIDTH = 1120 - 60; /* 1400 - 60 for hi-res */
      83             : static const long PS_HEIGH = 800 - 40; /* 1120 - 60 for hi-res */
      84             : 
      85             : static void
      86           0 : _psdraw_scale(PARI_plot *T, GEN w, GEN x, GEN y)
      87             : {
      88           0 :   pari_sp av = avma;
      89           0 :   FILE *F = fopen(current_psfile, "a");
      90           0 :   if (!F) pari_err_FILE("postscript file",current_psfile);
      91           0 :   fputs(rect2ps(w,x,y,T), F);
      92           0 :   fclose(F); set_avma(av);
      93           0 : }
      94             : static void
      95           0 : _psdraw(PARI_plot *T, GEN w, GEN x, GEN y)
      96           0 : { (void)T; _psdraw_scale(NULL,w,x,y); }
      97             : static void
      98          49 : pari_get_psplot(PARI_plot *T)
      99             : {
     100          49 :   T->width  = PS_WIDTH;
     101          49 :   T->height = PS_HEIGH;
     102          49 :   T->fheight= 15;
     103          49 :   T->fwidth = 6;
     104          49 :   T->hunit  = 5;
     105          49 :   T->vunit  = 5;
     106          49 :   T->dwidth = 0;
     107          49 :   T->dheight= 0;
     108          49 :   T->draw = NULL;
     109          49 : }
     110             : static void
     111          58 : pari_get_svgplot(PARI_plot *T)
     112             : {
     113          58 :   T->width   = 480;
     114          58 :   T->height  = 320;
     115          58 :   T->fheight = 12;
     116          58 :   T->fwidth  = 6;
     117          58 :   T->hunit   = 3;
     118          58 :   T->vunit   = 3;
     119          58 :   T->dwidth  = 0;
     120          58 :   T->dheight = 0;
     121          58 :   T->draw = NULL;
     122          58 : }
     123             : 
     124             : /********************************************************************/
     125             : /**                                                                **/
     126             : /**                      RECTPLOT FUNCTIONS                        **/
     127             : /**                                                                **/
     128             : /********************************************************************/
     129             : static void
     130           0 : get_plot_null(PARI_plot *T)
     131           0 : { (void)T; pari_err(e_MISC,"high resolution graphics disabled"); }
     132             : 
     133             : void
     134        1566 : pari_init_graphics(void) { pari_get_plot = &get_plot_null; }
     135             : 
     136             : void
     137        1556 : pari_set_plot_engine(void (*plot)(PARI_plot *))
     138             : {
     139             :   long n;
     140        1556 :   pari_get_plot = plot;
     141       29564 :   for (n = 0; n < NUMRECT; n++)
     142             :   {
     143       28008 :     PariRect *e = &rectgraph[n];
     144       28008 :     RHead(e) = RTail(e) = NULL;
     145       28008 :     RXsize(e) = RYsize(e) = 0;
     146             :   }
     147        1556 : }
     148             : 
     149             : void
     150        1556 : pari_kill_plot_engine(void)
     151             : {
     152             :   int i;
     153       29564 :   for (i=0; i<NUMRECT; i++)
     154             :   {
     155       28008 :     PariRect *e = &rectgraph[i];
     156       28008 :     if (RHead(e)) plotkill(i);
     157             :   }
     158        1556 :   if (rgb_colors)
     159             :   {
     160          18 :     pari_free((void*)rgb_colors->table);
     161          18 :     pari_free((void*)rgb_colors);
     162             :   }
     163        1556 : }
     164             : 
     165             : static PariRect *
     166       23012 : check_rect(long ne)
     167             : {
     168       23012 :   const char *f = "graphic function";
     169       23012 :   const long m = NUMRECT-1;
     170       23012 :   if (ne < 0) pari_err_DOMAIN(f, "rectwindow", "<", gen_0, stoi(ne));
     171       23004 :   if (ne > m) pari_err_DOMAIN(f, "rectwindow", ">", stoi(m), stoi(ne));
     172       23000 :   return &rectgraph[ne];
     173             : }
     174             : 
     175             : static PariRect *
     176       22769 : check_rect_init(long ne)
     177             : {
     178       22769 :   PariRect *e = check_rect(ne);
     179       22761 :   if (!RHead(e)) pari_err_TYPE("graphic function [use plotinit()]", stoi(ne));
     180       22761 :   return e;
     181             : }
     182             : static void
     183       30052 : Rchain(PariRect *e, RectObj *z)
     184             : {
     185       30052 :   if (!RHead(e)) RHead(e) = z; else RoNext(RTail(e)) = z;
     186       30052 :   RTail(e) = z;
     187       30052 :   RoNext(z) = NULL;
     188       30052 : }
     189             : 
     190             : static long
     191       10539 : rgb_to_long(long r, long g, long b)
     192       10539 : { return (r << 16) | (g << 8) | b; }
     193             : /* c from graphcolormap */
     194             : static long
     195       10535 : colormap_to_color(long i)
     196             : {
     197       10535 :   GEN c = GP_DATA->colormap;
     198       10535 :   long k = i+1, l = lg(c)-1;
     199             :   int r, g, b;
     200       10535 :   if (k > l)
     201           4 :     pari_err_COMPONENT("graphcolormap",">", stoi(l), stoi(k));
     202       10531 :   color_to_rgb(gel(c, k), &r,&g,&b);
     203       10531 :   return rgb_to_long(r, g, b);
     204             : }
     205             : 
     206             : static void
     207         235 : initrect_i(long ne, long x, long y)
     208             : {
     209             :   PariRect *e;
     210             :   RectObj *z;
     211             : 
     212         235 :   if (x <= 1) pari_err_DOMAIN("plotinit", "x", "<=", gen_1, stoi(x));
     213         235 :   if (y <= 1) pari_err_DOMAIN("plotinit", "y", "<=", gen_1, stoi(y));
     214         235 :   e = check_rect(ne); if (RHead(e)) plotkill(ne);
     215             : 
     216         231 :   current_color[ne] = colormap_to_color(DEFAULT_COLOR);
     217         231 :   z = (RectObj*) pari_malloc(sizeof(RectObj));
     218         231 :   RoType(z) = ROt_NULL;
     219         231 :   Rchain(e, z);
     220         231 :   RXsize(e) = x; RXcursor(e) = 0; RXscale(e) = 1; RXshift(e) = 0;
     221         231 :   RYsize(e) = y; RYcursor(e) = 0; RYscale(e) = 1; RYshift(e) = 0;
     222         231 : }
     223             : static long
     224         134 : initrect_get_arg(GEN x, long dft)
     225             : {
     226         134 :   if (!x) return dft;
     227          78 :   if (typ(x) != t_INT) pari_err_TYPE("plotinit",x);
     228          78 :   return itos(x);
     229             : }
     230             : void
     231          67 : plotinit(long ne, GEN x, GEN y, long flag)
     232             : {
     233          67 :   const long m = NUMRECT-3;
     234             :   long xi, yi;
     235             :   PARI_plot T;
     236             : 
     237          67 :   if (flag)
     238             :   {
     239           0 :     pari_get_plot(&T);
     240           0 :     xi = T.width -1; if (x) xi = DTOL(xi * gtodouble(x));
     241           0 :     yi = T.height-1; if (y) yi = DTOL(yi * gtodouble(y));
     242             :   }
     243             :   else
     244             :   {
     245          67 :     if (!x || !y) pari_get_plot(&T);
     246          67 :     xi = initrect_get_arg(x, T.width -1);
     247          67 :     yi = initrect_get_arg(y, T.height-1);
     248             :   }
     249          67 :   if (ne > m) pari_err_DOMAIN("plotinit", "rectwindow", ">", stoi(m), stoi(ne));
     250          63 :   initrect_i(ne, xi, yi);
     251          59 : }
     252             : 
     253             : GEN
     254          47 : plotcursor(long ne)
     255             : {
     256          47 :   PariRect *e = check_rect_init(ne);
     257          47 :   return mkvec2s((long)RXcursor(e), (long)RYcursor(e));
     258             : }
     259             : 
     260             : static void
     261         104 : plotscale0(long ne, double x1, double x2, double y1, double y2)
     262             : {
     263         104 :   PariRect *e = check_rect_init(ne);
     264             :   double x, y;
     265             : 
     266         104 :   x = RXshift(e) + RXscale(e) * RXcursor(e);
     267         104 :   y = RYshift(e) + RYscale(e) * RYcursor(e);
     268         104 :   RXscale(e) = RXsize(e)/(x2-x1); RXshift(e) = -x1*RXscale(e);
     269         104 :   RYscale(e) = RYsize(e)/(y1-y2); RYshift(e) = -y2*RYscale(e);
     270         104 :   RXcursor(e) = (x - RXshift(e)) / RXscale(e);
     271         104 :   RYcursor(e) = (y - RYshift(e)) / RYscale(e);
     272         104 : }
     273             : void
     274          18 : plotscale(long ne, GEN x1, GEN x2, GEN y1, GEN y2)
     275          18 : { plotscale0(ne, gtodouble(x1), gtodouble(x2), gtodouble(y1), gtodouble(y2)); }
     276             : 
     277             : static void
     278         711 : plotmove0(long ne, double x, double y, long relative)
     279             : {
     280         711 :   PariRect *e = check_rect_init(ne);
     281         703 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj1P));
     282             : 
     283         703 :   if (relative) { RXcursor(e) += x; RYcursor(e) += y; }
     284         685 :   else          { RXcursor(e) = x; RYcursor(e) = y; }
     285         703 :   RoType(z) = ROt_MV;
     286         703 :   RoMVx(z) = RXcursor(e) * RXscale(e) + RXshift(e);
     287         703 :   RoMVy(z) = RYcursor(e) * RYscale(e) + RYshift(e);
     288         703 :   Rchain(e, z);
     289         703 : }
     290             : static void
     291         574 : _move(long ne, double x, double y)
     292         574 : { plotmove0(ne,x,y,0); }
     293             : void
     294         119 : plotmove(long ne, GEN x, GEN y)
     295         119 : { plotmove0(ne,gtodouble(x),gtodouble(y),0); }
     296             : void
     297          18 : plotrmove(long ne, GEN x, GEN y)
     298          18 : { plotmove0(ne,gtodouble(x),gtodouble(y),1); }
     299             : 
     300             : /* ROt_MV/ROt_PT */
     301             : static void
     302          36 : plotpoint0(long ne, double x, double y,long relative)
     303             : {
     304          36 :   PariRect *e = check_rect_init(ne);
     305          36 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj1P));
     306             : 
     307          36 :   if (relative) { RXcursor(e) += x; RYcursor(e) += y; }
     308          18 :   else          { RXcursor(e) = x; RYcursor(e) = y; }
     309          36 :   RoPTx(z) = RXcursor(e)*RXscale(e) + RXshift(e);
     310          36 :   RoPTy(z) = RYcursor(e)*RYscale(e) + RYshift(e);
     311          72 :   RoType(z) = ( DTOL(RoPTx(z)) < 0
     312          36 :                 || DTOL(RoPTy(z)) < 0 || DTOL(RoPTx(z)) > RXsize(e)
     313          72 :                 || DTOL(RoPTy(z)) > RYsize(e) ) ? ROt_MV : ROt_PT;
     314          36 :   Rchain(e, z);
     315          36 :   RoCol(z) = current_color[ne];
     316          36 : }
     317             : static void
     318          18 : plotpoint(long ne, GEN x, GEN y)
     319          18 : { plotpoint0(ne,gtodouble(x),gtodouble(y),0); }
     320             : void
     321          18 : plotrpoint(long ne, GEN x, GEN y)
     322          18 : { plotpoint0(ne,gtodouble(x),gtodouble(y),1); }
     323             : 
     324             : GEN
     325           8 : plotcolor(long ne, GEN c)
     326             : {
     327           8 :   long t = typ(c), n = lg(GP_DATA->colormap)-2;
     328             :   int r, g, b;
     329           8 :   check_rect(ne);
     330           8 :   if (t == t_INT)
     331             :   {
     332           8 :     long i = itos(c);
     333           8 :     if (i < 0) pari_err_DOMAIN("plotcolor", "color", "<", gen_0, c);
     334           8 :     if (i > n) pari_err_DOMAIN("plotcolor", "color", ">", stoi(n), c);
     335           8 :     c = gel(GP_DATA->colormap,i+1);
     336             :   }
     337             :   else
     338             :   {
     339           0 :     if (t == t_VEC) { c = ZV_to_zv(c); t = typ(c); }
     340           0 :     if (t != t_VECSMALL && t != t_STR) pari_err_TYPE("plotcolor",c);
     341             :   }
     342           8 :   color_to_rgb(c, &r,&g,&b);
     343           8 :   current_color[ne] = rgb_to_long(r,g,b);
     344           8 :   return mkvec3s(r, g, b);
     345             : }
     346             : 
     347             : /* ROt_MV/ROt_LN */
     348             : static void
     349         180 : rectline0(long ne, double gx2, double gy2, long relative)
     350             : {
     351             :   double dx, dy, dxy, xmin, xmax, ymin, ymax, x1, y1, x2, y2;
     352         180 :   PariRect *e = check_rect_init(ne);
     353         180 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj2P));
     354         180 :   const double c = 1 + 1e-10;
     355             : 
     356         180 :   x1 = RXcursor(e)*RXscale(e) + RXshift(e);
     357         180 :   y1 = RYcursor(e)*RYscale(e) + RYshift(e);
     358         180 :   if (relative)
     359          18 :     { RXcursor(e)+=gx2; RYcursor(e)+=gy2; }
     360             :   else
     361         162 :     { RXcursor(e)=gx2; RYcursor(e)=gy2; }
     362         180 :   x2 = RXcursor(e)*RXscale(e) + RXshift(e);
     363         180 :   y2 = RYcursor(e)*RYscale(e) + RYshift(e);
     364         180 :   xmin = maxdd(mindd(x1,x2),0); xmax = mindd(maxdd(x1,x2),RXsize(e));
     365         180 :   ymin = maxdd(mindd(y1,y2),0); ymax = mindd(maxdd(y1,y2),RYsize(e));
     366         180 :   dxy = x1*y2 - y1*x2; dx = x2-x1; dy = y2-y1;
     367         180 :   if (dy)
     368             :   {
     369         108 :     double a = (dxy + RYsize(e)*dx) / dy, b = dxy / dy;
     370         108 :     if (dx*dy < 0)
     371          18 :     { xmin=maxdd(xmin,a); xmax=mindd(xmax,b); }
     372             :     else
     373          90 :     { xmin=maxdd(xmin,b); xmax=mindd(xmax,a); }
     374             :   }
     375         180 :   if (dx)
     376             :   {
     377         108 :     double a = (RXsize(e)*dy - dxy) / dx, b = -dxy / dx;
     378         108 :     if (dx*dy < 0)
     379          18 :     { ymin=maxdd(ymin,a); ymax=mindd(ymax,b); }
     380             :     else
     381          90 :     { ymin=maxdd(ymin,b); ymax=mindd(ymax,a); }
     382             :   }
     383         180 :   RoLNx1(z) = xmin;
     384         180 :   RoLNx2(z) = xmax;
     385         180 :   if (dx*dy < 0) { RoLNy1(z) = ymax; RoLNy2(z) = ymin; }
     386         162 :   else         { RoLNy1(z) = ymin; RoLNy2(z) = ymax; }
     387         180 :   RoType(z) = (xmin>xmax*c || ymin>ymax*c) ? ROt_MV : ROt_LN;
     388         180 :   Rchain(e, z);
     389         180 :   RoCol(z) = current_color[ne];
     390         180 : }
     391             : static void
     392         144 : _line(long ne, double x, double y)
     393         144 : { rectline0(ne, x, y, 0); }
     394             : void
     395          18 : plotline(long ne, GEN gx2, GEN gy2)
     396          18 : { rectline0(ne, gtodouble(gx2), gtodouble(gy2),0); }
     397             : void
     398          18 : plotrline(long ne, GEN gx2, GEN gy2)
     399          18 : { rectline0(ne, gtodouble(gx2), gtodouble(gy2),1); }
     400             : 
     401             : enum {
     402             :   TICKS_CLOCKW   = 1, /* Draw in clockwise direction */
     403             :   TICKS_ACLOCKW  = 2, /* Draw in anticlockwise direction */
     404             :   TICKS_ENDSTOO  = 4, /* Draw at endspoints if needed */
     405             :   TICKS_NODOUBLE = 8  /* Do not draw double-length ticks */
     406             : };
     407             : 
     408             : /* Given coordinates of ends of a line, and labels l1 l2 attached to the
     409             :  * ends, plot ticks where the label coordinate takes "round" values */
     410             : static void
     411         344 : rectticks(PARI_plot *WW, long ne, double dx1, double dy1, double dx2,
     412             :           double dy2, double l1, double l2, long flags)
     413             : {
     414             :   long dx, dy, dxy, dxy1, x1, y1, x2, y2, nticks, n, n1, dn;
     415             :   double minstep, maxstep, step, l_min, l_max, minl, maxl, dl, dtx, dty, x, y;
     416             :   double ddx, ddy;
     417         344 :   const double mult[3] = { 2./1., 5./2., 10./5. };
     418         344 :   PariRect *e = check_rect_init(ne);
     419         344 :   int do_double = !(flags & TICKS_NODOUBLE);
     420             : 
     421         344 :   x1 = DTOL(dx1*RXscale(e) + RXshift(e));
     422         344 :   y1 = DTOL(dy1*RYscale(e) + RYshift(e));
     423         344 :   x2 = DTOL(dx2*RXscale(e) + RXshift(e));
     424         344 :   y2 = DTOL(dy2*RYscale(e) + RYshift(e));
     425         344 :   dx = x2 - x1; if (dx < 0) dx = -dx;
     426         344 :   dy = y2 - y1; if (dy < 0) dy = -dy;
     427         344 :   dxy1 = maxss(dx, dy);
     428         344 :   dx /= WW->hunit;
     429         344 :   dy /= WW->vunit;
     430         344 :   if (dx > 1000 || dy > 1000)
     431           0 :     dxy = 1000; /* avoid overflow */
     432             :   else
     433         344 :     dxy = usqrt(dx*dx + dy*dy);
     434         344 :   nticks = (long) ((dxy + 2.5)/4);
     435         344 :   if (!nticks) return;
     436             : 
     437             :   /* Find nticks (or less) "round" numbers between l1 and l2. For our purpose
     438             :    * round numbers have "last significant" decimal digit either
     439             :    *    - any;
     440             :    *    - even;
     441             :    *    - divisible by 5.
     442             :    * We need to choose which alternative is better. */
     443         344 :   if (l1 < l2)
     444         172 :     l_min = l1, l_max = l2;
     445             :   else
     446         172 :     l_min = l2, l_max = l1;
     447         344 :   minstep = (l_max - l_min)/(nticks + 1);
     448         344 :   maxstep = 2.5*(l_max - l_min);
     449         344 :   step = exp(log(10.) * floor(log10(minstep)));
     450         344 :   if (!(flags & TICKS_ENDSTOO)) {
     451         344 :     double d = 2*(l_max - l_min)/dxy1; /* Two pixels off */
     452         344 :     l_min += d;
     453         344 :     l_max -= d;
     454             :   }
     455        1082 :   for (n = 0; ; n++)
     456             :   {
     457        1820 :     if (step >= maxstep) return;
     458             : 
     459        1082 :     if (step >= minstep) {
     460         344 :       minl = ceil(l_min/step);
     461         344 :       maxl = floor(l_max/step);
     462         344 :       if (minl <= maxl && maxl - minl + 1 <= nticks) {
     463         344 :         nticks = (long) (maxl - minl + 1);
     464         344 :         l_min = minl * step;
     465         344 :         l_max = maxl * step; break;
     466             :       }
     467             :     }
     468         738 :     step *= mult[ n % 3 ];
     469             :   }
     470             :   /* Where to position doubleticks. Variants:
     471             :    * small: each 5, double: each 10  ; n=2 mod 3
     472             :    * small: each 2, double: each 10  ; n=1 mod 3
     473             :    * small: each 1, double: each  5 */
     474         344 :   dn = (n % 3 == 2)? 2: 5;
     475         344 :   n1 = ((long)minl) % dn; /* unused if do_double = FALSE */
     476             : 
     477             :   /* now l_min and l_max keep min/max values of l with ticks, and nticks is
     478             :      the number of ticks to draw. */
     479         344 :   if (nticks == 1) ddx = ddy = 0; /* -Wall */
     480             :   else {
     481         344 :     dl = (l_max - l_min)/(nticks - 1);
     482         344 :     ddx = (dx2 - dx1) * dl / (l2 - l1);
     483         344 :     ddy = (dy2 - dy1) * dl / (l2 - l1);
     484             :   }
     485         344 :   x = dx1 + (dx2 - dx1) * (l_min - l1) / (l2 - l1);
     486         344 :   y = dy1 + (dy2 - dy1) * (l_min - l1) / (l2 - l1);
     487             :   /* assume hunit and vunit form a square. For clockwise ticks: */
     488         344 :   dtx = WW->hunit * dy/dxy * (y2 > y1 ? 1 : -1); /* y-coord runs down */
     489         344 :   dty = WW->vunit * dx/dxy * (x2 > x1 ? 1 : -1);
     490        8336 :   for (n = 0; n < nticks; n++, x += ddx, y += ddy) {
     491        7992 :     RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj2P));
     492        7992 :     double lunit = WW->hunit > 1 ? 1.5 : 2;
     493        7992 :     double l = (do_double && (n + n1) % dn == 0) ? lunit: 1;
     494             :     double x1, x2, y1, y2;
     495        7992 :     x1 = x2 = x*RXscale(e) + RXshift(e);
     496        7992 :     y1 = y2 = y*RYscale(e) + RYshift(e);
     497        7992 :     if (flags & TICKS_CLOCKW)  { x1 += dtx*l; y1 -= dty*l; }
     498        7992 :     if (flags & TICKS_ACLOCKW) { x2 -= dtx*l; y2 += dty*l; }
     499        7992 :     RoLNx1(z) = x1; RoLNy1(z) = y1;
     500        7992 :     RoLNx2(z) = x2; RoLNy2(z) = y2;
     501        7992 :     RoType(z) = ROt_LN;
     502        7992 :     Rchain(e, z);
     503        7992 :     RoCol(z) = current_color[ne];
     504             :   }
     505             : }
     506             : 
     507             : static void
     508         122 : rectbox0(long ne, double gx2, double gy2, long relative, long filled)
     509             : {
     510             :   double xx, yy, x1, y1, x2, y2, xmin, ymin, xmax, ymax;
     511         122 :   PariRect *e = check_rect_init(ne);
     512         122 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj2P));
     513             : 
     514         122 :   x1 = RXcursor(e)*RXscale(e) + RXshift(e);
     515         122 :   y1 = RYcursor(e)*RYscale(e) + RYshift(e);
     516         122 :   if (relative)
     517          18 :   { xx = RXcursor(e)+gx2; yy = RYcursor(e)+gy2; }
     518             :   else
     519         104 :   {  xx = gx2; yy = gy2; }
     520         122 :   x2 = xx*RXscale(e) + RXshift(e);
     521         122 :   y2 = yy*RYscale(e) + RYshift(e);
     522         122 :   xmin = maxdd(mindd(x1,x2),0); xmax = mindd(maxdd(x1,x2),RXsize(e));
     523         122 :   ymin = maxdd(mindd(y1,y2),0); ymax = mindd(maxdd(y1,y2),RYsize(e));
     524             : 
     525         122 :   RoType(z) = filled ? ROt_FBX: ROt_BX;
     526         122 :   RoBXx1(z) = xmin; RoBXy1(z) = ymin;
     527         122 :   RoBXx2(z) = xmax; RoBXy2(z) = ymax;
     528         122 :   Rchain(e, z);
     529         122 :   RoCol(z) = current_color[ne];
     530         122 : }
     531             : static void
     532          86 : _box(long ne, double x, double y)
     533          86 : { rectbox0(ne, x, y, 0, 0); }
     534             : void
     535          18 : plotbox(long ne, GEN gx2, GEN gy2, long f)
     536          18 : { rectbox0(ne, gtodouble(gx2), gtodouble(gy2), 0, f); }
     537             : void
     538          18 : plotrbox(long ne, GEN gx2, GEN gy2, long f)
     539          18 : { rectbox0(ne, gtodouble(gx2), gtodouble(gy2), 1, f); }
     540             : 
     541             : static void
     542       30372 : freeobj(RectObj *z) {
     543       30372 :   switch(RoType(z)) {
     544             :     case ROt_MP: case ROt_ML:
     545       10138 :       pari_free(RoMPxs(z));
     546       10138 :       pari_free(RoMPys(z)); break;
     547             :     case ROt_ST:
     548         420 :       pari_free(RoSTs(z)); break;
     549             :   }
     550       30372 :   pari_free(z);
     551       30372 : }
     552             : 
     553             : void
     554         231 : plotkill(long ne)
     555             : {
     556             :   RectObj *z, *t;
     557         231 :   PariRect *e = check_rect_init(ne);
     558             : 
     559         231 :   z = RHead(e);
     560         231 :   RHead(e) = RTail(e) = NULL;
     561         231 :   RXsize(e) = RYsize(e) = 0;
     562         231 :   RXcursor(e) = RYcursor(e) = 0;
     563         231 :   RXscale(e) = RYscale(e) = 1;
     564         231 :   RXshift(e) = RYshift(e) = 0;
     565         231 :   while (z) { t = RoNext(z); freeobj(z); z = t; }
     566         231 : }
     567             : 
     568             : /* ROt_MP */
     569             : static void
     570          36 : plotpoints0(long ne, double *X, double *Y, long lx)
     571             : {
     572             :   double *px, *py;
     573          36 :   long i, cp=0;
     574          36 :   PariRect *e = check_rect_init(ne);
     575          36 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObjMP));
     576             : 
     577          36 :   RoMPxs(z) = px = (double*) pari_malloc(lx*sizeof(double));
     578          36 :   RoMPys(z) = py = (double*) pari_malloc(lx*sizeof(double));
     579        3620 :   for (i=0; i<lx; i++)
     580             :   {
     581        3584 :     double x = RXscale(e)*X[i] + RXshift(e);
     582        3584 :     double y = RYscale(e)*Y[i] + RYshift(e);
     583        3584 :     if (x >= 0 && y >= 0 && x <= RXsize(e) && y <= RYsize(e))
     584             :     {
     585        3582 :       px[cp] = x; py[cp] = y; cp++;
     586             :     }
     587             :   }
     588          36 :   RoType(z) = ROt_MP;
     589          36 :   RoMPcnt(z) = cp;
     590          36 :   Rchain(e, z);
     591          36 :   RoCol(z) = current_color[ne];
     592          36 : }
     593             : void
     594          36 : plotpoints(long ne, GEN X, GEN Y)
     595             : {
     596          36 :   pari_sp av = avma;
     597             :   double *px, *py;
     598             :   long i, lx;
     599             : 
     600          36 :   if (!is_vec_t(typ(X)) || !is_vec_t(typ(Y))) { plotpoint(ne, X, Y); return; }
     601          18 :   lx = lg(X); if (lg(Y) != lx) pari_err_DIM("plotpoints");
     602          18 :   lx--; if (!lx) return;
     603             : 
     604          18 :   px = (double*)stack_malloc_align(lx*sizeof(double), sizeof(double)); X++;
     605          18 :   py = (double*)stack_malloc_align(lx*sizeof(double), sizeof(double)); Y++;
     606         198 :   for (i=0; i<lx; i++)
     607             :   {
     608         180 :     px[i] = gtodouble(gel(X,i));
     609         180 :     py[i] = gtodouble(gel(Y,i));
     610             :   }
     611          18 :   plotpoints0(ne,px,py,lx); set_avma(av);
     612             : }
     613             : 
     614             : /* ROt_ML */
     615             : static void
     616       10062 : rectlines0(long ne, double *x, double *y, long lx, long flag)
     617             : {
     618             :   long i,I;
     619             :   double *ptx,*pty;
     620       10062 :   PariRect *e = check_rect_init(ne);
     621       10062 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj2P));
     622             : 
     623       10062 :   I = flag ? lx+1 : lx;
     624       10062 :   ptx = (double*) pari_malloc(I*sizeof(double));
     625       10062 :   pty = (double*) pari_malloc(I*sizeof(double));
     626      183880 :   for (i=0; i<lx; i++)
     627             :   {
     628      173818 :     ptx[i] = RXscale(e)*x[i] + RXshift(e);
     629      173818 :     pty[i] = RYscale(e)*y[i] + RYshift(e);
     630             :   }
     631       10062 :   if (flag)
     632             :   {
     633           0 :     ptx[i] = RXscale(e)*x[0] + RXshift(e);
     634           0 :     pty[i] = RYscale(e)*y[0] + RYshift(e);
     635             :   }
     636       10062 :   Rchain(e, z);
     637       10062 :   RoType(z) = ROt_ML;
     638       10062 :   RoMLcnt(z) = lx;
     639       10062 :   RoMLxs(z) = ptx;
     640       10062 :   RoMLys(z) = pty;
     641       10062 :   RoCol(z) = current_color[ne];
     642       10062 : }
     643             : void
     644          36 : plotlines(long ne, GEN X, GEN Y, long flag)
     645             : {
     646          36 :   pari_sp av = avma;
     647             :   double *x, *y;
     648             :   long i, lx;
     649             : 
     650          36 :   if (!is_vec_t(typ(X)) || !is_vec_t(typ(Y))) { plotline(ne, X, Y); return; }
     651          18 :   lx = lg(X); if (lg(Y) != lx) pari_err_DIM("plotlines");
     652          18 :   lx--; if (!lx) return;
     653             : 
     654          18 :   x = (double*)stack_malloc_align(lx*sizeof(double), sizeof(double)); X++;
     655          18 :   y = (double*)stack_malloc_align(lx*sizeof(double), sizeof(double)); Y++;
     656         108 :   for (i=0; i<lx; i++)
     657             :   {
     658          90 :     x[i] = gtodouble(gel(X,i));
     659          90 :     y[i] = gtodouble(gel(Y,i));
     660             :   }
     661          18 :   rectlines0(ne,x,y,lx,flag); set_avma(av);
     662             : }
     663             : 
     664             : /* ROt_ST */
     665             : void
     666         380 : plotstring(long ne, char *str, long dir)
     667             : {
     668         380 :   PariRect *e = check_rect_init(ne);
     669         380 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObjST));
     670         380 :   long l = strlen(str);
     671         380 :   char *s = (char *) pari_malloc(l+1);
     672             : 
     673         380 :   memcpy(s,str,l+1);
     674         380 :   RoType(z) = ROt_ST;
     675         380 :   RoSTl(z) = l;
     676         380 :   RoSTs(z) = s;
     677         380 :   RoSTx(z) = RXscale(e)*RXcursor(e)+RXshift(e);
     678         380 :   RoSTy(z) = RYscale(e)*RYcursor(e)+RYshift(e);
     679         380 :   RoSTdir(z) = dir;
     680         380 :   Rchain(e, z);
     681         380 :   RoCol(z) = current_color[ne];
     682         380 : }
     683             : 
     684             : /* ROt_PTT */
     685             : void
     686          18 : plotpointtype(long ne, long type)
     687             : {
     688          18 :  if (ne == -1) plotpoint_itype = type;
     689             :  else {
     690          18 :    PariRect *e = check_rect_init(ne);
     691          18 :    RectObj *z = (RectObj*) pari_malloc(sizeof(RectObjPN));
     692          18 :    RoType(z) = ROt_PTT;
     693          18 :    RoPTTpen(z) = type;
     694          18 :    Rchain(e, z);
     695             :  }
     696          18 : }
     697             : 
     698             : /* ROt_PTS. FIXME: this function is a noop, since no graphic driver implement
     699             :  * this code. ne == -1 (change globally). */
     700             : void
     701           0 : plotpointsize(long ne, GEN size)
     702             : {
     703           0 :  if (ne == -1) { /*do nothing*/ }
     704             :  else {
     705           0 :    PariRect *e = check_rect_init(ne);
     706           0 :    RectObj *z = (RectObj*) pari_malloc(sizeof(RectObjPS));
     707           0 :    RoType(z) = ROt_PTS;
     708           0 :    RoPTSsize(z) = gtodouble(size);
     709           0 :    Rchain(e, z);
     710             :  }
     711           0 : }
     712             : 
     713             : void
     714       10292 : plotlinetype(long ne, long type)
     715             : {
     716       10292 :  if (ne == -1) rectline_itype = type;
     717             :  else {
     718       10292 :    PariRect *e = check_rect_init(ne);
     719       10292 :    RectObj *z = (RectObj*) pari_malloc(sizeof(RectObjPN));
     720       10292 :    RoType(z) = ROt_LNT;
     721       10292 :    RoLNTpen(z) = type;
     722       10292 :    Rchain(e, z);
     723             :  }
     724       10292 : }
     725             : 
     726             : #define RECT_CP_RELATIVE  0x1
     727             : #define RECT_CP_NW        0x0
     728             : #define RECT_CP_SW        0x2
     729             : #define RECT_CP_SE        0x4
     730             : #define RECT_CP_NE        0x6
     731             : 
     732             : static double*
     733           0 : cpd(double* R, size_t t)
     734           0 : { void *o = pari_malloc(t * sizeof(double)); memcpy(o,R,t); return (double*)o; }
     735             : static void*
     736         460 : cp(void* R, size_t t)
     737         460 : { void *o = pari_malloc(t); memcpy(o,R,t); return o; }
     738             : void
     739          36 : plotcopy(long source, long dest, GEN xoff, GEN yoff, long flag)
     740             : {
     741          36 :   PariRect *s = check_rect_init(source), *d = check_rect_init(dest);
     742          36 :   RectObj *R, *tail = RTail(d);
     743             :   long i, x, y;
     744          36 :   if (flag & RECT_CP_RELATIVE) {
     745          32 :     double xd = gtodouble(xoff), yd = gtodouble(yoff);
     746             :     PARI_plot T;
     747          32 :     if (xd > 1) pari_err_DOMAIN("plotcopy","dx",">",gen_1,xoff);
     748          28 :     if (xd < 0) pari_err_DOMAIN("plotcopy","dx","<",gen_0,xoff);
     749          24 :     if (yd > 1) pari_err_DOMAIN("plotcopy","dy",">",gen_1,yoff);
     750          20 :     if (yd < 0) pari_err_DOMAIN("plotcopy","dy","<",gen_0,yoff);
     751          16 :     pari_get_plot(&T);
     752          16 :     x = DTOL(xd * (T.width-1));
     753          16 :     y = DTOL(yd * (T.height-1));
     754             :   } else {
     755           4 :     if (typ(xoff) != t_INT) pari_err_TYPE("plotcopy",xoff);
     756           4 :     if (typ(yoff) != t_INT) pari_err_TYPE("plotcopy",yoff);
     757           4 :     x = itos(xoff);
     758           4 :     y = itos(yoff);
     759             :   }
     760          20 :   switch (flag & ~RECT_CP_RELATIVE)
     761             :   {
     762           8 :     case RECT_CP_NW: break;
     763           4 :     case RECT_CP_SW: y = RYsize(d) - RYsize(s) - y; break;
     764           4 :     case RECT_CP_SE: y = RYsize(d) - RYsize(s) - y; /* fall through */
     765           8 :     case RECT_CP_NE: x = RXsize(d) - RXsize(s) - x; break;
     766             :   }
     767         360 :   for (R = RHead(s); R; R = RoNext(R))
     768             :   {
     769             :     RectObj *o;
     770         340 :     switch(RoType(R))
     771             :     {
     772             :       case ROt_PT:
     773          40 :         o = (RectObj*)cp(R, sizeof(RectObj1P));
     774          40 :         RoPTx(o) += x; RoPTy(o) += y;
     775          40 :         break;
     776             :       case ROt_LN: case ROt_BX: case ROt_FBX:
     777          80 :         o = (RectObj*)cp(R, sizeof(RectObj2P));
     778          80 :         RoLNx1(o) += x; RoLNy1(o) += y;
     779          80 :         RoLNx2(o) += x; RoLNy2(o) += y;
     780          80 :         break;
     781             :       case ROt_MP: case ROt_ML:
     782          40 :         o = (RectObj*)cp(R, sizeof(RectObjMP));
     783          40 :         RoMPxs(o) = (double*)cp(RoMPxs(R), sizeof(double)*RoMPcnt(o));
     784          40 :         RoMPys(o) = (double*)cp(RoMPys(R), sizeof(double)*RoMPcnt(o));
     785          40 :         for (i=0; i<RoMPcnt(o); i++) { RoMPxs(o)[i] += x; RoMPys(o)[i] += y; }
     786          40 :         break;
     787             :       case ROt_ST:
     788          40 :         o = (RectObj*)cp(R, sizeof(RectObjST));
     789          40 :         RoSTs(o) = (char*)cp(RoSTs(R),RoSTl(R)+1);
     790          40 :         RoSTx(o) += x; RoSTy(o) += y;
     791          40 :         break;
     792             :       default: /* ROt_PTT, ROt_LNT, ROt_PTS */
     793         140 :         o = (RectObj*)cp(R, sizeof(RectObjPN));
     794         140 :         break;
     795             :     }
     796         340 :     RoNext(tail) = o; tail = o;
     797             :   }
     798          20 :   RoNext(tail) = NULL; RTail(d) = tail;
     799          20 : }
     800             : 
     801             : enum {CLIPLINE_NONEMPTY = 1, CLIPLINE_CLIP_1 = 2, CLIPLINE_CLIP_2 = 4};
     802             : /* A simpler way is to clip by 4 half-planes */
     803             : static int
     804         120 : clipline(double xmin, double xmax, double ymin, double ymax,
     805             :          double *x1p, double *y1p, double *x2p, double *y2p)
     806             : {
     807         120 :   int xy_exch = 0, rc = CLIPLINE_NONEMPTY;
     808             :   double t, sl;
     809             :   double xi, xmn, xmx;
     810             :   double yi, ymn, ymx;
     811             :   int x1_is_ymn, x1_is_xmn;
     812         120 :   double x1 = *x1p, x2 = *x2p, y1 = *y1p, y2 = *y2p;
     813             : 
     814         120 :   if ((x1 < xmin &&  x2 < xmin) || (x1 > xmax && x2 > xmax))
     815          40 :     return 0;
     816          80 :   if (fabs(x1 - x2) < fabs(y1 - y2)) { /* Exchange x and y */
     817          24 :     xy_exch = 1;
     818          24 :     dswap(xmin, ymin); dswap(x1, y1);
     819          24 :     dswap(xmax, ymax); dswap(x2, y2);
     820             :   }
     821             : 
     822             :   /* Build y as a function of x */
     823          80 :   xi = x1;
     824          80 :   yi = y1;
     825          80 :   sl = x1==x2? 0: (y2 - yi)/(x2 - xi);
     826             : 
     827          80 :   if (x1 > x2) {
     828          24 :     x1_is_xmn = 0;
     829          24 :     xmn = x2;
     830          24 :     xmx = x1;
     831             :   } else {
     832          56 :     x1_is_xmn = 1;
     833          56 :     xmn = x1;
     834          56 :     xmx = x2;
     835             :   }
     836             : 
     837          80 :   if (xmn < xmin) {
     838           8 :     xmn = xmin;
     839           8 :     rc |= x1_is_xmn? CLIPLINE_CLIP_1: CLIPLINE_CLIP_2;
     840             :   }
     841          80 :   if (xmx > xmax) {
     842          12 :     xmx = xmax;
     843          12 :     rc |= x1_is_xmn? CLIPLINE_CLIP_2: CLIPLINE_CLIP_1;
     844             :   }
     845          80 :   if (xmn > xmx) return 0;
     846             : 
     847          80 :   ymn = yi + (xmn - xi)*sl;
     848          80 :   ymx = yi + (xmx - xi)*sl;
     849             : 
     850          80 :   if (sl < 0) t = ymn, ymn = ymx, ymx = t;
     851          80 :   if (ymn > ymax || ymx < ymin) return 0;
     852             : 
     853          80 :   if (rc & CLIPLINE_CLIP_1) x1 = x1_is_xmn? xmn: xmx;
     854          80 :   if (rc & CLIPLINE_CLIP_2) x2 = x1_is_xmn? xmx: xmn;
     855             : 
     856             :   /* Now we know there is an intersection, need to move x1 and x2 */
     857          80 :   x1_is_ymn = ((sl >= 0) == (x1 < x2));
     858          80 :   if (ymn < ymin) {
     859           4 :     double x = (ymin - yi)/sl + xi; /* slope != 0  ! */
     860           4 :     if (x1_is_ymn) x1 = x, rc |= CLIPLINE_CLIP_1;
     861           0 :     else           x2 = x, rc |= CLIPLINE_CLIP_2;
     862             :   }
     863          80 :   if (ymx > ymax) {
     864           4 :     double x = (ymax - yi)/sl + xi; /* slope != 0  ! */
     865           4 :     if (x1_is_ymn) x2 = x, rc |= CLIPLINE_CLIP_2;
     866           0 :     else           x1 = x, rc |= CLIPLINE_CLIP_1;
     867             :   }
     868          80 :   if (rc & CLIPLINE_CLIP_1) y1 = yi + (x1 - xi)*sl;
     869          80 :   if (rc & CLIPLINE_CLIP_2) y2 = yi + (x2 - xi)*sl;
     870          80 :   if (xy_exch) /* Exchange x and y */
     871          24 :     *x1p = y1, *x2p = y2, *y1p = x1, *y2p = x2;
     872             :   else
     873          56 :     *x1p = x1, *x2p = x2, *y1p = y1, *y2p = y2;
     874          80 :   return rc;
     875             : }
     876             : 
     877             : void
     878          20 : plotclip(long rect)
     879             : {
     880          20 :   PariRect *s = check_rect_init(rect);
     881          20 :   RectObj *next, *R = RHead(s), **prevp = &RHead(s);
     882          20 :   double xmin = 0, xmax = RXsize(s);
     883          20 :   double ymin = 0, ymax = RYsize(s);
     884             : 
     885         380 :   for (; R; R = next) {
     886         360 :     int did_clip = 0;
     887             : #define REMOVE() { *prevp = next; freeobj(R); break; }
     888             : #define NEXT() { prevp = &RoNext(R); break; }
     889             : 
     890         360 :     next = RoNext(R);
     891         360 :     switch(RoType(R)) {
     892             :       case ROt_PT:
     893          40 :         if ( DTOL(RoPTx(R)) < xmin || DTOL(RoPTx(R)) > xmax
     894          24 :           || DTOL(RoPTy(R)) < ymin || DTOL(RoPTy(R)) > ymax) REMOVE();
     895          24 :         NEXT();
     896             :       case ROt_BX: case ROt_FBX:
     897          40 :         if (RoLNx1(R) < xmin) RoLNx1(R) = xmin, did_clip = 1;
     898          40 :         if (RoLNx2(R) < xmin) RoLNx2(R) = xmin, did_clip = 1;
     899          40 :         if (RoLNy1(R) < ymin) RoLNy1(R) = ymin, did_clip = 1;
     900          40 :         if (RoLNy2(R) < ymin) RoLNy2(R) = ymin, did_clip = 1;
     901          40 :         if (RoLNx1(R) > xmax) RoLNx1(R) = xmax, did_clip = 1;
     902          40 :         if (RoLNx2(R) > xmax) RoLNx2(R) = xmax, did_clip = 1;
     903          40 :         if (RoLNy1(R) > ymax) RoLNy1(R) = ymax, did_clip = 1;
     904          40 :         if (RoLNy2(R) > ymax) RoLNy2(R) = ymax, did_clip = 1;
     905             :         /* Remove zero-size clipped boxes */
     906          40 :         if (did_clip && RoLNx1(R) == RoLNx2(R)
     907           8 :                      && RoLNy1(R) == RoLNy2(R)) REMOVE();
     908          36 :         NEXT();
     909             :       case ROt_LN:
     910          40 :         if (!clipline(xmin, xmax, ymin, ymax,
     911             :                       &RoLNx1(R), &RoLNy1(R),
     912           8 :                       &RoLNx2(R), &RoLNy2(R))) REMOVE();
     913          32 :         NEXT();
     914             :       case ROt_MP: {
     915          20 :         int c = RoMPcnt(R), f = 0, t = 0;
     916             : 
     917         240 :         while (f < c) {
     918         200 :           if ( DTOL(RoMPxs(R)[f]) >= xmin && DTOL(RoMPxs(R)[f]) <= xmax
     919         120 :             && DTOL(RoMPys(R)[f]) >= ymin && DTOL(RoMPys(R)[f]) <= ymax) {
     920         120 :             if (t != f) {
     921           0 :               RoMPxs(R)[t] = RoMPxs(R)[f];
     922           0 :               RoMPys(R)[t] = RoMPys(R)[f];
     923             :             }
     924         120 :             t++;
     925             :           }
     926         200 :           f++;
     927             :         }
     928          20 :         if (t == 0) REMOVE();
     929          12 :         RoMPcnt(R) = t;
     930          12 :         NEXT();
     931             :       }
     932             :       case ROt_ML: {
     933             :         /* Hard case. Break a multiline into several pieces
     934             :          * if some part is clipped. */
     935          20 :         int c = RoMPcnt(R) - 1;
     936          20 :         int f = 0, t = 0, had_lines = 0, had_hole = 0, rc;
     937          20 :         double ox = RoMLxs(R)[0], oy = RoMLys(R)[0], oxn, oyn;
     938             : 
     939         120 :         while (f < c) {
     940             :         /* Endpoint of this segment is startpoint of next one: need to
     941             :          * preserve it if it is clipped. */
     942          80 :           oxn = RoMLxs(R)[f+1];
     943          80 :           oyn = RoMLys(R)[f+1];
     944         160 :           rc = clipline(xmin, xmax, ymin, ymax,
     945             :                   &ox, &oy, /* &RoMLxs(R)[f], &RoMLys(R)[f], */
     946         160 :                   &RoMLxs(R)[f+1], &RoMLys(R)[f+1]);
     947          80 :           RoMLxs(R)[f] = ox; ox = oxn;
     948          80 :           RoMLys(R)[f] = oy; oy = oyn;
     949          80 :           if (!rc) {
     950          32 :             if (had_lines) had_hole = 1;
     951          32 :             f++; continue;
     952             :           }
     953             : 
     954          48 :           if (!had_lines || (!(rc & CLIPLINE_CLIP_1) && !had_hole) ) {
     955             :             /* Continuous */
     956          48 :             had_lines = 1;
     957          48 :             if (t != f) {
     958           0 :               if (t == 0) {
     959           0 :                 RoMPxs(R)[t] = RoMPxs(R)[f];
     960           0 :                 RoMPys(R)[t] = RoMPys(R)[f];
     961             :               }
     962           0 :               RoMPxs(R)[t+1] = RoMPxs(R)[f+1];
     963           0 :               RoMPys(R)[t+1] = RoMPys(R)[f+1];
     964             :             }
     965          48 :             t++;
     966          48 :             f++;
     967          48 :             if (rc & CLIPLINE_CLIP_2) had_hole = 1, RoMLcnt(R) = t+1;
     968          48 :             continue;
     969             :           }
     970             :           /* Is not continuous, automatically R is not pari_free()ed.  */
     971           0 :           t++;
     972           0 :           RoMLcnt(R) = t;
     973           0 :           if (rc & CLIPLINE_CLIP_2) { /* Needs separate entry */
     974           0 :             RectObj *n = (RectObj*) pari_malloc(sizeof(RectObj2P));
     975           0 :             RoType(n) = ROt_LN;
     976           0 :             RoCol(n) = RoCol(R);
     977           0 :             RoLNx1(n) = RoMLxs(R)[f];   RoLNy1(n) = RoMLys(R)[f];
     978           0 :             RoLNx2(n) = RoMLxs(R)[f+1]; RoLNy2(n) = RoMLys(R)[f+1];
     979           0 :             RoNext(n) = next;
     980           0 :             RoNext(R) = n;
     981             :             /* Restore the unclipped value: */
     982           0 :             RoMLxs(R)[f+1] = oxn; RoMLys(R)[f+1] = oyn;
     983           0 :             f++;
     984           0 :             prevp = &RoNext(n);
     985             :           }
     986           0 :           if (f + 1 < c) { /* Are other lines */
     987           0 :             RectObj *n = (RectObj*) pari_malloc(sizeof(RectObjMP));
     988           0 :             RoType(n) = ROt_ML;
     989           0 :             RoCol(n) = RoCol(R);
     990           0 :             RoMLcnt(n) = c - f;
     991           0 :             RoMLxs(n) = cpd(RoMPxs(R) + f, c-f);
     992           0 :             RoMLys(n) = cpd(RoMPys(R) + f, c-f);
     993           0 :             RoMPxs(n)[0] = oxn;
     994           0 :             RoMPys(n)[0] = oyn;
     995           0 :             RoNext(n) = next;
     996           0 :             RoNext(R) = n;
     997           0 :             next = n;
     998             :           }
     999           0 :           break;
    1000             :         }
    1001          20 :         if (t == 0) REMOVE();
    1002          12 :         NEXT();
    1003             :       }
    1004             :     }
    1005             : #undef REMOVE
    1006             : #undef NEXT
    1007             :   }
    1008          20 : }
    1009             : 
    1010             : /********************************************************************/
    1011             : /**                                                                **/
    1012             : /**                        HI-RES PLOT                             **/
    1013             : /**                                                                **/
    1014             : /********************************************************************/
    1015             : static void
    1016      146088 : set_xrange(dblPointList *f, double x)
    1017      146088 : { if (x < f->xsml) f->xsml = x;
    1018      146088 :   if (x > f->xbig) f->xbig = x; }
    1019             : static void
    1020      138676 : Appendxat(dblPointList *f, dblPointList *l,long n,double x)
    1021      138676 : { (l->d)[n] = x; l->nb = n+1; set_xrange(f,x); }
    1022             : static void
    1023      204088 : set_yrange(dblPointList *f, double y)
    1024      204088 : { if (y < f->ysml) f->ysml = y;
    1025      204088 :   if (y > f->ybig) f->ybig = y; }
    1026             : static void
    1027      196676 : Appendyat(dblPointList *f, dblPointList *l,long n,double y)
    1028      196676 : { (l->d)[n] = y; l->nb = n+1; set_yrange(f,y); }
    1029             : static void
    1030      129920 : Appendx(dblPointList *f, dblPointList *l,double x) { Appendxat(f,l,l->nb,x); }
    1031             : static void
    1032      129920 : Appendy(dblPointList *f, dblPointList *l,double y) { Appendyat(f,l,l->nb,y); }
    1033             : 
    1034             : static void
    1035      138192 : get_xy(long cplx, GEN t, double *x, double *y)
    1036             : {
    1037             :   GEN a, b;
    1038      138192 :   if (cplx)
    1039             :   {
    1040           0 :     if (typ(t) == t_VEC)
    1041             :     {
    1042           0 :       if (lg(t) != 2) pari_err_DIM("get_xy");
    1043           0 :       t = gel(t,1);
    1044             :     }
    1045           0 :     a = real_i(t); b = imag_i(t);
    1046             :   }
    1047             :   else
    1048             :   {
    1049      138192 :     if (typ(t) != t_VEC || lg(t) != 3) pari_err_DIM("get_xy");
    1050      138192 :     a = gel(t,1); b = gel(t,2);
    1051             :   }
    1052      138192 :   *x = gtodouble(a);
    1053      138192 :   *y = gtodouble(b);
    1054      138192 : }
    1055             : /* t a t_VEC (possibly a scalar if cplx), get next (x,y) coordinate starting
    1056             :  * at index *i [update i] */
    1057             : static void
    1058        8756 : get_xy_from_vec(long cplx, GEN t, long *i, double *x, double *y)
    1059             : {
    1060             :   GEN a, b;
    1061        8756 :   if (cplx)
    1062             :   {
    1063           0 :     if (typ(t) == t_VEC) t = gel(t,*i);
    1064           0 :     a = real_i(t); b = imag_i(t); (*i)++;
    1065             :   }
    1066             :   else
    1067             :   {
    1068        8756 :     a = gel(t, (*i)++);
    1069        8756 :     b = gel(t, (*i)++);
    1070             :   }
    1071        8756 :   *x = gtodouble(a);
    1072        8756 :   *y = gtodouble(b);
    1073        8756 : }
    1074             : /* X,Y t_VEC; next (x,y) coordinate starting at index i; Y ignored if (cplx) */
    1075             : static void
    1076        7412 : get_xy_from_vec2(long cplx, GEN X, GEN Y, long i, double *x, double *y)
    1077             : {
    1078             :   GEN a, b;
    1079        7412 :   if (cplx)
    1080             :   {
    1081           0 :     GEN z = gel(X,i);
    1082           0 :     a = real_i(z); b = imag_i(z);
    1083             :   }
    1084             :   else
    1085             :   {
    1086        7412 :     a = gel(X,i); b = gel(Y,i);
    1087             :   }
    1088        7412 :   *x = gtodouble(a);
    1089        7412 :   *y = gtodouble(b);
    1090        7412 : }
    1091             : 
    1092             : /* Convert data from GEN to double before we call plotrecthrawin. */
    1093             : static dblPointList*
    1094          26 : gtodblList(GEN data, long flags)
    1095             : {
    1096             :   dblPointList *l, *L;
    1097             :   double *X, *Y;
    1098          26 :   long nl=lg(data)-1, lx1, i, j;
    1099          26 :   const long param = (flags & (PLOT_PARAMETRIC|PLOT_COMPLEX));
    1100          26 :   const long cplx = (flags & PLOT_COMPLEX);
    1101             : 
    1102          26 :   if (! is_vec_t(typ(data))) pari_err_TYPE("gtodblList",data);
    1103          26 :   if (!nl) return NULL;
    1104          26 :   lx1 = lg(gel(data,1));
    1105          26 :   if (!param && lx1 == 1) return NULL;
    1106             : 
    1107             :   /* Check input first */
    1108          26 :   if (nl == 1 && !cplx) pari_err_DIM("gtodblList");
    1109          52 :   for (i = 0; i < nl; i += cplx? 1: 2)
    1110             :   {
    1111          26 :     GEN x = gel(data,i+1), y = cplx? NULL: gel(data,i+2);
    1112          26 :     long lx = lg(x);
    1113          26 :     if (!is_vec_t(typ(x))) pari_err_TYPE("gtodblList",x);
    1114          26 :     if (y)
    1115             :     {
    1116          26 :       if (!is_vec_t(typ(y))) pari_err_TYPE("gtodblList",y);
    1117          26 :       if (lg(y) != lx || (!param && lx != lx1)) pari_err_DIM("gtodblList");
    1118             :     }
    1119             :   }
    1120             :   /* Now allocate memory, then convert coord. to double */
    1121          26 :   l = (dblPointList*)pari_malloc((cplx? 2*nl: nl)*sizeof(dblPointList));
    1122          26 :   L = &l[0];
    1123          52 :   for (i = 0; i < nl; i += cplx? 1: 2)
    1124             :   {
    1125          26 :     GEN x = gel(data,i+1), y = cplx? NULL: gel(data,i+2);
    1126          26 :     long lx = lg(x)-1;
    1127          26 :     l[i].d   = X = (double*)pari_malloc(lx*sizeof(double));
    1128          26 :     l[i+1].d = Y = (double*)pari_malloc(lx*sizeof(double));
    1129          26 :     for (j=1; j<=lx; j++) get_xy_from_vec2(cplx, x, y, j, X+(j-1), Y+(j-1));
    1130          26 :     l[i].nb = l[i+1].nb = lx;
    1131             :   }
    1132             :   /* Compute extremas */
    1133          26 :   if (param)
    1134             :   {
    1135          26 :     L->nb = cplx? nl: nl/2;
    1136          26 :     for (i=0; i < L->nb; i+=2)
    1137          26 :       if (l[i+1].nb) break;
    1138          26 :     if (i >= L->nb) { pari_free(l); return NULL; }
    1139          26 :     L->xsml = L->xbig = l[i  ].d[0];
    1140          26 :     L->ysml = L->ybig = l[i+1].d[0];
    1141          52 :     for (; i < L->nb; i+=2)
    1142             :     {
    1143          26 :       long nbi = l[i+1].nb; X = l[i].d; Y = l[i+1].d;
    1144          26 :       for (j = 0; j < nbi; j++) { set_xrange(L, X[j]); set_yrange(L, Y[j]); }
    1145             :     }
    1146             :   }
    1147             :   else
    1148             :   {
    1149           0 :     L->nb = nl-1;
    1150           0 :     X = L->d;   L->xsml = L->xbig = X[0];
    1151           0 :     Y = l[1].d; L->ysml = L->ybig = Y[0];
    1152           0 :     for (j=0; j < l[1].nb; j++) set_xrange(L, X[j]);
    1153           0 :     for (i=1; i <= L->nb; i++)
    1154             :     {
    1155           0 :       long nbi = l[i].nb; Y = l[i].d;
    1156           0 :       for (j = 0; j < nbi; j++) set_yrange(L, Y[j]);
    1157             :     }
    1158             :   }
    1159          26 :   return l;
    1160             : }
    1161             : 
    1162             : /* (x+y)/2 */
    1163             : static GEN
    1164      208940 : rmiddle(GEN x, GEN y) { GEN z = addrr(x,y); shiftr_inplace(z,-1); return z; }
    1165             : 
    1166             : static void
    1167       91404 : single_recursion(void *E, GEN(*eval)(void*,GEN), dblPointList *pl,
    1168             :                  GEN xleft,double yleft, GEN xright,double yright,long depth)
    1169             : {
    1170             :   GEN xx;
    1171       91404 :   pari_sp av = avma;
    1172       91404 :   double yy, dy=pl[0].ybig - pl[0].ysml;
    1173             : 
    1174       91404 :   if (depth==RECUR_MAXDEPTH) return;
    1175             : 
    1176       83124 :   xx = rmiddle(xleft,xright);
    1177       83124 :   yy = gtodouble(eval(E,xx));
    1178             : 
    1179       83124 :   if (dy && fabs(yleft+yright-2*yy) < dy*RECUR_PREC) return;
    1180       43708 :   single_recursion(E,eval, pl,xleft,yleft, xx,yy, depth+1);
    1181       43708 :   Appendx(&pl[0],&pl[0],rtodbl(xx));
    1182       43708 :   Appendy(&pl[0],&pl[1],yy);
    1183       43708 :   single_recursion(E,eval, pl,xx,yy, xright,yright, depth+1);
    1184       43708 :   set_avma(av);
    1185             : }
    1186             : 
    1187             : static void
    1188      138104 : param_recursion(void *E,GEN(*eval)(void*,GEN), long cplx, dblPointList *pl,
    1189             :   GEN tleft,double xleft, double yleft,
    1190             :   GEN tright,double xright,double yright, long depth)
    1191             : {
    1192             :   GEN tt;
    1193      138104 :   pari_sp av = avma;
    1194      138104 :   double xx, dy=pl[0].ybig - pl[0].ysml;
    1195      138104 :   double yy, dx=pl[0].xbig - pl[0].xsml;
    1196             : 
    1197      210348 :   if (depth==RECUR_MAXDEPTH) return;
    1198             : 
    1199      125816 :   tt = rmiddle(tleft,tright);
    1200      125816 :   get_xy(cplx, eval(E,tt), &xx,&yy);
    1201             : 
    1202      125816 :   if (dx && dy && fabs(xleft+xright-2*xx) < dx*RECUR_PREC
    1203       62848 :                && fabs(yleft+yright-2*yy) < dy*RECUR_PREC) return;
    1204       65860 :   param_recursion(E,eval, cplx, pl, tleft,xleft,yleft, tt,xx,yy, depth+1);
    1205       65860 :   Appendx(&pl[0],&pl[0],xx);
    1206       65860 :   Appendy(&pl[0],&pl[1],yy);
    1207       65860 :   param_recursion(E,eval,cplx, pl, tt,xx,yy, tright,xright,yright, depth+1);
    1208       65860 :   set_avma(av);
    1209             : }
    1210             : 
    1211             : /* Graph 'code' for parameter values in [a,b], using 'N' sample points
    1212             :  * (0 = use a default value); code is either a t_CLOSURE or a t_POL or a
    1213             :  * t_VEC of two t_POLs from rectsplines. Returns a dblPointList of
    1214             :  * (absolute) coordinates. */
    1215             : static dblPointList *
    1216       10052 : plotrecthin(void *E, GEN(*eval)(void*, GEN), GEN a, GEN b, ulong flags,
    1217             :             long N, long prec)
    1218             : {
    1219       10052 :   const double INF = 1.0/0.0;
    1220       10052 :   const long param = flags & (PLOT_PARAMETRIC|PLOT_COMPLEX);
    1221       10052 :   const long recur = flags & PLOT_RECURSIVE;
    1222       10052 :   const long cplx = flags & PLOT_COMPLEX;
    1223             :   GEN t,dx,x;
    1224             :   dblPointList *pl;
    1225       10052 :   long tx, i, j, sig, nc, nl, ncoords, nbpoints, non_vec = 0;
    1226             : 
    1227       10052 :   sig = gcmp(b,a); if (!sig) return NULL;
    1228       10052 :   if (sig < 0) swap(a, b);
    1229       10052 :   if (N == 1) pari_err_DOMAIN("ploth", "#points", "<", gen_2, stoi(N));
    1230       10048 :   if (!N) N = recur? 8: (param? 1500: 1000);
    1231             :   /* compute F(a) to determine nc = #curves; nl = #coord. lists */
    1232       10048 :   x = gtofp(a, prec);
    1233       10048 :   t = eval(E, x); tx = typ(t);
    1234       10048 :   if (cplx) nc = nl = (tx == t_VEC)? lg(t)-1: 1;
    1235       10048 :   else if (param)
    1236             :   {
    1237        6022 :     if (tx != t_VEC) pari_err_TYPE("ploth [not a t_VEC in parametric plot]", t);
    1238        6018 :     nl = lg(t)-1; nc = nl >> 1;
    1239        6018 :     if (odd(nl)) pari_err_TYPE("ploth [odd #components in parametric plot]",t);
    1240             :   }
    1241        4026 :   else if (!is_matvec_t(tx)) { nl = 2; non_vec = 1; nc = 1; }
    1242             :   else
    1243             :   {
    1244          12 :     if (tx != t_VEC) pari_err_TYPE("ploth [not a t_VEC]",t);
    1245          12 :     nl = lg(t);
    1246          12 :     nc = nl-1;
    1247             :   }
    1248       10044 :   if (!nc) return NULL;
    1249       10044 :   if (recur && nc > 1) pari_err_TYPE("ploth [multi-curves + recursive]",t);
    1250             : 
    1251       10040 :   ncoords = cplx? 2*nl: nl;
    1252       10040 :   nbpoints = recur? N << RECUR_MAXDEPTH: N;
    1253       10040 :   pl=(dblPointList*) pari_malloc(ncoords*sizeof(dblPointList));
    1254             :   /* set [xy]sml,[xy]big to default values */
    1255       10040 :   if (param)
    1256             :   {
    1257        6018 :     pl[0].xsml = INF;
    1258        6018 :     pl[0].xbig =-INF;
    1259             :   } else {
    1260        4022 :     pl[0].xsml = gtodouble(a);
    1261        4022 :     pl[0].xbig = gtodouble(b);
    1262             :   }
    1263       10040 :   pl[0].ysml = INF;
    1264       10040 :   pl[0].ybig =-INF;
    1265       30144 :   for (i = 0; i < ncoords; i++)
    1266             :   {
    1267       20104 :     pl[i].d = (double*)pari_malloc((nbpoints+1)*sizeof(double));
    1268       20104 :     pl[i].nb=0;
    1269             :   }
    1270       10040 :   dx = divru(gtofp(gsub(b,a),prec), N-1);
    1271       10040 :   if (recur)
    1272             :   { /* recursive plot */
    1273        9980 :     double yleft, yright = 0;
    1274        9980 :     if (param)
    1275             :     {
    1276        5992 :       GEN tleft = cgetr(prec), tright = cgetr(prec);
    1277        5992 :       double xleft, xright = 0;
    1278        5992 :       pari_sp av2 = avma;
    1279        5992 :       affgr(a,tleft);
    1280        5992 :       t = eval(E, tleft);
    1281        5992 :       get_xy(cplx,t, &xleft,&yleft);
    1282       12376 :       for (i=0; i<N-1; i++, set_avma(av2))
    1283             :       {
    1284        6384 :         if (i) { affrr(tright,tleft); xleft = xright; yleft = yright; }
    1285        6384 :         addrrz(tleft,dx,tright);
    1286        6384 :         t = eval(E, tright);
    1287        6384 :         get_xy(cplx,t, &xright,&yright);
    1288        6384 :         Appendx(&pl[0],&pl[0],xleft);
    1289        6384 :         Appendy(&pl[0],&pl[1],yleft);
    1290        6384 :         param_recursion(E,eval, cplx, pl, tleft,xleft,yleft, tright,xright,yright, 0);
    1291             :       }
    1292        5992 :       Appendx(&pl[0],&pl[0],xright);
    1293        5992 :       Appendy(&pl[0],&pl[1],yright);
    1294             :     }
    1295             :     else /* single curve */
    1296             :     {
    1297        3988 :       GEN xleft = cgetr(prec), xright = cgetr(prec);
    1298        3988 :       pari_sp av2 = avma;
    1299        3988 :       affgr(a,xleft);
    1300        3988 :       yleft = gtodouble(eval(E,xleft));
    1301        7976 :       for (i=0; i<N-1; i++, set_avma(av2))
    1302             :       {
    1303        3988 :         addrrz(xleft,dx,xright);
    1304        3988 :         yright = gtodouble(eval(E,xright));
    1305             : 
    1306        3988 :         Appendx(&pl[0],&pl[0],rtodbl(xleft));
    1307        3988 :         Appendy(&pl[0],&pl[1],yleft);
    1308             : 
    1309        3988 :         single_recursion(E,eval, pl,xleft,yleft,xright,yright,0);
    1310        3988 :         affrr(xright,xleft); yleft = yright;
    1311             :       }
    1312        3988 :       Appendx(&pl[0],&pl[0],rtodbl(xright));
    1313        3988 :       Appendy(&pl[0],&pl[1],yright);
    1314             :     }
    1315             :   }
    1316             :   else /* non-recursive plot */
    1317             :   {
    1318             :     pari_sp av2;
    1319          60 :     GEN worker = NULL, vx = NULL;
    1320          60 :     long pending = 0, workid;
    1321             :     struct pari_mt pt;
    1322          60 :     if (flags & PLOT_PARA && eval == gp_call)
    1323             :     {
    1324           0 :       worker = snm_closure(is_entry("_parvector_worker"), mkvec((GEN)E));
    1325           0 :       mt_queue_start_lim(&pt, worker, N-1);
    1326           0 :       vx = mkvec(x);
    1327             :     }
    1328          60 :     av2 = avma;
    1329          60 :     if (param)
    1330             :     {
    1331        8782 :       for (i=0; i<N || pending; i++, set_avma(av2))
    1332             :       {
    1333             :         long k, nt;
    1334        8760 :         if (worker)
    1335             :         {
    1336           0 :           mt_queue_submit(&pt, i, i<N ? vx : NULL);
    1337           0 :           t = mt_queue_get(&pt, &workid, &pending);
    1338           0 :           if (!t) continue;
    1339             :         }
    1340             :         else
    1341             :         {
    1342        8760 :           t = eval(E,x);
    1343        8760 :           workid = i;
    1344             :         }
    1345        8760 :         if (typ(t) != t_VEC)
    1346             :         {
    1347           4 :           if (cplx) nt = 1;
    1348           4 :           else nt = 0; /* trigger error */
    1349             :         }
    1350             :         else
    1351        8756 :           nt = lg(t)-1;
    1352        8760 :         if (nt != nl) pari_err_DIM("plotrecth");
    1353        8756 :         k = 0; j = 1;
    1354       26268 :         while (j <= nl)
    1355             :         {
    1356             :           double xx, yy;
    1357        8756 :           get_xy_from_vec(cplx, t, &j, &xx, &yy);
    1358        8756 :           Appendxat(&pl[0], &pl[k++], workid, xx);
    1359        8756 :           Appendyat(&pl[0], &pl[k++], workid, yy);
    1360             :         }
    1361        8756 :         if (i<N) affrr(addrr(x,dx), x);
    1362             :       }
    1363             :     }
    1364          34 :     else if (non_vec)
    1365       26026 :       for (i=0; i<N || pending; i++, set_avma(av2))
    1366             :       {
    1367       26000 :         if (worker)
    1368             :         {
    1369           0 :           mt_queue_submit(&pt, i, i<N ? vx : NULL);
    1370           0 :           t = mt_queue_get(&pt, &workid, &pending);
    1371             :         }
    1372             :         else
    1373             :         {
    1374       26000 :           t = eval(E,x);
    1375       26000 :           workid = i;
    1376             :         }
    1377       26000 :         if (t) Appendyat(&pl[0], &pl[1], workid, gtodouble(t));
    1378       26000 :         if (i<N)
    1379             :         {
    1380       26000 :           pl[0].d[i] = gtodouble(x);
    1381       26000 :           affrr(addrr(x,dx), x);
    1382             :         }
    1383             :       }
    1384             :     else /* vector of non-parametric curves */
    1385        8008 :       for (i=0; i<N || pending; i++, set_avma(av2))
    1386             :       {
    1387        8000 :         if (worker)
    1388             :         {
    1389           0 :           mt_queue_submit(&pt, i, i<N ? vx : NULL);
    1390           0 :           t = mt_queue_get(&pt, &workid, &pending);
    1391             :         }
    1392             :         else
    1393             :         {
    1394        8000 :           t = eval(E,x);
    1395        8000 :           workid = i;
    1396             :         }
    1397        8000 :         if (t)
    1398             :         {
    1399        8000 :           if (typ(t) != t_VEC || lg(t) != nl) pari_err_DIM("plotrecth");
    1400       40000 :           for (j=1; j<nl; j++)
    1401       32000 :             Appendyat(&pl[0], &pl[j], workid, gtodouble(gel(t,j)));
    1402             :         }
    1403        8000 :         if (i<N)
    1404             :         {
    1405        8000 :           pl[0].d[i] = gtodouble(x);
    1406        8000 :           affrr(addrr(x,dx), x);
    1407             :         }
    1408             :       }
    1409          56 :     if (worker) mt_queue_end(&pt);
    1410             :   }
    1411       10036 :   pl[0].nb = nc; return pl;
    1412             : }
    1413             : 
    1414             : static GEN
    1415      234176 : spline_eval(void* E, GEN x) { return gsubst((GEN)E,0,x); }
    1416             : 
    1417             : /* Uses highlevel plotting functions to implement splines as
    1418             :    a low-level plotting function. */
    1419             : static void
    1420           8 : rectsplines(long ne, double *x, double *y, long lx, long flag)
    1421             : {
    1422             :   long i, j;
    1423           8 :   pari_sp av0 = avma;
    1424           8 :   GEN X = pol_x(0), xa = cgetg(lx+1, t_VEC), ya = cgetg(lx+1, t_VEC);
    1425             :   GEN tas, pol3;
    1426           8 :   long param = flag & PLOT_PARAMETRIC;
    1427           8 :   const long fl = param | PLOT_RECURSIVE | PLOT_NO_RESCALE | PLOT_NO_FRAME
    1428             :                         | PLOT_NO_AXE_Y | PLOT_NO_AXE_X;
    1429             : 
    1430           8 :   if (lx < 4) pari_err(e_MISC, "Too few points (%ld) for spline plot", lx);
    1431       10008 :   for (i = 1; i <= lx; i++) {
    1432       10000 :     gel(xa,i) = dbltor(x[i-1]);
    1433       10000 :     gel(ya,i) = dbltor(y[i-1]);
    1434             :   }
    1435           8 :   if (param) {
    1436           4 :     tas = new_chunk(4);
    1437           4 :     for (j = 1; j <= 4; j++) gel(tas,j-1) = utoipos(j);
    1438           4 :     pol3 = cgetg(3, t_VEC);
    1439             :   }
    1440             :   else
    1441           4 :     tas = pol3 = NULL; /* gcc -Wall */
    1442        9984 :   for (i = 0; i <= lx - 4; i++) {
    1443        9976 :     pari_sp av = avma;
    1444             : 
    1445        9976 :     xa++; ya++;
    1446        9976 :     if (param) {
    1447        5988 :       gel(pol3,1) = polintspec(tas, xa, X, 4, NULL);
    1448        5988 :       gel(pol3,2) = polintspec(tas, ya, X, 4, NULL);
    1449             :     } else {
    1450        3988 :       pol3 = polintspec(xa, ya, X, 4, NULL);
    1451        3988 :       tas = xa;
    1452             :     }
    1453             :     /* Start with 3 points */
    1454       19952 :     plotrecth((void*)pol3, &spline_eval, ne,
    1455             :                i== 0 ? gel(tas,0) : gel(tas,1),
    1456        9976 :                i==lx-4 ? gel(tas,3) : gel(tas,2),
    1457             :                fl, 2, DEFAULTPREC);
    1458        9976 :     set_avma(av);
    1459             :   }
    1460           8 :   set_avma(av0);
    1461           8 : }
    1462             : 
    1463             : static void
    1464          50 : pari_get_fmtplot(GEN fmt, PARI_plot *T)
    1465             : {
    1466          50 :   char *f = GSTR(fmt);
    1467          50 :   if (!strcmp(f, "svg")) pari_get_svgplot(T);
    1468          21 :   else if (!strcmp(f, "ps")) pari_get_psplot(T);
    1469           0 :   else pari_err_TYPE("plotexport [unknown format]", fmt);
    1470          50 : }
    1471             : static GEN
    1472         107 : fmt_convert(GEN fmt, GEN w, GEN x, GEN y, PARI_plot *T)
    1473             : {
    1474         107 :   char *f, *s = NULL;
    1475         107 :   if (typ(fmt) != t_STR) pari_err_TYPE("plotexport",fmt);
    1476         107 :   f = GSTR(fmt);
    1477         107 :   if (!strcmp(f, "svg"))
    1478          58 :     s = rect2svg(w,x,y,T);
    1479          49 :   else if (!strcmp(f, "ps"))
    1480          49 :     s = rect2ps(w,x,y,T);
    1481             :   else
    1482           0 :     pari_err_TYPE("plotexport [unknown format]", fmt);
    1483         107 :   return strtoGENstr(s);
    1484             : }
    1485             : 
    1486             : static void
    1487          60 : Draw(PARI_plot *T, GEN w, GEN x, GEN y)
    1488          60 : { if (T->draw) T->draw(T, w,x,y); else get_plot_null(NULL); }
    1489             : static void
    1490       20124 : set_range(double m, double M, double *sml, double *big)
    1491             : {
    1492       20124 :   if (M - m < 1.e-9)
    1493             :   {
    1494           0 :     double d = fabs(m)/10; if (!d) d = 0.1;
    1495           0 :     M += d; m -= d;
    1496             :   }
    1497       20124 :   *sml = m; *big = M;
    1498       20124 : }
    1499             : /* Plot a dblPointList. Complete with axes, bounding box, etc.
    1500             :  *
    1501             :  * data is an array of structs. Its meaning depends on flags :
    1502             :  *
    1503             :  * + data[0] contains global extremas, the number of curves to plot
    1504             :  *   (data[0].nb) and a list of doubles (first set of x-coordinates).
    1505             :  *
    1506             :  * + data[i].nb (i>0) contains the number of points in the list
    1507             :  *   data[i].d (hopefully, data[2i].nb=data[2i+1].nb when i>0...)
    1508             :  *
    1509             :  * + If flags contain PLOT_PARAMETRIC, the array length should be
    1510             :  *   even, and successive pairs (data[2i].d, data[2i+1].d) represent
    1511             :  *   curves to plot.
    1512             :  *
    1513             :  * + If there is no such flag, the first element is an array with
    1514             :  *   x-coordinates and the following ones contain y-coordinates.
    1515             :  * If W != NULL, output wrt this PARI_plot using two drawing rectangles:
    1516             :  * one for labels, another for graphs. Else draw to rectwindow ne without
    1517             :  * labels.
    1518             :  * If fmt != NULL (requires W != NULL), output is a t_STR containing the
    1519             :  * converted picture, else a bounding box */
    1520             : static GEN
    1521       10062 : plotrecthrawin(GEN fmt, PARI_plot *W, long ne, dblPointList *data, long flags)
    1522             : {
    1523       10062 :   const long param = flags & (PLOT_PARAMETRIC|PLOT_COMPLEX);
    1524       10062 :   const long max_graphcolors = lg(GP_DATA->graphcolors)-1;
    1525       10062 :   const pari_sp av = avma;
    1526             :   dblPointList x, y;
    1527             :   double xsml, xbig, ysml, ybig;
    1528             :   long ltype, i, nc, w[3], wx[3], wy[3];
    1529             : 
    1530       10062 :   if (!data) return cgetg(1,t_VEC);
    1531       10062 :   x = data[0]; nc = x.nb;
    1532       10062 :   set_range(x.xsml, x.xbig, &xsml, &xbig);
    1533       10062 :   set_range(x.ysml, x.ybig, &ysml, &ybig);
    1534       10062 :   if (W)
    1535             :   { /* actual output; else output to rectwindow: no labels */
    1536          86 :     const long se = NUMRECT-2;
    1537             :     long lm, rm, tm, bm;
    1538             :     char YBIG[16], YSML[16], XSML[16], XBIG[16];
    1539             :     /* left/right/top/bottom margin */
    1540          86 :     sprintf(YSML,"%.5g", ysml); sprintf(YBIG,"%.5g", ybig);
    1541          86 :     sprintf(XSML,"%.5g", xsml); sprintf(XBIG,"%.5g", xbig);
    1542             :     /* left margin has y labels with hgap on both sides of text */
    1543          86 :     lm = maxss(strlen(YSML),strlen(YBIG))*W->fwidth + 2*W->hunit-1;
    1544          86 :     rm = W->hunit-1;
    1545          86 :     tm = W->vunit-1;
    1546          86 :     bm = W->vunit+W->fheight-1;
    1547          86 :     w[0] = wx[0] = wy[0] = evaltyp(t_VECSMALL) | evallg(3);
    1548          86 :     w[1] = se; wx[1] = 0;  wy[1] = 0;
    1549          86 :     w[2] = ne; wx[2] = lm; wy[2] = tm;
    1550             :    /* Window (width x height) is given in pixels, correct pixels are 0..n-1,
    1551             :     * whereas rect functions work with windows whose pixel range is [0,n] */
    1552          86 :     initrect_i(se, W->width - 1, W->height - 1);
    1553          86 :     initrect_i(ne, W->width - (lm+rm) - 1, W->height - (tm+bm) - 1);
    1554             :     /* draw labels on se */
    1555          86 :     _move(se,lm,0); plotstring(se, YBIG, RoSTdirRIGHT|RoSTdirHGAP|RoSTdirTOP);
    1556          86 :     _move(se,lm,W->height-bm); plotstring(se,YSML, RoSTdirRIGHT|RoSTdirHGAP|RoSTdirVGAP);
    1557          86 :     _move(se,lm,W->height-bm); plotstring(se, XSML, RoSTdirLEFT|RoSTdirTOP);
    1558          86 :     _move(se,W->width-rm-1, W->height-bm); plotstring(se, XBIG, RoSTdirRIGHT|RoSTdirTOP);
    1559             :   }
    1560       10062 :   if (!(flags & PLOT_NO_RESCALE)) plotscale0(ne, xsml, xbig, ysml, ybig);
    1561       10062 :   if (!(flags & PLOT_NO_FRAME))
    1562             :   {
    1563         172 :     long fl = (flags & PLOT_NODOUBLETICK)? TICKS_CLOCKW|TICKS_NODOUBLE
    1564          86 :                                          : TICKS_CLOCKW;
    1565             :     PARI_plot T, *pl;
    1566          86 :     if (W) pl = W; else { pl = &T; pari_get_plot(pl); }
    1567          86 :     plotlinetype(ne, -2); /* frame */
    1568          86 :     current_color[ne] = colormap_to_color(DEFAULT_COLOR);
    1569          86 :     _move(ne,xsml,ysml);
    1570          86 :     _box(ne,xbig,ybig);
    1571          86 :     if (!(flags & PLOT_NO_TICK_X)) {
    1572          86 :       rectticks(pl, ne, xsml, ysml, xbig, ysml, xsml, xbig, fl);
    1573          86 :       rectticks(pl, ne, xbig, ybig, xsml, ybig, xbig, xsml, fl);
    1574             :     }
    1575          86 :     if (!(flags & PLOT_NO_TICK_Y)) {
    1576          86 :       rectticks(pl, ne, xbig, ysml, xbig, ybig, ysml, ybig, fl);
    1577          86 :       rectticks(pl, ne, xsml, ybig, xsml, ysml, ybig, ysml, fl);
    1578             :     }
    1579             :   }
    1580       10062 :   if (!(flags & PLOT_NO_AXE_Y) && (xsml<=0 && xbig >=0))
    1581             :   {
    1582          72 :     plotlinetype(ne, -1); /* axes */
    1583          72 :     current_color[ne] = colormap_to_color(AXIS_COLOR);
    1584          72 :     _move(ne,0.0,ysml);
    1585          72 :     _line(ne,0.0,ybig);
    1586             :   }
    1587       10062 :   if (!(flags & PLOT_NO_AXE_X) && (ysml<=0 && ybig >=0))
    1588             :   {
    1589          72 :     plotlinetype(ne, -1); /* axes */
    1590          72 :     current_color[ne] = colormap_to_color(AXIS_COLOR);
    1591          72 :     _move(ne,xsml,0.0);
    1592          72 :     _line(ne,xbig,0.0);
    1593             :   }
    1594             : 
    1595       10062 :   if (param) {
    1596        6040 :     i = 0;
    1597        6040 :     flags |= PLOT_PARAMETRIC;
    1598        6040 :     flags &= (~PLOT_COMPLEX); /* turn COMPLEX to PARAMETRIC*/
    1599        4022 :   } else i = 1;
    1600       20132 :   for (ltype = 0; ltype < nc; ltype++)
    1601             :   {
    1602       10074 :     long c = GP_DATA->graphcolors[1+(ltype%max_graphcolors)];
    1603       10074 :     current_color[ne] = colormap_to_color(c);
    1604       10070 :     if (param) x = data[i++];
    1605             : 
    1606       10070 :     y = data[i++];
    1607       10070 :     if (flags & (PLOT_POINTS_LINES|PLOT_POINTS)) {
    1608          18 :       plotlinetype(ne, plotpoint_itype + ltype); /* Graphs */
    1609          18 :       plotpointtype(ne,plotpoint_itype + ltype); /* Graphs */
    1610          18 :       plotpoints0(ne, x.d, y.d, y.nb);
    1611          18 :       if (!(flags & PLOT_POINTS_LINES)) continue;
    1612             :     }
    1613             : 
    1614       10052 :     if (flags & PLOT_SPLINES) {
    1615             :       /* rectsplines will call us back with ltype == 0 */
    1616           8 :       int old = rectline_itype;
    1617           8 :       rectline_itype = rectline_itype + ltype;
    1618           8 :       rectsplines(ne, x.d, y.d, y.nb, flags);
    1619           8 :       rectline_itype = old;
    1620             :     } else {
    1621       10044 :       plotlinetype(ne, rectline_itype + ltype); /* Graphs */
    1622       10044 :       rectlines0(ne, x.d, y.d, y.nb, 0);
    1623             :     }
    1624             :   }
    1625       10058 :   for (i--; i>=0; i--) pari_free(data[i].d);
    1626       10058 :   pari_free(data);
    1627             : 
    1628       10058 :   if (W)
    1629             :   {
    1630          82 :     GEN s = NULL;
    1631          82 :     if (fmt) s = fmt_convert(fmt, w, wx, wy, W); else Draw(W, w,wx,wy);
    1632          82 :     plotkill(w[1]);
    1633          82 :     plotkill(w[2]);
    1634          82 :     if (fmt) return s;
    1635             :   }
    1636       10008 :   set_avma(av);
    1637       10008 :   retmkvec4(dbltor(xsml), dbltor(xbig), dbltor(ysml), dbltor(ybig));
    1638             : }
    1639             : 
    1640             : /*************************************************************************/
    1641             : /*                                                                       */
    1642             : /*                          HI-RES FUNCTIONS                             */
    1643             : /*                                                                       */
    1644             : /*************************************************************************/
    1645             : /* If T != NULL, draw using the attached graphic (using rectwindow ne as a temp)
    1646             :  * Else write to rectwindow 'ne'.
    1647             :  * Graph y=f(x), x=a..b, use n points */
    1648             : static GEN
    1649       10052 : plotrecth_i(GEN fmt, void *E, GEN(*f)(void*,GEN), PARI_plot *T, long ne,
    1650             :             GEN a,GEN b, ulong flags,long n, long prec)
    1651             : {
    1652       10052 :   pari_sp av = avma;
    1653       10052 :   dblPointList *pl = plotrecthin(E,f, a,b, flags, n, prec);
    1654       10036 :   set_avma(av); return plotrecthrawin(fmt, T, ne, pl, flags);
    1655             : }
    1656             : GEN
    1657        9976 : plotrecth(void *E, GEN(*f)(void*,GEN), long ne, GEN a,GEN b,
    1658             :           ulong flags, long n, long prec)
    1659        9976 : { return plotrecth_i(NULL, E,f, NULL, ne, a,b, flags&~PLOT_PARA, n, prec); }
    1660             : GEN
    1661           0 : plotrecth0(long ne, GEN a,GEN b,GEN code,ulong flags,long n, long prec)
    1662           0 : { EXPR_WRAP(code, plotrecth(EXPR_ARG, ne, a,b, flags, n, prec)); }
    1663             : GEN
    1664          44 : ploth(void *E, GEN(*f)(void*,GEN), GEN a, GEN b,long flags, long n, long prec)
    1665             : {
    1666          44 :   PARI_plot T; pari_get_plot(&T);
    1667          44 :   return plotrecth_i(NULL, E,f, &T, NUMRECT-1, a,b, flags&~PLOT_PARA,n, prec);
    1668             : }
    1669             : GEN
    1670           0 : parploth(GEN a, GEN b, GEN code, long flags, long n, long prec)
    1671             : {
    1672           0 :   PARI_plot T; pari_get_plot(&T);
    1673           0 :   return plotrecth_i(NULL, code,gp_call,&T, NUMRECT-1, a,b, flags|PLOT_PARA,n, prec);
    1674             : }
    1675             : GEN
    1676          44 : ploth0(GEN a, GEN b, GEN code, long flags,long n, long prec)
    1677          44 : { EXPR_WRAP(code, ploth(EXPR_ARG, a,b,flags,n, prec)); }
    1678             : GEN
    1679           0 : psploth(void *E, GEN(*f)(void*,GEN), GEN a,GEN b, long flags, long n, long prec)
    1680             : {
    1681           0 :   PARI_plot T; pari_get_psplot(&T); T.draw = &_psdraw;
    1682           0 :   return plotrecth_i(NULL, E,f, &T, NUMRECT-1, a,b, flags&~PLOT_PARA,n, prec);
    1683             : }
    1684             : GEN
    1685           0 : psploth0(GEN a, GEN b, GEN code, long flags, long n, long prec)
    1686           0 : { EXPR_WRAP(code, psploth(EXPR_ARG, a, b, flags, n, prec)); }
    1687             : GEN
    1688          32 : plothexport(GEN fmt, void *E, GEN(*f)(void*,GEN), GEN a,GEN b, long flags,
    1689             :             long n, long prec)
    1690             : {
    1691          32 :   pari_sp av = avma;
    1692             :   GEN s;
    1693          32 :   PARI_plot T; pari_get_fmtplot(fmt, &T);
    1694          32 :   s = plotrecth_i(fmt, E,f, &T, NUMRECT-1, a,b, flags&~PLOT_PARA,n, prec);
    1695          32 :   return gerepileuptoleaf(av, s);
    1696             : }
    1697             : GEN
    1698          32 : plothexport0(GEN fmt, GEN a, GEN b, GEN code, long flags, long n, long prec)
    1699          32 : { EXPR_WRAP(code, plothexport(fmt, EXPR_ARG, a, b, flags, n, prec)); }
    1700             : 
    1701             : /* Draw list of points */
    1702             : static GEN
    1703          26 : plotrecthraw_i(GEN fmt, PARI_plot *T, long ne, GEN data, long flags)
    1704             : {
    1705          26 :   dblPointList *pl = gtodblList(data,flags);
    1706          26 :   return plotrecthrawin(fmt, T, ne, pl, flags);
    1707             : }
    1708             : static GEN
    1709          26 : plothraw_i(GEN fmt, PARI_plot *T, GEN X, GEN Y, long flag)
    1710             : {
    1711          26 :   pari_sp av = avma;
    1712          26 :   switch (flag) {
    1713          18 :     case 0: flag = PLOT_PARAMETRIC|PLOT_POINTS; break;
    1714           8 :     case 1: flag = PLOT_PARAMETRIC; break;
    1715           0 :     default: flag |= PLOT_PARAMETRIC; break;
    1716             :   }
    1717          26 :   return gerepileupto(av, plotrecthraw_i(fmt, T, NUMRECT-1, mkvec2(X,Y), flag));
    1718             : }
    1719             : GEN
    1720           8 : plothraw(GEN X, GEN Y, long flags)
    1721           8 : { PARI_plot T; pari_get_plot(&T); return plothraw_i(NULL,&T,X,Y,flags); }
    1722             : GEN
    1723           0 : psplothraw(GEN X, GEN Y, long flags)
    1724           0 : { PARI_plot T; pari_get_psplot(&T); T.draw = &_psdraw;
    1725           0 :   return plothraw_i(NULL,&T,X,Y,flags); }
    1726             : GEN
    1727           0 : plotrecthraw(long ne, GEN data, long flags)
    1728           0 : { return plotrecthraw_i(NULL, NULL, ne, data, flags); }
    1729             : GEN
    1730          18 : plothrawexport(GEN fmt, GEN X, GEN Y, long flags)
    1731          18 : { PARI_plot T; pari_get_fmtplot(fmt,&T); return plothraw_i(fmt,&T,X,Y,flags); }
    1732             : 
    1733             : GEN
    1734           4 : plothsizes(long flag)
    1735             : {
    1736           4 :   GEN vect = cgetg(1+8,t_VEC);
    1737             :   PARI_plot T;
    1738             : 
    1739           4 :   pari_get_plot(&T);
    1740           4 :   gel(vect,1) = stoi(T.width);
    1741           4 :   gel(vect,2) = stoi(T.height);
    1742           4 :   if (flag) {
    1743           0 :     gel(vect,3) = dbltor(T.hunit*1.0/T.width);
    1744           0 :     gel(vect,4) = dbltor(T.vunit*1.0/T.height);
    1745           0 :     gel(vect,5) = dbltor(T.fwidth*1.0/T.width);
    1746           0 :     gel(vect,6) = dbltor(T.fheight*1.0/T.height);
    1747             :   } else {
    1748           4 :     gel(vect,3) = stoi(T.hunit);
    1749           4 :     gel(vect,4) = stoi(T.vunit);
    1750           4 :     gel(vect,5) = stoi(T.fwidth);
    1751           4 :     gel(vect,6) = stoi(T.fheight);
    1752             :   }
    1753           4 :   gel(vect,7) = stoi(T.dwidth);
    1754           4 :   gel(vect,8) = stoi(T.dheight);
    1755           4 :   return vect;
    1756             : }
    1757             : 
    1758             : /*************************************************************************/
    1759             : /*                                                                       */
    1760             : /*                         POSTSCRIPT OUTPUT                             */
    1761             : /*                                                                       */
    1762             : /*************************************************************************/
    1763             : static long
    1764          85 : wxy_n(GEN wxy)
    1765             : {
    1766             :   long n;
    1767          85 :   switch(typ(wxy))
    1768             :   {
    1769          15 :     case t_INT: return 1;
    1770             :     case t_VEC:
    1771          70 :       n = lg(wxy)-1;
    1772          70 :       if (n%3) pari_err_DIM("plotdraw");
    1773          70 :       return n/3;
    1774             :   }
    1775           0 :   pari_err_TYPE("plotdraw",wxy);
    1776             :   return 0;/*LCOV_EXCL_LINE*/
    1777             : }
    1778             : static void
    1779          85 : wxy_init(GEN wxy, GEN *pW, GEN *pX, GEN *pY, PARI_plot *T)
    1780             : {
    1781          85 :   long i, n = wxy_n(wxy);
    1782             :   GEN W, X, Y;
    1783          85 :   *pW = W = cgetg(n+1, t_VECSMALL); /* win number */
    1784          85 :   *pX = X = cgetg(n+1, t_VECSMALL);
    1785          85 :   *pY = Y = cgetg(n+1, t_VECSMALL); /* (x,y)-offset */
    1786          85 :   if (typ(wxy) == t_INT)
    1787             :   {
    1788          15 :     W[1] = itos(wxy); check_rect_init(W[1]);
    1789          15 :     X[1] = 0;
    1790          15 :     Y[1] = 0; return;
    1791             :   }
    1792         140 :   for (i = 1; i <= n; i++)
    1793             :   {
    1794          70 :     GEN w = gel(wxy,3*i-2), x = gel(wxy,3*i-1), y = gel(wxy,3*i);
    1795          70 :     if (typ(w) != t_INT) pari_err_TYPE("plotdraw",w);
    1796          70 :     W[i] = itos(w); check_rect_init(W[i]);
    1797          70 :     if (T) {
    1798           0 :       X[i] = DTOL(gtodouble(x)*(T->width - 1));
    1799           0 :       Y[i] = DTOL(gtodouble(y)*(T->height - 1));
    1800             :     } else {
    1801          70 :       X[i] = gtos(x);
    1802          70 :       Y[i] = gtos(y);
    1803             :     }
    1804             :   }
    1805             : }
    1806             : /* if flag is set, rescale wrt T */
    1807             : static void
    1808          28 : gendraw(PARI_plot *T, GEN wxy, long flag)
    1809             : {
    1810             :   GEN w, x, y, W, X, Y;
    1811             :   long i, l;
    1812          28 :   wxy_init(wxy, &w,&x,&y, flag? T: NULL);
    1813          28 :   l = lg(w);
    1814             :   /* malloc mandatory in case draw() forks then pari_close(). Done after
    1815             :    * wxy_init to avoid leak on error */
    1816          28 :   W = cgetalloc(t_VECSMALL, l);
    1817          28 :   X = cgetalloc(t_VECSMALL, l);
    1818          28 :   Y = cgetalloc(t_VECSMALL, l);
    1819          28 :   for (i = 1; i < l; i++) { W[i] = w[i]; X[i] = x[i]; Y[i] = y[i]; }
    1820          28 :   Draw(T,W,X,Y);
    1821          28 :   pari_free(W); pari_free(X); pari_free(Y);
    1822          28 : }
    1823             : void
    1824           0 : psdraw(GEN wxy, long flag)
    1825           0 : { PARI_plot T; pari_get_psplot(&T); T.draw = flag? &_psdraw: &_psdraw_scale;
    1826           0 :   gendraw(&T, wxy, flag); }
    1827             : void
    1828          28 : plotdraw(GEN wxy, long flag)
    1829          28 : { PARI_plot T; pari_get_plot(&T); gendraw(&T, wxy, flag); }
    1830             : GEN
    1831          57 : plotexport(GEN fmt, GEN wxy, long flag)
    1832             : {
    1833          57 :   pari_sp av = avma;
    1834             :   GEN w, x, y;
    1835          57 :   PARI_plot _T, *T = flag? &_T: NULL;
    1836          57 :   if (T) pari_get_plot(T);
    1837          57 :   wxy_init(wxy, &w, &x, &y, T);
    1838          57 :   return gerepileuptoleaf(av, fmt_convert(fmt, w, x, y, T));
    1839             : }
    1840             : 
    1841             : /* may be called after pari_close(): don't use the PARI stack */
    1842             : void
    1843         107 : gen_draw(struct plot_eng *eng, GEN w, GEN x, GEN y, double xs, double ys)
    1844             : {
    1845         107 :   void *data = eng->data;
    1846         107 :   long i, j, lw = lg(w);
    1847         107 :   long hgapsize = eng->pl->hunit, fheight = eng->pl->fheight;
    1848         107 :   long vgapsize = eng->pl->vunit,  fwidth = eng->pl->fwidth;
    1849         264 :   for(i = 1; i < lw; i++)
    1850             :   {
    1851         157 :     PariRect *e = &rectgraph[w[i]];
    1852             :     RectObj *R;
    1853         157 :     long x0 = x[i], y0 = y[i];
    1854        6752 :     for (R = RHead(e); R; R = RoNext(R))
    1855             :     {
    1856        6595 :       long col = RoCol(R);
    1857        6595 :       switch(RoType(R))
    1858             :       {
    1859             :       case ROt_PT:
    1860          64 :         eng->sc(data,col);
    1861          64 :         eng->pt(data, DTOL((RoPTx(R)+x0)*xs), DTOL((RoPTy(R)+y0)*ys));
    1862          64 :         break;
    1863             :       case ROt_LN:
    1864        5136 :         eng->sc(data,col);
    1865       15408 :         eng->ln(data, DTOL((RoLNx1(R)+x0)*xs), DTOL((RoLNy1(R)+y0)*ys),
    1866       10272 :                       DTOL((RoLNx2(R)+x0)*xs), DTOL((RoLNy2(R)+y0)*ys));
    1867        5136 :         break;
    1868             :       case ROt_BX:
    1869         132 :         eng->sc(data,col);
    1870         660 :         eng->bx(data,
    1871         132 :                 DTOL((RoBXx1(R)+x0)*xs),
    1872         132 :                 DTOL((RoBXy1(R)+y0)*ys),
    1873         132 :                 DTOL((RoBXx2(R)-RoBXx1(R))*xs),
    1874         132 :                 DTOL((RoBXy2(R)-RoBXy1(R))*ys));
    1875         132 :         break;
    1876             :       case ROt_FBX:
    1877           0 :         eng->sc(data,col);
    1878           0 :         eng->fb(data,
    1879           0 :                 DTOL((RoBXx1(R)+x0)*xs),
    1880           0 :                 DTOL((RoBXy1(R)+y0)*ys),
    1881           0 :                 DTOL((RoBXx2(R)-RoBXx1(R))*xs),
    1882           0 :                 DTOL((RoBXy2(R)-RoBXy1(R))*ys));
    1883           0 :         break;
    1884             :       case ROt_MP:
    1885             :         {
    1886          53 :           double *ptx = RoMPxs(R);
    1887          53 :           double *pty = RoMPys(R);
    1888          53 :           long     nb = RoMPcnt(R);
    1889          53 :           struct plot_points *points =
    1890          53 :             (struct plot_points *) pari_malloc(sizeof(*points)*nb);
    1891        1841 :           for(j=0;j<nb;j++)
    1892             :           {
    1893        1788 :             points[j].x = DTOL((ptx[j]+x0)*xs);
    1894        1788 :             points[j].y = DTOL((pty[j]+y0)*ys);
    1895             :           }
    1896          53 :           eng->sc(data,col);
    1897          53 :           eng->mp(data, nb, points);
    1898          53 :           pari_free(points);
    1899          53 :           break;
    1900             :         }
    1901             :       case ROt_ML:
    1902             :         {
    1903          75 :           double *ptx = RoMLxs(R);
    1904          75 :           double *pty = RoMLys(R);
    1905          75 :           long     nb = RoMLcnt(R);
    1906          75 :           struct plot_points *points =
    1907          75 :             (struct plot_points *) pari_malloc(sizeof(*points)*nb);
    1908       21674 :           for(j=0;j<nb;j++)
    1909             :           {
    1910       21599 :             points[j].x = DTOL((ptx[j]+x0)*xs);
    1911       21599 :             points[j].y = DTOL((pty[j]+y0)*ys);
    1912             :           }
    1913          75 :           eng->sc(data,col);
    1914          75 :           eng->ml(data, nb, points);
    1915          75 :           pari_free(points);
    1916          75 :           break;
    1917             :         }
    1918             :       case ROt_ST:
    1919             :         {
    1920         236 :           long dir = RoSTdir(R);
    1921         236 :           long h = dir & RoSTdirHPOS_mask, hgap  = 0;
    1922         236 :           long v = dir & RoSTdirVPOS_mask, vgap  = 0;
    1923         236 :           long x, y, l = RoSTl(R);
    1924         236 :           long shift = (h == RoSTdirLEFT ? 0 : (h == RoSTdirRIGHT? 2: 1));
    1925         236 :           long vshift= (v == RoSTdirBOTTOM? 0: (v == RoSTdirTOP? 2: 1));
    1926         236 :           if (dir & RoSTdirHGAP)
    1927         100 :             hgap = (h == RoSTdirLEFT) ? hgapsize : -hgapsize;
    1928         236 :           if (dir & RoSTdirVGAP)
    1929          50 :             vgap = (v == RoSTdirBOTTOM) ? 2*vgapsize : -2*vgapsize;
    1930         236 :           x = DTOL(xs * (RoSTx(R) + x0 + hgap - (l * fwidth * shift)/2));
    1931         236 :           y = DTOL(ys * (RoSTy(R) + y0 - (vgap - vshift*(fheight-1))/2));
    1932         236 :           eng->sc(data,col);
    1933         236 :           eng->st(data, x, y, RoSTs(R), l);
    1934         236 :           break;
    1935             :         }
    1936             :       default:
    1937         899 :         break;
    1938             :       }
    1939             :     }
    1940             :   }
    1941         107 : }
    1942             : /*************************************************************************/
    1943             : /*                               SVG                                     */
    1944             : /*************************************************************************/
    1945             : 
    1946             : struct svg_data {
    1947             :   pari_str str;
    1948             :   char hexcolor[8];  /* "#rrggbb\0" */
    1949             : };
    1950             : #define data_str(d) (&((struct svg_data*)(d))->str)
    1951             : #define data_hexcolor(d) (((struct svg_data*)(d))->hexcolor)
    1952             : 
    1953             : /* Work with precision 1/scale */
    1954             : static const float SVG_SCALE = 1024.0;
    1955             : 
    1956             : static float
    1957       39706 : svg_rescale(float x) { return x / SVG_SCALE; }
    1958             : 
    1959             : static void
    1960         992 : svg_point(void *data, long x, long y)
    1961             : {
    1962         992 :   pari_str *S = data_str(data);
    1963             : 
    1964        1984 :   str_printf(S, "<circle cx='%.2f' cy='%.2f' r='0.5' ",
    1965        1984 :     svg_rescale(x), svg_rescale(y));
    1966         992 :   str_printf(S, "style='fill:%s;stroke:none;'/>", data_hexcolor(data));
    1967         992 : }
    1968             : 
    1969             : static void
    1970        2371 : svg_line(void *data, long x1, long y1, long x2, long y2)
    1971             : {
    1972        2371 :   pari_str *S = data_str(data);
    1973             : 
    1974        9484 :   str_printf(S, "<line x1='%.2f' y1='%.2f' x2='%.2f' y2='%.2f' ",
    1975        9484 :     svg_rescale(x1), svg_rescale(y1), svg_rescale(x2), svg_rescale(y2));
    1976        2371 :   str_printf(S, "style='fill:none;stroke:%s;'/>", data_hexcolor(data));
    1977        2371 : }
    1978             : 
    1979             : static void
    1980          76 : svg_rect(void *data, long x, long y, long w, long h)
    1981             : {
    1982          76 :   pari_str *S = data_str(data);
    1983             : 
    1984         304 :   str_printf(S, "<rect x='%.2f' y='%.2f' width='%.2f' height='%.2f' ",
    1985         304 :     svg_rescale(x), svg_rescale(y), svg_rescale(w), svg_rescale(h));
    1986          76 :   str_printf(S, "style='fill:none;stroke:%s;'/>", data_hexcolor(data));
    1987          76 : }
    1988             : 
    1989             : static void
    1990           0 : svg_fillrect(void *data, long x, long y, long w, long h)
    1991             : {
    1992           0 :   pari_str *S = data_str(data);
    1993           0 :   const char * color = data_hexcolor(data);
    1994           0 :   str_printf(S, "<rect x='%.2f' y='%.2f' width='%.2f' height='%.2f' ",
    1995           0 :     svg_rescale(x), svg_rescale(y), svg_rescale(w), svg_rescale(h));
    1996           0 :   str_printf(S, "style='fill:%s;stroke:%s;'/>", color, color);
    1997           0 : }
    1998             : 
    1999             : static void
    2000          32 : svg_points(void *data, long nb, struct plot_points *p)
    2001             : {
    2002             :   long i;
    2003         981 :   for (i = 0; i < nb; i++)
    2004         949 :     svg_point(data, p[i].x, p[i].y);
    2005          32 : }
    2006             : 
    2007             : static void
    2008        2765 : svg_color(void *data, long col)
    2009             : {
    2010             :   static const char hex[] = "0123456789abcdef";
    2011        2765 :   char *c = data_hexcolor(data);
    2012             :   int r, g, b;
    2013        2765 :   long_to_rgb(col, &r, &g, &b);
    2014        2765 :   c[0] = '#';
    2015        2765 :   c[1] = hex[r / 16];
    2016        2765 :   c[2] = hex[r & 15];
    2017        2765 :   c[3] = hex[g / 16];
    2018        2765 :   c[4] = hex[g & 15];
    2019        2765 :   c[5] = hex[b / 16];
    2020        2765 :   c[6] = hex[b & 15];
    2021        2765 :   c[7] = '\0';
    2022        2765 : }
    2023             : 
    2024             : static void
    2025          47 : svg_lines(void *data, long nb, struct plot_points *p)
    2026             : {
    2027             :   long i;
    2028          47 :   pari_str *S = data_str(data);
    2029             : 
    2030          47 :   str_printf(S, "<polyline points='");
    2031       13876 :   for (i = 0; i < nb; i++)
    2032             :   {
    2033       13829 :     if (i > 0) str_printf(S, " ");
    2034       13829 :     str_printf(S, "%.2f,%.2f", svg_rescale(p[i].x), svg_rescale(p[i].y));
    2035             :   }
    2036          47 :   str_printf(S, "' style='fill:none;stroke:%s;'/>", data_hexcolor(data));
    2037          47 : }
    2038             : 
    2039             : static void
    2040         138 : svg_text(void *data, long x, long y, char *text, long numtext)
    2041             : {
    2042         138 :   pari_str *S = data_str(data);
    2043             :   (void)numtext;
    2044         276 :   str_printf(S, "<text x='%.5f' y='%.5f' font-size='%ld' style='fill:%s;'>%s</text>",
    2045         276 :     svg_rescale(x),svg_rescale(y), 12, data_hexcolor(data), text);
    2046         138 : }
    2047             : 
    2048             : static void
    2049          58 : svg_head(PARI_plot *T, pari_str *S)
    2050             : {
    2051          58 :   str_printf(S, "<svg width='%ld' height='%ld' version='1.1' xmlns='http://www.w3.org/2000/svg'>", T->width, T->height);
    2052          58 : }
    2053             : 
    2054             : static void
    2055          58 : svg_tail(pari_str *S)
    2056             : {
    2057          58 :   str_printf(S, "</svg>");
    2058          58 : }
    2059             : 
    2060             : char *
    2061          58 : rect2svg(GEN w, GEN x, GEN y, PARI_plot *T)
    2062             : {
    2063             :   struct plot_eng pl;
    2064             :   struct svg_data data;
    2065             :   PARI_plot U;
    2066             : 
    2067          58 :   str_init(&data.str, 1);
    2068          58 :   svg_color(&data, 0);
    2069          58 :   if (!T)
    2070             :   {
    2071          29 :     long i, l = lg(w), xmax = 0, ymax = 0;
    2072          29 :     T = &U; pari_get_svgplot(T);
    2073          58 :     for (i = 1; i < l; i++)
    2074             :     {
    2075          29 :       PariRect *e = check_rect_init(w[i]);
    2076          29 :       xmax = maxss(xmax, RXsize(e) + x[i]);
    2077          29 :       ymax = maxss(ymax, RYsize(e) + y[i]);
    2078             :     }
    2079          29 :     T->width = xmax;
    2080          29 :     T->height = ymax;
    2081             :   }
    2082          58 :   pl.data = &data;
    2083          58 :   pl.sc = &svg_color;
    2084          58 :   pl.pt = &svg_point;
    2085          58 :   pl.ln = &svg_line;
    2086          58 :   pl.bx = &svg_rect;
    2087          58 :   pl.fb = &svg_fillrect;
    2088          58 :   pl.mp = &svg_points;
    2089          58 :   pl.ml = &svg_lines;
    2090          58 :   pl.st = &svg_text;
    2091          58 :   pl.pl = T;
    2092             : 
    2093          58 :   svg_head(T, &data.str);
    2094          58 :   gen_draw(&pl, w, x, y, SVG_SCALE, SVG_SCALE);
    2095          58 :   svg_tail(&data.str);
    2096             : 
    2097          58 :   return data.str.string;
    2098             : }
    2099             : 
    2100             : /*************************************************************************/
    2101             : /*                            POSTSCRIPT                                 */
    2102             : /*************************************************************************/
    2103             : static void
    2104        2989 : ps_sc(void *data, long col)
    2105             : {
    2106        2989 :   pari_str *S = (pari_str*)data;
    2107        2989 :   int r, g, b; long_to_rgb(col, &r, &g, &b);
    2108        2989 :   if (!r && !g && !b)
    2109        2940 :     str_puts(S,"c0\n");
    2110             :   else
    2111          49 :     str_printf(S,"%.6f %.6f %.6f c\n", r/255., g/255., b/255.);
    2112        2989 : }
    2113             : 
    2114             : static void
    2115         860 : ps_point(void *data, long x, long y)
    2116             : {
    2117         860 :   pari_str *S = (pari_str*)data;
    2118         860 :   str_printf(S,"%ld %ld p\n",y,x);
    2119         860 : }
    2120             : 
    2121             : static void
    2122        2765 : ps_line(void *data, long x1, long y1, long x2, long y2)
    2123             : {
    2124        2765 :   pari_str *S = (pari_str*)data;
    2125        2765 :   str_printf(S,"%ld %ld m %ld %ld l\n",y1,x1,y2,x2);
    2126        2765 :   str_printf(S,"stroke\n");
    2127        2765 : }
    2128             : 
    2129             : static void
    2130          56 : ps_rect(void *data, long x, long y, long w, long h)
    2131             : {
    2132          56 :   pari_str *S = (pari_str*)data;
    2133          56 :   str_printf(S,"%ld %ld m %ld %ld l %ld %ld l %ld %ld l closepath stroke\n",
    2134             :              y,x, y,x+w, y+h,x+w, y+h,x);
    2135          56 : }
    2136             : 
    2137             : static void
    2138           0 : ps_fillrect(void *data, long x, long y, long w, long h)
    2139             : {
    2140           0 :   pari_str *S = (pari_str*)data;
    2141           0 :   str_printf(S,"%ld %ld m %ld %ld l %ld %ld l %ld %ld l closepath fill\n",
    2142             :              y,x, y,x+w, y+h,x+w, y+h,x);
    2143           0 : }
    2144             : 
    2145             : static void
    2146          21 : ps_points(void *data, long nb, struct plot_points *p)
    2147             : {
    2148             :   long i;
    2149          21 :   for (i=0; i<nb; i++) ps_point(data, p[i].x, p[i].y);
    2150          21 : }
    2151             : 
    2152             : static void
    2153          28 : ps_lines(void *data, long nb, struct plot_points *p)
    2154             : {
    2155          28 :   pari_str *S = (pari_str*)data;
    2156             :   long i;
    2157          28 :   str_printf(S,"%ld %ld m\n",p[0].y,p[0].x);
    2158          28 :   for (i=1; i<nb; i++) str_printf(S, "%ld %ld l\n", p[i].y, p[i].x);
    2159          28 :   str_printf(S,"stroke\n");
    2160          28 : }
    2161             : 
    2162             : static void
    2163          98 : ps_string(void *data, long x, long y, char *s, long length)
    2164             : {
    2165          98 :   pari_str *S = (pari_str*)data;
    2166             :   (void)length;
    2167          98 :   if (strpbrk(s, "(\\)")) {
    2168           7 :     str_printf(S,"(");
    2169          49 :     while (*s) {
    2170          35 :       if ( *s=='(' || *s==')' || *s=='\\' ) str_putc(S,'\\');
    2171          35 :       str_putc(S, *s);
    2172          35 :       s++;
    2173             :     }
    2174             :   } else
    2175          91 :     str_printf(S,"(%s", s);
    2176          98 :   str_printf(S,") %ld %ld m 90 rotate show -90 rotate\n", y, x);
    2177          98 : }
    2178             : 
    2179             : char *
    2180          49 : rect2ps_i(GEN w, GEN x, GEN y, PARI_plot *T, int plotps)
    2181             : {
    2182             :   struct plot_eng pl;
    2183             :   PARI_plot U;
    2184             :   pari_str S;
    2185          49 :   double xs = 0.65, ys = 0.65;
    2186          49 :   if (T) /* res wrt T dimens */
    2187             :   {
    2188          21 :     if (plotps)
    2189           0 :       xs = ys = 1;
    2190             :     else
    2191             :     {
    2192          21 :       xs *= ((double)PS_WIDTH) / T->width;
    2193          21 :       ys *= ((double)PS_HEIGH) / T->height;
    2194             :     }
    2195             :   }
    2196             :   else
    2197             :   {
    2198          28 :     T = &U; pari_get_psplot(T);
    2199             :   }
    2200          49 :   str_init(&S, 1);
    2201             :   /* Definitions taken from post terminal of Gnuplot. */
    2202          49 :   str_printf(&S, "%%!\n\
    2203             : 50 50 translate\n\
    2204             : /p {moveto 0 2 rlineto 2 0 rlineto 0 -2 rlineto closepath fill} def\n\
    2205             : /c0 {0 0 0 setrgbcolor} def\n\
    2206             : /c {setrgbcolor} def\n\
    2207             : /l {lineto} def\n\
    2208             : /m {moveto} def\n"
    2209          49 : "/Times-Roman findfont %ld scalefont setfont\n", DTOL(T->fheight * xs));
    2210             : 
    2211          49 :   pl.sc = &ps_sc;
    2212          49 :   pl.pt = &ps_point;
    2213          49 :   pl.ln = &ps_line;
    2214          49 :   pl.bx = &ps_rect;
    2215          49 :   pl.fb = &ps_fillrect;
    2216          49 :   pl.mp = &ps_points;
    2217          49 :   pl.ml = &ps_lines;
    2218          49 :   pl.st = &ps_string;
    2219          49 :   pl.pl = T;
    2220          49 :   pl.data = (void*)&S;
    2221             : 
    2222          49 :   if (plotps) str_printf(&S,"0 %ld translate -90 rotate\n", T->height - 50);
    2223          49 :   gen_draw(&pl, w, x, y, xs, ys);
    2224          49 :   str_puts(&S,"stroke showpage\n");
    2225          49 :   *S.cur = 0; return S.string;
    2226             : }
    2227             : char *
    2228          49 : rect2ps(GEN w, GEN x, GEN y, PARI_plot *T)
    2229          49 : { return rect2ps_i(w,x,y,T,0); }
    2230             : 
    2231             : void
    2232           0 : pari_plot_by_file(const char *env, const char *suf, const char *img)
    2233             : {
    2234           0 :   const char *cmd, *s = pari_unique_filename_suffix("plotfile", suf);
    2235           0 :   FILE *f = fopen(s, "w");
    2236           0 :   if (!f) pari_err_FILE("image file", s);
    2237           0 :   fputs(img, f); (void)fclose(f);
    2238           0 :   cmd = os_getenv(env);
    2239             : #ifdef GP_MIME_OPEN
    2240           0 :   if (!cmd) cmd = GP_MIME_OPEN;
    2241             : #else
    2242             :   if (!cmd) cmd = "open -W";
    2243             : #endif
    2244           0 :   cmd = pari_sprintf("%s \"%s\" 2>/dev/null", cmd, s);
    2245           0 :   gpsystem(cmd);
    2246           0 :   pari_unlink(s);
    2247           0 :   pari_free((char*)s);
    2248           0 : }
    2249             : 
    2250             : /*************************************************************************/
    2251             : /*                                                                       */
    2252             : /*                           RGB COLORS                                  */
    2253             : /*                                                                       */
    2254             : /*************************************************************************/
    2255             : /* generated from /etc/X11/rgb.txt by the following perl script
    2256             : #!/usr/bin/perl
    2257             : while(<>)
    2258             : {
    2259             :   ($hex, $name) = split(/\t\t/, $_);
    2260             :   $hex =~ s/^ +//; chomp($name); $name =~ s, *,,g;
    2261             :   $hex = sprintf("0x%02x%02x%02x", split(/\s+/, $hex));
    2262             :   $name = lc($name); next if ($done{$name});
    2263             :   $done{$name} = 1;
    2264             :   print "COL(\"$name\", $hex),\n";
    2265             : }
    2266             : */
    2267             : 
    2268             : #define COL(x,y) {(void*)x,(void*)y,0,NULL}
    2269             : static hashentry col_list[] = {
    2270             : COL("", 0x000000),
    2271             : COL("snow", 0xfffafa),
    2272             : COL("ghostwhite", 0xf8f8ff),
    2273             : COL("whitesmoke", 0xf5f5f5),
    2274             : COL("gainsboro", 0xdcdcdc),
    2275             : COL("floralwhite", 0xfffaf0),
    2276             : COL("oldlace", 0xfdf5e6),
    2277             : COL("linen", 0xfaf0e6),
    2278             : COL("antiquewhite", 0xfaebd7),
    2279             : COL("papayawhip", 0xffefd5),
    2280             : COL("blanchedalmond", 0xffebcd),
    2281             : COL("bisque", 0xffe4c4),
    2282             : COL("peachpuff", 0xffdab9),
    2283             : COL("navajowhite", 0xffdead),
    2284             : COL("moccasin", 0xffe4b5),
    2285             : COL("cornsilk", 0xfff8dc),
    2286             : COL("ivory", 0xfffff0),
    2287             : COL("lemonchiffon", 0xfffacd),
    2288             : COL("seashell", 0xfff5ee),
    2289             : COL("honeydew", 0xf0fff0),
    2290             : COL("mintcream", 0xf5fffa),
    2291             : COL("azure", 0xf0ffff),
    2292             : COL("aliceblue", 0xf0f8ff),
    2293             : COL("lavender", 0xe6e6fa),
    2294             : COL("lavenderblush", 0xfff0f5),
    2295             : COL("mistyrose", 0xffe4e1),
    2296             : COL("white", 0xffffff),
    2297             : COL("black", 0x000000),
    2298             : COL("darkslategray", 0x2f4f4f),
    2299             : COL("darkslategrey", 0x2f4f4f),
    2300             : COL("dimgray", 0x696969),
    2301             : COL("dimgrey", 0x696969),
    2302             : COL("slategray", 0x708090),
    2303             : COL("slategrey", 0x708090),
    2304             : COL("lightslategray", 0x778899),
    2305             : COL("lightslategrey", 0x778899),
    2306             : COL("gray", 0xbebebe),
    2307             : COL("grey", 0xbebebe),
    2308             : COL("lightgrey", 0xd3d3d3),
    2309             : COL("lightgray", 0xd3d3d3),
    2310             : COL("midnightblue", 0x191970),
    2311             : COL("navy", 0x000080),
    2312             : COL("navyblue", 0x000080),
    2313             : COL("cornflowerblue", 0x6495ed),
    2314             : COL("darkslateblue", 0x483d8b),
    2315             : COL("slateblue", 0x6a5acd),
    2316             : COL("mediumslateblue", 0x7b68ee),
    2317             : COL("lightslateblue", 0x8470ff),
    2318             : COL("mediumblue", 0x0000cd),
    2319             : COL("royalblue", 0x4169e1),
    2320             : COL("blue", 0x0000ff),
    2321             : COL("dodgerblue", 0x1e90ff),
    2322             : COL("deepskyblue", 0x00bfff),
    2323             : COL("skyblue", 0x87ceeb),
    2324             : COL("lightskyblue", 0x87cefa),
    2325             : COL("steelblue", 0x4682b4),
    2326             : COL("lightsteelblue", 0xb0c4de),
    2327             : COL("lightblue", 0xadd8e6),
    2328             : COL("powderblue", 0xb0e0e6),
    2329             : COL("paleturquoise", 0xafeeee),
    2330             : COL("darkturquoise", 0x00ced1),
    2331             : COL("mediumturquoise", 0x48d1cc),
    2332             : COL("turquoise", 0x40e0d0),
    2333             : COL("cyan", 0x00ffff),
    2334             : COL("lightcyan", 0xe0ffff),
    2335             : COL("cadetblue", 0x5f9ea0),
    2336             : COL("mediumaquamarine", 0x66cdaa),
    2337             : COL("aquamarine", 0x7fffd4),
    2338             : COL("darkgreen", 0x006400),
    2339             : COL("darkolivegreen", 0x556b2f),
    2340             : COL("darkseagreen", 0x8fbc8f),
    2341             : COL("seagreen", 0x2e8b57),
    2342             : COL("mediumseagreen", 0x3cb371),
    2343             : COL("lightseagreen", 0x20b2aa),
    2344             : COL("palegreen", 0x98fb98),
    2345             : COL("springgreen", 0x00ff7f),
    2346             : COL("lawngreen", 0x7cfc00),
    2347             : COL("green", 0x00ff00),
    2348             : COL("chartreuse", 0x7fff00),
    2349             : COL("mediumspringgreen", 0x00fa9a),
    2350             : COL("greenyellow", 0xadff2f),
    2351             : COL("limegreen", 0x32cd32),
    2352             : COL("yellowgreen", 0x9acd32),
    2353             : COL("forestgreen", 0x228b22),
    2354             : COL("olivedrab", 0x6b8e23),
    2355             : COL("darkkhaki", 0xbdb76b),
    2356             : COL("khaki", 0xf0e68c),
    2357             : COL("palegoldenrod", 0xeee8aa),
    2358             : COL("lightgoldenrodyellow", 0xfafad2),
    2359             : COL("lightyellow", 0xffffe0),
    2360             : COL("yellow", 0xffff00),
    2361             : COL("gold", 0xffd700),
    2362             : COL("lightgoldenrod", 0xeedd82),
    2363             : COL("goldenrod", 0xdaa520),
    2364             : COL("darkgoldenrod", 0xb8860b),
    2365             : COL("rosybrown", 0xbc8f8f),
    2366             : COL("indianred", 0xcd5c5c),
    2367             : COL("saddlebrown", 0x8b4513),
    2368             : COL("sienna", 0xa0522d),
    2369             : COL("peru", 0xcd853f),
    2370             : COL("burlywood", 0xdeb887),
    2371             : COL("beige", 0xf5f5dc),
    2372             : COL("wheat", 0xf5deb3),
    2373             : COL("sandybrown", 0xf4a460),
    2374             : COL("tan", 0xd2b48c),
    2375             : COL("chocolate", 0xd2691e),
    2376             : COL("firebrick", 0xb22222),
    2377             : COL("brown", 0xa52a2a),
    2378             : COL("darksalmon", 0xe9967a),
    2379             : COL("salmon", 0xfa8072),
    2380             : COL("lightsalmon", 0xffa07a),
    2381             : COL("orange", 0xffa500),
    2382             : COL("darkorange", 0xff8c00),
    2383             : COL("coral", 0xff7f50),
    2384             : COL("lightcoral", 0xf08080),
    2385             : COL("tomato", 0xff6347),
    2386             : COL("orangered", 0xff4500),
    2387             : COL("red", 0xff0000),
    2388             : COL("hotpink", 0xff69b4),
    2389             : COL("deeppink", 0xff1493),
    2390             : COL("pink", 0xffc0cb),
    2391             : COL("lightpink", 0xffb6c1),
    2392             : COL("palevioletred", 0xdb7093),
    2393             : COL("maroon", 0xb03060),
    2394             : COL("mediumvioletred", 0xc71585),
    2395             : COL("violetred", 0xd02090),
    2396             : COL("magenta", 0xff00ff),
    2397             : COL("violet", 0xee82ee),
    2398             : COL("plum", 0xdda0dd),
    2399             : COL("orchid", 0xda70d6),
    2400             : COL("mediumorchid", 0xba55d3),
    2401             : COL("darkorchid", 0x9932cc),
    2402             : COL("darkviolet", 0x9400d3),
    2403             : COL("blueviolet", 0x8a2be2),
    2404             : COL("purple", 0xa020f0),
    2405             : COL("mediumpurple", 0x9370db),
    2406             : COL("thistle", 0xd8bfd8),
    2407             : COL("snow1", 0xfffafa),
    2408             : COL("snow2", 0xeee9e9),
    2409             : COL("snow3", 0xcdc9c9),
    2410             : COL("snow4", 0x8b8989),
    2411             : COL("seashell1", 0xfff5ee),
    2412             : COL("seashell2", 0xeee5de),
    2413             : COL("seashell3", 0xcdc5bf),
    2414             : COL("seashell4", 0x8b8682),
    2415             : COL("antiquewhite1", 0xffefdb),
    2416             : COL("antiquewhite2", 0xeedfcc),
    2417             : COL("antiquewhite3", 0xcdc0b0),
    2418             : COL("antiquewhite4", 0x8b8378),
    2419             : COL("bisque1", 0xffe4c4),
    2420             : COL("bisque2", 0xeed5b7),
    2421             : COL("bisque3", 0xcdb79e),
    2422             : COL("bisque4", 0x8b7d6b),
    2423             : COL("peachpuff1", 0xffdab9),
    2424             : COL("peachpuff2", 0xeecbad),
    2425             : COL("peachpuff3", 0xcdaf95),
    2426             : COL("peachpuff4", 0x8b7765),
    2427             : COL("navajowhite1", 0xffdead),
    2428             : COL("navajowhite2", 0xeecfa1),
    2429             : COL("navajowhite3", 0xcdb38b),
    2430             : COL("navajowhite4", 0x8b795e),
    2431             : COL("lemonchiffon1", 0xfffacd),
    2432             : COL("lemonchiffon2", 0xeee9bf),
    2433             : COL("lemonchiffon3", 0xcdc9a5),
    2434             : COL("lemonchiffon4", 0x8b8970),
    2435             : COL("cornsilk1", 0xfff8dc),
    2436             : COL("cornsilk2", 0xeee8cd),
    2437             : COL("cornsilk3", 0xcdc8b1),
    2438             : COL("cornsilk4", 0x8b8878),
    2439             : COL("ivory1", 0xfffff0),
    2440             : COL("ivory2", 0xeeeee0),
    2441             : COL("ivory3", 0xcdcdc1),
    2442             : COL("ivory4", 0x8b8b83),
    2443             : COL("honeydew1", 0xf0fff0),
    2444             : COL("honeydew2", 0xe0eee0),
    2445             : COL("honeydew3", 0xc1cdc1),
    2446             : COL("honeydew4", 0x838b83),
    2447             : COL("lavenderblush1", 0xfff0f5),
    2448             : COL("lavenderblush2", 0xeee0e5),
    2449             : COL("lavenderblush3", 0xcdc1c5),
    2450             : COL("lavenderblush4", 0x8b8386),
    2451             : COL("mistyrose1", 0xffe4e1),
    2452             : COL("mistyrose2", 0xeed5d2),
    2453             : COL("mistyrose3", 0xcdb7b5),
    2454             : COL("mistyrose4", 0x8b7d7b),
    2455             : COL("azure1", 0xf0ffff),
    2456             : COL("azure2", 0xe0eeee),
    2457             : COL("azure3", 0xc1cdcd),
    2458             : COL("azure4", 0x838b8b),
    2459             : COL("slateblue1", 0x836fff),
    2460             : COL("slateblue2", 0x7a67ee),
    2461             : COL("slateblue3", 0x6959cd),
    2462             : COL("slateblue4", 0x473c8b),
    2463             : COL("royalblue1", 0x4876ff),
    2464             : COL("royalblue2", 0x436eee),
    2465             : COL("royalblue3", 0x3a5fcd),
    2466             : COL("royalblue4", 0x27408b),
    2467             : COL("blue1", 0x0000ff),
    2468             : COL("blue2", 0x0000ee),
    2469             : COL("blue3", 0x0000cd),
    2470             : COL("blue4", 0x00008b),
    2471             : COL("dodgerblue1", 0x1e90ff),
    2472             : COL("dodgerblue2", 0x1c86ee),
    2473             : COL("dodgerblue3", 0x1874cd),
    2474             : COL("dodgerblue4", 0x104e8b),
    2475             : COL("steelblue1", 0x63b8ff),
    2476             : COL("steelblue2", 0x5cacee),
    2477             : COL("steelblue3", 0x4f94cd),
    2478             : COL("steelblue4", 0x36648b),
    2479             : COL("deepskyblue1", 0x00bfff),
    2480             : COL("deepskyblue2", 0x00b2ee),
    2481             : COL("deepskyblue3", 0x009acd),
    2482             : COL("deepskyblue4", 0x00688b),
    2483             : COL("skyblue1", 0x87ceff),
    2484             : COL("skyblue2", 0x7ec0ee),
    2485             : COL("skyblue3", 0x6ca6cd),
    2486             : COL("skyblue4", 0x4a708b),
    2487             : COL("lightskyblue1", 0xb0e2ff),
    2488             : COL("lightskyblue2", 0xa4d3ee),
    2489             : COL("lightskyblue3", 0x8db6cd),
    2490             : COL("lightskyblue4", 0x607b8b),
    2491             : COL("slategray1", 0xc6e2ff),
    2492             : COL("slategray2", 0xb9d3ee),
    2493             : COL("slategray3", 0x9fb6cd),
    2494             : COL("slategray4", 0x6c7b8b),
    2495             : COL("lightsteelblue1", 0xcae1ff),
    2496             : COL("lightsteelblue2", 0xbcd2ee),
    2497             : COL("lightsteelblue3", 0xa2b5cd),
    2498             : COL("lightsteelblue4", 0x6e7b8b),
    2499             : COL("lightblue1", 0xbfefff),
    2500             : COL("lightblue2", 0xb2dfee),
    2501             : COL("lightblue3", 0x9ac0cd),
    2502             : COL("lightblue4", 0x68838b),
    2503             : COL("lightcyan1", 0xe0ffff),
    2504             : COL("lightcyan2", 0xd1eeee),
    2505             : COL("lightcyan3", 0xb4cdcd),
    2506             : COL("lightcyan4", 0x7a8b8b),
    2507             : COL("paleturquoise1", 0xbbffff),
    2508             : COL("paleturquoise2", 0xaeeeee),
    2509             : COL("paleturquoise3", 0x96cdcd),
    2510             : COL("paleturquoise4", 0x668b8b),
    2511             : COL("cadetblue1", 0x98f5ff),
    2512             : COL("cadetblue2", 0x8ee5ee),
    2513             : COL("cadetblue3", 0x7ac5cd),
    2514             : COL("cadetblue4", 0x53868b),
    2515             : COL("turquoise1", 0x00f5ff),
    2516             : COL("turquoise2", 0x00e5ee),
    2517             : COL("turquoise3", 0x00c5cd),
    2518             : COL("turquoise4", 0x00868b),
    2519             : COL("cyan1", 0x00ffff),
    2520             : COL("cyan2", 0x00eeee),
    2521             : COL("cyan3", 0x00cdcd),
    2522             : COL("cyan4", 0x008b8b),
    2523             : COL("darkslategray1", 0x97ffff),
    2524             : COL("darkslategray2", 0x8deeee),
    2525             : COL("darkslategray3", 0x79cdcd),
    2526             : COL("darkslategray4", 0x528b8b),
    2527             : COL("aquamarine1", 0x7fffd4),
    2528             : COL("aquamarine2", 0x76eec6),
    2529             : COL("aquamarine3", 0x66cdaa),
    2530             : COL("aquamarine4", 0x458b74),
    2531             : COL("darkseagreen1", 0xc1ffc1),
    2532             : COL("darkseagreen2", 0xb4eeb4),
    2533             : COL("darkseagreen3", 0x9bcd9b),
    2534             : COL("darkseagreen4", 0x698b69),
    2535             : COL("seagreen1", 0x54ff9f),
    2536             : COL("seagreen2", 0x4eee94),
    2537             : COL("seagreen3", 0x43cd80),
    2538             : COL("seagreen4", 0x2e8b57),
    2539             : COL("palegreen1", 0x9aff9a),
    2540             : COL("palegreen2", 0x90ee90),
    2541             : COL("palegreen3", 0x7ccd7c),
    2542             : COL("palegreen4", 0x548b54),
    2543             : COL("springgreen1", 0x00ff7f),
    2544             : COL("springgreen2", 0x00ee76),
    2545             : COL("springgreen3", 0x00cd66),
    2546             : COL("springgreen4", 0x008b45),
    2547             : COL("green1", 0x00ff00),
    2548             : COL("green2", 0x00ee00),
    2549             : COL("green3", 0x00cd00),
    2550             : COL("green4", 0x008b00),
    2551             : COL("chartreuse1", 0x7fff00),
    2552             : COL("chartreuse2", 0x76ee00),
    2553             : COL("chartreuse3", 0x66cd00),
    2554             : COL("chartreuse4", 0x458b00),
    2555             : COL("olivedrab1", 0xc0ff3e),
    2556             : COL("olivedrab2", 0xb3ee3a),
    2557             : COL("olivedrab3", 0x9acd32),
    2558             : COL("olivedrab4", 0x698b22),
    2559             : COL("darkolivegreen1", 0xcaff70),
    2560             : COL("darkolivegreen2", 0xbcee68),
    2561             : COL("darkolivegreen3", 0xa2cd5a),
    2562             : COL("darkolivegreen4", 0x6e8b3d),
    2563             : COL("khaki1", 0xfff68f),
    2564             : COL("khaki2", 0xeee685),
    2565             : COL("khaki3", 0xcdc673),
    2566             : COL("khaki4", 0x8b864e),
    2567             : COL("lightgoldenrod1", 0xffec8b),
    2568             : COL("lightgoldenrod2", 0xeedc82),
    2569             : COL("lightgoldenrod3", 0xcdbe70),
    2570             : COL("lightgoldenrod4", 0x8b814c),
    2571             : COL("lightyellow1", 0xffffe0),
    2572             : COL("lightyellow2", 0xeeeed1),
    2573             : COL("lightyellow3", 0xcdcdb4),
    2574             : COL("lightyellow4", 0x8b8b7a),
    2575             : COL("yellow1", 0xffff00),
    2576             : COL("yellow2", 0xeeee00),
    2577             : COL("yellow3", 0xcdcd00),
    2578             : COL("yellow4", 0x8b8b00),
    2579             : COL("gold1", 0xffd700),
    2580             : COL("gold2", 0xeec900),
    2581             : COL("gold3", 0xcdad00),
    2582             : COL("gold4", 0x8b7500),
    2583             : COL("goldenrod1", 0xffc125),
    2584             : COL("goldenrod2", 0xeeb422),
    2585             : COL("goldenrod3", 0xcd9b1d),
    2586             : COL("goldenrod4", 0x8b6914),
    2587             : COL("darkgoldenrod1", 0xffb90f),
    2588             : COL("darkgoldenrod2", 0xeead0e),
    2589             : COL("darkgoldenrod3", 0xcd950c),
    2590             : COL("darkgoldenrod4", 0x8b6508),
    2591             : COL("rosybrown1", 0xffc1c1),
    2592             : COL("rosybrown2", 0xeeb4b4),
    2593             : COL("rosybrown3", 0xcd9b9b),
    2594             : COL("rosybrown4", 0x8b6969),
    2595             : COL("indianred1", 0xff6a6a),
    2596             : COL("indianred2", 0xee6363),
    2597             : COL("indianred3", 0xcd5555),
    2598             : COL("indianred4", 0x8b3a3a),
    2599             : COL("sienna1", 0xff8247),
    2600             : COL("sienna2", 0xee7942),
    2601             : COL("sienna3", 0xcd6839),
    2602             : COL("sienna4", 0x8b4726),
    2603             : COL("burlywood1", 0xffd39b),
    2604             : COL("burlywood2", 0xeec591),
    2605             : COL("burlywood3", 0xcdaa7d),
    2606             : COL("burlywood4", 0x8b7355),
    2607             : COL("wheat1", 0xffe7ba),
    2608             : COL("wheat2", 0xeed8ae),
    2609             : COL("wheat3", 0xcdba96),
    2610             : COL("wheat4", 0x8b7e66),
    2611             : COL("tan1", 0xffa54f),
    2612             : COL("tan2", 0xee9a49),
    2613             : COL("tan3", 0xcd853f),
    2614             : COL("tan4", 0x8b5a2b),
    2615             : COL("chocolate1", 0xff7f24),
    2616             : COL("chocolate2", 0xee7621),
    2617             : COL("chocolate3", 0xcd661d),
    2618             : COL("chocolate4", 0x8b4513),
    2619             : COL("firebrick1", 0xff3030),
    2620             : COL("firebrick2", 0xee2c2c),
    2621             : COL("firebrick3", 0xcd2626),
    2622             : COL("firebrick4", 0x8b1a1a),
    2623             : COL("brown1", 0xff4040),
    2624             : COL("brown2", 0xee3b3b),
    2625             : COL("brown3", 0xcd3333),
    2626             : COL("brown4", 0x8b2323),
    2627             : COL("salmon1", 0xff8c69),
    2628             : COL("salmon2", 0xee8262),
    2629             : COL("salmon3", 0xcd7054),
    2630             : COL("salmon4", 0x8b4c39),
    2631             : COL("lightsalmon1", 0xffa07a),
    2632             : COL("lightsalmon2", 0xee9572),
    2633             : COL("lightsalmon3", 0xcd8162),
    2634             : COL("lightsalmon4", 0x8b5742),
    2635             : COL("orange1", 0xffa500),
    2636             : COL("orange2", 0xee9a00),
    2637             : COL("orange3", 0xcd8500),
    2638             : COL("orange4", 0x8b5a00),
    2639             : COL("darkorange1", 0xff7f00),
    2640             : COL("darkorange2", 0xee7600),
    2641             : COL("darkorange3", 0xcd6600),
    2642             : COL("darkorange4", 0x8b4500),
    2643             : COL("coral1", 0xff7256),
    2644             : COL("coral2", 0xee6a50),
    2645             : COL("coral3", 0xcd5b45),
    2646             : COL("coral4", 0x8b3e2f),
    2647             : COL("tomato1", 0xff6347),
    2648             : COL("tomato2", 0xee5c42),
    2649             : COL("tomato3", 0xcd4f39),
    2650             : COL("tomato4", 0x8b3626),
    2651             : COL("orangered1", 0xff4500),
    2652             : COL("orangered2", 0xee4000),
    2653             : COL("orangered3", 0xcd3700),
    2654             : COL("orangered4", 0x8b2500),
    2655             : COL("red1", 0xff0000),
    2656             : COL("red2", 0xee0000),
    2657             : COL("red3", 0xcd0000),
    2658             : COL("red4", 0x8b0000),
    2659             : COL("debianred", 0xd70751),
    2660             : COL("deeppink1", 0xff1493),
    2661             : COL("deeppink2", 0xee1289),
    2662             : COL("deeppink3", 0xcd1076),
    2663             : COL("deeppink4", 0x8b0a50),
    2664             : COL("hotpink1", 0xff6eb4),
    2665             : COL("hotpink2", 0xee6aa7),
    2666             : COL("hotpink3", 0xcd6090),
    2667             : COL("hotpink4", 0x8b3a62),
    2668             : COL("pink1", 0xffb5c5),
    2669             : COL("pink2", 0xeea9b8),
    2670             : COL("pink3", 0xcd919e),
    2671             : COL("pink4", 0x8b636c),
    2672             : COL("lightpink1", 0xffaeb9),
    2673             : COL("lightpink2", 0xeea2ad),
    2674             : COL("lightpink3", 0xcd8c95),
    2675             : COL("lightpink4", 0x8b5f65),
    2676             : COL("palevioletred1", 0xff82ab),
    2677             : COL("palevioletred2", 0xee799f),
    2678             : COL("palevioletred3", 0xcd6889),
    2679             : COL("palevioletred4", 0x8b475d),
    2680             : COL("maroon1", 0xff34b3),
    2681             : COL("maroon2", 0xee30a7),
    2682             : COL("maroon3", 0xcd2990),
    2683             : COL("maroon4", 0x8b1c62),
    2684             : COL("violetred1", 0xff3e96),
    2685             : COL("violetred2", 0xee3a8c),
    2686             : COL("violetred3", 0xcd3278),
    2687             : COL("violetred4", 0x8b2252),
    2688             : COL("magenta1", 0xff00ff),
    2689             : COL("magenta2", 0xee00ee),
    2690             : COL("magenta3", 0xcd00cd),
    2691             : COL("magenta4", 0x8b008b),
    2692             : COL("orchid1", 0xff83fa),
    2693             : COL("orchid2", 0xee7ae9),
    2694             : COL("orchid3", 0xcd69c9),
    2695             : COL("orchid4", 0x8b4789),
    2696             : COL("plum1", 0xffbbff),
    2697             : COL("plum2", 0xeeaeee),
    2698             : COL("plum3", 0xcd96cd),
    2699             : COL("plum4", 0x8b668b),
    2700             : COL("mediumorchid1", 0xe066ff),
    2701             : COL("mediumorchid2", 0xd15fee),
    2702             : COL("mediumorchid3", 0xb452cd),
    2703             : COL("mediumorchid4", 0x7a378b),
    2704             : COL("darkorchid1", 0xbf3eff),
    2705             : COL("darkorchid2", 0xb23aee),
    2706             : COL("darkorchid3", 0x9a32cd),
    2707             : COL("darkorchid4", 0x68228b),
    2708             : COL("purple1", 0x9b30ff),
    2709             : COL("purple2", 0x912cee),
    2710             : COL("purple3", 0x7d26cd),
    2711             : COL("purple4", 0x551a8b),
    2712             : COL("mediumpurple1", 0xab82ff),
    2713             : COL("mediumpurple2", 0x9f79ee),
    2714             : COL("mediumpurple3", 0x8968cd),
    2715             : COL("mediumpurple4", 0x5d478b),
    2716             : COL("thistle1", 0xffe1ff),
    2717             : COL("thistle2", 0xeed2ee),
    2718             : COL("thistle3", 0xcdb5cd),
    2719             : COL("thistle4", 0x8b7b8b),
    2720             : COL("gray0", 0x000000),
    2721             : COL("grey0", 0x000000),
    2722             : COL("gray1", 0x030303),
    2723             : COL("grey1", 0x030303),
    2724             : COL("gray2", 0x050505),
    2725             : COL("grey2", 0x050505),
    2726             : COL("gray3", 0x080808),
    2727             : COL("grey3", 0x080808),
    2728             : COL("gray4", 0x0a0a0a),
    2729             : COL("grey4", 0x0a0a0a),
    2730             : COL("gray5", 0x0d0d0d),
    2731             : COL("grey5", 0x0d0d0d),
    2732             : COL("gray6", 0x0f0f0f),
    2733             : COL("grey6", 0x0f0f0f),
    2734             : COL("gray7", 0x121212),
    2735             : COL("grey7", 0x121212),
    2736             : COL("gray8", 0x141414),
    2737             : COL("grey8", 0x141414),
    2738             : COL("gray9", 0x171717),
    2739             : COL("grey9", 0x171717),
    2740             : COL("gray10", 0x1a1a1a),
    2741             : COL("grey10", 0x1a1a1a),
    2742             : COL("gray11", 0x1c1c1c),
    2743             : COL("grey11", 0x1c1c1c),
    2744             : COL("gray12", 0x1f1f1f),
    2745             : COL("grey12", 0x1f1f1f),
    2746             : COL("gray13", 0x212121),
    2747             : COL("grey13", 0x212121),
    2748             : COL("gray14", 0x242424),
    2749             : COL("grey14", 0x242424),
    2750             : COL("gray15", 0x262626),
    2751             : COL("grey15", 0x262626),
    2752             : COL("gray16", 0x292929),
    2753             : COL("grey16", 0x292929),
    2754             : COL("gray17", 0x2b2b2b),
    2755             : COL("grey17", 0x2b2b2b),
    2756             : COL("gray18", 0x2e2e2e),
    2757             : COL("grey18", 0x2e2e2e),
    2758             : COL("gray19", 0x303030),
    2759             : COL("grey19", 0x303030),
    2760             : COL("gray20", 0x333333),
    2761             : COL("grey20", 0x333333),
    2762             : COL("gray21", 0x363636),
    2763             : COL("grey21", 0x363636),
    2764             : COL("gray22", 0x383838),
    2765             : COL("grey22", 0x383838),
    2766             : COL("gray23", 0x3b3b3b),
    2767             : COL("grey23", 0x3b3b3b),
    2768             : COL("gray24", 0x3d3d3d),
    2769             : COL("grey24", 0x3d3d3d),
    2770             : COL("gray25", 0x404040),
    2771             : COL("grey25", 0x404040),
    2772             : COL("gray26", 0x424242),
    2773             : COL("grey26", 0x424242),
    2774             : COL("gray27", 0x454545),
    2775             : COL("grey27", 0x454545),
    2776             : COL("gray28", 0x474747),
    2777             : COL("grey28", 0x474747),
    2778             : COL("gray29", 0x4a4a4a),
    2779             : COL("grey29", 0x4a4a4a),
    2780             : COL("gray30", 0x4d4d4d),
    2781             : COL("grey30", 0x4d4d4d),
    2782             : COL("gray31", 0x4f4f4f),
    2783             : COL("grey31", 0x4f4f4f),
    2784             : COL("gray32", 0x525252),
    2785             : COL("grey32", 0x525252),
    2786             : COL("gray33", 0x545454),
    2787             : COL("grey33", 0x545454),
    2788             : COL("gray34", 0x575757),
    2789             : COL("grey34", 0x575757),
    2790             : COL("gray35", 0x595959),
    2791             : COL("grey35", 0x595959),
    2792             : COL("gray36", 0x5c5c5c),
    2793             : COL("grey36", 0x5c5c5c),
    2794             : COL("gray37", 0x5e5e5e),
    2795             : COL("grey37", 0x5e5e5e),
    2796             : COL("gray38", 0x616161),
    2797             : COL("grey38", 0x616161),
    2798             : COL("gray39", 0x636363),
    2799             : COL("grey39", 0x636363),
    2800             : COL("gray40", 0x666666),
    2801             : COL("grey40", 0x666666),
    2802             : COL("gray41", 0x696969),
    2803             : COL("grey41", 0x696969),
    2804             : COL("gray42", 0x6b6b6b),
    2805             : COL("grey42", 0x6b6b6b),
    2806             : COL("gray43", 0x6e6e6e),
    2807             : COL("grey43", 0x6e6e6e),
    2808             : COL("gray44", 0x707070),
    2809             : COL("grey44", 0x707070),
    2810             : COL("gray45", 0x737373),
    2811             : COL("grey45", 0x737373),
    2812             : COL("gray46", 0x757575),
    2813             : COL("grey46", 0x757575),
    2814             : COL("gray47", 0x787878),
    2815             : COL("grey47", 0x787878),
    2816             : COL("gray48", 0x7a7a7a),
    2817             : COL("grey48", 0x7a7a7a),
    2818             : COL("gray49", 0x7d7d7d),
    2819             : COL("grey49", 0x7d7d7d),
    2820             : COL("gray50", 0x7f7f7f),
    2821             : COL("grey50", 0x7f7f7f),
    2822             : COL("gray51", 0x828282),
    2823             : COL("grey51", 0x828282),
    2824             : COL("gray52", 0x858585),
    2825             : COL("grey52", 0x858585),
    2826             : COL("gray53", 0x878787),
    2827             : COL("grey53", 0x878787),
    2828             : COL("gray54", 0x8a8a8a),
    2829             : COL("grey54", 0x8a8a8a),
    2830             : COL("gray55", 0x8c8c8c),
    2831             : COL("grey55", 0x8c8c8c),
    2832             : COL("gray56", 0x8f8f8f),
    2833             : COL("grey56", 0x8f8f8f),
    2834             : COL("gray57", 0x919191),
    2835             : COL("grey57", 0x919191),
    2836             : COL("gray58", 0x949494),
    2837             : COL("grey58", 0x949494),
    2838             : COL("gray59", 0x969696),
    2839             : COL("grey59", 0x969696),
    2840             : COL("gray60", 0x999999),
    2841             : COL("grey60", 0x999999),
    2842             : COL("gray61", 0x9c9c9c),
    2843             : COL("grey61", 0x9c9c9c),
    2844             : COL("gray62", 0x9e9e9e),
    2845             : COL("grey62", 0x9e9e9e),
    2846             : COL("gray63", 0xa1a1a1),
    2847             : COL("grey63", 0xa1a1a1),
    2848             : COL("gray64", 0xa3a3a3),
    2849             : COL("grey64", 0xa3a3a3),
    2850             : COL("gray65", 0xa6a6a6),
    2851             : COL("grey65", 0xa6a6a6),
    2852             : COL("gray66", 0xa8a8a8),
    2853             : COL("grey66", 0xa8a8a8),
    2854             : COL("gray67", 0xababab),
    2855             : COL("grey67", 0xababab),
    2856             : COL("gray68", 0xadadad),
    2857             : COL("grey68", 0xadadad),
    2858             : COL("gray69", 0xb0b0b0),
    2859             : COL("grey69", 0xb0b0b0),
    2860             : COL("gray70", 0xb3b3b3),
    2861             : COL("grey70", 0xb3b3b3),
    2862             : COL("gray71", 0xb5b5b5),
    2863             : COL("grey71", 0xb5b5b5),
    2864             : COL("gray72", 0xb8b8b8),
    2865             : COL("grey72", 0xb8b8b8),
    2866             : COL("gray73", 0xbababa),
    2867             : COL("grey73", 0xbababa),
    2868             : COL("gray74", 0xbdbdbd),
    2869             : COL("grey74", 0xbdbdbd),
    2870             : COL("gray75", 0xbfbfbf),
    2871             : COL("grey75", 0xbfbfbf),
    2872             : COL("gray76", 0xc2c2c2),
    2873             : COL("grey76", 0xc2c2c2),
    2874             : COL("gray77", 0xc4c4c4),
    2875             : COL("grey77", 0xc4c4c4),
    2876             : COL("gray78", 0xc7c7c7),
    2877             : COL("grey78", 0xc7c7c7),
    2878             : COL("gray79", 0xc9c9c9),
    2879             : COL("grey79", 0xc9c9c9),
    2880             : COL("gray80", 0xcccccc),
    2881             : COL("grey80", 0xcccccc),
    2882             : COL("gray81", 0xcfcfcf),
    2883             : COL("grey81", 0xcfcfcf),
    2884             : COL("gray82", 0xd1d1d1),
    2885             : COL("grey82", 0xd1d1d1),
    2886             : COL("gray83", 0xd4d4d4),
    2887             : COL("grey83", 0xd4d4d4),
    2888             : COL("gray84", 0xd6d6d6),
    2889             : COL("grey84", 0xd6d6d6),
    2890             : COL("gray85", 0xd9d9d9),
    2891             : COL("grey85", 0xd9d9d9),
    2892             : COL("gray86", 0xdbdbdb),
    2893             : COL("grey86", 0xdbdbdb),
    2894             : COL("gray87", 0xdedede),
    2895             : COL("grey87", 0xdedede),
    2896             : COL("gray88", 0xe0e0e0),
    2897             : COL("grey88", 0xe0e0e0),
    2898             : COL("gray89", 0xe3e3e3),
    2899             : COL("grey89", 0xe3e3e3),
    2900             : COL("gray90", 0xe5e5e5),
    2901             : COL("grey90", 0xe5e5e5),
    2902             : COL("gray91", 0xe8e8e8),
    2903             : COL("grey91", 0xe8e8e8),
    2904             : COL("gray92", 0xebebeb),
    2905             : COL("grey92", 0xebebeb),
    2906             : COL("gray93", 0xededed),
    2907             : COL("grey93", 0xededed),
    2908             : COL("gray94", 0xf0f0f0),
    2909             : COL("grey94", 0xf0f0f0),
    2910             : COL("gray95", 0xf2f2f2),
    2911             : COL("grey95", 0xf2f2f2),
    2912             : COL("gray96", 0xf5f5f5),
    2913             : COL("grey96", 0xf5f5f5),
    2914             : COL("gray97", 0xf7f7f7),
    2915             : COL("grey97", 0xf7f7f7),
    2916             : COL("gray98", 0xfafafa),
    2917             : COL("grey98", 0xfafafa),
    2918             : COL("gray99", 0xfcfcfc),
    2919             : COL("grey99", 0xfcfcfc),
    2920             : COL("gray100", 0xffffff),
    2921             : COL("grey100", 0xffffff),
    2922             : COL("darkgrey", 0xa9a9a9),
    2923             : COL("darkgray", 0xa9a9a9),
    2924             : COL("darkblue", 0x00008b),
    2925             : COL("darkcyan", 0x008b8b),
    2926             : COL("darkmagenta", 0x8b008b),
    2927             : COL("darkred", 0x8b0000),
    2928             : COL("lightgreen", 0x90ee90),
    2929             : COL(NULL,0) /* sentinel */
    2930             : };
    2931             : #undef COL
    2932             : 
    2933             : void
    2934       16293 : long_to_rgb(long c, int *r, int *g, int *b)
    2935             : {
    2936       16293 :   *b = c & 0xff; c >>= 8;
    2937       16293 :   *g = c & 0xff; c >>= 8;
    2938       16293 :   *r = c;
    2939       16293 : }
    2940             : static int
    2941           0 : hex2(const char *s)
    2942             : {
    2943           0 :   int m = 0, c = 0, i;
    2944           0 :   for (i = 0; i < 2; i++, s++)
    2945             :   {
    2946           0 :     if (*s >= '0' && *s <= '9')
    2947           0 :       c = *s - '0';
    2948           0 :     else if (*s >= 'A' && *s <= 'F')
    2949           0 :       c = *s - 'A' + 10;
    2950           0 :     else if (*s >= 'a' && *s <= 'f')
    2951           0 :       c = *s - 'a' + 10;
    2952           0 :     else pari_err(e_MISC,"incorrect hexadecimal number: %s", s);
    2953           0 :     m = 16*m + c;
    2954             :   }
    2955           0 :   return m;
    2956             : }
    2957             : void
    2958       10539 : colorname_to_rgb(const char *s, int *r, int *g, int *b)
    2959             : {
    2960       10539 :   if (!rgb_colors) rgb_colors = hashstr_import_static(col_list, 1000);
    2961       10539 :   if (*s == '#' && strlen(s) == 7)
    2962             :   {
    2963           0 :     *r = hex2(s+1);
    2964           0 :     *g = hex2(s+3);
    2965           0 :     *b = hex2(s+5);
    2966             :   }
    2967             :   else
    2968             :   {
    2969       10539 :     hashentry *ep = hash_search(rgb_colors, (void*)s);
    2970       10539 :     if (!ep) pari_err(e_MISC, "unknown color %s", s);
    2971       10539 :     long_to_rgb((long)ep->val, r,g,b);
    2972             :   }
    2973       10539 : }
    2974             : 
    2975             : static void
    2976           0 : chk_8bit(int v, GEN c)
    2977           0 : { if (v & ~0xff) pari_err(e_MISC, "invalid RGB code: %Ps", c); }
    2978             : void
    2979       10539 : color_to_rgb(GEN c, int *r, int *g, int *b)
    2980             : {
    2981       10539 :   switch(typ(c))
    2982             :   {
    2983             :     case t_STR:
    2984       10539 :       colorname_to_rgb(GSTR(c), r,g,b);
    2985       10539 :       break;
    2986             :     default: /* t_VECSMALL: */
    2987           0 :       *r = c[1]; chk_8bit(*r, c);
    2988           0 :       *g = c[2]; chk_8bit(*g, c);
    2989           0 :       *b = c[3]; chk_8bit(*b, c);
    2990           0 :       break;
    2991             :   }
    2992       10539 : }

Generated by: LCOV version 1.13