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