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

Generated by: LCOV version 1.13