home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / winsrc.zip / ENCODER.C < prev    next >
Text File  |  1990-12-16  |  24KB  |  736 lines

  1. /*
  2.     encoder.c - GIF Encoder and associated routines
  3.     This module is linked as an overlay, use ENTER_OVLY and EXIT_OVLY.
  4. */
  5.  
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "fractint.h"
  10. #include "fractype.h"
  11.  
  12. /* routines in this module    */
  13.  
  14. void encoder_overlay(void);
  15. int  savetodisk(char *);
  16. int  encoder();
  17.  
  18. static void setup_save_info(struct fractal_info *);
  19. static void inittable(void);
  20. static void shftwrite();
  21. static void raster(unsigned int);
  22. static int  extend_blk_len(int datalen);
  23. static void put_extend_blk(int block_id,int block_len,char far *block_data);
  24.  
  25. extern int initbatch;
  26. extern char far *resume_info;        /* pointer to resume info if allocated */
  27. extern int  resume_len;         /* length of resume info */
  28. extern char LName[];
  29.  
  30. extern char FormName[40];        /* formula name */
  31. extern int  active_system;        /* 0=dos, 1=windows */
  32.  
  33. extern    int    sxdots,sydots;        /* # of dots on the physical screen    */
  34. extern    int    sxoffs,syoffs;        /* physical top left of logical screen */
  35. extern    int    xdots, ydots;        /* # of dots on the logical screen     */
  36. extern    int    viewwindow;        /* 0 for full screen, 1 for window */
  37. extern    float    finalaspectratio;    /* for view shape and rotation */
  38. extern    int    viewxdots,viewydots;    /* explicit view sizing */
  39. extern    int    colors;         /* maximum colors available */
  40. extern    int    dotmode;        /* so we can detect disk-video */
  41. extern    char overwrite;         /* overwrite on/off */
  42. extern    int    resave_flag;        /* resaving after a timed save */
  43. extern    int    timedsave;        /* if doing an auto save */
  44. extern    int    disk16bit;        /* 16 bit continuous potential */
  45.  
  46. extern unsigned char dacbox[256][3];    /* Video-DAC (filled in by SETVIDEO) */
  47. extern int    daclearn, daccount;    /* used by the color-cyclers */
  48. extern int    reallyega;        /* "reall-an-EGA" flag */
  49. extern int    extraseg;        /* used by Save-to-GIF routines */
  50. extern int    debugflag;
  51.  
  52. extern int    gif87a_flag;        /* if 1, supress GIF extension blocks */
  53.  
  54. extern int    calc_status;
  55. extern long   calctime;
  56. extern char   stdcalcmode;
  57. extern int    fractype;
  58. extern double xxmin,xxmax;
  59. extern double yymin,yymax;
  60. extern double xx3rd,yy3rd;
  61. extern double param[4];
  62. extern int    maxit;            /* try this many iterations */
  63. extern int    inside;            /* inside color: 1=blue     */
  64. extern int    outside;            /* outside color, if set    */
  65. extern int    finattract;        /* finite attractor option  */
  66. extern int    forcesymmetry;
  67. extern int    LogFlag;            /* non-zero if logarithmic palettes */
  68. extern int    rflag, rseed;
  69. extern int    periodicitycheck;
  70. extern char   useinitorbit;
  71. extern struct complex initorbit;
  72. extern int    pot16bit;
  73. extern float  finalaspectratio;
  74. extern double potparam[3];        /* three potential parameters*/
  75. extern double inversion[];
  76. extern int    decomp[];
  77. extern int    distest;            /* non-zero if distance estimator   */
  78. extern int    init3d[20];        /* '3d=nn/nn/nn/...' values */
  79. extern char   floatflag;        /* floating-point fractals? */
  80. extern int    biomorph;
  81. extern int    bailout;            /* user input bailout value */
  82. extern int    previewfactor;
  83. extern int    xtrans;
  84. extern int    ytrans;
  85. extern int    red_crop_left;
  86. extern int    red_crop_right;
  87. extern int    blue_crop_left;
  88. extern int    blue_crop_right;
  89. extern int    red_bright;
  90. extern int    blue_bright;
  91. extern int    xadjust;
  92. extern int    eyeseparation;
  93. extern int    glassestype;
  94. extern int    save_system;
  95. extern int    save_release;
  96. extern int    display3d;        /* 3D display flag: 0 = OFF */
  97. extern int    Ambient;
  98. extern int    RANDOMIZE;
  99. extern int    haze;
  100. extern int    transparent[2];
  101.  
  102. extern int    timer(int timertype,int(*subrtn)(),...);
  103.  
  104. /*
  105.             Save-To-Disk Routines (GIF)
  106.  
  107. GIF and 'Graphics Interchange Format' are trademarks (tm) of Compuserve
  108. Incorporated, an H&R Block Company.
  109.  
  110.  
  111. The following routines perform the GIF encoding when the 's' key is pressed.
  112. The routines refer to several variables that are declared elsewhere
  113. [colors, xdots, ydots, and 'dacbox'], and rely on external routines to
  114. actually read and write screen pixels [getcolor(x,y) and putcolor(x,y,color)].
  115. (Writing pixels is just stuffed in here as a sort of visual status report,
  116. and has nothing to do with any GIF function.)    They also rely on the
  117. existence of an externally-defined 64K dataspace and they use the routines
  118. 'toextra()' and 'cmpextra()' to deal with that dataspace (in the same manner
  119. as 'memcpy()' and 'memcmp()' would).   Otherwise, they perform a generic
  120. GIF-encoder function.
  121.  
  122. Note that these routines use small string- and hash-tables, and "flush"
  123. the GIF entries whenever the hash-table gets two-thirds full or the string
  124. table gets full.   They also use the GIF encoding technique of limiting the
  125. encoded string length to a specific size, "adding" a string to the hash table
  126. at that point even if a matching string exists ("adding" is in quotes, because
  127. if a matching string exists we can increment the code counter but safely throw
  128. the duplicate string away, saving both string space and a hash table entry).
  129.  
  130.    This results in relatively good speed and small data space, but at the
  131. expense of compression efficiency (filesize).    These trade-offs could be
  132. adjusted by modifying the #DEFINEd variables below.
  133.  
  134. Note that the 'strlocn' and 'teststring' routines are declared
  135. to be external just so that they can be defined (and the space re-used)
  136. elsewhere.  The actual declarations are in the assembler code.
  137.  
  138. */
  139.  
  140. #define MAXTEST   100        /* maximum single string length */
  141. #define MAXSTRING 64000     /* total space reserved for strings */
  142.                 /* maximum number of strings available */
  143. #define MAXENTRY  5003        /* (a prime number is best for hashing) */
  144.  
  145. extern unsigned int strlocn[MAXENTRY];
  146. extern unsigned char teststring[MAXTEST];
  147. extern unsigned char block[266];   /* GIF-encoded blocks go here */
  148.  
  149. static int numsaves = 0;    /* For adjusting 'save-to-disk' filenames */
  150.  
  151. static FILE *out;
  152. static int last_colorbar;
  153. static int save16bit;
  154. static int outcolor1s, outcolor2s;
  155.  
  156. static int lentest, lastentry, numentries, numrealentries;
  157. static unsigned int nextentry;
  158. static int clearcode, endcode;
  159. static unsigned int hashcode;
  160.  
  161. static unsigned char blockcount;
  162. static int startbits, codebits, bytecount, bitcount;
  163.  
  164. static char paletteBW[] = {            /* B&W palette */
  165.       0,  0,  0, 63, 63, 63,
  166.     };
  167. static char paletteCGA[] = {            /* 4-color (CGA) palette  */
  168.       0,  0,  0, 21, 63, 63, 63, 21, 63, 63, 63, 63,
  169.     };
  170. static char paletteEGA[] = {            /* 16-color (EGA/CGA) pal */
  171.       0,  0,  0,  0,  0, 42,  0, 42,  0,  0, 42, 42,
  172.      42,  0,  0, 42,  0, 42, 42, 21,  0, 42, 42, 42,
  173.      21, 21, 21, 21, 21, 63, 21, 63, 21, 21, 63, 63,
  174.      63, 21, 21, 63, 21, 63, 63, 63, 21, 63, 63, 63,
  175.     };
  176. int paletteVGA[] = {            /* VGA palette - to DAC registers */
  177.     0, 1, 2, 3, 4, 5,20, 7,56,57,58,59,60,61,62,63,
  178.     };
  179.  
  180. void encoder_overlay() { }    /* for restore_active_ovly */
  181.  
  182. int savetodisk(filename)    /* save-to-disk routine */
  183. char *filename;
  184. {
  185. char tmpmsg[41]; /* before openfile in case of overrun */
  186. char openfile[80], openfiletype[10];
  187. char tmpfile[80];
  188. int newfile;
  189. int i, j, outcolor1, outcolor2, interrupted;
  190.  
  191. ENTER_OVLY(OVLY_ENCODER);
  192.  
  193. if (extraseg == 0) {            /* not enough memory for this */
  194.     buzzer(2);
  195.     EXIT_OVLY;
  196.     return -1;
  197.     }
  198. restart:
  199.  
  200. save16bit = disk16bit;
  201. if (gif87a_flag) /* not storing non-standard fractal info */
  202.     save16bit = 0;
  203.  
  204. strcpy(openfile,filename);        /* decode and open the filename */
  205. strcpy(openfiletype,DEFAULTFRACTALTYPE);/* determine the file extension */
  206. if (save16bit)
  207.     strcpy(openfiletype,".pot");
  208. for (i = 0; i < strlen(openfile); i++)
  209.     if (openfile[i] == '.') {
  210.         strcpy(openfiletype,&openfile[i]);
  211.         openfile[i] = 0;
  212.         }
  213. if (resave_flag == 0)
  214.     updatesavename(filename); /* for next time */
  215.  
  216. strcat(openfile,openfiletype);
  217.  
  218. strcpy(tmpfile,openfile);
  219. if (access(openfile,0) != 0) /* file doesn't exist */
  220.     newfile = 1;
  221. else { /* file already exists */
  222.     if ((overwrite==0) && resave_flag == 0)
  223.         goto restart;
  224.     if (access(openfile,2) != 0) {
  225.         buzzer(2);
  226.         sprintf(tmpmsg," ?? Can't write %s ",openfile);
  227.         texttempmsg(tmpmsg);
  228.         EXIT_OVLY;
  229.         return -1;
  230.         }
  231.     newfile = 0;
  232.     i = strlen(tmpfile);
  233.     while (--i >= 0 && tmpfile[i] != '\\')
  234.         tmpfile[i] = 0;
  235.     strcat(tmpfile,"fractint.tmp");
  236.     }
  237. if ((out=fopen(tmpfile,"wb")) == NULL) {
  238.     buzzer(2);
  239.     sprintf(tmpmsg," ?? Couldn't create %s ",tmpfile);
  240.     texttempmsg(tmpmsg);
  241.     EXIT_OVLY;
  242.     return -1;
  243.     }
  244.  
  245. if (dotmode == 11) {            /* disk-video */
  246.     char buf[60];
  247.     sprintf(buf,"Saving %s",openfile);
  248.     dvid_status(1,buf);
  249.     }
  250.  
  251. if (debugflag != 200)
  252.     interrupted = encoder();
  253. else
  254.     interrupted = timer(2,NULL);    /* invoke encoder() via timer */
  255.  
  256. fclose(out);
  257.  
  258. if (interrupted) {
  259.     char buf[200];
  260.     sprintf(buf,"Save of %s interrupted.\nCancel to ",openfile);
  261.     if (newfile)
  262.         strcat(buf,"delete the file,\ncontinue to keep the partial image.");
  263.     else
  264.         strcat(buf,"retain the original file,\ncontinue to replace original with new partial image.");
  265.     interrupted = 1;
  266.     if (stopmsg(2,buf) < 0) {
  267.         interrupted = -1;
  268.         unlink(tmpfile);
  269.         }
  270.     }
  271.  
  272. if (newfile == 0 && interrupted >= 0) { /* replace the real file */
  273.     if (unlink(openfile) != 0) {
  274.         buzzer(2);
  275.         sprintf(tmpmsg," ?? Can't replace %s ",openfile);
  276.         texttempmsg(tmpmsg);
  277.         EXIT_OVLY;
  278.         return -1;
  279.         }
  280.     if (rename(tmpfile,openfile) != 0) {
  281.         buzzer(2);
  282.         sprintf(tmpmsg," ?? Can't rename %s ",tmpfile);
  283.         texttempmsg(tmpmsg);
  284.         EXIT_OVLY;
  285.         return -1;
  286.         }
  287.     }
  288.  
  289. if (dotmode != 11) {            /* supress this on disk-video */
  290.     if (active_system == 0) {        /* no bars in Windows version */
  291.     outcolor1 = outcolor1s;
  292.     outcolor2 = outcolor2s;
  293.     for (j = 0; j <= last_colorbar; j++) {
  294.         if ((j & 4) == 0) {
  295.             if (++outcolor1 >= colors) outcolor1 = 0;
  296.             if (++outcolor2 >= colors) outcolor2 = 0;
  297.             }
  298.         for (i = 0; 250*i < xdots; i++) { /* clear vert status bars */
  299.             putcolor(i,j,getcolor(i,j)^outcolor1);
  300.             putcolor(xdots-1-i,j,getcolor(xdots-1-i,j)^outcolor2);
  301.             }
  302.         }
  303.     }
  304.     }
  305. else                    /* disk-video */
  306.     dvid_status(1,"");
  307. if (interrupted) {
  308.     texttempmsg(" *interrupted* save ");
  309.     EXIT_OVLY;
  310.     return -1;
  311.     }
  312. if (timedsave == 0) {
  313.     buzzer(0);
  314.     if (initbatch == 0) {
  315.         sprintf(tmpmsg," File saved as %s ",openfile);
  316.         texttempmsg(tmpmsg);
  317.         }
  318.     }
  319. EXIT_OVLY;
  320. return 0;
  321. }
  322.  
  323.  
  324. int encoder()
  325. {
  326. int i, j, ydot, xdot, color, outcolor1, outcolor2;
  327. int width;
  328. int rownum, rowlimit;
  329. unsigned int hashentry;
  330. unsigned char bitsperpixel, x;
  331. int entrynum;
  332. struct fractal_info save_info;
  333.  
  334. setup_save_info(&save_info);
  335.  
  336. bitsperpixel = 0;            /* calculate bits / pixel */
  337. for (i = colors; i >= 2; i /= 2 )
  338.     bitsperpixel++;
  339.  
  340. startbits = bitsperpixel+1;        /* start coding with this many bits */
  341. if (colors == 2)
  342.     startbits++;            /* B&W Klooge */
  343.  
  344. clearcode = 1 << (startbits - 1);    /* set clear and end codes */
  345. endcode = clearcode+1;
  346.  
  347. outcolor1 = 0;                /* use these colors to show progress */
  348. outcolor2 = 1;                /* (this has nothing to do with GIF) */
  349. if (colors > 2) {
  350.     outcolor1 = 2;
  351.     outcolor2 = 3;
  352.     }
  353. if (((++numsaves) & 1) == 0) {            /* reverse the colors on alt saves */
  354.     i = outcolor1;
  355.     outcolor1 = outcolor2;
  356.     outcolor2 = i;
  357.     }
  358. outcolor1s = outcolor1;
  359. outcolor2s = outcolor2;
  360.  
  361. if (gif87a_flag == 1)
  362.     fwrite("GIF87a",1,6,out);              /* old GIF Signature */
  363. else
  364.     fwrite("GIF89a",1,6,out);              /* new GIF Signature */
  365.  
  366. width = xdots;
  367. rowlimit = ydots;
  368. if (save16bit) {
  369.     /* pot16bit info is stored as:
  370.        file:    double width rows, right side of row is low 8 bits
  371.        diskvid: ydots rows of colors followed by ydots rows of low 8 bits
  372.        decoder: returns (row of color info then row of low 8 bits) * ydots
  373.        */
  374.     rowlimit <<= 1;
  375.     width <<= 1;
  376.     }
  377. fwrite(&width,2,1,out);         /* screen descriptor */
  378. fwrite(&ydots,2,1,out);
  379. x = 128 + ((6-1)<<4) + (bitsperpixel-1); /* color resolution == 6 bits worth */
  380. fwrite(&x,1,1,out);
  381. fputc(0,out);                /* background color */
  382. i = 0;
  383. /** PB, changed to always store pixel aspect ratio, some utilities
  384.     have been reported to like it **/
  385. /**
  386. if ( finalaspectratio < SCREENASPECT-0.01
  387.   || finalaspectratio > SCREENASPECT+0.01) {
  388.  **/
  389. if (viewwindow                    /* less than full screen?  */
  390.   && (viewxdots == 0 || viewydots == 0))    /* and we picked the dots? */
  391.    i = ((double)sydots / (double)sxdots) * 64.0/SCREENASPECT - 14.5;
  392. else /* must risk loss of precision if numbers low */
  393.    i = (((double)ydots / (double)xdots) / finalaspectratio) * 64 - 14.5;
  394. if (i < 1)   i = 1;
  395. if (i > 255) i = 255;
  396. if (gif87a_flag) i = 0;    /* for some decoders which can't handle aspect */
  397. fputc(i,out);                /* pixel aspect ratio */
  398.  
  399. if (colors == 256) {            /* write out the 256-color palette */
  400.     if (dacbox[0][0] != 255)    /* got a DAC - must be a VGA */
  401.         shftwrite(dacbox,colors);
  402.     else                /* uh oh - better fake it */
  403.         for (i = 0; i < 256; i += 16)
  404.             shftwrite(paletteEGA,16);
  405.     }
  406. if (colors == 2)            /* write out the B&W palette */
  407.     shftwrite(paletteBW,colors);
  408. if (colors == 4)            /* write out the CGA palette */
  409.     shftwrite(paletteCGA,colors);
  410. if (colors == 16)            /* Either EGA or VGA */
  411.     if (dacbox[0][0] != 255) {    /* got a  DAC - must be a VGA */
  412.         if (reallyega)        /* well, maybe really an EGA */
  413.             shftwrite(dacbox,colors);
  414.         else
  415.             for (i = 0; i < colors; i++)
  416.                 shftwrite(dacbox[paletteVGA[i]],1);
  417.         }
  418.         else            /* no DAC - must be an EGA */
  419.             shftwrite(paletteEGA,colors);
  420.  
  421. fwrite(",",1,1,out);                    /* Image Descriptor */
  422. i = 0;
  423. fwrite(&i,2,1,out);
  424. fwrite(&i,2,1,out);
  425. fwrite(&width,2,1,out);
  426. fwrite(&ydots,2,1,out);
  427. fwrite(&i,1,1,out);
  428.  
  429. bitsperpixel = startbits - 1;        /* raster data starts here */
  430. fwrite(&bitsperpixel,1,1,out);
  431.  
  432. codebits = startbits;            /* start encoding */
  433.  
  434. raster(9999);                /* initialize the raster routine */
  435.  
  436. inittable();                /* initialize the LZW tables */
  437.  
  438. for (rownum = 0; rownum < ydots; rownum++) {  /* scan through the dots */
  439.     for (ydot = rownum; ydot < rowlimit; ydot += ydots) {
  440.     for (xdot = 0; xdot < xdots; xdot++) {
  441.         if (save16bit == 0 || ydot < ydots)
  442.             color = getcolor(xdot,ydot);
  443.         else
  444.             color = readdisk(xdot+sxoffs,ydot+syoffs);
  445.         teststring[0] = ++lentest;
  446.         teststring[lentest] = color;
  447.         if (lentest == 1) {        /* root entry? */
  448.             lastentry = color;
  449.             continue;
  450.             }
  451.         if (lentest == 2)        /* init   the hash code */
  452.             hashcode = 301 * (teststring[1]+1);
  453.         hashcode *= (color + lentest);    /* update the hash code */
  454.         hashentry = ++hashcode % MAXENTRY;
  455.         for( i = 0; i < MAXENTRY; i++) {
  456.             if (++hashentry >= MAXENTRY) hashentry = 0;
  457.             if (cmpextra(strlocn[hashentry]+2,
  458.                 teststring,lentest+1) == 0)
  459.                     break;
  460.             if (strlocn[hashentry] == 0) i = MAXENTRY;
  461.             }
  462.         /* found an entry and string length isn't too bad */
  463.         if (strlocn[hashentry] != 0 && lentest < MAXTEST-3) {
  464.             fromextra(strlocn[hashentry],&entrynum,2);
  465.             lastentry = entrynum;
  466.             continue;
  467.             }
  468.         raster(lastentry);            /* write entry */
  469.         numentries++;        /* act like you added one, anyway */
  470.         if (strlocn[hashentry] == 0) {    /* add new string, if any */
  471.             entrynum = numentries+endcode;
  472.             strlocn[hashentry] = nextentry;
  473.             toextra(nextentry, &entrynum,2);
  474.             toextra(nextentry+2,
  475.                 teststring,lentest+1);
  476.             nextentry += lentest+3;
  477.             numrealentries++;
  478.             }
  479.         teststring[0] = 1;        /* reset current entry */
  480.         teststring[1] = color;
  481.         lentest = 1;
  482.         lastentry = color;
  483.  
  484.         if ((numentries+endcode) == (1<<codebits))
  485.             codebits++;         /* use longer encoding */
  486.  
  487.         if ( numentries + endcode > 4093 ||    /* out of room? */
  488.             numrealentries > (MAXENTRY*2)/3 ||
  489.             nextentry > MAXSTRING-MAXTEST-5) {
  490.             raster(lastentry);        /* flush & restart */
  491.             inittable();
  492.             }
  493.         }
  494.     if (dotmode != 11            /* supress this on disk-video */
  495.         && active_system == 0        /* and in Windows version     */
  496.         && ydot == rownum) {
  497.         if ((ydot & 4) == 0) {
  498.             if (++outcolor1 >= colors) outcolor1 = 0;
  499.             if (++outcolor2 >= colors) outcolor2 = 0;
  500.             }
  501.         for (i = 0; 250*i < xdots; i++) {    /* display vert status bars */
  502.                             /*   (this is NOT GIF-related)    */
  503.             /* PB Changed following code to xor color, so that
  504.                image can be restored at end and resumed
  505.                putcolor(      i,ydot,outcolor1);
  506.                putcolor(xdots-1-i,ydot,outcolor2);
  507.             */
  508.             putcolor(i,ydot,getcolor(i,ydot)^outcolor1);
  509.             putcolor(xdots-1-i,ydot,getcolor(xdots-1-i,ydot)^outcolor2);
  510.             }
  511.         last_colorbar = ydot;
  512.         }
  513.     if (kbhit())                /* keyboard hit - bail out */
  514.         ydot = rownum = 9999;
  515.     }
  516.     }
  517.  
  518. raster(lastentry);            /* tidy up - dump the last code */
  519.  
  520. raster(endcode);            /* finish the map */
  521.  
  522. fputc(0,out);                /* raster data ends here */
  523.  
  524. if (gif87a_flag == 0) { /* store non-standard fractal info */
  525.     /* loadfile.c has notes about extension block structure */
  526.     if (ydot >= 9999)
  527.         save_info.calc_status = 0; /* partial save is not resumable */
  528.     save_info.tot_extend_len = 0;
  529.     if (resume_info != NULL && save_info.calc_status == 2) {
  530.         /* resume info block, 002 */
  531.         save_info.tot_extend_len += extend_blk_len(resume_len);
  532.         put_extend_blk(2,resume_len,resume_info);
  533.         }
  534.     if (save_info.fractal_type == FORMULA || save_info.fractal_type == FFORMULA) {
  535.         /* formula info block, 003 */
  536.         save_info.tot_extend_len += extend_blk_len(40);
  537.         put_extend_blk(3,40,FormName);
  538.         }
  539.     if (save_info.fractal_type == LSYSTEM) {
  540.         /* formula info block, 003 */
  541.         save_info.tot_extend_len += extend_blk_len(40);
  542.         put_extend_blk(3,40,LName);
  543.         }
  544.  
  545.     /* main and last block, 001 */
  546.     save_info.tot_extend_len += extend_blk_len(sizeof(save_info));
  547.     put_extend_blk(1,sizeof(save_info),(char far *)&save_info);
  548.     }
  549.  
  550. fwrite(";",1,1,out);                    /* GIF Terminator */
  551. return ((ydot < 9999) ? 0 : 1);
  552. }
  553.  
  554. static void shftwrite(color,numcolors)    /* shift IBM colors to GIF format */
  555. unsigned char color[];
  556. int numcolors;
  557. {
  558. unsigned char thiscolor;
  559. int i,j;
  560. for (i = 0; i < numcolors; i++)
  561.     for (j = 0; j < 3; j++) {
  562.         thiscolor = color[3*i+j];
  563.         thiscolor = thiscolor << 2;
  564.         thiscolor += (thiscolor >> 6);
  565.         /*    fwrite(&thiscolor,1,1,out); */
  566.         fputc(thiscolor,out);
  567.         }
  568. }
  569.  
  570. static void inittable()         /* routine to init tables */
  571. {
  572. int i;
  573.  
  574. raster(clearcode);            /* signal that table is initialized */
  575.  
  576. numentries = 0;             /* initialize the table */
  577. numrealentries = 0;
  578. nextentry = 1;
  579. lentest = 0;
  580. codebits = startbits;
  581.  
  582. toextra(0,"\0",1);                      /* clear the hash entries */
  583. for (i = 0; i < MAXENTRY; i++)
  584.     strlocn[i] = 0;
  585.  
  586. }
  587.  
  588. static void raster(code)        /* routine to block and output codes */
  589. unsigned int code;
  590. {
  591. unsigned int icode, i, j;
  592.  
  593. if (code == 9999) {            /* special start-up signal */
  594.     bytecount = 0;
  595.     bitcount = 0;
  596.     for (i = 0; i < 266; i++)
  597.         block[i] = 0;
  598.     return;
  599.     }
  600.  
  601. icode = code << bitcount;        /* update the bit string */
  602. block[bytecount  ] |= (icode & 255);
  603. block[bytecount+1] |= ((icode>>8) & 255);
  604. icode = (code>>8) << bitcount;
  605. block[bytecount+2] |= ((icode>>8) & 255);
  606. bitcount += codebits;
  607. while (bitcount >= 8) {         /* locate next starting point */
  608.     bitcount -= 8;
  609.     bytecount++;
  610.     }
  611.  
  612. if (bytecount > 250 || code == endcode) {    /* time to write a block */
  613.     if (code == endcode)
  614.         while (bitcount > 0) {        /* if EOF, find the real end */
  615.             bitcount -= 8;
  616.             bytecount++;
  617.             }
  618.     i = bytecount;
  619.     blockcount = i;
  620.     fwrite(&blockcount,1,1,out);        /* write the block */
  621.     fwrite(block,i,1,out);
  622.     bytecount = 0;                /* now re-start the block */
  623.     for (j = 0; j < 5; j++)         /* (may have leftover bits) */
  624.         block[j] = block[j+i];
  625.     for (j = 5; j < 266; j++)
  626.         block[j] = 0;
  627.     }
  628. }
  629.  
  630.  
  631. static int extend_blk_len(int datalen)
  632. {
  633.    return(datalen + (datalen+254)/255 + 15);
  634.    /*       data   +    1.per.block   + 14 for id + 1 for null at end  */
  635. }
  636.  
  637. static void put_extend_blk(int block_id,int block_len,char far *block_data)
  638. {
  639.    int i,j;
  640.    char header[15];
  641.    strcpy(header,"!\377\013fractint");
  642.    sprintf(&header[11],"%03u",block_id);
  643.    fwrite(header,14,1,out);
  644.    i = (block_len + 254) / 255;
  645.    while (--i >= 0) {
  646.       block_len -= (j = min(block_len,255));
  647.       fputc(j,out);
  648.       while (--j >= 0)
  649.      fputc(*(block_data++),out);
  650.       }
  651.    fputc(0,out);
  652. }
  653.  
  654. static void setup_save_info(struct fractal_info *save_info)
  655. {
  656.    int i;
  657.    /* set save parameters in save structure */
  658.    strcpy(save_info->info_id, INFO_ID);
  659.    save_info->version          = 5; /* file version, independant of system */
  660.    save_info->iterations      = maxit;
  661.    save_info->fractal_type    = fractype;
  662.    save_info->xmin          = xxmin;
  663.    save_info->xmax          = xxmax;
  664.    save_info->ymin          = yymin;
  665.    save_info->ymax          = yymax;
  666.    save_info->creal          = param[0];
  667.    save_info->cimag          = param[1];
  668.    save_info->videomodeax     = videoentry.videomodeax;
  669.    save_info->videomodebx     = videoentry.videomodebx;
  670.    save_info->videomodecx     = videoentry.videomodecx;
  671.    save_info->videomodedx     = videoentry.videomodedx;
  672.    save_info->dotmode          = videoentry.dotmode % 100;
  673.    save_info->xdots          = videoentry.xdots;
  674.    save_info->ydots          = videoentry.ydots;
  675.    save_info->colors          = videoentry.colors;
  676.    save_info->parm3          = param[2];
  677.    save_info->parm4          = param[3];
  678.    save_info->potential[0]    = potparam[0];
  679.    save_info->potential[1]    = potparam[1];
  680.    save_info->potential[2]    = potparam[2];
  681.    save_info->rflag          = rflag;
  682.    save_info->rseed          = rseed;
  683.    save_info->inside          = inside;
  684.    save_info->logmap          = LogFlag;
  685.    save_info->invert[0]       = inversion[0];
  686.    save_info->invert[1]       = inversion[1];
  687.    save_info->invert[2]       = inversion[2];
  688.    save_info->decomp[0]       = decomp[0];
  689.    save_info->biomorph          = biomorph;
  690.    save_info->symmetry          = forcesymmetry;
  691.    for (i = 0; i < 16; i++)
  692.       save_info->init3d[i] = init3d[i];
  693.    save_info->previewfactor   = previewfactor;
  694.    save_info->xtrans          = xtrans;
  695.    save_info->ytrans          = ytrans;
  696.    save_info->red_crop_left   = red_crop_left;
  697.    save_info->red_crop_right  = red_crop_right;
  698.    save_info->blue_crop_left  = blue_crop_left;
  699.    save_info->blue_crop_right = blue_crop_right;
  700.    save_info->red_bright      = red_bright;
  701.    save_info->blue_bright     = blue_bright;
  702.    save_info->xadjust          = xadjust;
  703.    save_info->eyeseparation   = eyeseparation;
  704.    save_info->glassestype     = glassestype;
  705.    save_info->outside          = outside;
  706.    save_info->x3rd          = xx3rd;
  707.    save_info->y3rd          = yy3rd;
  708.    save_info->calc_status     = calc_status;
  709.    save_info->stdcalcmode     = stdcalcmode;
  710.    save_info->distest          = distest;
  711.    save_info->floatflag       = floatflag;
  712.    save_info->bailout          = bailout;
  713.    save_info->calctime          = calctime;
  714.    save_info->trigndx[0]      = trigndx[0];
  715.    save_info->trigndx[1]      = trigndx[1];
  716.    save_info->trigndx[2]      = trigndx[2];
  717.    save_info->trigndx[3]      = trigndx[3];
  718.    save_info->finattract      = finattract;
  719.    save_info->initorbit[0]    = initorbit.x;
  720.    save_info->initorbit[1]    = initorbit.y;
  721.    save_info->useinitorbit    = useinitorbit;
  722.    save_info->periodicity     = periodicitycheck;
  723.    save_info->pot16bit          = disk16bit;
  724.    save_info->faspectratio    = finalaspectratio;
  725.    save_info->system          = save_system;
  726.    save_info->release          = save_release;
  727.    save_info->flag3d          = display3d;
  728.    save_info->ambient          = Ambient;
  729.    save_info->randomize       = RANDOMIZE;
  730.    save_info->haze          = haze;
  731.    save_info->transparent[0]  = transparent[0];
  732.    save_info->transparent[1]  = transparent[1];
  733.    for (i = 0; i < sizeof(save_info->future)/sizeof(int); i++)
  734.       save_info->future[i] = 0;
  735. }
  736.