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