home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fractint / fras1611.zip / FRACSUBR.C < prev    next >
C/C++ Source or Header  |  1991-07-15  |  32KB  |  992 lines

  1. /*
  2. FRACSUBR.C contains subroutines which belong primarily to CALCFRAC.C and
  3. FRACTALS.C, i.e. which are non-fractal-specific fractal engine subroutines.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdarg.h>
  8. #include <float.h>
  9. #include "fractint.h"
  10. #include "fractype.h"
  11. #include "mpmath.h"
  12.  
  13. /* routines in this module    */
  14.  
  15. void calcfracinit(void);
  16. void adjust_corner(void);
  17. int  alloc_resume(int,int);
  18. int  put_resume(int,...);
  19. int  start_resume(void);
  20. int  get_resume(int,...);
  21. void end_resume(void);
  22. void iplot_orbit(long,long,int);
  23. void plot_orbit(double,double,int);
  24. void scrub_orbit(void);
  25. int  add_worklist(int,int,int,int,int,int,int);
  26. void tidy_worklist(void);
  27. int  ssg_blocksize(void);
  28. void get_julia_attractor(double,double);
  29.  
  30. static long   _fastcall fudgetolong(double d);
  31. static double _fastcall fudgetodouble(long l);
  32. static void   _fastcall adjust_to_limits(double);
  33. static void   _fastcall smallest_add(double *);
  34. static int    _fastcall ratio_bad(double,double);
  35. static void   _fastcall plotdorbit(double,double,int);
  36. static int    _fastcall combine_worklist(void);
  37.  
  38.  
  39. extern int    calc_status;        /* status of calculations */
  40. extern char far *resume_info;        /* pointer to resume info if allocated */
  41.        int    resume_len;        /* length of resume info */
  42. static int    resume_offset;        /* offset in resume info gets */
  43. extern double plotmx1,plotmx2,plotmy1,plotmy2; /* real->screen conversion */
  44. extern int    orbit_ptr;        /* pointer into save_orbit array */
  45. extern int far *save_orbit;        /* array to save orbit values */
  46. extern int    orbit_color;        /* XOR color */
  47. extern int    num_worklist;        /* resume worklist for standard engine */
  48. extern int    fractype;         /* fractal type */
  49. extern char   stdcalcmode;        /* '1', '2', 'g', 'b'       */
  50. extern char   floatflag;        /* floating-point fractals? */
  51. extern int    integerfractal;        /* TRUE if fractal uses integer math */
  52. extern struct workliststuff worklist[MAXCALCWORK];
  53. extern int    sxdots,sydots;        /* # of dots on the physical screen    */
  54. extern int    sxoffs,syoffs;        /* physical top left of logical screen */
  55. extern int    xdots, ydots;        /* # of dots on the logical screen     */
  56. extern int    colors;            /* maximum colors available */
  57. extern long   fudge;            /* fudge factor (2**n) */
  58. extern int    bitshift;         /* bit shift for fudge */
  59. extern double xxmin,xxmax,yymin,yymax,xx3rd,yy3rd; /* corners */
  60. extern long   xmin, xmax, ymin, ymax, x3rd, y3rd;  /* integer equivs */
  61. extern int    maxit;            /* try this many iterations */
  62. extern int    attractors;        /* number of finite attractors    */
  63. extern struct complex  attr[];        /* finite attractor vals (f.p)    */
  64. extern struct lcomplex lattr[];     /* finite attractor vals (int)    */
  65. extern struct complex  old,new;
  66. extern struct lcomplex lold,lnew;
  67. extern double tempsqrx,tempsqry;
  68. extern long   ltempsqrx,ltempsqry;
  69. extern int    xxstart,xxstop;        /* these are same as worklist, */
  70. extern int    yystart,yystop,yybegin;    /* declared as separate items  */
  71. extern int    periodicitycheck;
  72. extern int    basin;
  73. extern int    finattract;
  74. extern int    pixelpi;            /* value of pi in pixels */
  75. extern double closenuff;
  76. extern long   lclosenuff;
  77. extern int    ixstart, ixstop, iystart, iystop;
  78. extern int    color;
  79. extern int    decomp[];
  80. extern double potparam[3];        /* three potential parameters*/
  81. extern int    distest;            /* non-zero if distance estimator */
  82. extern double param[4];         /* up to four parameters */
  83. extern int    invert;            /* non-zero if inversion active */
  84. extern int    biomorph,usr_biomorph;
  85. extern int    debugflag; /* internal use only - you didn't see this */
  86. extern long   creal, cimag;        /* for calcmand */
  87. extern long   delx, dely;        /* screen pixel increments  */
  88. extern long   delx2, dely2;        /* screen pixel increments  */
  89. extern double delxx, delyy;        /* screen pixel increments  */
  90. extern double delxx2, delyy2;        /* screen pixel increments  */
  91. extern long   delmin;            /* for calcfrac/calcmand    */
  92. extern double ddelmin;            /* same as a double        */
  93. extern int    potflag;            /* continuous potential flag */
  94. extern int    bailout;
  95. extern double rqlim;
  96. extern double  dxsize, dysize;        /* xdots-1, ydots-1        */
  97.  
  98. extern long   far *lx0, far *ly0;    /* x, y grid            */
  99. extern long   far *lx1, far *ly1;    /* adjustment for rotate    */
  100. /* note that lx1 & ly1 values can overflow into sign bit; since     */
  101. /* they're used only to add to lx0/ly0, 2s comp straightens it out  */
  102. extern double far *dx0, far *dy0;    /* floating pt equivs */
  103. extern double far *dx1, far *dy1;
  104.  
  105. extern char   usr_floatflag;
  106. extern char   usr_stdcalcmode;
  107. extern int    usr_periodicitycheck;
  108. extern int    usr_distest;
  109.  
  110. extern int StandardFractal(void);
  111. extern int calcmand(void);
  112.  
  113. #define FUDGEFACTOR    29    /* fudge all values up by 2**this */
  114. #define FUDGEFACTOR2    24    /* (or maybe this)          */
  115.  
  116.  
  117. void calcfracinit() /* initialize a *pile* of stuff for fractal calculation */
  118. {
  119.    int i;
  120.    double ftemp;
  121.  
  122.    floatflag        = usr_floatflag;
  123.  
  124. init_restart:
  125.  
  126.    /* the following variables may be forced to a different setting due to
  127.       calc routine constraints;  usr_xxx is what the user last said is wanted,
  128.       xxx is what we actually do in the current situation */
  129.    stdcalcmode        = usr_stdcalcmode;
  130.    periodicitycheck = usr_periodicitycheck;
  131.    distest        = usr_distest;
  132.    biomorph        = usr_biomorph;
  133.  
  134.    potflag = 0;
  135.    if (potparam[0] != 0.0
  136.      && colors >= 256
  137.      && (curfractalspecific->calctype == StandardFractal
  138.      || curfractalspecific->calctype == calcmand)) {
  139.       potflag = 1;
  140.       distest = 0;    /* can't do distest too */
  141.       }
  142.  
  143.    if (distest)
  144.       floatflag = 1;  /* force floating point for dist est */
  145.  
  146.    if (floatflag) { /* ensure type matches floatflag */
  147.       if (curfractalspecific->isinteger != 0
  148.     && curfractalspecific->tofloat != NOFRACTAL)
  149.      fractype = curfractalspecific->tofloat;
  150.       }
  151.    else {
  152.       if (curfractalspecific->isinteger == 0
  153.     && curfractalspecific->tofloat != NOFRACTAL)
  154.      fractype = curfractalspecific->tofloat;
  155.       }
  156.    curfractalspecific = &fractalspecific[fractype];
  157.  
  158.    integerfractal = curfractalspecific->isinteger;
  159.  
  160.    if (fractype == JULIBROT)
  161.       rqlim = 4;
  162.    else if (potflag && potparam[2] != 0.0)
  163.       rqlim = potparam[2];
  164. /* else if (decomp[0] > 0 && decomp[1] > 0)
  165.       rqlim = (double)decomp[1]; */
  166.    else if (bailout) /* user input bailout */
  167.       rqlim = bailout;
  168.    else if (biomorph != -1) /* biomorph benefits from larger bailout */
  169.       rqlim = 100;
  170.    else
  171.       rqlim = curfractalspecific->orbit_bailout;
  172.    if (integerfractal) /* the bailout limit mustn't be too high here */
  173.       if (rqlim > 127.0) rqlim = 127.0;
  174.  
  175.    if (curfractalspecific->flags&NOROTATE != 0) {
  176.       /* ensure min<max and unrotated rectangle */
  177.       if (xxmin > xxmax) { ftemp = xxmax; xxmax = xxmin; xxmin = ftemp; }
  178.       if (yymin > yymax) { ftemp = yymax; yymax = yymin; yymin = ftemp; }
  179.       xx3rd = xxmin; yy3rd = yymin;
  180.       }
  181.  
  182.    /* set up bitshift for integer math */
  183.    bitshift = FUDGEFACTOR2; /* by default, the smaller shift */
  184.    if (integerfractal > 1)  /* use specific override from table */
  185.       bitshift = integerfractal;
  186.    if (integerfractal == 0) /* float? */
  187.       if ((i = curfractalspecific->tofloat) != NOFRACTAL) /* -> int? */
  188.      if (fractalspecific[i].isinteger > 1) /* specific shift? */
  189.         bitshift = fractalspecific[i].isinteger;
  190.  
  191.    if (fractype == MANDEL || fractype == JULIA) { /* adust shift bits if.. */
  192.       if (potflag == 0                  /* not using potential */
  193.     && (fractype != MANDEL              /* and not an int mandel */
  194.         || (param[0] == 0.0 && param[1] == 0.0))  /* w/ "fudged" params */
  195.     && !invert                  /* and not inverting */
  196.     && biomorph == -1              /* and not biomorphing */
  197.     && rqlim <= 4.0               /* and bailout not too high */
  198.     && debugflag != 1234)              /* and not debugging */
  199.      bitshift = FUDGEFACTOR;          /* use the larger bitshift */
  200.       if (param[0] < -1.99 || param[0] > 1.99) param[0] = 1.99;
  201.       if (param[1] < -1.99 || param[1] > 1.99) param[1] = 1.99;
  202.       }
  203.  
  204.    fudge = 1L << bitshift;
  205.  
  206.    /* now setup arrays of real coordinates corresponding to each pixel */
  207.  
  208.    adjust_to_limits(1.0); /* make sure all corners in valid range */
  209.  
  210.    delxx  = (xxmax - xx3rd) / dxsize; /* calculate stepsizes */
  211.    delyy  = (yymax - yy3rd) / dysize;
  212.    delxx2 = (xx3rd - xxmin) / dysize;
  213.    delyy2 = (yy3rd - yymin) / dxsize;
  214.  
  215.    creal = fudgetolong(param[0]); /* integer equivs for it all */
  216.    cimag = fudgetolong(param[1]);
  217.    xmin  = fudgetolong(xxmin);
  218.    xmax  = fudgetolong(xxmax);
  219.    x3rd  = fudgetolong(xx3rd);
  220.    ymin  = fudgetolong(yymin);
  221.    ymax  = fudgetolong(yymax);
  222.    y3rd  = fudgetolong(yy3rd);
  223.    delx  = fudgetolong(delxx);
  224.    dely  = fudgetolong(delyy);
  225.    delx2 = fudgetolong(delxx2);
  226.    dely2 = fudgetolong(delyy2);
  227.  
  228.    if (fractype != PLASMA) { /* skip this if plasma to avoid 3d problems */
  229.       if (integerfractal && !invert) {
  230.      if (    (delx  == 0 && delxx  != 0.0)
  231.          || (delx2 == 0 && delxx2 != 0.0)
  232.          || (dely  == 0 && delyy  != 0.0)
  233.          || (dely2 == 0 && delyy2 != 0.0) )
  234.         goto expand_retry;
  235.      lx0[0] = xmin;         /* fill up the x, y grids */
  236.      ly0[0] = ymax;
  237.      lx1[0] = ly1[0] = 0;
  238.      for (i = 1; i < xdots; i++ ) {
  239.         lx0[i] = lx0[i-1] + delx;
  240.         ly1[i] = ly1[i-1] - dely2;
  241.         }
  242.      for (i = 1; i < ydots; i++ ) {
  243.         ly0[i] = ly0[i-1] - dely;
  244.         lx1[i] = lx1[i-1] + delx2;
  245.         }
  246.      /* past max res?  check corners within 10% of expected */
  247.      if (    ratio_bad((double)lx0[xdots-1]-xmin,(double)xmax-x3rd)
  248.          || ratio_bad((double)ly0[ydots-1]-ymax,(double)y3rd-ymax)
  249.          || ratio_bad((double)lx1[(ydots>>1)-1],((double)x3rd-xmin)/2)
  250.          || ratio_bad((double)ly1[(xdots>>1)-1],((double)ymin-y3rd)/2) ) {
  251. expand_retry:
  252.         if (integerfractal        /* integer fractal type? */
  253.           && curfractalspecific->tofloat != NOFRACTAL)
  254.            floatflag = 1;        /* switch to floating pt */
  255.         else
  256.            adjust_to_limits(2.0);    /* double the size */
  257.         if (calc_status == 2)    /* due to restore of an old file? */
  258.            calc_status = 0;     /*   whatever, it isn't resumable */
  259.         goto init_restart;
  260.         }
  261.      /* re-set corners to match reality */
  262.      xmax = lx0[xdots-1] + lx1[ydots-1];
  263.      ymin = ly0[ydots-1] + ly1[xdots-1];
  264.      x3rd = xmin + lx1[ydots-1];
  265.      y3rd = ly0[ydots-1];
  266.      xxmin = fudgetodouble(xmin);
  267.      xxmax = fudgetodouble(xmax);
  268.      xx3rd = fudgetodouble(x3rd);
  269.      yymin = fudgetodouble(ymin);
  270.      yymax = fudgetodouble(ymax);
  271.      yy3rd = fudgetodouble(y3rd);
  272.      }
  273.       else {
  274.      /* set up dx0 and dy0 analogs of lx0 and ly0 */
  275.      /* put fractal parameters in doubles */
  276.      dx0[0] = xxmin;        /* fill up the x, y grids */
  277.      dy0[0] = yymax;
  278.      dx1[0] = dy1[0] = 0;
  279.      for (i = 1; i < xdots; i++ ) {
  280.         dx0[i] = dx0[i-1] + delxx;
  281.         dy1[i] = dy1[i-1] - delyy2;
  282.         }
  283.      for (i = 1; i < ydots; i++ ) {
  284.         dy0[i] = dy0[i-1] - delyy;
  285.         dx1[i] = dx1[i-1] + delxx2;
  286.         }
  287.      if (    ratio_bad(dx0[xdots-1]-xxmin,xxmax-xx3rd)
  288.          || ratio_bad(dy0[ydots-1]-yymax,yy3rd-yymax)
  289.          || ratio_bad(dx1[ydots-1],xx3rd-xxmin)
  290.          || ratio_bad(dy1[xdots-1],yymin-yy3rd))
  291.         goto expand_retry;
  292.      /* re-set corners to match reality */
  293.      xxmax = dx0[xdots-1] + dx1[ydots-1];
  294.      yymin = dy0[ydots-1] + dy1[xdots-1];
  295.      xx3rd = xxmin + dx1[ydots-1];
  296.      yy3rd = dy0[ydots-1];
  297.      }
  298.       }
  299.  
  300.    /* for periodicity close-enough, and for unity: */
  301.    /*      min(max(delx,delx2),max(dely,dely2)       */
  302.    ddelmin = fabs(delxx);
  303.    if (fabs(delxx2) > ddelmin)
  304.       ddelmin = fabs(delxx2);
  305.    if (fabs(delyy) > fabs(delyy2)) {
  306.       if (fabs(delyy) < ddelmin)
  307.      ddelmin = fabs(delyy);
  308.       }
  309.    else
  310.       if (fabs(delyy2) < ddelmin)
  311.      ddelmin = fabs(delyy2);
  312.    delmin = fudgetolong(ddelmin);
  313.  
  314.    /* calculate factors which plot real values to screen co-ords */
  315.    /* calcfrac.c plot_orbit routines have comments about this     */
  316.    ftemp = (0.0-delyy2) * delxx2 * dxsize * dysize
  317.        - (xxmax-xx3rd) * (yy3rd-yymax);
  318.    plotmx1 = delxx2 * dxsize * dysize / ftemp;
  319.    plotmx2 = (yy3rd-yymax) * dxsize / ftemp;
  320.    plotmy1 = (0.0-delyy2) * dxsize * dysize / ftemp;
  321.    plotmy2 = (xxmax-xx3rd) * dysize / ftemp;
  322.  
  323. }
  324.  
  325. static long _fastcall fudgetolong(double d)
  326. {
  327.    if ((d *= fudge) > 0) d += 0.5;
  328.    else          d -= 0.5;
  329.    return (long)d;
  330. }
  331.  
  332. static double _fastcall fudgetodouble(long l)
  333. {
  334.    char buf[30];
  335.    double d;
  336.    sprintf(buf,"%.9g",(double)l / fudge);
  337.    sscanf(buf,"%lg",&d);
  338.    return d;
  339. }
  340.  
  341.  
  342. void adjust_corner()
  343. {
  344.    /* make edges very near vert/horiz exact, to ditch rounding errs and */
  345.    /* to avoid problems when delta per axis makes too large a ratio    */
  346.    double ftemp,ftemp2;
  347.    if( (ftemp=fabs(xx3rd-xxmin)) < (ftemp2=fabs(xxmax-xx3rd)) ) {
  348.       if (ftemp*10000 < ftemp2 && yy3rd != yymax)
  349.      xx3rd = xxmin;
  350.       }
  351.    else if (ftemp2*10000 < ftemp && yy3rd != yymin)
  352.       xx3rd = xxmax;
  353.    if( (ftemp=fabs(yy3rd-yymin)) < (ftemp2=fabs(yymax-yy3rd)) ) {
  354.       if (ftemp*10000 < ftemp2 && xx3rd != xxmax)
  355.      yy3rd = yymin;
  356.       }
  357.    else if (ftemp2*10000 < ftemp && xx3rd != xxmin)
  358.       yy3rd = yymax;
  359. }
  360.  
  361. static void _fastcall adjust_to_limits(double expand)
  362. {  double cornerx[4],cornery[4];
  363.    double lowx,highx,lowy,highy,limit,ftemp;
  364.    double centerx,centery,adjx,adjy;
  365.    int i;
  366.    limit = 32767.99;
  367.    if (bitshift >= 24) limit = 31.99;
  368.    if (bitshift >= 29) limit = 3.99;
  369.    centerx = (xxmin+xxmax)/2;
  370.    centery = (yymin+yymax)/2;
  371.    if (xxmin == centerx) { /* ohoh, infinitely thin, fix it */
  372.       smallest_add(&xxmax);
  373.       xxmin -= xxmax-centerx;
  374.       }
  375.    if (yymin == centery) {
  376.       smallest_add(&yymax);
  377.       yymin -= yymax-centery;
  378.       }
  379.    if (xx3rd == centerx)
  380.       smallest_add(&xx3rd);
  381.    if (yy3rd == centery)
  382.       smallest_add(&yy3rd);
  383.    /* setup array for easier manipulation */
  384.    cornerx[0] = xxmin; cornerx[1] = xxmax;
  385.    cornerx[2] = xx3rd; cornerx[3] = xxmin+(xxmax-xx3rd);
  386.    cornery[0] = yymax; cornery[1] = yymin;
  387.    cornery[2] = yy3rd; cornery[3] = yymin+(yymax-yy3rd);
  388.    /* if caller wants image size adjusted, do that first */
  389.    if (expand != 1.0)
  390.       for (i=0; i<4; ++i) {
  391.      cornerx[i] = centerx + (cornerx[i]-centerx)*expand;
  392.      cornery[i] = centery + (cornery[i]-centery)*expand;
  393.       }
  394.    /* get min/max x/y values */
  395.    lowx = highx = cornerx[0];
  396.    lowy = highy = cornery[0];
  397.    for (i=1; i<4; ++i) {
  398.       if (cornerx[i] < lowx)  lowx  = cornerx[i];
  399.       if (cornerx[i] > highx) highx = cornerx[i];
  400.       if (cornery[i] < lowy)  lowy  = cornery[i];
  401.       if (cornery[i] > highy) highy = cornery[i];
  402.       }
  403.    /* if image is too large, downsize it maintaining center */
  404.    ftemp = highx-lowx;
  405.    if (highy-lowy > ftemp) ftemp = highy-lowy;
  406.    if ((ftemp = limit*2/ftemp) < 1.0)
  407.       for (i=0; i<4; ++i) {
  408.      cornerx[i] = centerx + (cornerx[i]-centerx)*ftemp;
  409.      cornery[i] = centery + (cornery[i]-centery)*ftemp;
  410.      }
  411.    /* if any corner has x or y past limit, move the image */
  412.    adjx = adjy = 0;
  413.    for (i=0; i<4; ++i) {
  414.       if (cornerx[i] > limit     && (ftemp = cornerx[i] - limit) > adjx)
  415.      adjx = ftemp;
  416.       if (cornerx[i] < 0.0-limit && (ftemp = cornerx[i] + limit) < adjx)
  417.      adjx = ftemp;
  418.       if (cornery[i] > limit     && (ftemp = cornery[i] - limit) > adjy)
  419.      adjy = ftemp;
  420.       if (cornery[i] < 0.0-limit && (ftemp = cornery[i] + limit) < adjy)
  421.      adjy = ftemp;
  422.       }
  423.    if (calc_status == 2 && (adjx != 0 || adjy != 0))
  424.       calc_status = 0;
  425.    xxmin = cornerx[0] - adjx;
  426.    xxmax = cornerx[1] - adjx;
  427.    xx3rd = cornerx[2] - adjx;
  428.    yymax = cornery[0] - adjy;
  429.    yymin = cornery[1] - adjy;
  430.    yy3rd = cornery[2] - adjy;
  431.    adjust_corner(); /* make 3rd corner exact if very near other co-ords */
  432. }
  433.  
  434. static void _fastcall smallest_add(double *num)
  435. {
  436.    *num += *num * 5.0e-16;
  437. }
  438.  
  439. static int _fastcall ratio_bad(double actual, double desired)
  440. {  double ftemp;
  441.    if (desired != 0)
  442.       if ((ftemp = actual / desired) < 0.95 || ftemp > 1.05)
  443.      return(1);
  444.    return(0);
  445. }
  446.  
  447.  
  448. /* Save/resume stuff:
  449.  
  450.    Engines which aren't resumable can simply ignore all this.
  451.  
  452.    Before calling the (per_image,calctype) routines (engine), calcfract sets:
  453.       "resuming" to 0 if new image, nonzero if resuming a partially done image
  454.       "calc_status" to 1
  455.    If an engine is interrupted and wants to be able to resume it must:
  456.       store whatever status info it needs to be able to resume later
  457.       set calc_status to 2 and return
  458.    If subsequently called with resuming!=0, the engine must restore status
  459.    info and continue from where it left off.
  460.  
  461.    Since the info required for resume can get rather large for some types,
  462.    it is not stored directly in save_info.  Instead, memory is dynamically
  463.    allocated as required, and stored in .fra files as a separate block.
  464.    To save info for later resume, an engine routine can use:
  465.       alloc_resume(maxsize,version)
  466.      Maxsize must be >= max bytes subsequently saved + 2; over-allocation
  467.      is harmless except for possibility of insufficient mem available;
  468.      undersize is not checked and probably causes serious misbehaviour.
  469.      Version is an arbitrary number so that subsequent revisions of the
  470.      engine can be made backward compatible.
  471.      Alloc_resume sets calc_status to 2 (resumable) if it succeeds; to 3
  472.      if it cannot allocate memory (and issues warning to user).
  473.       put_resume({bytes,&argument,} ... 0)
  474.      Can be called as often as required to store the info.
  475.      Arguments must not be far addresses.
  476.      Is not protected against calls which use more space than allocated.
  477.    To reload info when resuming, use:
  478.       start_resume()
  479.      initializes and returns version number
  480.       get_resume({bytes,&argument,} ... 0)
  481.      inverse of store_resume
  482.       end_resume()
  483.      optional, frees the memory area sooner than would happen otherwise
  484.  
  485.    Example, save info:
  486.       alloc_resume(sizeof(parmarray)+100,2);
  487.       put_resume(sizeof(int),&row, sizeof(int),&col,
  488.          sizeof(parmarray),parmarray, 0);
  489.     restore info:
  490.       vsn = start_resume();
  491.       get_resume(sizeof(int),&row, sizeof(int),&col, 0);
  492.       if (vsn >= 2)
  493.      get_resume(sizeof(parmarray),parmarray,0);
  494.       end_resume();
  495.  
  496.    Engines which allocate a large far memory chunk of their own might
  497.    directly set resume_info, resume_len, calc_status to avoid doubling
  498.    transient memory needs by using these routines.
  499.  
  500.    StandardFractal, calcmand, solidguess, and bound_trace_main are a related
  501.    set of engines for escape-time fractals.  They use a common worklist
  502.    structure for save/resume.  Fractals using these must specify calcmand
  503.    or StandardFractal as the engine in fractalspecificinfo.
  504.    Other engines don't get btm nor ssg, don't get off-axis symmetry nor
  505.    panning (the worklist stuff), and are on their own for save/resume.
  506.  
  507.    */
  508.  
  509. int put_resume(int len, ...)
  510. {
  511.    va_list arg_marker;    /* variable arg list */
  512.    char *source_ptr;
  513.    if (resume_info == NULL)
  514.       return(-1);
  515.    va_start(arg_marker,len);
  516.    while (len)
  517.    {
  518.       source_ptr = va_arg(arg_marker,char *);
  519.       far_memcpy(resume_info+resume_len,source_ptr,len);
  520.       resume_len += len;
  521.       len = va_arg(arg_marker,int);
  522.    }
  523.    return(0);
  524. }
  525.  
  526. int alloc_resume(int alloclen, int version)
  527. {
  528.    if (resume_info != NULL) /* free the prior area if there is one */
  529.       farmemfree(resume_info);
  530.    if ((resume_info = farmemalloc((long)alloclen))== NULL)
  531.    {
  532.       static char msg[] = {"\
  533. Warning - insufficient free memory to save status.\n\
  534. You will not be able to resume calculating this image."};
  535.       stopmsg(0,msg);
  536.       calc_status = 3;
  537.       return(-1);
  538.    }
  539.    resume_len = 0;
  540.    put_resume(sizeof(int),&version,0);
  541.    calc_status = 2;
  542.    return(0);
  543. }
  544.  
  545. int get_resume(int len, ...)
  546. {
  547.    va_list arg_marker;    /* variable arg list */
  548.    char far *source_ptr;
  549.    char *dest_ptr;
  550.    if (resume_info == NULL)
  551.       return(-1);
  552.    va_start(arg_marker,len);
  553.    while (len)
  554.    {
  555.       dest_ptr = va_arg(arg_marker,char *);
  556.       far_memcpy(dest_ptr,resume_info+resume_offset,len);
  557.       resume_offset += len;
  558.       len = va_arg(arg_marker,int);
  559.    }
  560.    return(0);
  561. }
  562.  
  563. int start_resume()
  564. {
  565.    int version;
  566.    if (resume_info == NULL)
  567.       return(-1);
  568.    resume_offset = 0;
  569.    get_resume(sizeof(int),&version,0);
  570.    return(version);
  571. }
  572.  
  573. void end_resume()
  574. {
  575.    if (resume_info != NULL) /* free the prior area if there is one */
  576.    {
  577.       farmemfree(resume_info);
  578.       resume_info = NULL;
  579.    }
  580. }
  581.  
  582.  
  583. /* Showing orbit requires converting real co-ords to screen co-ords.
  584.    Define:
  585.        Xs == xxmax-xx3rd           Ys == yy3rd-yymax
  586.        W  == xdots-1               D  == ydots-1
  587.    We know that:
  588.        realx == lx0[col] + lx1[row]
  589.        realy == ly0[row] + ly1[col]
  590.        lx0[col] == (col/width) * Xs + xxmin
  591.        lx1[row] == row * delxx
  592.        ly0[row] == (row/D) * Ys + yymax
  593.        ly1[col] == col * (0-delyy)
  594.   so:
  595.        realx == (col/W) * Xs + xxmin + row * delxx
  596.        realy == (row/D) * Ys + yymax + col * (0-delyy)
  597.   and therefore:
  598.        row == (realx-xxmin - (col/W)*Xs) / Xv     (1)
  599.        col == (realy-yymax - (row/D)*Ys) / Yv     (2)
  600.   substitute (2) into (1) and solve for row:
  601.        row == ((realx-xxmin)*(0-delyy2)*W*D - (realy-yymax)*Xs*D)
  602.               / ((0-delyy2)*W*delxx2*D-Ys*Xs)
  603.   */
  604.  
  605. static void _fastcall plotdorbit(double dx, double dy, int color)
  606. {
  607.    int i, j, c;
  608.    int save_sxoffs,save_syoffs;
  609.    if (orbit_ptr >= 1500) return;
  610.    i = dy * plotmx1 - dx * plotmx2; i += sxoffs;
  611.    if (i < 0 || i >= sxdots) return;
  612.    j = dx * plotmy1 - dy * plotmy2; j += syoffs;
  613.    if (j < 0 || j >= sydots) return;
  614.    save_sxoffs = sxoffs;
  615.    save_syoffs = syoffs;
  616.    sxoffs = syoffs = 0;
  617.    /* save orbit value */
  618.    if(color == -1)
  619.    {
  620.       *(save_orbit + orbit_ptr++) = i;
  621.       *(save_orbit + orbit_ptr++) = j;
  622.       *(save_orbit + orbit_ptr++) = c = getcolor(i,j);
  623.       putcolor(i,j,c^orbit_color);
  624.    }
  625.    else
  626.       putcolor(i,j,color);
  627.    sxoffs = save_sxoffs;
  628.    syoffs = save_syoffs;
  629. }
  630.  
  631. void iplot_orbit(ix, iy, color)
  632. long ix, iy;
  633. int color;
  634. {
  635.    plotdorbit((double)ix/fudge-xxmin,(double)iy/fudge-yymax,color);
  636. }
  637.  
  638. void plot_orbit(real,imag,color)
  639. double real,imag;
  640. int color;
  641. {
  642.    plotdorbit(real-xxmin,imag-yymax,color);
  643. }
  644.  
  645. void scrub_orbit()
  646. {
  647.    int i,j,c;
  648.    int save_sxoffs,save_syoffs;
  649.    save_sxoffs = sxoffs;
  650.    save_syoffs = syoffs;
  651.    sxoffs = syoffs = 0;
  652.    while(orbit_ptr > 0)
  653.    {
  654.       c = *(save_orbit + --orbit_ptr);
  655.       j = *(save_orbit + --orbit_ptr);
  656.       i = *(save_orbit + --orbit_ptr);
  657.       putcolor(i,j,c);
  658.    }
  659.    sxoffs = save_sxoffs;
  660.    syoffs = save_syoffs;
  661. }
  662.  
  663.  
  664. int add_worklist(int xfrom, int xto, int yfrom, int yto, int ybegin,
  665. int pass, int sym)
  666. {
  667.    int i;
  668.    if (num_worklist >= MAXCALCWORK)
  669.       return(-1);
  670.    worklist[num_worklist].xxstart = xfrom;
  671.    worklist[num_worklist].xxstop  = xto;
  672.    worklist[num_worklist].yystart = yfrom;
  673.    worklist[num_worklist].yystop  = yto;
  674.    worklist[num_worklist].yybegin = ybegin;
  675.    worklist[num_worklist].pass      = pass;
  676.    worklist[num_worklist].sym      = sym;
  677.    ++num_worklist;
  678.    tidy_worklist();
  679.    return(0);
  680. }
  681.  
  682. static int _fastcall combine_worklist() /* look for 2 entries which can freely merge */
  683. {
  684.    int i,j;
  685.    for (i=0; i<num_worklist; ++i)
  686.       if (worklist[i].yystart == worklist[i].yybegin)
  687.      for (j=i+1; j<num_worklist; ++j)
  688.         if (worklist[j].sym == worklist[i].sym
  689.         && worklist[j].yystart == worklist[j].yybegin
  690.         && worklist[i].pass == worklist[j].pass)
  691.         {
  692.            if ( worklist[i].xxstart == worklist[j].xxstart
  693.            && worklist[i].xxstop  == worklist[j].xxstop)
  694.            {
  695.           if (worklist[i].yystop+1 == worklist[j].yystart)
  696.           {
  697.              worklist[i].yystop = worklist[j].yystop;
  698.              return(j);
  699.           }
  700.           if (worklist[j].yystop+1 == worklist[i].yystart)
  701.           {
  702.              worklist[i].yystart = worklist[j].yystart;
  703.              worklist[i].yybegin = worklist[j].yybegin;
  704.              return(j);
  705.           }
  706.            }
  707.            if ( worklist[i].yystart == worklist[j].yystart
  708.            && worklist[i].yystop  == worklist[j].yystop)
  709.            {
  710.           if (worklist[i].xxstop+1 == worklist[j].xxstart)
  711.           {
  712.              worklist[i].xxstop = worklist[j].xxstop;
  713.              return(j);
  714.           }
  715.           if (worklist[j].xxstop+1 == worklist[i].xxstart)
  716.           {
  717.              worklist[i].xxstart = worklist[j].xxstart;
  718.              return(j);
  719.           }
  720.            }
  721.         }
  722.    return(0); /* nothing combined */
  723. }
  724.  
  725. void tidy_worklist() /* combine mergeable entries, resort */
  726. {
  727.    int i,j;
  728.    struct workliststuff tempwork;
  729.    while (i=combine_worklist())
  730.    { /* merged two, delete the gone one */
  731.       while (++i < num_worklist)
  732.      worklist[i-1] = worklist[i];
  733.       --num_worklist;
  734.    }
  735.    for (i=0; i<num_worklist; ++i)
  736.       for (j=i+1; j<num_worklist; ++j)
  737.      if (worklist[j].pass < worklist[i].pass
  738.          || (worklist[j].pass == worklist[i].pass
  739.          && (worklist[j].yystart < worklist[i].yystart
  740.          || (   worklist[j].yystart == worklist[i].yystart
  741.          && worklist[j].xxstart <  worklist[i].xxstart))))
  742.      { /* dumb sort, swap 2 entries to correct order */
  743.         tempwork = worklist[i];
  744.         worklist[i] = worklist[j];
  745.         worklist[j] = tempwork;
  746.      }
  747. }
  748.  
  749.  
  750. void get_julia_attractor (double real, double imag)
  751. {
  752.    struct lcomplex lresult;
  753.    struct complex result;
  754.    int savper,savmaxit;
  755.  
  756.    if (attractors == 0 && finattract == 0) /* not magnet & not requested */
  757.       return;
  758.  
  759.    if (attractors >= N_ATTR)     /* space for more attractors ?  */
  760.       return;               /* Bad luck - no room left !    */
  761.  
  762.    savper = periodicitycheck;
  763.    savmaxit = maxit;
  764.    periodicitycheck = 0;
  765.    old.x = real;            /* prepare for f.p orbit calc */
  766.    old.y = imag;
  767.    tempsqrx = sqr(old.x);
  768.    tempsqry = sqr(old.y);
  769.  
  770.    lold.x = real;        /* prepare for int orbit calc */
  771.    lold.y = imag;
  772.    ltempsqrx = tempsqrx;
  773.    ltempsqry = tempsqry;
  774.  
  775.    lold.x = lold.x << bitshift;
  776.    lold.y = lold.y << bitshift;
  777.    ltempsqrx = ltempsqrx << bitshift;
  778.    ltempsqry = ltempsqry << bitshift;
  779.  
  780.    if (maxit < 500)        /* we're going to try at least this hard */
  781.       maxit = 500;
  782.    color = 0;
  783.    while (++color < maxit)
  784.       if(curfractalspecific->orbitcalc())
  785.      break;
  786.    if (color >= maxit)        /* if orbit stays in the lake */
  787.    {
  788.       if (integerfractal)   /* remember where it went to */
  789.      lresult = lnew;
  790.       else
  791.      result =  new;
  792.       if(!curfractalspecific->orbitcalc()) /* if it stays in the lake */
  793.       {                /* and doen't move far, probably */
  794.      if (integerfractal)   /*   found a finite attractor    */
  795.      {
  796.         if(labs(lresult.x-lnew.x) < lclosenuff
  797.         && labs(lresult.y-lnew.y) < lclosenuff)
  798.         {
  799.            lattr[attractors] = lnew;
  800.            attractors++;   /* another attractor - coloured lakes ! */
  801.         }
  802.      }
  803.      else
  804.      {
  805.         if(fabs(result.x-new.x) < closenuff
  806.         && fabs(result.y-new.y) < closenuff)
  807.         {
  808.            attr[attractors] = new;
  809.            attractors++;   /* another attractor - coloured lakes ! */
  810.         }
  811.      }
  812.       }
  813.    }
  814.    if(attractors==0)
  815.       periodicitycheck = savper;
  816.    maxit = savmaxit;
  817. }
  818.  
  819.  
  820. #define maxyblk 7    /* must match calcfrac.c */
  821. #define maxxblk 202  /* must match calcfrac.c */
  822. int ssg_blocksize() /* used by solidguessing and by zoom panning */
  823. {
  824.    int blocksize,i;
  825.    /* blocksize 4 if <300 rows, 8 if 300-599, 16 if 600-1199, 32 if >=1200 */
  826.    blocksize=4;
  827.    i=300;
  828.    while(i<=ydots)
  829.    {
  830.       blocksize+=blocksize;
  831.       i+=i;
  832.    }
  833.    /* increase blocksize if prefix array not big enough */
  834.    while(blocksize*(maxxblk-2)<xdots || blocksize*(maxyblk-2)*16<ydots)
  835.       blocksize+=blocksize;
  836.    return(blocksize);
  837. }
  838.  
  839.  
  840. /* Symmetry plot for period PI */
  841. void _fastcall symPIplot(x, y, color)
  842. int x, y, color ;
  843. {
  844.    while(x <= xxstop)
  845.    {
  846.       putcolor(x, y, color) ;
  847.       x += pixelpi;
  848.    }
  849. }
  850. /* Symmetry plot for period PI plus Origin Symmetry */
  851. void _fastcall symPIplot2J(x, y, color)
  852. int x, y, color ;
  853. {
  854.    int i,j;
  855.    while(x <= xxstop)
  856.    {
  857.       putcolor(x, y, color) ;
  858.       if ((i=yystop-(y-yystart)) > iystop && i < ydots
  859.       && (j=xxstop-(x-xxstart)) < xdots)
  860.      putcolor(j, i, color) ;
  861.       x += pixelpi;
  862.    }
  863. }
  864. /* Symmetry plot for period PI plus Both Axis Symmetry */
  865. void _fastcall symPIplot4J(x, y, color)
  866. int x, y, color ;
  867. {
  868.    int i,j;
  869.    while(x <= (xxstart+xxstop)/2)
  870.    {
  871.       j = xxstop-(x-xxstart);
  872.       putcolor(       x , y , color) ;
  873.       if (j < xdots)
  874.      putcolor(j , y , color) ;
  875.       if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  876.       {
  877.      putcolor(x , i , color) ;
  878.      if (j < xdots)
  879.         putcolor(j , i , color) ;
  880.       }
  881.       x += pixelpi;
  882.    }
  883. }
  884.  
  885. /* Symmetry plot for X Axis Symmetry */
  886. void _fastcall symplot2(x, y, color)
  887. int x, y, color ;
  888. {
  889.    int i;
  890.    putcolor(x, y, color) ;
  891.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  892.       putcolor(x, i, color) ;
  893. }
  894.  
  895. /* Symmetry plot for Y Axis Symmetry */
  896. void _fastcall symplot2Y(x, y, color)
  897. int x, y, color ;
  898. {
  899.    int i;
  900.    putcolor(x, y, color) ;
  901.    if ((i=xxstop-(x-xxstart)) < xdots)
  902.       putcolor(i, y, color) ;
  903. }
  904.  
  905. /* Symmetry plot for Origin Symmetry */
  906. void _fastcall symplot2J(x, y, color)
  907. int x, y, color ;
  908. {
  909.    int i,j;
  910.    putcolor(x, y, color) ;
  911.    if ((i=yystop-(y-yystart)) > iystop && i < ydots
  912.        && (j=xxstop-(x-xxstart)) < xdots)
  913.       putcolor(j, i, color) ;
  914. }
  915.  
  916. /* Symmetry plot for Both Axis Symmetry */
  917. void _fastcall symplot4(x, y, color)
  918. int x, y, color ;
  919. {
  920.    int i,j;
  921.    j = xxstop-(x-xxstart);
  922.    putcolor(       x , y, color) ;
  923.    if (j < xdots)
  924.       putcolor(    j , y, color) ;
  925.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  926.    {
  927.       putcolor(    x , i, color) ;
  928.       if (j < xdots)
  929.      putcolor(j , i, color) ;
  930.    }
  931. }
  932.  
  933. /* Symmetry plot for X Axis Symmetry - Striped Newtbasin version */
  934. void _fastcall symplot2basin(x, y, color)
  935. int x, y, color ;
  936. {
  937.    int i,stripe;
  938.    extern int degree;
  939.    putcolor(x, y, color) ;
  940.    if(basin==2 && color > 8)
  941.       stripe=8;
  942.    else
  943.       stripe = 0;
  944.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  945.    {
  946.       color -= stripe;              /* reconstruct unstriped color */
  947.       color = (degree+1-color)%degree+1;  /* symmetrical color */
  948.       color += stripe;              /* add stripe */
  949.       putcolor(x, i,color)  ;
  950.    }
  951. }
  952.  
  953. /* Symmetry plot for Both Axis Symmetry  - Newtbasin version */
  954. void _fastcall symplot4basin(x, y, color)
  955. int x, y, color ;
  956. {
  957.    extern int degree;
  958.    int i,j,color1,stripe;
  959.    if(color == 0) /* assumed to be "inside" color */
  960.    {
  961.       symplot4(x, y, color);
  962.       return;
  963.    }
  964.    if(basin==2 && color > 8)
  965.       stripe = 8;
  966.    else
  967.       stripe = 0;
  968.    color -= stripe;          /* reconstruct unstriped color */
  969.    color1 = degree/2+degree+2 - color;
  970.    if(color < degree/2+2)
  971.       color1 = degree/2+2 - color;
  972.    else
  973.       color1 = degree/2+degree+2 - color;
  974.    j = xxstop-(x-xxstart);
  975.    putcolor(       x, y, color+stripe) ;
  976.    if (j < xdots)
  977.       putcolor(    j, y, color1+stripe) ;
  978.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  979.    {
  980.       putcolor(    x, i, stripe + (degree+1 - color)%degree+1) ;
  981.       if (j < xdots)
  982.      putcolor(j, i, stripe + (degree+1 - color1)%degree+1) ;
  983.    }
  984. }
  985.  
  986. /* Do nothing plot!!! */
  987. void _fastcall noplot(int x,int y,int color)
  988. {
  989. }
  990.  
  991.  
  992.