home *** CD-ROM | disk | FTP | other *** search
/ Fractal Creations (Second Edition) / FRACTALS_2E.iso / frasrc.exe / CALCFRAC.C < prev    next >
C/C++ Source or Header  |  1993-08-21  |  87KB  |  2,906 lines

  1. /*
  2. CALCFRAC.C contains the high level ("engine") code for calculating the
  3. fractal images (well, SOMEBODY had to do it!).
  4. Original author Tim Wegner, but just about ALL the authors have contributed
  5. SOME code to this routine at one time or another, or contributed to one of
  6. the many massive restructurings.
  7. This module is linked as an overlay, use ENTER_OVLY and EXIT_OVLY.
  8. The following modules work very closely with CALCFRAC.C:
  9.   FRACTALS.C    the fractal-specific code for escape-time fractals.
  10.   FRACSUBR.C    assorted subroutines belonging mainly to calcfrac.
  11.   CALCMAND.ASM    fast Mandelbrot/Julia integer implementation
  12. Additional fractal-specific modules are also invoked from CALCFRAC:
  13.   LORENZ.C    engine level and fractal specific code for attractors.
  14.   JB.C        julibrot logic
  15.   PARSER.C    formula fractals
  16.   and more
  17.  -------------------------------------------------------------------- */
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <float.h>
  23. #include <limits.h>
  24. #ifndef XFRACT
  25. #include <dos.h>
  26. #endif
  27. #include <limits.h>
  28. #include "fractint.h"
  29. #include "fractype.h"
  30. #include "mpmath.h"
  31. #include "targa_lc.h"
  32. #include "prototyp.h"
  33.  
  34. /* routines in this module    */
  35.  
  36. static void perform_worklist(void);
  37. static int  OneOrTwoPass(void);
  38. static int  _fastcall StandardCalc(int);
  39. static int  _fastcall potential(double,int);
  40. static void decomposition(void);
  41. static int  bound_trace_main(void);
  42. static void step_col_row(void);
  43. static int  _fastcall boundary_trace(int,int);
  44. static int  _fastcall calc_xy(int,int);
  45. static int  _fastcall fillseg1(int,int,int,int);
  46. static int  _fastcall fillseg(int,int,int,int);
  47. static void _fastcall reverse_string(BYTE *,BYTE *,int);
  48. static int  solidguess(void);
  49. static int  _fastcall guessrow(int,int,int);
  50. static void _fastcall plotblock(int,int,int,int);
  51. static void _fastcall setsymmetry(int,int);
  52. static int  _fastcall xsym_split(int,int);
  53. static int  _fastcall ysym_split(int,int);
  54. static void set_Plasma_palette(void);
  55. static U16 _fastcall adjust(int xa,int ya,int x,int y,int xb,int yb);
  56. static void _fastcall subDivide(int x1,int y1,int x2,int y2);
  57. static int _fastcall new_subD (int x1,int y1,int x2,int y2, int recur);
  58. static void verhulst(void);
  59. static void Bif_Period_Init(void);
  60. static int  _fastcall Bif_Periodic(int);
  61. static void set_Cellular_palette(void);
  62.  
  63. /**CJLT new function prototypes: */
  64. static int tesseral(void);
  65. static int _fastcall tesschkcol(int,int,int);
  66. static int _fastcall tesschkrow(int,int,int);
  67. static int _fastcall tesscol(int,int,int);
  68. static int _fastcall tessrow(int,int,int);
  69. /* added for testing autologmap() */
  70. static int autologmap(void);
  71.  
  72. extern _CMPLX initorbit;
  73. extern char useinitorbit;
  74. _LCMPLX linitorbit;
  75. extern int showdot;
  76. extern unsigned int decoderline[];
  77. extern int overflow;
  78. long lmagnitud, llimit, llimit2, lclosenuff, l16triglim;
  79. _CMPLX init,tmp,old,new,saved;
  80. extern int biomorph,usr_biomorph;
  81. extern _LCMPLX linit;
  82. extern int basin;
  83. extern int cpu;
  84. extern char savename[80];   /* save files using this name */
  85. extern int resave_flag;
  86. extern int started_resaves;
  87. extern int dotmode;
  88. extern int save_release;    /* use this to maintain compatibility */
  89.  
  90. int color, oldcolor, row, col, passes;
  91. int realcolor;
  92. int iterations, invert;
  93. double f_radius,f_xcenter, f_ycenter; /* for inversion */
  94. extern double far *dx0, far *dy0;
  95. extern double far *dx1, far *dy1;
  96.  
  97. extern int LogFlag;
  98. extern BYTE far *LogTable;
  99. extern int rangeslen;
  100. extern int far *ranges;
  101.  
  102. void (_fastcall *plot)(int,int,int) = putcolor;
  103. typedef void (_fastcall *PLOT)(int,int,int);
  104.  
  105. extern double inversion[];        /* inversion radius, f_xcenter, f_ycenter */
  106. extern int    xdots, ydots;        /* coordinates of dots on the screen  */
  107. extern int    sxdots,sydots;
  108. extern int    sxoffs,syoffs;
  109. extern int    colors;         /* maximum colors available */
  110. extern int    andcolor;        /* colors-1         */
  111. extern int    inside;         /* "inside" color to use    */
  112. extern int    fillcolor;         /* "fillcolor" color to use    */
  113. extern int    outside;        /* "outside" color to use   */
  114. extern int    finattract;
  115. double        min_orbit;        /* orbit value closest to origin */
  116. int        min_index;        /* iteration of min_orbit */
  117. extern int    maxit;            /* try this many iterations */
  118. extern int    fractype;        /* fractal type */
  119. extern char    stdcalcmode;        /* '1', '2', 'g', 'b' */
  120. extern int    debugflag;        /* for debugging purposes */
  121. extern    int    diskvideo;        /* for disk-video klooges */
  122. extern int    calc_status;        /* status of calculations */
  123. extern long    calctime;        /* total calc time for image */
  124.  
  125. extern int rflag, rseed;
  126. extern int decomp[];
  127. extern int distest,distestwidth;
  128.  
  129. extern double    param[];        /* parameters */
  130. extern int    potflag;        /* potential enabled? */
  131. extern double    potparam[];        /* potential parameters */
  132. extern int    pot16bit;        /* store 16bit continuous potential */
  133. extern long    far *lx0, far *ly0; /* X, Y points */
  134. extern long    far *lx1, far *ly1; /* X, Y points */
  135. extern long    fudge;            /* fudge factor (2**n) */
  136. extern int    bitshift;        /* bit shift for fudge */
  137. extern long    delmin;         /* for periodicity checking */
  138.  
  139. extern double xxmin,xxmax,yymin,yymax,xx3rd,yy3rd; /* corners */
  140. extern long   xmin, xmax, ymin, ymax;           /* integer equivs */
  141. extern long   delx,dely;               /* X, Y increments */
  142. extern double delxx,delxx2,delyy,delyy2;
  143. double deltaX, deltaY;
  144. double magnitude, rqlim, rqlim2, rqlim_save;
  145. extern _CMPLX parm,parm2;
  146. int (*calctype)();
  147. double closenuff;
  148. int pixelpi; /* value of pi in pixels */
  149. unsigned long lm;        /* magnitude limit (CALCMAND) */
  150. extern long linitx,linity;    /* in calcmand */
  151. extern unsigned long savedmask; /* in calcmand */
  152. extern int TranspSymmetry;
  153.  
  154. /* ORBIT variables */
  155. int    show_orbit;            /* flag to turn on and off */
  156. int    orbit_ptr;            /* pointer into save_orbit array */
  157. int far *save_orbit;            /* array to save orbit values */
  158. int    orbit_color=15;         /* XOR color */
  159.  
  160. int    ixstart, ixstop, iystart, iystop;    /* start, stop here */
  161. int    symmetry;       /* symmetry flag */
  162. int    reset_periodicity; /* nonzero if escape time pixel rtn to reset */
  163. int    kbdcount, max_kbdcount;    /* avoids checking keyboard too often */
  164.  
  165. extern    int    integerfractal;     /* TRUE if fractal uses integer math */
  166.  
  167. char far *resume_info = NULL;        /* pointer to resume info if allocated */
  168. int resuming;                /* nonzero if resuming after interrupt */
  169. int num_worklist;            /* resume worklist for standard engine */
  170. struct workliststuff worklist[MAXCALCWORK];
  171. int xxstart,xxstop;            /* these are same as worklist, */
  172. int yystart,yystop,yybegin;        /* declared as separate items  */
  173. int workpass,worksym;            /* for the sake of calcmand    */
  174.  
  175. extern long timer_interval;        /* timer(...) total */
  176.  
  177. VOIDFARPTR typespecific_workarea = NULL;
  178.  
  179. static double dem_delta, dem_width;    /* distance estimator variables */
  180. static double dem_toobig;
  181. #define DEM_BAILOUT 535.5  /* (pb: not sure if this is special or arbitrary) */
  182.  
  183. /* variables which must be visible for tab_display */
  184. int got_status; /* -1 if not, 0 for 1or2pass, 1 for ssg, 2 for btm, 3 for 3d */
  185. int curpass,totpasses;
  186. int currow,curcol;
  187.  
  188. /* static vars for solidguess & its subroutines */
  189. static int maxblock,halfblock;
  190. static int guessplot;            /* paint 1st pass row at a time?   */
  191. static int right_guess,bottom_guess;
  192. #define maxyblk 7    /* maxxblk*maxyblk*2 <= 4096, the size of "prefix" */
  193. #define maxxblk 202  /* each maxnblk is oversize by 2 for a "border" */
  194.              /* maxxblk defn must match fracsubr.c */
  195. /* next has a skip bit for each maxblock unit;
  196.    1st pass sets bit  [1]... off only if block's contents guessed;
  197.    at end of 1st pass [0]... bits are set if any surrounding block not guessed;
  198.    bits are numbered [..][y/16+1][x+1]&(1<<(y&15)) */
  199. extern unsigned int prefix[2][maxyblk][maxxblk]; /* common temp */
  200. /* size of next puts a limit of MAXPIXELS pixels across on solid guessing logic */
  201. #ifndef XFRACT
  202. extern char dstack[4096];               /* common temp, two put_line calls */
  203. extern char suffix[4096];             /* cellular, put_line/get_line calls */
  204. extern unsigned int prefix[2][maxyblk][maxxblk]; /* common temp */
  205. #else
  206. BYTE dstack[4096];              /* common temp, two put_line calls */
  207. unsigned int prefix[2][maxyblk][maxxblk]; /* common temp */
  208. #endif
  209.  
  210. int nxtscreenflag; /* for cellular next screen generation */
  211. int    attractors;            /* number of finite attractors  */
  212. _CMPLX    attr[N_ATTR];        /* finite attractor vals (f.p)  */
  213. _LCMPLX lattr[N_ATTR];        /* finite attractor vals (int)  */
  214. int    attrperiod[N_ATTR];        /* period of the finite attractor */
  215.  
  216. /***** vars for new btm *****/
  217. enum direction {North,East,South,West};
  218. enum direction going_to;
  219. int trail_row, trail_col;
  220.  
  221. #ifndef sqr
  222. #define sqr(x) ((x)*(x))
  223. #endif
  224.  
  225. #ifndef lsqr
  226. #define lsqr(x) (multiply((x),(x),bitshift))
  227. #endif
  228.  
  229. /* -------------------------------------------------------------------- */
  230. /*        These variables are external for speed's sake only      */
  231. /* -------------------------------------------------------------------- */
  232. extern _LCMPLX lold,lnew,lparm,lparm2;     /* added "lold" */
  233.  
  234. int periodicitycheck;
  235. extern long ltempsqrx,ltempsqry;
  236. extern double tempsqrx,tempsqry;
  237. extern _LCMPLX ltmp;
  238. extern int display3d;
  239.  
  240.  
  241. void calcfrac_overlay() { }    /* for restore_active_ovly */
  242. extern int first_err;   /* flag for math errors */
  243.  
  244. /******* calcfract - the top level routine for generating an image *******/
  245.  
  246. int calcfract()
  247. {
  248.    ENTER_OVLY(OVLY_CALCFRAC);
  249.  
  250.    first_err = 1;
  251.    attractors = 0;        /* default to no known finite attractors  */
  252.    display3d = 0;
  253.    basin = 0;
  254.  
  255.    init_misc();  /* set up some variables in parser.c */
  256.  
  257.    /* following delta values useful only for types with rotation disabled */
  258.    /* currently used only by bifurcation */
  259.    if (integerfractal)
  260.    {
  261.       distest = 0;
  262.       deltaX = (double)lx0[     1] / fudge - xxmin;
  263.       deltaY = yymax - (double)ly0[     1] / fudge;
  264.    }
  265.    else
  266.    {
  267.       deltaX = dx0[     1] - xxmin;
  268.       deltaY = yymax - dy0[     1];
  269.    }
  270.  
  271.    parm.x   = param[0];
  272.    parm.y   = param[1];
  273.    parm2.x  = param[2];
  274.    parm2.y  = param[3];
  275.  
  276.    if (LogFlag && colors < 16) {
  277.       static char far msg[]={"Need at least 16 colors to use logmap"};
  278.       stopmsg(0,msg);
  279.       LogFlag = 0;
  280.       }
  281.    if (LogFlag || rangeslen)
  282.       if (!(LogTable = farmemalloc((long)maxit + 1))) {
  283.       static char far msg[]={"Insufficient memory for logmap/ranges with this maxiter"};
  284.       stopmsg(0,msg);
  285.       }
  286.       else if (rangeslen) {
  287.      int i,k,l,m,numval,flip,altern;
  288.      i = k = l = 0;
  289.      while (i < rangeslen) {
  290.         m = flip = 0;
  291.         altern = 32767;
  292.         if ((numval = ranges[i++]) < 0) {
  293.            altern = ranges[i++];    /* sub-range iterations */
  294.            numval = ranges[i++];
  295.            }
  296.         if (numval > maxit || i >= rangeslen)
  297.            numval = maxit;
  298.         while (l <= numval)  {
  299.            LogTable[l++] = k + flip;
  300.            if (++m >= altern) {
  301.           flip ^= 1;        /* Alternate colors */
  302.           m = 0;
  303.           }
  304.            }
  305.         ++k;
  306.         if (altern != 32767) ++k;
  307.         }
  308.      }
  309.       else
  310.      SetupLogTable();
  311.  
  312.    lm = 4L << bitshift;         /* CALCMAND magnitude limit */
  313.  
  314.    /* ORBIT stuff */
  315.    save_orbit = (int far *)((double huge *)dx0 + 4*MAXPIXELS);
  316.    show_orbit = 0;
  317.    orbit_ptr = 0;
  318.    orbit_color = 15;
  319.    if(colors < 16)
  320.       orbit_color = 1;
  321.  
  322.    if(inversion[0] != 0.0)
  323.    {
  324.       f_radius      = inversion[0];
  325.       f_xcenter   = inversion[1];
  326.       f_ycenter   = inversion[2];
  327.  
  328.       if (inversion[0] == AUTOINVERT)  /*  auto calc radius 1/6 screen */
  329.      inversion[0] = f_radius = min(fabs(xxmax - xxmin),
  330.          fabs(yymax - yymin)) / 6.0;
  331.  
  332.       if (invert < 2 || inversion[1] == AUTOINVERT)  /* xcenter not already set */
  333.       {
  334.      inversion[1] = f_xcenter = (xxmin + xxmax) / 2.0;
  335.      if (fabs(f_xcenter) < fabs(xxmax-xxmin) / 100)
  336.         inversion[1] = f_xcenter = 0.0;
  337.       }
  338.  
  339.       if (invert < 3 || inversion[2] == AUTOINVERT)  /* ycenter not already set */
  340.       {
  341.      inversion[2] = f_ycenter = (yymin + yymax) / 2.0;
  342.      if (fabs(f_ycenter) < fabs(yymax-yymin) / 100)
  343.         inversion[2] = f_ycenter = 0.0;
  344.       }
  345.  
  346.       invert = 3; /* so values will not be changed if we come back */
  347.    }
  348.  
  349.    closenuff = delmin >> abs(periodicitycheck); /* for periodicity checking */
  350.    closenuff /= fudge;
  351.    rqlim_save = rqlim;
  352.    rqlim2 = sqrt(rqlim);
  353.    if (integerfractal)        /* for integer routines (lambda) */
  354.    {
  355.       lparm.x = parm.x * fudge;    /* real portion of Lambda */
  356.       lparm.y = parm.y * fudge;    /* imaginary portion of Lambda */
  357.       lparm2.x = parm2.x * fudge;  /* real portion of Lambda2 */
  358.       lparm2.y = parm2.y * fudge;  /* imaginary portion of Lambda2 */
  359.       llimit = rqlim * fudge;       /* stop if magnitude exceeds this */
  360.       if (llimit <= 0) llimit = 0x7fffffff; /* klooge for integer math */
  361.       llimit2 = rqlim2 * fudge;    /* stop if magnitude exceeds this */
  362.       lclosenuff = closenuff * fudge;    /* "close enough" value */
  363.       l16triglim = 8L<<16;       /* domain limit of fast trig functions */
  364.       linitorbit.x = initorbit.x * fudge;
  365.       linitorbit.y = initorbit.y * fudge;
  366.    }
  367.    resuming = (calc_status == 2);
  368.    if (!resuming) /* free resume_info memory if any is hanging around */
  369.    {
  370.       end_resume();
  371.       if (resave_flag) {
  372.      updatesavename(savename); /* do the pending increment */
  373.      resave_flag = started_resaves = 0;
  374.      }
  375.       calctime = 0;
  376.    }
  377.  
  378.    if (curfractalspecific->calctype != StandardFractal
  379.        && curfractalspecific->calctype != calcmand
  380.        && curfractalspecific->calctype != calcmandfp
  381.        && curfractalspecific->calctype != lyapunov
  382.        && curfractalspecific->calctype != calcfroth)
  383.    {
  384.       calctype = curfractalspecific->calctype; /* per_image can override */
  385.       symmetry = curfractalspecific->symmetry; /*   calctype & symmetry  */
  386.       plot = putcolor; /* defaults when setsymmetry not called or does nothing */
  387.       iystart = ixstart = yystart = xxstart = yybegin = 0;
  388.       iystop = yystop = ydots -1;
  389.       ixstop = xxstop = xdots -1;
  390.       calc_status = 1; /* mark as in-progress */
  391.       distest = 0; /* only standard escape time engine supports distest */
  392.       /* per_image routine is run here */
  393.       if (curfractalspecific->per_image())
  394.       { /* not a stand-alone */
  395.      /* next two lines in case periodicity changed */
  396.      closenuff = delmin >> abs(periodicitycheck); /* for periodicity checking */
  397.      closenuff /= fudge;
  398.      lclosenuff = closenuff * fudge;    /* "close enough" value */
  399.      setsymmetry(symmetry,0);
  400.      timer(0,calctype); /* non-standard fractal engine */
  401.       }
  402.       if (check_key())
  403.       {
  404.      if (calc_status == 1) /* calctype didn't set this itself, */
  405.         calc_status = 3;   /* so mark it interrupted, non-resumable */
  406.       }
  407.       else
  408.      calc_status = 4; /* no key, so assume it completed */
  409.    }
  410.    else /* standard escape-time engine */
  411.       timer(0,(int (*)())perform_worklist);
  412.    calctime += timer_interval;
  413.  
  414.    if(LogTable)
  415.    {
  416.       farmemfree(LogTable);
  417.       LogTable = NULL;
  418.    }
  419.    if(typespecific_workarea)
  420.    {
  421.       free_workarea();
  422.    }
  423.  
  424.    EXIT_OVLY;
  425.    return((calc_status == 4) ? 0 : -1);
  426. }
  427.  
  428. /**************** general escape-time engine routines *********************/
  429.  
  430. static void perform_worklist()
  431. {
  432.    int i;
  433.    long tmplong; /* this temp must be signed */
  434.  
  435.    if (potflag && pot16bit)
  436.    {
  437.       int tmpcalcmode = stdcalcmode;
  438.  
  439.       stdcalcmode = '1'; /* force 1 pass */
  440.       if (resuming == 0)
  441.      if (pot_startdisk() < 0)
  442.      {
  443.         pot16bit = 0;    /* startdisk failed or cancelled */
  444.         stdcalcmode = tmpcalcmode;    /* maybe we can carry on??? */
  445.      }
  446.    }
  447.    if (stdcalcmode == 'b' && (curfractalspecific->flags & NOTRACE))
  448.       stdcalcmode = '1';
  449.    if (stdcalcmode == 'g' && (curfractalspecific->flags & NOGUESS))
  450.       stdcalcmode = '1';
  451.  
  452.    /* default setup a new worklist */
  453.    num_worklist = 1;
  454.    worklist[0].xxstart = 0;
  455.    worklist[0].yystart = worklist[0].yybegin = 0;
  456.    worklist[0].xxstop = xdots - 1;
  457.    worklist[0].yystop = ydots - 1;
  458.    worklist[0].pass = worklist[0].sym = 0;
  459.    if (resuming) /* restore worklist, if we can't the above will stay in place */
  460.    {
  461.       start_resume();
  462.       get_resume(sizeof(int),&num_worklist,sizeof(worklist),worklist,0);
  463.       end_resume();
  464.    }
  465.  
  466.    if (distest) /* setup stuff for distance estimator */
  467.    {
  468.       double ftemp,ftemp2;
  469.       dem_delta = sqr(delxx) + sqr(delyy2);
  470.       if ((ftemp = sqr(delyy) + sqr(delxx2)) > dem_delta)
  471.      dem_delta = ftemp;
  472.       if (distestwidth == 0)
  473.      distestwidth = 71;
  474.       ftemp = distestwidth;
  475.       dem_delta *= sqr(ftemp)/10000; /* multiply by thickness desired */
  476.       dem_width = ( sqrt( sqr(xxmax-xxmin) + sqr(xx3rd-xxmin) ) * ydots/xdots
  477.       + sqrt( sqr(yymax-yymin) + sqr(yy3rd-yymin) ) ) / distest;
  478.       ftemp = (rqlim < DEM_BAILOUT) ? DEM_BAILOUT : rqlim;
  479.       ftemp += 3; /* bailout plus just a bit */
  480.       ftemp2 = log(ftemp);
  481.       dem_toobig = sqr(ftemp) * sqr(ftemp2) * 4 / dem_delta;
  482.    }
  483.  
  484.    while (num_worklist > 0)
  485.    {
  486.       calctype = curfractalspecific->calctype; /* per_image can override */
  487.       symmetry = curfractalspecific->symmetry; /*   calctype & symmetry  */
  488.       plot = putcolor; /* defaults when setsymmetry not called or does nothing */
  489.  
  490.       /* pull top entry off worklist */
  491.       ixstart = xxstart = worklist[0].xxstart;
  492.       ixstop  = xxstop    = worklist[0].xxstop;
  493.       iystart = yystart = worklist[0].yystart;
  494.       iystop  = yystop    = worklist[0].yystop;
  495.       yybegin  = worklist[0].yybegin;
  496.       workpass = worklist[0].pass;
  497.       worksym  = worklist[0].sym;
  498.       --num_worklist;
  499.       for (i=0; i<num_worklist; ++i)
  500.      worklist[i] = worklist[i+1];
  501.  
  502.       calc_status = 1; /* mark as in-progress */
  503.  
  504.       curfractalspecific->per_image();
  505.  
  506.       /* some common initialization for escape-time pixel level routines */
  507.       closenuff = delmin >> abs(periodicitycheck); /* for periodicity checking */
  508.       closenuff /= fudge;
  509.       lclosenuff = closenuff * fudge;    /* "close enough" value */
  510.       kbdcount=max_kbdcount;
  511.       /* savedmask is for calcmand's periodicity checking */
  512.       savedmask = 0xC0000000; /* top 2 bits on */
  513.       tmplong = (delmin >> abs(periodicitycheck)) | 1;
  514.       while (tmplong > 0) /* while top bit not on */
  515.       {
  516.      tmplong <<= 1;
  517.      savedmask = (savedmask >> 1) | 0x80000000;
  518.       }
  519.  
  520.       setsymmetry(symmetry,1);
  521.  
  522. /* added for testing autologmap() */
  523.       if (!(resuming)&&(abs(LogFlag) ==2))
  524.        {  /* calculate round screen edges to work out best start for logmap */
  525.          LogFlag = ( autologmap() * (LogFlag / abs(LogFlag)));
  526.          SetupLogTable();
  527.        }
  528.  
  529.       /* call the appropriate escape-time engine */
  530.       switch (stdcalcmode)
  531.       {
  532.       case 't':
  533.      tesseral();
  534.      break;
  535.       case 'b':
  536.      bound_trace_main();
  537.      break;
  538.       case 'g':
  539.      solidguess();
  540.      break;
  541.       default:
  542.      OneOrTwoPass();
  543.       }
  544.  
  545.       if (check_key()) /* interrupted? */
  546.      break;
  547.    }
  548.  
  549.    if (num_worklist > 0)
  550.    {  /* interrupted, resumable */
  551.       alloc_resume(sizeof(worklist)+10,1);
  552.       put_resume(sizeof(int),&num_worklist,sizeof(worklist),worklist,0);
  553.    }
  554.    else
  555.       calc_status = 4; /* completed */
  556. }
  557.  
  558. static int OneOrTwoPass()
  559. {
  560.    int i;
  561.    totpasses = 1;
  562.    if (stdcalcmode == '2') totpasses = 2;
  563.    if (stdcalcmode == '2' && workpass == 0) /* do 1st pass of two */
  564.    {
  565.       if (StandardCalc(1) == -1)
  566.       {
  567.      add_worklist(xxstart,xxstop,yystart,yystop,row,0,worksym);
  568.      return(-1);
  569.       }
  570.       if (num_worklist > 0) /* worklist not empty, defer 2nd pass */
  571.       {
  572.      add_worklist(xxstart,xxstop,yystart,yystop,yystart,1,worksym);
  573.      return(0);
  574.       }
  575.       workpass = 1;
  576.       yybegin = yystart;
  577.    }
  578.    /* second or only pass */
  579.    if (StandardCalc(2) == -1)
  580.    {
  581.       i = yystop;
  582.       if (iystop != yystop) /* must be due to symmetry */
  583.      i -= row - iystart;
  584.       add_worklist(xxstart,xxstop,row,i,row,workpass,worksym);
  585.       return(-1);
  586.    }
  587.    return(0);
  588. }
  589.  
  590. static int _fastcall StandardCalc(int passnum)
  591. {
  592.    got_status = 0;
  593.    curpass = passnum;
  594.    row = yybegin;
  595.    while (row <= iystop)
  596.    {
  597.       currow = row;
  598.       reset_periodicity = 1;
  599.       col = ixstart;
  600.       while (col <= ixstop)
  601.       {
  602.          if(showdot>0)
  603.             (*plot) (col, row, showdot&(colors-1));
  604.  
  605.      /* on 2nd pass of two, skip even pts */
  606.      if (passnum == 1 || stdcalcmode == '1' || (row&1) != 0 || (col&1) != 0)
  607.      {
  608.         if ((*calctype)() == -1) /* StandardFractal(), calcmand() or calcmandfp() */
  609.            return(-1); /* interrupted */
  610.         reset_periodicity = 0;
  611.         if (passnum == 1) /* first pass, copy pixel and bump col */
  612.         {
  613.            if ((row&1) == 0 && row < iystop)
  614.            {
  615.           (*plot)(col,row+1,color);
  616.           if ((col&1) == 0 && col < ixstop)
  617.              (*plot)(col+1,row+1,color);
  618.            }
  619.            if ((col&1) == 0 && col < ixstop)
  620.           (*plot)(++col,row,color);
  621.         }
  622.      }
  623.      ++col;
  624.       }
  625.       if (passnum == 1 && (row&1) == 0)
  626.      ++row;
  627.       ++row;
  628.    }
  629.    return(0);
  630. }
  631.  
  632. int calcmand()        /* fast per pixel 1/2/b/g, called with row & col set */
  633. {
  634.    /* setup values from far array to avoid using es reg in calcmand.asm */
  635.    linitx = lx0[col] + lx1[row];
  636.    linity = ly0[row] + ly1[col];
  637.    if (calcmandasm() >= 0)
  638.    {
  639.       if (LogTable /* map color, but not if maxit & adjusted for inside,etc */
  640.       && (realcolor < maxit || (inside < 0 && color == maxit)))
  641.      color = LogTable[min(color, maxit)];
  642.       if (color >= colors) /* don't use color 0 unless from inside/outside */
  643.      if (colors < 16)
  644.         color &= andcolor;
  645.      else
  646.         color = ((color - 1) % andcolor) + 1;  /* skip color zero */
  647.       if(debugflag != 470)
  648.          if(color <= 0 && stdcalcmode == 'b' )   /* fix BTM bug */
  649.             color = 1;
  650.       (*plot) (col, row, color);
  651.    }
  652.    return (color);
  653. }
  654.  
  655. /************************************************************************/
  656. /* added by Wes Loewer - sort of a floating point version of calcmand() */
  657. /* can also handle invert, any rqlim, potflag, zmag, epsilon cross,     */
  658. /* and all the current outside options    -Wes Loewer 11/03/91          */
  659. /************************************************************************/
  660. int calcmandfp()
  661. {
  662.    if(invert)
  663.       invertz2(&init);
  664.    else
  665.    {
  666.       init.y = dy0[row]+dy1[col];
  667.       init.x = dx0[col]+dx1[row];
  668.    }
  669.    if (calcmandfpasm() >= 0)
  670.    {
  671.       if (potflag)
  672.      color = potential(magnitude, realcolor);
  673.       if (LogTable /* map color, but not if maxit & adjusted for inside,etc */
  674.           && (realcolor < maxit || (inside < 0 && color == maxit)))
  675.         color = LogTable[min(color, maxit)];
  676.      if (color >= colors) /* don't use color 0 unless from inside/outside */
  677.         if (colors < 16)
  678.            color &= andcolor;
  679.         else
  680.            color = ((color - 1) % andcolor) + 1;  /* skip color zero */
  681.          if(debugflag != 470)
  682.             if(color == 0 && stdcalcmode == 'b' )   /* fix BTM bug */
  683.                color = 1;
  684.       (*plot) (col, row, color);
  685.    }
  686.    return (color);
  687. }
  688.  
  689. #define green 2
  690. #define yellow 6
  691. int StandardFractal()    /* per pixel 1/2/b/g, called with row & col set */
  692. {
  693.    double tantable[16];
  694.    int hooper;
  695.    double close;
  696.    long lclose;
  697.    int cyclelen = -1;
  698.    int savedcolor;
  699.    int caught_a_cycle;
  700.    int savedand, savedincr;    /* for periodicity checking */
  701.    _LCMPLX lsaved;
  702.    int i, attracted;
  703.    _LCMPLX lat;
  704.    _CMPLX  at;
  705.    _CMPLX deriv;
  706.    int dem_color;
  707.    _CMPLX dem_new;
  708.    close = .01;
  709.    lclose = close*fudge;
  710.  
  711.    if(inside == STARTRAIL)
  712.    {
  713.       int i;
  714.       for(i=0;i<16;i++)
  715.      tantable[i] = 0.0;
  716.    }
  717.    else if(inside == EPSCROSS)
  718.    {
  719.       close = .01;
  720.       lclose = close*fudge;
  721.    }
  722.    if (periodicitycheck == 0 || inside == ZMAG || inside == STARTRAIL)
  723.       oldcolor = 32767;     /* don't check periodicity at all */
  724.    else if (inside == PERIOD)    /* for display-periodicity */
  725.       oldcolor = maxit*4/5;    /* don't check until nearly done */
  726.    else if (reset_periodicity)
  727.       oldcolor = 250;        /* don't check periodicity 1st 250 iterations */
  728.  
  729.    /* really fractal specific, but we'll leave it here */
  730.    if (!integerfractal)
  731.    {
  732.       if (useinitorbit == 1)
  733.      saved = initorbit;
  734.       else {
  735.      saved.x = 0;
  736.      saved.y = 0;
  737.       }
  738.       init.y = dy0[row] + dy1[col];
  739.       if (distest)
  740.       {
  741.      rqlim = rqlim_save;          /* start with regular bailout */
  742.      if (distest != 1 || colors == 2) /* not doing regular outside colors */
  743.         if (rqlim < DEM_BAILOUT)      /* so go straight for dem bailout */
  744.            rqlim = DEM_BAILOUT;
  745.      deriv.x = 1;
  746.      deriv.y = 0;
  747.      magnitude = 0;
  748.      dem_color = -1;
  749.       }
  750.    }
  751.    else
  752.    {
  753.       if (useinitorbit == 1)
  754.      lsaved = linitorbit;
  755.       else {
  756.      lsaved.x = 0;
  757.      lsaved.y = 0;
  758.       }
  759.       linit.y = ly0[row] + ly1[col];
  760.    }
  761.    orbit_ptr = 0;
  762.    color = 0;
  763.    if(fractype==JULIAFP || fractype==JULIA)
  764.       color = -1;
  765.    caught_a_cycle = 0;
  766.    if (inside == PERIOD) {
  767.        savedand = 16;        /* begin checking every 16th cycle */
  768.    } else {
  769.        savedand = 1;        /* begin checking every other cycle */
  770.    }
  771.    savedincr = 1;        /* start checking the very first time */
  772.  
  773.    if (inside <= BOF60 && inside >= BOF61)
  774.    {
  775.       magnitude = lmagnitud = 0;
  776.       min_orbit = 100000.0;
  777.    }
  778.    overflow = 0;        /* reset integer math overflow flag */
  779.  
  780.    curfractalspecific->per_pixel(); /* initialize the calculations */
  781.  
  782.    attracted = FALSE;
  783.    while (++color < maxit)
  784.    {
  785.       if(showdot>0)
  786.          (*plot) (col, row, showdot&(colors-1));
  787.  
  788.       /* calculation of one orbit goes here */
  789.       /* input in "old" -- output in "new" */
  790.  
  791.       if (distest)
  792.       {
  793.      double ftemp;
  794.      /* Distance estimator for points near Mandelbrot set */
  795.      /* Original code by Phil Wilson, hacked around by PB */
  796.      /* Algorithms from Peitgen & Saupe, Science of Fractal Images, p.198 */
  797.      ftemp     = 2 * (old.x * deriv.x - old.y * deriv.y) + 1;
  798.      deriv.y = 2 * (old.y * deriv.x + old.x * deriv.y);
  799.      deriv.x = ftemp;
  800.      if (sqr(deriv.x)+sqr(deriv.y) > dem_toobig)
  801.         break;
  802.      /* if above exit taken, the later test vs dem_delta will place this
  803.             point on the boundary, because mag(old)<bailout just now */
  804.      if (curfractalspecific->orbitcalc())
  805.      {
  806.         if (dem_color < 0)          /* note "regular" color for later */
  807.         {
  808.            dem_color = color;
  809.            dem_new = new;
  810.         }
  811.         if (rqlim >= DEM_BAILOUT  /* exit if past real bailout */
  812.         || magnitude >= (rqlim = DEM_BAILOUT) /* reset to real bailout */
  813.         || magnitude == 0)     /* exit if type doesn't "floatbailout" */
  814.            break;
  815.         old = new;              /* carry on till past real bailout */
  816.      }
  817.       }
  818.       else /* the usual case */
  819.      if (curfractalspecific->orbitcalc() && inside != STARTRAIL)
  820.         break;
  821.       if (show_orbit)
  822.      if (!integerfractal)
  823.         plot_orbit(new.x, new.y, -1);
  824.      else
  825.         iplot_orbit(lnew.x, lnew.y, -1);
  826.       if(inside == STARTRAIL)
  827.       {
  828.      if(0 < color && color < 16)
  829.      {
  830.      if (integerfractal)
  831.      {
  832.            new.x = lnew.x;
  833.            new.x /= fudge;
  834.            new.y = lnew.y;
  835.            new.y /= fudge;
  836.         }
  837.         tantable[color-1] = new.y/(new.x+.000001);
  838.      }
  839.       }
  840.       else if(inside == EPSCROSS)
  841.       {
  842.      hooper = 0;
  843.      if(integerfractal)
  844.      {
  845.         if(labs(lnew.x) < lclose)
  846.         {
  847.            hooper = 1; /* close to y axis */
  848.            goto plot_inside;
  849.         }
  850.         else if(labs(lnew.y) < lclose)
  851.         {
  852.            hooper = 2; /* close to x axis */
  853.            goto plot_inside;
  854.         }
  855.      }
  856.      else
  857.      {
  858.         if(fabs(new.x) < close)
  859.         {
  860.            hooper = 1; /* close to y axis */
  861.            goto plot_inside;
  862.         }
  863.         else if(fabs(new.y) < close)
  864.         {
  865.            hooper = 2; /* close to x axis */
  866.            goto plot_inside;
  867.         }
  868.      }
  869.       }
  870.       else if (inside <= BOF60 && inside >= BOF61)
  871.       {
  872.      if (integerfractal)
  873.      {
  874.         if (lmagnitud == 0)
  875.            lmagnitud = lsqr(lnew.x) + lsqr(lnew.y);
  876.         magnitude = lmagnitud;
  877.         magnitude = magnitude / fudge;
  878.      }
  879.      else
  880.         if (magnitude == 0.0)
  881.            magnitude = sqr(new.x) + sqr(new.y);
  882.      if (magnitude < min_orbit)
  883.      {
  884.         min_orbit = magnitude;
  885.         min_index = color + 1;
  886.      }
  887.       }
  888.  
  889.  
  890.       if (attractors > 0)    /* finite attractor in the list   */
  891.       {             /* NOTE: Integer code is UNTESTED */
  892.      if (integerfractal)
  893.      {
  894.         for (i = 0; i < attractors; i++)
  895.         {
  896.         lat.x = lnew.x - lattr[i].x;
  897.         lat.x = lsqr(lat.x);
  898.         if (lat.x < l_at_rad)
  899.         {
  900.            lat.y = lnew.y - lattr[i].y;
  901.            lat.y = lsqr(lat.y);
  902.            if (lat.y < l_at_rad)
  903.            {
  904.               if ((lat.x + lat.y) < l_at_rad)
  905.               {
  906.              attracted = TRUE;
  907.              if (finattract<0) color = (color%attrperiod[i])+1;
  908.              break;
  909.               }
  910.            }
  911.         }
  912.         }
  913.      }
  914.      else
  915.      {
  916.         for (i = 0; i < attractors; i++)
  917.         {
  918.         at.x = new.x - attr[i].x;
  919.         at.x = sqr(at.x);
  920.         if (at.x < f_at_rad)
  921.         {
  922.            at.y = new.y - attr[i].y;
  923.            at.y = sqr(at.y);
  924.            if ( at.y < f_at_rad)
  925.            {
  926.               if ((at.x + at.y) < f_at_rad)
  927.               {
  928.              attracted = TRUE;
  929.              if (finattract<0) color = (color%attrperiod[i])+1;
  930.              break;
  931.               }
  932.            }
  933.         }
  934.         }
  935.      }
  936.      if (attracted)
  937.         break;        /* AHA! Eaten by an attractor */
  938.       }
  939.  
  940.       if (color > oldcolor)    /* check periodicity */
  941.       {
  942.      if ((color & savedand) == 0)         /* time to save a new value */
  943.      {
  944.         savedcolor = color;
  945.         if (!integerfractal)
  946.            saved = new;  /* floating pt fractals */
  947.         else
  948.            lsaved = lnew;/* integer fractals */
  949.         if (--savedincr == 0)    /* time to lengthen the periodicity? */
  950.         {
  951.            savedand = (savedand << 1) + 1;         /* longer periodicity */
  952.            savedincr = 4;/* restart counter */
  953.         }
  954.      }
  955.      else             /* check against an old save */
  956.      {
  957.         if (!integerfractal)     /* floating-pt periodicity chk */
  958.         {
  959.            if (fabs(saved.x - new.x) < closenuff)
  960.           if (fabs(saved.y - new.y) < closenuff)
  961.           {
  962.              caught_a_cycle = 1;
  963.              cyclelen = color-savedcolor;
  964.              color = maxit - 1;
  965.           }
  966.         }
  967.         else         /* integer periodicity check */
  968.         {
  969.            if (labs(lsaved.x - lnew.x) < lclosenuff)
  970.           if (labs(lsaved.y - lnew.y) < lclosenuff)
  971.           {
  972.              caught_a_cycle = 1;
  973.              cyclelen = color-savedcolor;
  974.              color = maxit - 1;
  975.           }
  976.         }
  977.      }
  978.       }
  979.    }
  980.  
  981.    if (show_orbit)
  982.       scrub_orbit();
  983.  
  984.    realcolor = color;        /* save this before we start adjusting it */
  985.    if (color >= maxit)
  986.       oldcolor = 0;        /* check periodicity immediately next time */
  987.    else
  988.    {
  989.       oldcolor = color + 10;    /* check when past this + 10 next time */
  990.       if (color == 0)
  991.      color = 1;        /* needed to make same as calcmand */
  992.    }
  993.  
  994.    if (potflag)
  995.    {
  996.       if (integerfractal)    /* adjust integer fractals */
  997.       {
  998.      new.x = ((double)lnew.x) / fudge;
  999.      new.y = ((double)lnew.y) / fudge;
  1000.       }
  1001.       magnitude = sqr(new.x) + sqr(new.y);
  1002.       color = potential(magnitude, color);
  1003.       if (LogTable)
  1004.      color = LogTable[min(color, maxit)];
  1005.       goto plot_pixel;        /* skip any other adjustments */
  1006.    }
  1007.  
  1008.    if (color >= maxit)        /* an "inside" point */
  1009.       goto plot_inside;     /* distest, decomp, biomorph don't apply */
  1010.  
  1011.  
  1012.    if (outside < -1)  /* these options by Richard Hughes modified by TW */
  1013.    {
  1014.       if (integerfractal)
  1015.       {
  1016.      new.x = ((double)lnew.x) / fudge;
  1017.      new.y = ((double)lnew.y) / fudge;
  1018.       }
  1019.       /* Add 7 to overcome negative values on the MANDEL    */
  1020.       if (outside == REAL)         /* "real" */
  1021.      color += new.x + 7;
  1022.       else if (outside == IMAG)         /* "imag" */
  1023.      color += new.y + 7;
  1024.       else if (outside == MULT  && new.y)  /* "mult" */
  1025.       color *= (new.x/new.y);
  1026.       else if (outside == SUM)         /* "sum" */
  1027.       color += new.x + new.y;
  1028.  
  1029.       /* eliminate negative colors & wrap arounds */
  1030.       if (color < 0 || color > maxit)
  1031.       color = 0;
  1032.    }
  1033.  
  1034.    if (distest)
  1035.    {
  1036.       double dist,temp;
  1037.       dist = sqr(new.x) + sqr(new.y);
  1038.       temp = log(dist);
  1039.       dist = dist * sqr(temp) / ( sqr(deriv.x) + sqr(deriv.y) );
  1040.       if (dist < dem_delta)    /* point is on the edge */
  1041.       {
  1042.      if (distest > 0)
  1043.         goto plot_inside;    /* show it as an inside point */
  1044.      color = 0 - distest;    /* show boundary as specified color */
  1045.      goto plot_pixel;    /* no further adjustments apply */
  1046.       }
  1047.       if (colors == 2)
  1048.       {
  1049.      color = !inside;    /* the only useful distest 2 color use */
  1050.      goto plot_pixel;    /* no further adjustments apply */
  1051.       }
  1052.       if (distest > 1)        /* pick color based on distance */
  1053.       {
  1054.      color = sqrt(dist / dem_width + 1);
  1055.          color &= INT_MAX;     /* oops - color can be negative */
  1056.      goto plot_pixel;    /* no further adjustments apply */
  1057.       }
  1058.       color = dem_color;    /* use pixel's "regular" color */
  1059.       new = dem_new;
  1060.    }
  1061.  
  1062.    if (decomp[0] > 0)
  1063.       decomposition();
  1064.    else if (biomorph != -1)
  1065.    {
  1066.       if (integerfractal)
  1067.       {
  1068.      if (labs(lnew.x) < llimit2 || labs(lnew.y) < llimit2)
  1069.         color = biomorph;
  1070.       }
  1071.       else
  1072.      if (fabs(new.x) < rqlim2 || fabs(new.y) < rqlim2)
  1073.         color = biomorph;
  1074.    }
  1075.  
  1076.    if (outside >= 0 && attracted == FALSE) /* merge escape-time stripes */
  1077.       color = outside;
  1078.    else if (LogTable)
  1079.       color = LogTable[min(color, maxit)];
  1080.    goto plot_pixel;
  1081.  
  1082.    plot_inside: /* we're "inside" */
  1083.    if (periodicitycheck < 0 && caught_a_cycle)
  1084.       color = 7;           /* show periodicity */
  1085.    else if (inside >= 0)
  1086.       color = inside;           /* set to specified color, ignore logpal */
  1087.    else
  1088.    {
  1089.       if(inside == STARTRAIL)
  1090.       {
  1091.      int i;
  1092.      double diff;
  1093.      color = 0;
  1094.      for(i=1;i<16;i++)
  1095.      {
  1096.         diff = tantable[0] - tantable[i];
  1097.         if(fabs(diff) < .05)
  1098.         {
  1099.            color = i;
  1100.            break;
  1101.         }
  1102.      }
  1103.       }
  1104.       else if(inside== PERIOD) {
  1105.       if (cyclelen>0) {
  1106.           color = cyclelen;
  1107.       } else {
  1108.           color = maxit;
  1109.       }
  1110.       }
  1111.       else if(inside == EPSCROSS)
  1112.       {
  1113.      if(hooper==1)
  1114.         color = green;
  1115.      else if(hooper==2)
  1116.         color = yellow;
  1117.      else
  1118.         color = maxit;
  1119.      if (show_orbit)
  1120.         scrub_orbit();
  1121.       }
  1122.       else if (inside == BOF60)
  1123.      color = sqrt(min_orbit) * 75;
  1124.       else if (inside == BOF61)
  1125.      color = min_index;
  1126.       else if (inside == ZMAG)
  1127.       {
  1128.      if (integerfractal)
  1129.      {
  1130.         /*
  1131.         new.x = ((double)lnew.x) / fudge;
  1132.         new.y = ((double)lnew.y) / fudge;
  1133.         */
  1134.         color = (((double)lsqr(lnew.x))/fudge + ((double)lsqr(lnew.y))/fudge) * (maxit>>1) + 1;
  1135.      }
  1136.      else
  1137.         color = (sqr(new.x) + sqr(new.y)) * (maxit>>1) + 1;
  1138.       }
  1139.       else /* inside == -1 */
  1140.      color = maxit;
  1141.       if (LogTable)
  1142.      color = LogTable[min(color, maxit)];
  1143.    }
  1144.  
  1145.    plot_pixel:
  1146.  
  1147.    if (color >= colors) /* don't use color 0 unless from inside/outside */
  1148.       if (colors < 16)
  1149.      color &= andcolor;
  1150.       else
  1151.      color = ((color - 1) % andcolor) + 1;    /* skip color zero */
  1152.    if(debugflag != 470)
  1153.       if(color <= 0 && stdcalcmode == 'b' )   /* fix BTM bug */
  1154.          color = 1;
  1155.    (*plot) (col, row, color);
  1156.  
  1157.    if ((kbdcount -= realcolor) <= 0)
  1158.    {
  1159.       if (check_key())
  1160.      return (-1);
  1161.       kbdcount = max_kbdcount;
  1162.    }
  1163.  
  1164.    return (color);
  1165. }
  1166. #undef green
  1167. #undef yellow
  1168.  
  1169.  
  1170.  
  1171. /**************** standardfractal doodad subroutines *********************/
  1172.  
  1173. static void decomposition()
  1174. {
  1175.    static double far cos45       = 0.70710678118654750; /* cos 45    degrees */
  1176.    static double far sin45       = 0.70710678118654750; /* sin 45    degrees */
  1177.    static double far cos22_5   = 0.92387953251128670; /* cos 22.5    degrees */
  1178.    static double far sin22_5   = 0.38268343236508980; /* sin 22.5    degrees */
  1179.    static double far cos11_25  = 0.98078528040323040; /* cos 11.25    degrees */
  1180.    static double far sin11_25  = 0.19509032201612820; /* sin 11.25    degrees */
  1181.    static double far cos5_625  = 0.99518472667219690; /* cos 5.625    degrees */
  1182.    static double far sin5_625  = 0.09801714032956060; /* sin 5.625    degrees */
  1183.    static double far tan22_5   = 0.41421356237309500; /* tan 22.5    degrees */
  1184.    static double far tan11_25  = 0.19891236737965800; /* tan 11.25    degrees */
  1185.    static double far tan5_625  = 0.09849140335716425; /* tan 5.625    degrees */
  1186.    static double far tan2_8125 = 0.04912684976946725; /* tan 2.8125 degrees */
  1187.    static double far tan1_4063 = 0.02454862210892544; /* tan 1.4063 degrees */
  1188.    static long far lcos45      ; /* cos 45      degrees */
  1189.    static long far lsin45      ; /* sin 45      degrees */
  1190.    static long far lcos22_5   ; /* cos 22.5   degrees */
  1191.    static long far lsin22_5   ; /* sin 22.5   degrees */
  1192.    static long far lcos11_25  ; /* cos 11.25  degrees */
  1193.    static long far lsin11_25  ; /* sin 11.25  degrees */
  1194.    static long far lcos5_625  ; /* cos 5.625  degrees */
  1195.    static long far lsin5_625  ; /* sin 5.625  degrees */
  1196.    static long far ltan22_5   ; /* tan 22.5   degrees */
  1197.    static long far ltan11_25  ; /* tan 11.25  degrees */
  1198.    static long far ltan5_625  ; /* tan 5.625  degrees */
  1199.    static long far ltan2_8125 ; /* tan 2.8125 degrees */
  1200.    static long far ltan1_4063 ; /* tan 1.4063 degrees */
  1201.    static reset_fudge = -1;
  1202.    int temp = 0;
  1203.    int i;
  1204.    _LCMPLX lalt;
  1205.    _CMPLX alt;
  1206.    color = 0;
  1207.    if (integerfractal) /* the only case */
  1208.    {
  1209.       if (reset_fudge != fudge)
  1210.       {
  1211.      reset_fudge = fudge;
  1212.      lcos45     = cos45     *fudge;
  1213.      lsin45     = sin45     *fudge;
  1214.      lcos22_5   = cos22_5     *fudge;
  1215.      lsin22_5   = sin22_5     *fudge;
  1216.      lcos11_25  = cos11_25     *fudge;
  1217.      lsin11_25  = sin11_25     *fudge;
  1218.      lcos5_625  = cos5_625     *fudge;
  1219.      lsin5_625  = sin5_625     *fudge;
  1220.      ltan22_5   = tan22_5     *fudge;
  1221.      ltan11_25  = tan11_25     *fudge;
  1222.      ltan5_625  = tan5_625     *fudge;
  1223.      ltan2_8125 = tan2_8125  *fudge;
  1224.      ltan1_4063 = tan1_4063  *fudge;
  1225.       }
  1226.       if (lnew.y < 0)
  1227.       {
  1228.      temp = 2;
  1229.      lnew.y = -lnew.y;
  1230.       }
  1231.  
  1232.       if (lnew.x < 0)
  1233.       {
  1234.      ++temp;
  1235.      lnew.x = -lnew.x;
  1236.       }
  1237.  
  1238.       if (decomp[0] >= 8)
  1239.       {
  1240.      temp <<= 1;
  1241.      if (lnew.x < lnew.y)
  1242.      {
  1243.         ++temp;
  1244.         lalt.x = lnew.x; /* just */
  1245.         lnew.x = lnew.y; /* swap */
  1246.         lnew.y = lalt.x; /* them */
  1247.      }
  1248.  
  1249.      if (decomp[0] >= 16)
  1250.      {
  1251.         temp <<= 1;
  1252.         if (multiply(lnew.x,ltan22_5,bitshift) < lnew.y)
  1253.         {
  1254.            ++temp;
  1255.            lalt = lnew;
  1256.            lnew.x = multiply(lalt.x,lcos45,bitshift) +
  1257.            multiply(lalt.y,lsin45,bitshift);
  1258.            lnew.y = multiply(lalt.x,lsin45,bitshift) -
  1259.            multiply(lalt.y,lcos45,bitshift);
  1260.         }
  1261.  
  1262.         if (decomp[0] >= 32)
  1263.         {
  1264.            temp <<= 1;
  1265.            if (multiply(lnew.x,ltan11_25,bitshift) < lnew.y)
  1266.            {
  1267.           ++temp;
  1268.           lalt = lnew;
  1269.           lnew.x = multiply(lalt.x,lcos22_5,bitshift) +
  1270.               multiply(lalt.y,lsin22_5,bitshift);
  1271.           lnew.y = multiply(lalt.x,lsin22_5,bitshift) -
  1272.               multiply(lalt.y,lcos22_5,bitshift);
  1273.            }
  1274.  
  1275.            if (decomp[0] >= 64)
  1276.            {
  1277.           temp <<= 1;
  1278.           if (multiply(lnew.x,ltan5_625,bitshift) < lnew.y)
  1279.           {
  1280.              ++temp;
  1281.              lalt = lnew;
  1282.              lnew.x = multiply(lalt.x,lcos11_25,bitshift) +
  1283.              multiply(lalt.y,lsin11_25,bitshift);
  1284.              lnew.y = multiply(lalt.x,lsin11_25,bitshift) -
  1285.              multiply(lalt.y,lcos11_25,bitshift);
  1286.           }
  1287.  
  1288.           if (decomp[0] >= 128)
  1289.           {
  1290.              temp <<= 1;
  1291.              if (multiply(lnew.x,ltan2_8125,bitshift) < lnew.y)
  1292.              {
  1293.             ++temp;
  1294.             lalt = lnew;
  1295.             lnew.x = multiply(lalt.x,lcos5_625,bitshift) +
  1296.                 multiply(lalt.y,lsin5_625,bitshift);
  1297.             lnew.y = multiply(lalt.x,lsin5_625,bitshift) -
  1298.                 multiply(lalt.y,lcos5_625,bitshift);
  1299.              }
  1300.  
  1301.              if (decomp[0] == 256)
  1302.              {
  1303.             temp <<= 1;
  1304.             if (multiply(lnew.x,ltan1_4063,bitshift) < lnew.y)
  1305.                if ((lnew.x*ltan1_4063 < lnew.y))
  1306.                   ++temp;
  1307.              }
  1308.           }
  1309.            }
  1310.         }
  1311.      }
  1312.       }
  1313.    }
  1314.    else /* double case */
  1315.    {
  1316.       if (new.y < 0)
  1317.       {
  1318.      temp = 2;
  1319.      new.y = -new.y;
  1320.       }
  1321.       if (new.x < 0)
  1322.       {
  1323.      ++temp;
  1324.      new.x = -new.x;
  1325.       }
  1326.       if (decomp[0] >= 8)
  1327.       {
  1328.      temp <<= 1;
  1329.      if (new.x < new.y)
  1330.      {
  1331.         ++temp;
  1332.         alt.x = new.x; /* just */
  1333.         new.x = new.y; /* swap */
  1334.         new.y = alt.x; /* them */
  1335.      }
  1336.      if (decomp[0] >= 16)
  1337.      {
  1338.         temp <<= 1;
  1339.         if (new.x*tan22_5 < new.y)
  1340.         {
  1341.            ++temp;
  1342.            alt = new;
  1343.            new.x = alt.x*cos45 + alt.y*sin45;
  1344.            new.y = alt.x*sin45 - alt.y*cos45;
  1345.         }
  1346.  
  1347.         if (decomp[0] >= 32)
  1348.         {
  1349.            temp <<= 1;
  1350.            if (new.x*tan11_25 < new.y)
  1351.            {
  1352.           ++temp;
  1353.           alt = new;
  1354.           new.x = alt.x*cos22_5 + alt.y*sin22_5;
  1355.           new.y = alt.x*sin22_5 - alt.y*cos22_5;
  1356.            }
  1357.  
  1358.            if (decomp[0] >= 64)
  1359.            {
  1360.           temp <<= 1;
  1361.           if (new.x*tan5_625 < new.y)
  1362.           {
  1363.              ++temp;
  1364.              alt = new;
  1365.              new.x = alt.x*cos11_25 + alt.y*sin11_25;
  1366.              new.y = alt.x*sin11_25 - alt.y*cos11_25;
  1367.           }
  1368.  
  1369.           if (decomp[0] >= 128)
  1370.           {
  1371.              temp <<= 1;
  1372.              if (new.x*tan2_8125 < new.y)
  1373.              {
  1374.             ++temp;
  1375.             alt = new;
  1376.             new.x = alt.x*cos5_625 + alt.y*sin5_625;
  1377.             new.y = alt.x*sin5_625 - alt.y*cos5_625;
  1378.              }
  1379.  
  1380.              if (decomp[0] == 256)
  1381.              {
  1382.             temp <<= 1;
  1383.             if ((new.x*tan1_4063 < new.y))
  1384.                ++temp;
  1385.              }
  1386.           }
  1387.            }
  1388.         }
  1389.      }
  1390.       }
  1391.    }
  1392.    for (i = 1; temp > 0; ++i)
  1393.    {
  1394.       if (temp & 1)
  1395.      color = (1 << i) - 1 - color;
  1396.       temp >>= 1;
  1397.    }
  1398.    if (decomp[0] == 2)
  1399.       color &= 1;
  1400.    if (colors > decomp[0])
  1401.       color++;
  1402. }
  1403.  
  1404. /******************************************************************/
  1405. /* Continuous potential calculation for Mandelbrot and Julia      */
  1406. /* Reference: Science of Fractal Images p. 190.           */
  1407. /* Special thanks to Mark Peterson for his "MtMand" program that  */
  1408. /* beautifully approximates plate 25 (same reference) and spurred */
  1409. /* on the inclusion of similar capabilities in FRACTINT.      */
  1410. /*                                  */
  1411. /* The purpose of this function is to calculate a color value      */
  1412. /* for a fractal that varies continuously with the screen pixels  */
  1413. /* locations for better rendering in 3D.              */
  1414. /*                                  */
  1415. /* Here "magnitude" is the modulus of the orbit value at          */
  1416. /* "iterations". The potparms[] are user-entered paramters        */
  1417. /* controlling the level and slope of the continuous potential      */
  1418. /* surface. Returns color.  - Tim Wegner 6/25/89          */
  1419. /*                                  */
  1420. /*               -- Change history --              */
  1421. /*                                  */
  1422. /* 09/12/89   - added floatflag support and fixed float underflow */
  1423. /*                                  */
  1424. /******************************************************************/
  1425.  
  1426. static int _fastcall potential(double mag, int iterations)
  1427. {
  1428.    float f_mag,f_tmp,pot;
  1429.    double d_tmp;
  1430.    int i_pot;
  1431.    long l_pot;
  1432.    extern char floatflag;
  1433.  
  1434.    if(iterations < maxit)
  1435.    {
  1436.       pot = i_pot = iterations+2;
  1437.       if(i_pot <= 0 || mag <= 1.0)
  1438.      pot = 0.0;
  1439.       else /* pot = log(mag) / pow(2.0, (double)pot); */
  1440.       {
  1441.      if(i_pot < 120 && !floatflag) /* empirically determined limit of fShift */
  1442.      {
  1443.         f_mag = mag;
  1444.         fLog14(f_mag,f_tmp); /* this SHOULD be non-negative */
  1445.         fShift(f_tmp,(char)-i_pot,pot);
  1446.      }
  1447.      else
  1448.      {
  1449.         d_tmp = log(mag)/(double)pow(2.0,(double)pot);
  1450.         if(d_tmp > FLT_MIN) /* prevent float type underflow */
  1451.            pot = d_tmp;
  1452.         else
  1453.            pot = 0.0;
  1454.      }
  1455.       }
  1456.       /* following transformation strictly for aesthetic reasons */
  1457.       /* meaning of parameters:
  1458.         potparam[0] -- zero potential level - highest color -
  1459.         potparam[1] -- slope multiplier -- higher is steeper
  1460.         potparam[2] -- rqlim value if changeable (bailout for modulus) */
  1461.  
  1462.       if(pot > 0.0)
  1463.       {
  1464.      if(floatflag)
  1465.         pot = (float)sqrt((double)pot);
  1466.      else
  1467.      {
  1468.         fSqrt14(pot,f_tmp);
  1469.         pot = f_tmp;
  1470.      }
  1471.      pot = potparam[0] - pot*potparam[1] - 1.0;
  1472.       }
  1473.       else
  1474.      pot = potparam[0] - 1.0;
  1475.       if(pot < 1.0)
  1476.      pot = 1.0; /* avoid color 0 */
  1477.    }
  1478.    else if(inside >= 0)
  1479.       pot = inside;
  1480.    else /* inside < 0 implies inside=maxit, so use 1st pot param instead */
  1481.       pot = potparam[0];
  1482.  
  1483.    i_pot = (l_pot = pot * 256) >> 8;
  1484.    if(i_pot >= colors)
  1485.    {
  1486.       i_pot = colors - 1;
  1487.       l_pot = 255;
  1488.    }
  1489.  
  1490.    if(pot16bit)
  1491.    {
  1492.       if (dotmode != 11) /* if putcolor won't be doing it for us */
  1493.      writedisk(col+sxoffs,row+syoffs,i_pot);
  1494.       writedisk(col+sxoffs,row+sydots+syoffs,(int)l_pot);
  1495.    }
  1496.  
  1497.    return(i_pot);
  1498. }
  1499.  
  1500.  
  1501. /******************* boundary trace method ***************************
  1502. Fractint's original btm was written by David Guenther.  There were a few
  1503. rare circumstances in which the original btm would not trace or fill
  1504. correctly, even on Mandelbrot Sets.  The code below was adapted from
  1505. "Mandelbrot Sets by Wesley Loewer" (see calmanfp.asm) which was written
  1506. before I was introduced to Fractint.  It should be noted that without
  1507. David Guenther's implimentation of a btm, I doubt that I would have been
  1508. able to impliment my own code into Fractint.  There are several things in
  1509. the following code that are not original with me but came from David
  1510. Guenther's code.  I've noted these places with the initials DG.
  1511.  
  1512.                     Wesley Loewer 3/8/92
  1513. *********************************************************************/
  1514. #define bkcolor 0  /* I have some ideas for the future with this. -Wes */
  1515. #define advance_match()     coming_from = ((going_to = (going_to - 1) & 0x03) - 1) & 0x03
  1516. #define advance_no_match()  going_to = (going_to + 1) & 0x03
  1517.  
  1518. static
  1519. int  bound_trace_main()
  1520.     {
  1521.     enum direction coming_from;
  1522.     unsigned int match_found, continue_loop;
  1523.     int trail_color, fillcolor_used, last_fillcolor_used;
  1524.     int max_putline_length;
  1525.     int right, left, length;
  1526.  
  1527.     if (inside == 0 || outside == 0)
  1528.     {
  1529.     static char far msg[]=
  1530.         "Boundary tracing cannot be used with inside=0 or outside=0.";
  1531.     stopmsg(0,msg);
  1532.     return(-1);
  1533.     }
  1534.     if (colors < 16)
  1535.     {
  1536.     static char far msg[]=
  1537.         "Boundary tracing cannot be used with < 16 colors.";
  1538.     stopmsg(0,msg);
  1539.     return(-1);
  1540.     }
  1541.  
  1542.     got_status = 2;
  1543.     max_putline_length = 0; /* reset max_putline_length */
  1544.     for (currow = iystart; currow <= iystop; currow++)
  1545.     {
  1546.     reset_periodicity = 1; /* reset for a new row */
  1547.     color = bkcolor;
  1548.     for (curcol = ixstart; curcol <= ixstop; curcol++)
  1549.         {
  1550.         if (getcolor(curcol, currow) != bkcolor)
  1551.         continue;
  1552.  
  1553.         trail_color = color;
  1554.         row = currow;
  1555.         col = curcol;
  1556.         if ((*calctype)()== -1) /* color, row, col are global */
  1557.         {
  1558.         if (iystop != yystop)  /* DG */
  1559.            iystop = yystop - (currow - yystart); /* allow for sym */
  1560.         add_worklist(xxstart,xxstop,currow,iystop,currow,0,worksym);
  1561.         return -1;
  1562.         }
  1563.         reset_periodicity = 0; /* normal periodicity checking */
  1564.  
  1565.         /*
  1566.         This next line may cause a few more pixels to be calculated,
  1567.         but at the savings of quite a bit of overhead
  1568.         */
  1569.         if (color != trail_color)  /* DG */
  1570.         continue;
  1571.  
  1572.         /* sweep clockwise to trace outline */
  1573.         trail_row = currow;
  1574.         trail_col = curcol;
  1575.         trail_color = color;
  1576.         fillcolor_used = fillcolor > 0 ? fillcolor : trail_color;
  1577.         coming_from = West;
  1578.         going_to = East;
  1579.         match_found = 0;
  1580.         continue_loop = TRUE;
  1581.         do
  1582.         {
  1583.         step_col_row();
  1584.         if (row >= currow
  1585.             && col >= ixstart
  1586.             && col <= ixstop
  1587.             && row <= iystop)
  1588.             {
  1589.             /* the order of operations in this next line is critical */
  1590.             if ((color = getcolor(col, row)) == bkcolor && (*calctype)()== -1)
  1591.                 /* color, row, col are global for (*calctype)() */
  1592.             {
  1593.             if (iystop != yystop)  /* DG */
  1594.                iystop = yystop - (currow - yystart); /* allow for sym */
  1595.             add_worklist(xxstart,xxstop,currow,iystop,currow,0,worksym);
  1596.             return -1;
  1597.             }
  1598.             else if (color == trail_color)
  1599.             {
  1600.             if (match_found < 4) /* to keep it from overflowing */
  1601.                 match_found++;
  1602.             trail_row = row;
  1603.             trail_col = col;
  1604.             advance_match();
  1605.             }
  1606.             else
  1607.             {
  1608.             advance_no_match();
  1609.             continue_loop = going_to != coming_from || match_found;
  1610.             }
  1611.             }
  1612.         else
  1613.             {
  1614.             advance_no_match();
  1615.             continue_loop = going_to != coming_from || match_found;
  1616.             }
  1617.         } while (continue_loop && (col != curcol || row != currow));
  1618.  
  1619.         if (match_found <= 3)  /* DG */
  1620.         { /* no hole */
  1621.         color = bkcolor;
  1622.         reset_periodicity = 1;
  1623.         continue;
  1624.         }
  1625.  
  1626. /*
  1627. Fill in region by looping around again, filling lines to the left
  1628. whenever going_to is South or West
  1629. */
  1630.         trail_row = currow;
  1631.         trail_col = curcol;
  1632.         coming_from = West;
  1633.         going_to = East;
  1634.         do
  1635.         {
  1636.         match_found = FALSE;
  1637.         do
  1638.             {
  1639.             step_col_row();
  1640.             if (row >= currow
  1641.                 && col >= ixstart
  1642.                 && col <= ixstop
  1643.                 && row <= iystop
  1644.                 && getcolor(col,row) == trail_color)
  1645.                   /* getcolor() must be last */
  1646.             {
  1647.             if (going_to == South
  1648.                 || (going_to == West && coming_from != East))
  1649.                 { /* fill a row, but only once */
  1650.                 right = col;
  1651.                 while (--right >= ixstart && (color = getcolor(right,row)) == trail_color)
  1652.                 ; /* do nothing */
  1653.                 if (color == bkcolor) /* check last color */
  1654.                 {
  1655.                 left = right;
  1656.                 while (getcolor(--left,row) == bkcolor)
  1657.                       /* Should NOT be possible for left < ixstart */
  1658.                     ; /* do nothing */
  1659.                 left++; /* one pixel too far */
  1660.                 if (right == left) /* only one hole */
  1661.                     (*plot)(left,row,fillcolor_used);
  1662.                 else
  1663.                     { /* fill the line to the left */
  1664.                     length=right-left+1;
  1665.                     if (fillcolor_used != last_fillcolor_used || length > max_putline_length)
  1666.                     { /* only reset dstack if necessary */
  1667.                     memset(dstack,fillcolor_used,length);
  1668.                     last_fillcolor_used = fillcolor_used;
  1669.                     max_putline_length = length;
  1670.                     }
  1671.                     put_line(row,left,right,dstack);
  1672.                     /* here's where all the symmetry goes */
  1673.                     if (plot == putcolor)
  1674.                     kbdcount -= length >> 4; /* seems like a reasonable value */
  1675.                     else if (plot == symplot2) /* X-axis symmetry */
  1676.                     {
  1677.                     put_line(yystop-(row-yystart),left,right,dstack);
  1678.                     kbdcount -= length >> 3;
  1679.                     }
  1680.                     else if (plot == symplot2Y) /* Y-axis symmetry */
  1681.                     {
  1682.                     put_line(row,xxstop-(right-xxstart),xxstop-(left-xxstart),dstack);
  1683.                     kbdcount -= length >> 3;
  1684.                     }
  1685.                     else if (plot == symplot2J)  /* Origin symmetry */
  1686.                     {
  1687.                     put_line(yystop-(row-yystart),xxstop-(right-xxstart),xxstop-(left-xxstart),dstack);
  1688.                     kbdcount -= length >> 3;
  1689.                     }
  1690.                     else if (plot == symplot4) /* X-axis and Y-axis symmetry */
  1691.                     {
  1692.                     put_line(yystop-(row-yystart),left,right,dstack);
  1693.                     put_line(row,xxstop-(right-xxstart),xxstop-(left-xxstart),dstack);
  1694.                     put_line(yystop-(row-yystart),xxstop-(right-xxstart),xxstop-(left-xxstart),dstack);
  1695.                     kbdcount -= length >> 2;
  1696.                     }
  1697.                     else    /* cheap and easy way out */
  1698.                     {
  1699.                     int c;
  1700.                     for (c = left; c <= right; c++)  /* DG */
  1701.                         (*plot)(c,row,fillcolor_used);
  1702.                     kbdcount -= length >> 1;
  1703.                     }
  1704.                     }
  1705.                 } /* end of fill line */
  1706.  
  1707.                 if(--kbdcount<=0)
  1708.                 {
  1709.                 if(check_key())
  1710.                     {
  1711.                     if (iystop != yystop)
  1712.                        iystop = yystop - (currow - yystart); /* allow for sym */
  1713.                     add_worklist(xxstart,xxstop,currow,iystop,currow,0,worksym);
  1714.                     return(-1);
  1715.                     }
  1716.                 kbdcount=max_kbdcount;
  1717.                 }
  1718.                 }
  1719.             trail_row = row;
  1720.             trail_col = col;
  1721.             advance_match();
  1722.             match_found = TRUE;
  1723.             }
  1724.             else
  1725.             advance_no_match();
  1726.             } while (!match_found && going_to != coming_from);
  1727.  
  1728.         if (!match_found)
  1729.             { /* next one has to be a match */
  1730.             step_col_row();
  1731.             trail_row = row;
  1732.             trail_col = col;
  1733.             advance_match();
  1734.             }
  1735.         } while (trail_col != curcol || trail_row != currow);
  1736.         reset_periodicity = 1; /* reset after a trace/fill */
  1737.         color = bkcolor;
  1738.         }
  1739.     }
  1740.     return 0;
  1741.     }
  1742.  
  1743. /*******************************************************************/
  1744. /* take one step in the direction of going_to */
  1745. static void step_col_row()
  1746.     {
  1747.     switch (going_to)
  1748.     {
  1749.     case North:
  1750.         col = trail_col;
  1751.         row = trail_row - 1;
  1752.         break;
  1753.     case East:
  1754.         col = trail_col + 1;
  1755.         row = trail_row;
  1756.         break;
  1757.     case South:
  1758.         col = trail_col;
  1759.         row = trail_row + 1;
  1760.         break;
  1761.     case West:
  1762.         col = trail_col - 1;
  1763.         row = trail_row;
  1764.         break;
  1765.     }
  1766.     }
  1767.  
  1768. /******************* end of boundary trace method *******************/
  1769.  
  1770.  
  1771. /************************ super solid guessing *****************************/
  1772.  
  1773. /*
  1774.    I, Timothy Wegner, invented this solidguessing idea and implemented it in
  1775.    more or less the overall framework you see here.  I am adding this note
  1776.    now in a possibly vain attempt to secure my place in history, because
  1777.    Pieter Branderhorst has totally rewritten this routine, incorporating
  1778.    a *MUCH* more sophisticated algorithm.  His revised code is not only
  1779.    faster, but is also more accurate. Harrumph!
  1780. */
  1781.  
  1782. static int solidguess()
  1783. {
  1784.    int i,x,y,xlim,ylim,blocksize;
  1785.    unsigned int *pfxp0,*pfxp1;
  1786.    unsigned int u;
  1787.  
  1788.    guessplot=(plot!=putcolor && plot!=symplot2 && plot!=symplot2J);
  1789.    /* check if guessing at bottom & right edges is ok */
  1790.    bottom_guess = (plot == symplot2 || (plot == putcolor && iystop+1 == ydots));
  1791.    right_guess    = (plot == symplot2J
  1792.        || ((plot == putcolor || plot == symplot2) && ixstop+1 == xdots));
  1793.  
  1794.    i = maxblock = blocksize = ssg_blocksize();
  1795.    totpasses = 1;
  1796.    while ((i >>= 1) > 1) ++totpasses;
  1797.  
  1798.    /* ensure window top and left are on required boundary, treat window
  1799.      as larger than it really is if necessary (this is the reason symplot
  1800.      routines must check for > xdots/ydots before plotting sym points) */
  1801.    ixstart &= -1 - (maxblock-1);
  1802.    iystart = yybegin;
  1803.    iystart &= -1 - (maxblock-1);
  1804.  
  1805.    got_status = 1;
  1806.  
  1807.    if (workpass == 0) /* otherwise first pass already done */
  1808.    {
  1809.       /* first pass, calc every blocksize**2 pixel, quarter result & paint it */
  1810.       curpass = 1;
  1811.       if (iystart <= yystart) /* first time for this window, init it */
  1812.       {
  1813.      currow = 0;
  1814.      memset(&prefix[1][0][0],0,maxxblk*maxyblk*2); /* noskip flags off */
  1815.      reset_periodicity = 1;
  1816.      row=iystart;
  1817.      for(col=ixstart; col<=ixstop; col+=maxblock)
  1818.      { /* calc top row */
  1819.         if((*calctype)()== -1)
  1820.         {
  1821.            add_worklist(xxstart,xxstop,yystart,yystop,yybegin,0,worksym);
  1822.            goto exit_solidguess;
  1823.         }
  1824.         reset_periodicity = 0;
  1825.      }
  1826.       }
  1827.       else
  1828.      memset(&prefix[1][0][0],-1,maxxblk*maxyblk*2); /* noskip flags on */
  1829.       for(y=iystart; y<=iystop; y+=blocksize)
  1830.       {
  1831.      currow = y;
  1832.      i = 0;
  1833.      if(y+blocksize<=iystop)
  1834.      { /* calc the row below */
  1835.         row=y+blocksize;
  1836.         reset_periodicity = 1;
  1837.         for(col=ixstart; col<=ixstop; col+=maxblock)
  1838.         {
  1839.            if((i=(*calctype)()) == -1)
  1840.           break;
  1841.            reset_periodicity = 0;
  1842.         }
  1843.      }
  1844.      reset_periodicity = 1;
  1845.      if (i == -1 || guessrow(1,y,blocksize) != 0) /* interrupted? */
  1846.      {
  1847.         if (y < yystart)
  1848.            y = yystart;
  1849.         add_worklist(xxstart,xxstop,yystart,yystop,y,0,worksym);
  1850.         goto exit_solidguess;
  1851.      }
  1852.       }
  1853.  
  1854.       if (num_worklist) /* work list not empty, just do 1st pass */
  1855.       {
  1856.      add_worklist(xxstart,xxstop,yystart,yystop,yystart,1,worksym);
  1857.      goto exit_solidguess;
  1858.       }
  1859.       ++workpass;
  1860.       iystart = yystart & (-1 - (maxblock-1));
  1861.  
  1862.       /* calculate skip flags for skippable blocks */
  1863.       xlim=(ixstop+maxblock)/maxblock+1;
  1864.       ylim=((iystop+maxblock)/maxblock+15)/16+1;
  1865.       if(right_guess==0) /* no right edge guessing, zap border */
  1866.      for(y=0;y<=ylim;++y)
  1867.         prefix[1][y][xlim]= -1;
  1868.       if(bottom_guess==0) /* no bottom edge guessing, zap border */
  1869.       {
  1870.      i=(iystop+maxblock)/maxblock+1;
  1871.      y=i/16+1;
  1872.      i=1<<(i&15);
  1873.      for(x=0;x<=xlim;++x)
  1874.         prefix[1][y][x]|=i;
  1875.       }
  1876.       /* set each bit in prefix[0] to OR of it & surrounding 8 in prefix[1] */
  1877.       for(y=0;++y<ylim;)
  1878.       {
  1879.      pfxp0= &prefix[0][y][0];
  1880.      pfxp1= &prefix[1][y][0];
  1881.      for(x=0;++x<xlim;)
  1882.      {
  1883.         ++pfxp1;
  1884.         u= *(pfxp1-1)|*pfxp1|*(pfxp1+1);
  1885.         *(++pfxp0)=u|(u>>1)|(u<<1)
  1886.            |((*(pfxp1-(maxxblk+1))|*(pfxp1-maxxblk)|*(pfxp1-(maxxblk-1)))>>15)
  1887.           |((*(pfxp1+(maxxblk-1))|*(pfxp1+maxxblk)|*(pfxp1+(maxxblk+1)))<<15);
  1888.      }
  1889.       }
  1890.    }
  1891.    else /* first pass already done */
  1892.       memset(&prefix[0][0][0],-1,maxxblk*maxyblk*2); /* noskip flags on */
  1893.  
  1894.    /* remaining pass(es), halve blocksize & quarter each blocksize**2 */
  1895.    i = workpass;
  1896.    while (--i > 0) /* allow for already done passes */
  1897.       blocksize = blocksize>>1;
  1898.    reset_periodicity = 1;
  1899.    while((blocksize=blocksize>>1)>=2)
  1900.    {
  1901.       curpass = workpass + 1;
  1902.       for(y=iystart; y<=iystop; y+=blocksize)
  1903.       {
  1904.      currow = y;
  1905.      if(guessrow(0,y,blocksize)!=0)
  1906.      {
  1907.         if (y < yystart)
  1908.            y = yystart;
  1909.         add_worklist(xxstart,xxstop,yystart,yystop,y,workpass,worksym);
  1910.         goto exit_solidguess;
  1911.      }
  1912.       }
  1913.       ++workpass;
  1914.       if (num_worklist /* work list not empty, do one pass at a time */
  1915.       && blocksize>2) /* if 2, we just did last pass */
  1916.       {
  1917.      add_worklist(xxstart,xxstop,yystart,yystop,yystart,workpass,worksym);
  1918.      goto exit_solidguess;
  1919.       }
  1920.       iystart = yystart & (-1 - (maxblock-1));
  1921.    }
  1922.  
  1923.    exit_solidguess:
  1924.    return(0);
  1925. }
  1926.  
  1927. #define calcadot(c,x,y) { col=x; row=y; if((c=(*calctype)())== -1) return -1; }
  1928.  
  1929. static int _fastcall guessrow(int firstpass,int y,int blocksize)
  1930. {
  1931.    int x,i,j,color;
  1932.    int xplushalf,xplusblock;
  1933.    int ylessblock,ylesshalf,yplushalf,yplusblock;
  1934.    int       c21,c31,c41;     /* cxy is the color of pixel at (x,y) */
  1935.    int c12,c22,c32,c42;     /* where c22 is the topleft corner of */
  1936.    int c13,c23,c33;        /* the block being handled in current */
  1937.    int       c24,    c44;     /* iteration                  */
  1938.    int guessed23,guessed32,guessed33,guessed12,guessed13;
  1939.    int prev11,fix21,fix31;
  1940.    unsigned int *pfxptr,pfxmask;
  1941.  
  1942.    halfblock=blocksize>>1;
  1943.    i=y/maxblock;
  1944.    pfxptr= &prefix[firstpass][(i>>4)+1][ixstart/maxblock];
  1945.    pfxmask=1<<(i&15);
  1946.    ylesshalf=y-halfblock;
  1947.    ylessblock=y-blocksize; /* constants, for speed */
  1948.    yplushalf=y+halfblock;
  1949.    yplusblock=y+blocksize;
  1950.    prev11= -1;
  1951.    c24=c12=c13=c22=getcolor(ixstart,y);
  1952.    c31=c21=getcolor(ixstart,(y>0)?ylesshalf:0);
  1953.    if(yplusblock<=iystop)
  1954.       c24=getcolor(ixstart,yplusblock);
  1955.    else if(bottom_guess==0)
  1956.       c24= -1;
  1957.    guessed12=guessed13=0;
  1958.  
  1959.    for(x=ixstart; x<=ixstop;)  /* increment at end, or when doing continue */
  1960.    {
  1961.       if((x&(maxblock-1))==0)  /* time for skip flag stuff */
  1962.       {
  1963.      ++pfxptr;
  1964.      if(firstpass==0 && (*pfxptr&pfxmask)==0)  /* check for fast skip */
  1965.      {
  1966.         /* next useful in testing to make skips visible */
  1967.         /*
  1968.                if(halfblock==1)
  1969.                {
  1970.                   (*plot)(x+1,y,0); (*plot)(x,y+1,0); (*plot)(x+1,y+1,0);
  1971.                   }
  1972.              */
  1973.         x+=maxblock;
  1974.         prev11=c31=c21=c24=c12=c13=c22;
  1975.         guessed12=guessed13=0;
  1976.         continue;
  1977.      }
  1978.       }
  1979.  
  1980.       if(firstpass)  /* 1st pass, paint topleft corner */
  1981.      plotblock(0,x,y,c22);
  1982.       /* setup variables */
  1983.       xplusblock=(xplushalf=x+halfblock)+halfblock;
  1984.       if(xplushalf>ixstop)
  1985.       {
  1986.      if(right_guess==0)
  1987.         c31= -1;
  1988.       }
  1989.       else if(y>0)
  1990.      c31=getcolor(xplushalf,ylesshalf);
  1991.       if(xplusblock<=ixstop)
  1992.       {
  1993.      if(yplusblock<=iystop)
  1994.         c44=getcolor(xplusblock,yplusblock);
  1995.      c41=getcolor(xplusblock,(y>0)?ylesshalf:0);
  1996.      c42=getcolor(xplusblock,y);
  1997.       }
  1998.       else if(right_guess==0)
  1999.      c41=c42=c44= -1;
  2000.       if(yplusblock>iystop)
  2001.      c44=(bottom_guess)?c42:-1;
  2002.  
  2003.       /* guess or calc the remaining 3 quarters of current block */
  2004.       guessed23=guessed32=guessed33=1;
  2005.       c23=c32=c33=c22;
  2006.       if(yplushalf>iystop)
  2007.       {
  2008.      if(bottom_guess==0)
  2009.         c23=c33= -1;
  2010.      guessed23=guessed33= -1;
  2011.       }
  2012.       if(xplushalf>ixstop)
  2013.       {
  2014.      if(right_guess==0)
  2015.         c32=c33= -1;
  2016.      guessed32=guessed33= -1;
  2017.       }
  2018.       while(1) /* go around till none of 23,32,33 change anymore */
  2019.       {
  2020.      if(guessed33>0
  2021.          && (c33!=c44 || c33!=c42 || c33!=c24 || c33!=c32 || c33!=c23))
  2022.      {
  2023.         calcadot(c33,xplushalf,yplushalf);
  2024.         guessed33=0;
  2025.      }
  2026.      if(guessed32>0
  2027.          && (c32!=c33 || c32!=c42 || c32!=c31 || c32!=c21
  2028.          || c32!=c41 || c32!=c23))
  2029.      {
  2030.         calcadot(c32,xplushalf,y);
  2031.         guessed32=0;
  2032.         continue;
  2033.      }
  2034.      if(guessed23>0
  2035.          && (c23!=c33 || c23!=c24 || c23!=c13 || c23!=c12 || c23!=c32))
  2036.      {
  2037.         calcadot(c23,x,yplushalf);
  2038.         guessed23=0;
  2039.         continue;
  2040.      }
  2041.      break;
  2042.       }
  2043.  
  2044.       if(firstpass) /* note whether any of block's contents were calculated */
  2045.      if(guessed23==0 || guessed32==0 || guessed33==0)
  2046.         *pfxptr|=pfxmask;
  2047.  
  2048.       if(halfblock>1) /* not last pass, check if something to display */
  2049.      if(firstpass)    /* display guessed corners, fill in block */
  2050.      {
  2051.         if(guessplot)
  2052.         {
  2053.            if(guessed23>0)
  2054.           (*plot)(x,yplushalf,c23);
  2055.            if(guessed32>0)
  2056.           (*plot)(xplushalf,y,c32);
  2057.            if(guessed33>0)
  2058.           (*plot)(xplushalf,yplushalf,c33);
  2059.         }
  2060.         plotblock(1,x,yplushalf,c23);
  2061.         plotblock(0,xplushalf,y,c32);
  2062.         plotblock(1,xplushalf,yplushalf,c33);
  2063.      }
  2064.      else  /* repaint changed blocks */
  2065.      {
  2066.         if(c23!=c22)
  2067.            plotblock(-1,x,yplushalf,c23);
  2068.         if(c32!=c22)
  2069.            plotblock(-1,xplushalf,y,c32);
  2070.         if(c33!=c22)
  2071.            plotblock(-1,xplushalf,yplushalf,c33);
  2072.      }
  2073.  
  2074.       /* check if some calcs in this block mean earlier guesses need fixing */
  2075.       fix21=((c22!=c12 || c22!=c32)
  2076.       && c21==c22 && c21==c31 && c21==prev11
  2077.       && y>0
  2078.       && (x==ixstart || c21==getcolor(x-halfblock,ylessblock))
  2079.       && (xplushalf>ixstop || c21==getcolor(xplushalf,ylessblock))
  2080.       && c21==getcolor(x,ylessblock));
  2081.       fix31=(c22!=c32
  2082.       && c31==c22 && c31==c42 && c31==c21 && c31==c41
  2083.       && y>0 && xplushalf<=ixstop
  2084.       && c31==getcolor(xplushalf,ylessblock)
  2085.       && (xplusblock>ixstop || c31==getcolor(xplusblock,ylessblock))
  2086.       && c31==getcolor(x,ylessblock));
  2087.       prev11=c31; /* for next time around */
  2088.       if(fix21)
  2089.       {
  2090.      calcadot(c21,x,ylesshalf);
  2091.      if(halfblock>1 && c21!=c22)
  2092.         plotblock(-1,x,ylesshalf,c21);
  2093.       }
  2094.       if(fix31)
  2095.       {
  2096.      calcadot(c31,xplushalf,ylesshalf);
  2097.      if(halfblock>1 && c31!=c22)
  2098.         plotblock(-1,xplushalf,ylesshalf,c31);
  2099.       }
  2100.       if(c23!=c22)
  2101.       {
  2102.      if(guessed12)
  2103.      {
  2104.         calcadot(c12,x-halfblock,y);
  2105.         if(halfblock>1 && c12!=c22)
  2106.            plotblock(-1,x-halfblock,y,c12);
  2107.      }
  2108.      if(guessed13)
  2109.      {
  2110.         calcadot(c13,x-halfblock,yplushalf);
  2111.         if(halfblock>1 && c13!=c22)
  2112.            plotblock(-1,x-halfblock,yplushalf,c13);
  2113.      }
  2114.       }
  2115.       c22=c42;
  2116.       c24=c44;
  2117.       c13=c33;
  2118.       c31=c21=c41;
  2119.       c12=c32;
  2120.       guessed12=guessed32;
  2121.       guessed13=guessed33;
  2122.       x+=blocksize;
  2123.    } /* end x loop */
  2124.  
  2125.    if(firstpass==0 || guessplot) return 0;
  2126.  
  2127.    /* paint rows the fast way */
  2128.    for(i=0;i<halfblock;++i)
  2129.    {
  2130.       if((j=y+i)<=iystop)
  2131.      put_line(j,xxstart,ixstop,&dstack[xxstart]);
  2132.       if((j=y+i+halfblock)<=iystop)
  2133.      put_line(j,xxstart,ixstop,&dstack[xxstart+MAXPIXELS]);
  2134.       if(keypressed()) return -1;
  2135.    }
  2136.    if(plot!=putcolor)  /* symmetry, just vertical & origin the fast way */
  2137.    {
  2138.       if(plot==symplot2J) /* origin sym, reverse lines */
  2139.      for(i=(ixstop+xxstart+1)/2;--i>=xxstart;)
  2140.      {
  2141.         color=dstack[i];
  2142.         dstack[i]=dstack[j=ixstop-(i-xxstart)];
  2143.         dstack[j]=color;
  2144.         j+=MAXPIXELS;
  2145.         color=dstack[i+MAXPIXELS];
  2146.         dstack[i+MAXPIXELS]=dstack[j];
  2147.         dstack[j]=color;
  2148.      }
  2149.       for(i=0;i<halfblock;++i)
  2150.       {
  2151.      if((j=yystop-(y+i-yystart))>iystop && j<ydots)
  2152.         put_line(j,xxstart,ixstop,&dstack[xxstart]);
  2153.      if((j=yystop-(y+i+halfblock-yystart))>iystop && j<ydots)
  2154.         put_line(j,xxstart,ixstop,&dstack[xxstart+MAXPIXELS]);
  2155.      if(keypressed()) return -1;
  2156.       }
  2157.    }
  2158.    return 0;
  2159. }
  2160.  
  2161. static void _fastcall plotblock(int buildrow,int x,int y,int color)
  2162. {
  2163.    int i,xlim,ylim;
  2164.    if((xlim=x+halfblock)>ixstop)
  2165.       xlim=ixstop+1;
  2166.    if(buildrow>=0 && guessplot==0) /* save it for later put_line */
  2167.    {
  2168.       if(buildrow==0)
  2169.      for(i=x;i<xlim;++i)
  2170.         dstack[i]=color;
  2171.       else
  2172.      for(i=x;i<xlim;++i)
  2173.         dstack[i+MAXPIXELS]=color;
  2174.       if (x>=xxstart) /* when x reduced for alignment, paint those dots too */
  2175.      return; /* the usual case */
  2176.    }
  2177.    /* paint it */
  2178.    if((ylim=y+halfblock)>iystop)
  2179.    {
  2180.       if(y>iystop)
  2181.      return;
  2182.       ylim=iystop+1;
  2183.    }
  2184.    for(i=x;++i<xlim;)
  2185.       (*plot)(i,y,color); /* skip 1st dot on 1st row */
  2186.    while(++y<ylim)
  2187.       for(i=x;i<xlim;++i)
  2188.      (*plot)(i,y,color);
  2189. }
  2190.  
  2191.  
  2192. /************************* symmetry plot setup ************************/
  2193.  
  2194. static int _fastcall xsym_split(int xaxis_row,int xaxis_between)
  2195. {
  2196.    int i;
  2197.    if ((worksym&0x11) == 0x10) /* already decided not sym */
  2198.       return(1);
  2199.    if ((worksym&1) != 0) /* already decided on sym */
  2200.       iystop = (yystart+yystop)/2;
  2201.    else /* new window, decide */
  2202.    {
  2203.       worksym |= 0x10;
  2204.       if (xaxis_row <= yystart || xaxis_row >= yystop)
  2205.      return(1); /* axis not in window */
  2206.       i = xaxis_row + (xaxis_row - yystart);
  2207.       if (xaxis_between)
  2208.      ++i;
  2209.       if (i > yystop) /* split into 2 pieces, bottom has the symmetry */
  2210.       {
  2211.      if (num_worklist >= MAXCALCWORK-1) /* no room to split */
  2212.         return(1);
  2213.      iystop = xaxis_row - (yystop - xaxis_row);
  2214.      if (!xaxis_between)
  2215.         --iystop;
  2216.      add_worklist(xxstart,xxstop,iystop+1,yystop,iystop+1,workpass,0);
  2217.      yystop = iystop;
  2218.      return(1); /* tell set_symmetry no sym for current window */
  2219.       }
  2220.       if (i < yystop) /* split into 2 pieces, top has the symmetry */
  2221.       {
  2222.      if (num_worklist >= MAXCALCWORK-1) /* no room to split */
  2223.         return(1);
  2224.      add_worklist(xxstart,xxstop,i+1,yystop,i+1,workpass,0);
  2225.      yystop = i;
  2226.       }
  2227.       iystop = xaxis_row;
  2228.       worksym |= 1;
  2229.    }
  2230.    symmetry = 0;
  2231.    return(0); /* tell set_symmetry its a go */
  2232. }
  2233.  
  2234. static int _fastcall ysym_split(int yaxis_col,int yaxis_between)
  2235. {
  2236.    int i;
  2237.    if ((worksym&0x22) == 0x20) /* already decided not sym */
  2238.       return(1);
  2239.    if ((worksym&2) != 0) /* already decided on sym */
  2240.       ixstop = (xxstart+xxstop)/2;
  2241.    else /* new window, decide */
  2242.    {
  2243.       worksym |= 0x20;
  2244.       if (yaxis_col <= xxstart || yaxis_col >= xxstop)
  2245.      return(1); /* axis not in window */
  2246.       i = yaxis_col + (yaxis_col - xxstart);
  2247.       if (yaxis_between)
  2248.      ++i;
  2249.       if (i > xxstop) /* split into 2 pieces, right has the symmetry */
  2250.       {
  2251.      if (num_worklist >= MAXCALCWORK-1) /* no room to split */
  2252.         return(1);
  2253.      ixstop = yaxis_col - (xxstop - yaxis_col);
  2254.      if (!yaxis_between)
  2255.         --ixstop;
  2256.      add_worklist(ixstop+1,xxstop,yystart,yystop,yystart,workpass,0);
  2257.      xxstop = ixstop;
  2258.      return(1); /* tell set_symmetry no sym for current window */
  2259.       }
  2260.       if (i < xxstop) /* split into 2 pieces, left has the symmetry */
  2261.       {
  2262.      if (num_worklist >= MAXCALCWORK-1) /* no room to split */
  2263.         return(1);
  2264.      add_worklist(i+1,xxstop,yystart,yystop,yystart,workpass,0);
  2265.      xxstop = i;
  2266.       }
  2267.       ixstop = yaxis_col;
  2268.       worksym |= 2;
  2269.    }
  2270.    symmetry = 0;
  2271.    return(0); /* tell set_symmetry its a go */
  2272. }
  2273.  
  2274. #ifdef _MSC_VER
  2275. #pragma optimize ("ea", off)
  2276. #endif
  2277.  
  2278. static void _fastcall setsymmetry(int sym, int uselist) /* set up proper symmetrical plot functions */
  2279. {
  2280.    extern int forcesymmetry;
  2281.    int i;
  2282.    int parmszero;
  2283.    int xaxis_row, yaxis_col;         /* pixel number for origin */
  2284.    int xaxis_between, yaxis_between; /* if axis between 2 pixels, not on one */
  2285.    double ftemp;
  2286.    symmetry = 1;
  2287.    TranspSymmetry = sym;         /* for tp3d.c, MCP 6-1-90 */
  2288.    if(sym == NOPLOT && forcesymmetry == 999)
  2289.    {
  2290.       plot = noplot;
  2291.       return;
  2292.    }
  2293.    /* NOTE: 16-bit potential disables symmetry */
  2294.    /* also any decomp= option and any inversion not about the origin */
  2295.    /* also any rotation other than 180deg and any off-axis stretch */
  2296.    if ((potflag && pot16bit) || (invert && inversion[2] != 0.0)
  2297.        || decomp[0] != 0
  2298.        || xxmin!=xx3rd || yymin!=yy3rd)
  2299.       return;
  2300.    if(sym != XAXIS && sym != XAXIS_NOPARM && inversion[1] != 0.0 && forcesymmetry == 999)
  2301.       return;
  2302.    if(forcesymmetry < 999)
  2303.       sym = forcesymmetry;
  2304.    else if(forcesymmetry == 1000)
  2305.       forcesymmetry = sym;  /* for backwards compatibility */
  2306.    else if(outside==REAL || outside==IMAG || outside==MULT || outside==SUM)
  2307.       return;
  2308.    parmszero = (parm.x == 0.0 && parm.y == 0.0 && useinitorbit != 1);
  2309.    switch (fractype)
  2310.    { case LMANLAMFNFN:      /* These need only P1 checked. */
  2311.      case FPMANLAMFNFN:     /* P2 is used for a switch value */
  2312.      case LMANFNFN:         /* These have NOPARM set in fractalp.c, */
  2313.      case FPMANFNFN:        /* but it only applies to P1. */
  2314.      case FPMANDELZPOWER:   /* or P2 is an exponent */
  2315.      case LMANDELZPOWER:
  2316.      case FPMANZTOZPLUSZPWR:
  2317.        break;
  2318.      default:   /* Check P2 for the rest */
  2319.        parmszero = (parmszero && parm2.x == 0.0 && parm2.y == 0.0);
  2320.    }
  2321.    xaxis_row = yaxis_col = -1;
  2322.    if (fabs(yymin+yymax) < fabs(yymin)+fabs(yymax)) /* axis is on screen */
  2323.    {
  2324.       ftemp = (0.0-yymax) / (yymin-yymax) * (ydots-1) + 0.25;
  2325.       xaxis_row = ftemp;
  2326.       xaxis_between = (ftemp - xaxis_row >= 0.5);
  2327.       if (uselist == 0 && (!xaxis_between || (xaxis_row+1)*2 != ydots))
  2328.      xaxis_row = -1; /* can't split screen, so dead center or not at all */
  2329.    }
  2330.    if (fabs(xxmin+xxmax) < fabs(xxmin)+fabs(xxmax)) /* axis is on screen */
  2331.    {
  2332.       ftemp = (0.0-xxmin) / (xxmax-xxmin) * (xdots-1) + 0.25;
  2333.       yaxis_col = ftemp;
  2334.       yaxis_between = (ftemp - yaxis_col >= 0.5);
  2335.       if (uselist == 0 && (!yaxis_between || (yaxis_col+1)*2 != xdots))
  2336.      yaxis_col = -1; /* can't split screen, so dead center or not at all */
  2337.    }
  2338.    switch(sym)         /* symmetry switch */
  2339.    {
  2340.    case XAXIS_NOREAL:     /* X-axis Symmetry (no real param) */
  2341.       if (parm.x != 0.0) break;
  2342.       goto xsym;
  2343.    case XAXIS_NOIMAG:     /* X-axis Symmetry (no imag param) */
  2344.       if (parm.y != 0.0) break;
  2345.       goto xsym;
  2346.    case XAXIS_NOPARM:                 /* X-axis Symmetry  (no params)*/
  2347.       if (!parmszero)
  2348.      break;
  2349.       xsym:
  2350.    case XAXIS:                 /* X-axis Symmetry */
  2351.       if (xsym_split(xaxis_row,xaxis_between) == 0)
  2352.      if(basin)
  2353.         plot = symplot2basin;
  2354.      else
  2355.         plot = symplot2;
  2356.       break;
  2357.    case YAXIS_NOPARM:                 /* Y-axis Symmetry (No Parms)*/
  2358.       if (!parmszero)
  2359.      break;
  2360.    case YAXIS:                 /* Y-axis Symmetry */
  2361.       if (ysym_split(yaxis_col,yaxis_between) == 0)
  2362.      plot = symplot2Y;
  2363.       break;
  2364.    case XYAXIS_NOPARM:                 /* X-axis AND Y-axis Symmetry (no parms)*/
  2365.       if(!parmszero)
  2366.      break;
  2367.    case XYAXIS:              /* X-axis AND Y-axis Symmetry */
  2368.       xsym_split(xaxis_row,xaxis_between);
  2369.       ysym_split(yaxis_col,yaxis_between);
  2370.       switch (worksym & 3)
  2371.       {
  2372.       case 1: /* just xaxis symmetry */
  2373.      if(basin)
  2374.         plot = symplot2basin;
  2375.      else
  2376.         plot = symplot2 ;
  2377.      break;
  2378.       case 2: /* just yaxis symmetry */
  2379.      if (basin) /* got no routine for this case */
  2380.      {
  2381.         ixstop = xxstop; /* fix what split should not have done */
  2382.         symmetry = 1;
  2383.      }
  2384.      else
  2385.         plot = symplot2Y;
  2386.      break;
  2387.       case 3: /* both axes */
  2388.      if(basin)
  2389.         plot = symplot4basin;
  2390.      else
  2391.         plot = symplot4 ;
  2392.       }
  2393.       break;
  2394.    case ORIGIN_NOPARM:                 /* Origin Symmetry (no parms)*/
  2395.       if (!parmszero)
  2396.      break;
  2397.    case ORIGIN:              /* Origin Symmetry */
  2398.       originsym:
  2399.       if ( xsym_split(xaxis_row,xaxis_between) == 0
  2400.       && ysym_split(yaxis_col,yaxis_between) == 0)
  2401.       {
  2402.      plot = symplot2J;
  2403.      ixstop = xxstop; /* didn't want this changed */
  2404.       }
  2405.       else
  2406.       {
  2407.      iystop = yystop; /* in case first split worked */
  2408.      symmetry = 1;
  2409.      worksym = 0x30; /* let it recombine with others like it */
  2410.       }
  2411.       break;
  2412.    case PI_SYM_NOPARM:
  2413.       if (!parmszero)
  2414.      break;
  2415.    case PI_SYM:              /* PI symmetry */
  2416.       if(fabs(xxmax - xxmin) < PI/4)   
  2417.          break; /* no point in pi symmetry if values too close */
  2418.       if(invert && forcesymmetry == 999)
  2419.      goto originsym;
  2420.       plot = symPIplot ;
  2421.       symmetry = 0;
  2422.       if ( xsym_split(xaxis_row,xaxis_between) == 0
  2423.       && ysym_split(yaxis_col,yaxis_between) == 0)
  2424.      if(parm.y == 0.0)
  2425.         plot = symPIplot4J; /* both axes */
  2426.      else
  2427.         plot = symPIplot2J; /* origin */
  2428.       else
  2429.       {
  2430.      iystop = yystop; /* in case first split worked */
  2431.      worksym = 0x30;  /* don't mark pisym as ysym, just do it unmarked */
  2432.       }
  2433.       pixelpi = (PI/fabs(xxmax-xxmin))*xdots; /* PI in pixels */
  2434.       if ( (ixstop = xxstart+pixelpi-1 ) > xxstop)
  2435.      ixstop = xxstop;
  2436.       if (plot == symPIplot4J && ixstop > (i = (xxstart+xxstop)/2))
  2437.      ixstop = i;
  2438.       break;
  2439.    default:             /* no symmetry */
  2440.       break;
  2441.    }
  2442. }
  2443.  
  2444. #ifdef _MSC_VER
  2445. #pragma optimize ("ea", on)
  2446. #endif
  2447.  
  2448. /**************** tesseral method by CJLT begins here*********************/
  2449. /*  reworked by PB for speed and resumeability */
  2450.  
  2451. struct tess { /* one of these per box to be done gets stacked */
  2452.    int x1,x2,y1,y2;     /* left/right top/bottom x/y coords  */
  2453.    int top,bot,lft,rgt;  /* edge colors, -1 mixed, -2 unknown */
  2454. };
  2455.  
  2456. static int tesseral()
  2457. {
  2458.    register struct tess *tp;
  2459.  
  2460.    guessplot = (plot != putcolor && plot != symplot2);
  2461.    tp = (struct tess *)&dstack[0];
  2462.    tp->x1 = ixstart;                  /* set up initial box */
  2463.    tp->x2 = ixstop;
  2464.    tp->y1 = iystart;
  2465.    tp->y2 = iystop;
  2466.  
  2467.    if (workpass == 0) { /* not resuming */
  2468.       tp->top = tessrow(ixstart,ixstop,iystart);     /* Do top row */
  2469.       tp->bot = tessrow(ixstart,ixstop,iystop);      /* Do bottom row */
  2470.       tp->lft = tesscol(ixstart,iystart+1,iystop-1); /* Do left column */
  2471.       tp->rgt = tesscol(ixstop,iystart+1,iystop-1);  /* Do right column */
  2472.       if (check_key()) { /* interrupt before we got properly rolling */
  2473.          add_worklist(xxstart,xxstop,yystart,yystop,yystart,0,worksym);
  2474.          return(-1);
  2475.       }
  2476.    }
  2477.  
  2478.    else { /* resuming, rebuild work stack */
  2479.       int i,mid,curx,cury,xsize,ysize;
  2480.       struct tess *tp2;
  2481.       tp->top = tp->bot = tp->lft = tp->rgt = -2;
  2482.       cury = yybegin & 0xfff;
  2483.       ysize = 1;
  2484.       i = (unsigned)yybegin >> 12;
  2485.       while (--i >= 0) ysize <<= 1;
  2486.       curx = workpass & 0xfff;
  2487.       xsize = 1;
  2488.       i = (unsigned)workpass >> 12;
  2489.       while (--i >= 0) xsize <<= 1;
  2490.       while (1) {
  2491.          tp2 = tp;
  2492.          if (tp->x2 - tp->x1 > tp->y2 - tp->y1) { /* next divide down middle */
  2493.             if (tp->x1 == curx && (tp->x2 - tp->x1 - 2) < xsize)
  2494.                break;
  2495.             mid = (tp->x1 + tp->x2) >> 1;         /* Find mid point */
  2496.             if (mid > curx) { /* stack right part */
  2497.                memcpy(++tp,tp2,sizeof(*tp));
  2498.                tp->x2 = mid;
  2499.             }
  2500.             tp2->x1 = mid;
  2501.          }
  2502.          else {                   /* next divide across */
  2503.             if (tp->y1 == cury && (tp->y2 - tp->y1 - 2) < ysize) break;
  2504.             mid = (tp->y1 + tp->y2) >> 1;         /* Find mid point */
  2505.             if (mid > cury) { /* stack bottom part */
  2506.                memcpy(++tp,tp2,sizeof(*tp));
  2507.                tp->y2 = mid;
  2508.             }
  2509.             tp2->y1 = mid;
  2510.          }
  2511.       }
  2512.    }
  2513.  
  2514.    got_status = 4; /* for tab_display */
  2515.  
  2516.    while (tp >= (struct tess *)&dstack[0]) { /* do next box */
  2517.       curcol = tp->x1; /* for tab_display */
  2518.       currow = tp->y1;
  2519.  
  2520.       if (tp->top == -1 || tp->bot == -1 || tp->lft == -1 || tp->rgt == -1)
  2521.          goto tess_split;
  2522.       /* for any edge whose color is unknown, set it */
  2523.       if (tp->top == -2)
  2524.          tp->top = tesschkrow(tp->x1,tp->x2,tp->y1);
  2525.       if (tp->top == -1)
  2526.          goto tess_split;
  2527.       if (tp->bot == -2)
  2528.          tp->bot = tesschkrow(tp->x1,tp->x2,tp->y2);
  2529.       if (tp->bot != tp->top)
  2530.          goto tess_split;
  2531.       if (tp->lft == -2)
  2532.          tp->lft = tesschkcol(tp->x1,tp->y1,tp->y2);
  2533.       if (tp->lft != tp->top)
  2534.          goto tess_split;
  2535.       if (tp->rgt == -2)
  2536.          tp->rgt = tesschkcol(tp->x2,tp->y1,tp->y2);
  2537.       if (tp->rgt != tp->top)
  2538.          goto tess_split;
  2539.  
  2540.       {
  2541.       int mid,midcolor;
  2542.       if (tp->x2 - tp->x1 > tp->y2 - tp->y1) { /* divide down the middle */
  2543.          mid = (tp->x1 + tp->x2) >> 1;         /* Find mid point */
  2544.          midcolor = tesscol(mid, tp->y1+1, tp->y2-1); /* Do mid column */
  2545.          if (midcolor != tp->top) goto tess_split;
  2546.          }
  2547.       else {                   /* divide across the middle */
  2548.          mid = (tp->y1 + tp->y2) >> 1;         /* Find mid point */
  2549.          midcolor = tessrow(tp->x1+1, tp->x2-1, mid); /* Do mid row */
  2550.          if (midcolor != tp->top) goto tess_split;
  2551.          }
  2552.       }
  2553.  
  2554.       {  /* all 4 edges are the same color, fill in */
  2555.          int i,j;
  2556.          i = 0;
  2557.          if(fillcolor != 0)
  2558.          {
  2559.          if(fillcolor > 0)
  2560.             tp->top = fillcolor & (colors-1);
  2561.          if (guessplot || (j = tp->x2 - tp->x1 - 1) < 2) { /* paint dots */
  2562.             for (col = tp->x1 + 1; col < tp->x2; col++)
  2563.                for (row = tp->y1 + 1; row < tp->y2; row++) {
  2564.                   (*plot)(col,row,tp->top);
  2565.                   if (++i > 500) {
  2566.                      if (check_key()) goto tess_end;
  2567.                      i = 0;
  2568.                   }
  2569.                }
  2570.          }
  2571.          else { /* use put_line for speed */
  2572.             memset(&dstack[MAXPIXELS],tp->top,j);
  2573.             for (row = tp->y1 + 1; row < tp->y2; row++) {
  2574.                put_line(row,tp->x1+1,tp->x2-1,&dstack[MAXPIXELS]);
  2575.                if (plot != putcolor) /* symmetry */
  2576.                   if ((j = yystop-(row-yystart)) > iystop && j < ydots)
  2577.                      put_line(j,tp->x1+1,tp->x2-1,&dstack[MAXPIXELS]);
  2578.                if (++i > 25) {
  2579.                   if (check_key()) goto tess_end;
  2580.                   i = 0;
  2581.                }
  2582.             }
  2583.          }
  2584.          }
  2585.          --tp;
  2586.       }
  2587.       continue;
  2588.  
  2589.       tess_split:
  2590.       {  /* box not surrounded by same color, sub-divide */
  2591.          int mid,midcolor;
  2592.          struct tess *tp2;
  2593.          if (tp->x2 - tp->x1 > tp->y2 - tp->y1) { /* divide down the middle */
  2594.             mid = (tp->x1 + tp->x2) >> 1;         /* Find mid point */
  2595.             midcolor = tesscol(mid, tp->y1+1, tp->y2-1); /* Do mid column */
  2596.             if (midcolor == -3) goto tess_end;
  2597.             if (tp->x2 - mid > 1) {    /* right part >= 1 column */
  2598.                if (tp->top == -1) tp->top = -2;
  2599.                if (tp->bot == -1) tp->bot = -2;
  2600.                tp2 = tp;
  2601.                if (mid - tp->x1 > 1) { /* left part >= 1 col, stack right */
  2602.                   memcpy(++tp,tp2,sizeof(*tp));
  2603.                   tp->x2 = mid;
  2604.                   tp->rgt = midcolor;
  2605.                }
  2606.                tp2->x1 = mid;
  2607.                tp2->lft = midcolor;
  2608.             }
  2609.             else
  2610.                --tp;
  2611.          }
  2612.          else {                   /* divide across the middle */
  2613.             mid = (tp->y1 + tp->y2) >> 1;         /* Find mid point */
  2614.             midcolor = tessrow(tp->x1+1, tp->x2-1, mid); /* Do mid row */
  2615.             if (midcolor == -3) goto tess_end;
  2616.             if (tp->y2 - mid > 1) {    /* bottom part >= 1 column */
  2617.                if (tp->lft == -1) tp->lft = -2;
  2618.                if (tp->rgt == -1) tp->rgt = -2;
  2619.                tp2 = tp;
  2620.                if (mid - tp->y1 > 1) { /* top also >= 1 col, stack bottom */
  2621.                   memcpy(++tp,tp2,sizeof(*tp));
  2622.                   tp->y2 = mid;
  2623.                   tp->bot = midcolor;
  2624.                }
  2625.                tp2->y1 = mid;
  2626.                tp2->top = midcolor;
  2627.             }
  2628.             else
  2629.                --tp;
  2630.          }
  2631.       }
  2632.  
  2633.    }
  2634.  
  2635.    tess_end:
  2636.    if (tp >= (struct tess *)&dstack[0]) { /* didn't complete */
  2637.       int i,xsize,ysize;
  2638.       xsize = ysize = 1;
  2639.       i = 2;
  2640.       while (tp->x2 - tp->x1 - 2 >= i) {
  2641.          i <<= 1;
  2642.          ++xsize;
  2643.       }
  2644.       i = 2;
  2645.       while (tp->y2 - tp->y1 - 2 >= i) {
  2646.          i <<= 1;
  2647.          ++ysize;
  2648.       }
  2649.       add_worklist(xxstart,xxstop,yystart,yystop,
  2650.           (ysize<<12)+tp->y1,(xsize<<12)+tp->x1,worksym);
  2651.       return(-1);
  2652.    }
  2653.    return(0);
  2654.  
  2655. } /* tesseral */
  2656.  
  2657. static int _fastcall tesschkcol(int x,int y1,int y2)
  2658. {
  2659.    int i;
  2660.    i = getcolor(x,++y1);
  2661.    while (--y2 > y1)
  2662.       if (getcolor(x,y2) != i) return -1;
  2663.    return i;
  2664. }
  2665.  
  2666. static int _fastcall tesschkrow(int x1,int x2,int y)
  2667. {
  2668.    int i;
  2669.    i = getcolor(x1,y);
  2670.    while (x2 > x1) {
  2671.       if (getcolor(x2,y) != i) return -1;
  2672.       --x2;
  2673.    }
  2674.    return i;
  2675. }
  2676.  
  2677. static int _fastcall tesscol(int x,int y1,int y2)
  2678. {
  2679.    int colcolor,i;
  2680.    col = x;
  2681.    row = y1;
  2682.    reset_periodicity = 1;
  2683.    colcolor = (*calctype)();
  2684.    reset_periodicity = 0;
  2685.    while (++row <= y2) { /* generate the column */
  2686.       if ((i = (*calctype)()) < 0) return -3;
  2687.       if (i != colcolor) colcolor = -1;
  2688.    }
  2689.    return colcolor;
  2690. }
  2691.  
  2692. static int _fastcall tessrow(int x1,int x2,int y)
  2693. {
  2694.    int rowcolor,i;
  2695.    row = y;
  2696.    col = x1;
  2697.    reset_periodicity = 1;
  2698.    rowcolor = (*calctype)();
  2699.    reset_periodicity = 0;
  2700.    while (++col <= x2) { /* generate the row */
  2701.       if ((i = (*calctype)()) < 0) return -3;
  2702.       if (i != rowcolor) rowcolor = -1;
  2703.    }
  2704.    return rowcolor;
  2705. }
  2706.  
  2707. /* added for testing autologmap() */ /* CAE 9211 fixed missing comment */
  2708. /* insert at end of CALCFRAC.C */
  2709.  
  2710. static int autologmap()   /*RB*/
  2711. {  /* calculate round screen edges to avoid wasted colours in logmap */
  2712.  int mincolour,lag;
  2713.  mincolour=32767;
  2714.  row=iystart;
  2715.  reset_periodicity = 0;
  2716.  for (col=ixstart;col<ixstop;col++) /* top row */
  2717.     {
  2718.       color=(*calctype)();
  2719.       if (realcolor < mincolour) mincolour=realcolor ;
  2720.       if ( col >=32 ) (*plot)(col-32,row,0);
  2721.     }                                    /* these lines tidy up for BTM etc */
  2722.     for (lag=32;lag>0;lag--) (*plot)(col-lag,row,0);
  2723.  
  2724.  col=ixstop;
  2725.  for (row=iystart;row<iystop;row++) /* right  side */
  2726.     {
  2727.       color=(*calctype)();
  2728.       if (realcolor < mincolour) mincolour=realcolor ;
  2729.       if ( row >=32 ) (*plot)(col,row-32,0);
  2730.     }
  2731.     for (lag=32;lag>0;lag--) (*plot)(col,row-lag,0);
  2732.  
  2733.  col=ixstart;
  2734.  for (row=iystart;row<iystop;row++) /* left  side */
  2735.     {
  2736.       color=(*calctype)();
  2737.       if (realcolor < mincolour) mincolour=realcolor ;
  2738.       if ( row >=32 ) (*plot)(col,row-32,0);
  2739.     }
  2740.     for (lag=32;lag>0;lag--) (*plot)(col,row-lag,0);
  2741.  
  2742.  row=iystop ;
  2743.  for (col=ixstart;col<ixstop;col++) /* bottom row */
  2744.     {
  2745.       color=(*calctype)();
  2746.       if (realcolor < mincolour) mincolour=realcolor ;
  2747.       if ( col >=32 ) (*plot)(col-32,row,0);
  2748.     }
  2749.     for (lag=32;lag>0;lag--) (*plot)(col-lag,row,0);
  2750.  
  2751.  if (mincolour==2) resuming=1; /* insure autologmap not called again */
  2752.  
  2753.  return mincolour;
  2754. }
  2755.  
  2756. /* Symmetry plot for period PI */
  2757. void _fastcall symPIplot(x, y, color)
  2758. int x, y, color ;
  2759. {
  2760.    while(x <= xxstop)
  2761.    {
  2762.       putcolor(x, y, color) ;
  2763.       x += pixelpi;
  2764.    }
  2765. }
  2766. /* Symmetry plot for period PI plus Origin Symmetry */
  2767. void _fastcall symPIplot2J(x, y, color)
  2768. int x, y, color ;
  2769. {
  2770.    int i,j;
  2771.    while(x <= xxstop)
  2772.    {
  2773.       putcolor(x, y, color) ;
  2774.       if ((i=yystop-(y-yystart)) > iystop && i < ydots
  2775.       && (j=xxstop-(x-xxstart)) < xdots)
  2776.      putcolor(j, i, color) ;
  2777.       x += pixelpi;
  2778.    }
  2779. }
  2780. /* Symmetry plot for period PI plus Both Axis Symmetry */
  2781. void _fastcall symPIplot4J(x, y, color)
  2782. int x, y, color ;
  2783. {
  2784.    int i,j;
  2785.    while(x <= (xxstart+xxstop)/2)
  2786.    {
  2787.       j = xxstop-(x-xxstart);
  2788.       putcolor(       x , y , color) ;
  2789.       if (j < xdots)
  2790.      putcolor(j , y , color) ;
  2791.       if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  2792.       {
  2793.      putcolor(x , i , color) ;
  2794.      if (j < xdots)
  2795.         putcolor(j , i , color) ;
  2796.       }
  2797.       x += pixelpi;
  2798.    }
  2799. }
  2800.  
  2801. /* Symmetry plot for X Axis Symmetry */
  2802. void _fastcall symplot2(x, y, color)
  2803. int x, y, color ;
  2804. {
  2805.    int i;
  2806.    putcolor(x, y, color) ;
  2807.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  2808.       putcolor(x, i, color) ;
  2809. }
  2810.  
  2811. /* Symmetry plot for Y Axis Symmetry */
  2812. void _fastcall symplot2Y(x, y, color)
  2813. int x, y, color ;
  2814. {
  2815.    int i;
  2816.    putcolor(x, y, color) ;
  2817.    if ((i=xxstop-(x-xxstart)) < xdots)
  2818.       putcolor(i, y, color) ;
  2819. }
  2820.  
  2821. /* Symmetry plot for Origin Symmetry */
  2822. void _fastcall symplot2J(x, y, color)
  2823. int x, y, color ;
  2824. {
  2825.    int i,j;
  2826.    putcolor(x, y, color) ;
  2827.    if ((i=yystop-(y-yystart)) > iystop && i < ydots
  2828.        && (j=xxstop-(x-xxstart)) < xdots)
  2829.       putcolor(j, i, color) ;
  2830. }
  2831.  
  2832. /* Symmetry plot for Both Axis Symmetry */
  2833. void _fastcall symplot4(x, y, color)
  2834. int x, y, color ;
  2835. {
  2836.    int i,j;
  2837.    j = xxstop-(x-xxstart);
  2838.    putcolor(       x , y, color) ;
  2839.    if (j < xdots)
  2840.       putcolor(    j , y, color) ;
  2841.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  2842.    {
  2843.       putcolor(    x , i, color) ;
  2844.       if (j < xdots)
  2845.      putcolor(j , i, color) ;
  2846.    }
  2847. }
  2848.  
  2849. /* Symmetry plot for X Axis Symmetry - Striped Newtbasin version */
  2850. void _fastcall symplot2basin(x, y, color)
  2851. int x, y, color ;
  2852. {
  2853.    int i,stripe;
  2854.    extern int degree;
  2855.    putcolor(x, y, color) ;
  2856.    if(basin==2 && color > 8)
  2857.       stripe=8;
  2858.    else
  2859.       stripe = 0;
  2860.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  2861.    {
  2862.       color -= stripe;              /* reconstruct unstriped color */
  2863.       color = (degree+1-color)%degree+1;  /* symmetrical color */
  2864.       color += stripe;              /* add stripe */
  2865.       putcolor(x, i,color)  ;
  2866.    }
  2867. }
  2868.  
  2869. /* Symmetry plot for Both Axis Symmetry  - Newtbasin version */
  2870. void _fastcall symplot4basin(x, y, color)
  2871. int x, y, color ;
  2872. {
  2873.    extern int degree;
  2874.    int i,j,color1,stripe;
  2875.    if(color == 0) /* assumed to be "inside" color */
  2876.    {
  2877.       symplot4(x, y, color);
  2878.       return;
  2879.    }
  2880.    if(basin==2 && color > 8)
  2881.       stripe = 8;
  2882.    else
  2883.       stripe = 0;
  2884.    color -= stripe;          /* reconstruct unstriped color */
  2885.    color1 = degree/2+degree+2 - color;
  2886.    if(color < degree/2+2)
  2887.       color1 = degree/2+2 - color;
  2888.    else
  2889.       color1 = degree/2+degree+2 - color;
  2890.    j = xxstop-(x-xxstart);
  2891.    putcolor(       x, y, color+stripe) ;
  2892.    if (j < xdots)
  2893.       putcolor(    j, y, color1+stripe) ;
  2894.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  2895.    {
  2896.       putcolor(    x, i, stripe + (degree+1 - color)%degree+1) ;
  2897.       if (j < xdots)
  2898.      putcolor(j, i, stripe + (degree+1 - color1)%degree+1) ;
  2899.    }
  2900. }
  2901.  
  2902. /* Do nothing plot!!! */
  2903. void _fastcall noplot(int x,int y,int color)
  2904. {
  2905. }
  2906.