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