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

Generated by: LCOV version 1.11