home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / winsrc.zip / LOADFILE.C < prev    next >
Text File  |  1990-11-22  |  18KB  |  605 lines

  1. /*
  2.     loadfile.c - load an existing fractal image, control level
  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. #include "targa_lc.h"
  12.  
  13. /* routines in this module    */
  14.  
  15. void loadfile_overlay(void);
  16. int  read_overlay(void);
  17.  
  18. static int  find_fractal_info(char *,struct fractal_info *);
  19. static void load_ext_blk(char far *loadptr,int loadlen);
  20. static void skip_ext_blk(int *,int *);
  21. static void backwardscompat();
  22.  
  23. int filetype;
  24. int loaded3d;
  25.  
  26. extern int    showfile;         /* has file been displayed yet? */
  27. extern char   readname[];        /* name of fractal input file */
  28. extern char   far *resume_info;     /* pointer to resume info if alloc'd */
  29. extern int    resume_len;        /* length of resume info */
  30. extern int    adapter;
  31. extern int    calc_status;
  32. extern int    initmode;         /* initial video mode        */
  33. extern long   calctime;
  34. extern int    initbatch;        /* 1 if batch run (no kbd)  */
  35. extern int    maxit;
  36. extern int    initincr;         /* maxiter incrmnt        */
  37. extern char   usr_stdcalcmode;        /* pass mode            */
  38. extern int    fractype;         /* fractal type         */
  39. extern double xxmin,xxmax;        /* corner values        */
  40. extern double yymin,yymax;        /* corner values        */
  41. extern double xx3rd,yy3rd;        /* corner values        */
  42. extern double param[4];         /* parameters            */
  43. extern int    inside;            /* inside color: 1=blue     */
  44. extern int    outside;            /* outside color, if set    */
  45. extern int    finattract;        /* finite attractor option  */
  46. extern int    forcesymmetry;
  47. extern int    LogFlag;            /* non-zero if logarithmic palettes */
  48. extern int    rflag, rseed;
  49. extern int    usr_periodicitycheck;
  50. extern char   useinitorbit;
  51. extern struct complex initorbit;
  52. extern int    potflag;            /* continuous potential flag */
  53. extern int    pot16bit;
  54. extern double potparam[3];        /* three potential parameters*/
  55. extern double inversion[];
  56. extern int    decomp[];
  57. extern int    usr_distest;        /* non-zero if distance estimator   */
  58. extern int    init3d[20];        /* '3d=nn/nn/nn/...' values */
  59. extern char   usr_floatflag;        /* floating-point fractals? */
  60. extern int    biomorph;
  61. extern char   FormName[];
  62. extern char   LName[];
  63. extern int    bailout;            /* user input bailout value */
  64. extern int    previewfactor;
  65. extern int    xtrans;
  66. extern int    ytrans;
  67. extern int    red_crop_left;
  68. extern int    red_crop_right;
  69. extern int    blue_crop_left;
  70. extern int    blue_crop_right;
  71. extern int    red_bright;
  72. extern int    blue_bright;
  73. extern int    xadjust;
  74. extern int    eyeseparation;
  75. extern int    glassestype;
  76. extern int    display3d;        /* 3D display flag: 0 = OFF */
  77. extern int    overlay3d;        /* 3D overlay flag: 0 = OFF */
  78. extern int    viewwindow;        /* 0 for full screen, 1 for window */
  79. extern float  viewreduction;        /* window auto-sizing */
  80. extern float  finalaspectratio;     /* for view shape and rotation */
  81. extern int    xdots, ydots;        /* # of dots on the logical screen */
  82. extern int    viewxdots,viewydots;    /* explicit view sizing */
  83. extern int    save_system,save_release;
  84. extern int    Ambient;
  85. extern int    RANDOMIZE;
  86. extern int    haze;
  87. extern int    transparent[2];
  88.  
  89. static FILE *fp;
  90.  
  91. int fileydots, filexdots, filecolors;
  92. float fileaspectratio;
  93.  
  94. int skipxdots,skipydots;    /* for decoder, when reducing image */
  95.  
  96.  
  97. void loadfile_overlay() { }    /* for restore_active_ovly */
  98.  
  99.  
  100. int read_overlay()    /* read overlay/3D files, if reqr'd */
  101. {
  102.    struct fractal_info read_info;
  103.    char oldfloatflag;
  104.    char msg[110];
  105.  
  106.    ENTER_OVLY(OVLY_LOADFILE);
  107.  
  108.    showfile = 1;        /* for any abort exit, pretend done */
  109.    initmode = -1;        /* no viewing mode set yet */
  110.    oldfloatflag = usr_floatflag;
  111.    loaded3d = 0;
  112.  
  113.    if(strchr(readname,'.') == NULL)
  114.       strcat(readname,".gif");
  115.  
  116.    if(find_fractal_info(readname,&read_info)) { /* didn't find a useable file */
  117.       sprintf(msg,"Sorry, %s isn't a file I can decode.",readname);
  118.       stopmsg(0,msg);
  119.       EXIT_OVLY;
  120.       return(-1);
  121.       }
  122.  
  123.    maxit    = read_info.iterations;
  124.    fractype    = read_info.fractal_type;
  125.    xxmin    = read_info.xmin;
  126.    xxmax    = read_info.xmax;
  127.    yymin    = read_info.ymin;
  128.    yymax    = read_info.ymax;
  129.    param[0]    = read_info.creal;
  130.    param[1]    = read_info.cimag;
  131.    save_release = 1100; /* unless we find out better later on */
  132.  
  133.    if(read_info.version > 0) {
  134.       param[2]        = read_info.parm3;
  135.       param[3]        = read_info.parm4;
  136.       potparam[0]   = read_info.potential[0];
  137.       potparam[1]   = read_info.potential[1];
  138.       potparam[2]   = read_info.potential[2];
  139.       potflag        = (potparam[0] != 0.0);
  140.       rflag        = read_info.rflag;
  141.       rseed        = read_info.rseed;
  142.       inside        = read_info.inside;
  143.       LogFlag        = read_info.logmap;
  144.       inversion[0]  = read_info.invert[0];
  145.       inversion[1]  = read_info.invert[1];
  146.       inversion[2]  = read_info.invert[2];
  147.       decomp[0]     = read_info.decomp[0];
  148.       decomp[1]     = read_info.decomp[1];
  149.       biomorph        = read_info.biomorph;
  150.       forcesymmetry = read_info.symmetry;
  151.       }
  152.  
  153.    if(read_info.version > 1) {
  154.       save_release  = 1200;
  155.       if (!display3d) {
  156.      int i;
  157.      for (i = 0; i < 16; i++)
  158.         init3d[i] = read_info.init3d[i];
  159.      previewfactor     = read_info.previewfactor;
  160.      xtrans      = read_info.xtrans;
  161.      ytrans      = read_info.ytrans;
  162.      red_crop_left     = read_info.red_crop_left;
  163.      red_crop_right  = read_info.red_crop_right;
  164.      blue_crop_left  = read_info.blue_crop_left;
  165.      blue_crop_right = read_info.blue_crop_right;
  166.      red_bright     = read_info.red_bright;
  167.      blue_bright     = read_info.blue_bright;
  168.      xadjust     = read_info.xadjust;
  169.      eyeseparation     = read_info.eyeseparation;
  170.      glassestype     = read_info.glassestype;
  171.      }
  172.       }
  173.  
  174.    if(read_info.version > 2) {
  175.       save_release = 1300;
  176.       outside       = read_info.outside;
  177.       }
  178.  
  179.    calc_status = 0;      /* defaults if version < 4 */
  180.    xx3rd = xxmin;
  181.    yy3rd = yymin;
  182.    usr_distest = 0;
  183.    calctime = 0;
  184.    if(read_info.version > 3) {
  185.       save_release = 1400;
  186.       xx3rd      = read_info.x3rd;
  187.       yy3rd      = read_info.y3rd;
  188.       calc_status = read_info.calc_status;
  189.       usr_stdcalcmode = read_info.stdcalcmode;
  190.       usr_distest     = read_info.distest;
  191.       usr_floatflag   = read_info.floatflag;
  192.       bailout      = read_info.bailout;
  193.       calctime      = read_info.calctime;
  194.       trigndx[0]  = read_info.trigndx[0];
  195.       trigndx[1]  = read_info.trigndx[1];
  196.       trigndx[2]  = read_info.trigndx[2];
  197.       trigndx[3]  = read_info.trigndx[3];
  198.       finattract  = read_info.finattract;
  199.       initorbit.x = read_info.initorbit[0];
  200.       initorbit.y = read_info.initorbit[1];
  201.       useinitorbit = read_info.useinitorbit;
  202.       usr_periodicitycheck = read_info.periodicity;
  203.       }
  204.  
  205.    pot16bit = 0;
  206.    save_system = 0;
  207.    if(read_info.version > 4) {
  208.       pot16bit       = read_info.pot16bit;
  209.       if (pot16bit)
  210.      filexdots >>= 1;
  211.       fileaspectratio = read_info.faspectratio;
  212.       if (fileaspectratio < 0.01)    /* fix files produced in early v14.1 */
  213.      fileaspectratio = SCREENASPECT;
  214.       save_system  = read_info.system;
  215.       save_release = read_info.release; /* from fmt 5 on we know real number */
  216.       if (read_info.version == 5    /* except a few early fmt 5 cases: */
  217.       && (save_release <= 0 || save_release >= 2000)) {
  218.      save_release = 1410;
  219.      save_system = 0;
  220.      }
  221.       if (!display3d && read_info.flag3d > 0) {
  222.      loaded3d    = 1;
  223.      Ambient    = read_info.ambient;
  224.      RANDOMIZE    = read_info.randomize;
  225.      haze        = read_info.haze;
  226.      transparent[0] = read_info.transparent[0];
  227.      transparent[1] = read_info.transparent[1];
  228.      }
  229.       }
  230.  
  231.    if(read_info.version < 4) { /* pre-version 14.0? */
  232.       backwardscompat(&read_info); /* translate obsolete types */
  233.       if(LogFlag)
  234.      LogFlag = 2;
  235.       usr_floatflag = (fractalspecific[fractype].isinteger) ? 0 : 1;
  236.       }
  237.  
  238.    if (read_info.version < 5) { /* pre-version 15.0? */
  239.       if (LogFlag == 2) /* logmap=old changed again in format 5! */
  240.      LogFlag = -1;
  241.       if (decomp[0] > 0 && decomp[1] > 0)
  242.      bailout = decomp[1];
  243.       }
  244.  
  245.    fractalspecific[fractype].paramvalue[0] = param[0];
  246.    fractalspecific[fractype].paramvalue[1] = param[1];
  247.    fractalspecific[fractype].paramvalue[2] = param[2];
  248.    fractalspecific[fractype].paramvalue[3] = param[3];
  249.    set_trig_pointers(-1);
  250.  
  251.    if (display3d)            /* PB - a klooge till the meaning of */
  252.       usr_floatflag = oldfloatflag; /*    floatflag in line3d is clarified */
  253.  
  254.    if (overlay3d) {
  255.       initmode = adapter;       /* use previous adapter mode for overlays */
  256.       if (filexdots > xdots || fileydots > ydots) {
  257.      stopmsg(0,"Can't overlay with a larger image");
  258.      EXIT_OVLY;
  259.      initmode = -1;
  260.      return(-1);
  261.      }
  262.       }
  263.    else {
  264.       int olddisplay3d,i;
  265.       olddisplay3d = display3d;
  266.       display3d = loaded3d; /* for <tab> display during next */
  267.       i = get_video_mode(&read_info);
  268.       display3d = olddisplay3d;
  269.       if (i) {
  270.      EXIT_OVLY;
  271.      initmode = -1;
  272.      return(-1);
  273.      }
  274.       }
  275.  
  276.    if (display3d) {
  277.       calc_status = 0;
  278.       fractype = PLASMA;
  279.       param[0] = 0;
  280.       if (!initbatch)
  281.      if (get_3d_params() < 0) {
  282.         EXIT_OVLY;
  283.         initmode = -1;
  284.         return(-1);
  285.         }
  286.       }
  287.  
  288.    showfile = 0;           /* trigger the file load */
  289.  
  290.    EXIT_OVLY;
  291.    return(0);
  292. }
  293.  
  294.  
  295. static int find_fractal_info(gif_file,info)
  296. char *gif_file;
  297. struct fractal_info *info;
  298. {
  299.    unsigned char gifstart[18];
  300.    char temp1[81];
  301.    int scan_extend, block_type, block_len, data_len;
  302.    int fractinf_len;
  303.    int hdr_offset;
  304.    int i;
  305.  
  306.    if((fp = fopen(gif_file,"rb"))==NULL)
  307.       return(-1);
  308.  
  309.    fread(gifstart,18,1,fp);
  310.    if (strncmp(gifstart,"GIF",3)!=0) { /* not GIF, maybe old .tga? */
  311.       if(fread(info,sizeof(struct fractal_info),1,fp)==1 &&
  312.          strncmp(info->info_id,"Fractal",8)==0) {
  313.      filetype = 1; /* Targa 16 */
  314.      fileydots = *(int *)&gifstart[O_VSIZE];
  315.      filexdots = *(int *)&gifstart[O_HSIZE];
  316.      filecolors = info->colors;
  317.      fileaspectratio = SCREENASPECT;
  318.      if(fileydots == info->ydots && filexdots == info->xdots) {
  319.         fclose(fp);
  320.         return(0);
  321.         }
  322.      }
  323.       fclose(fp);
  324.       return(-1);
  325.       }
  326.  
  327.    filetype = 0; /* GIF */
  328.    filexdots = gifstart[7]*256+gifstart[6];
  329.    fileydots = gifstart[9]*256+gifstart[8];
  330.    filecolors = 2 << (gifstart[10] & 7);
  331.    fileaspectratio = 0; /* unknown */
  332.    if (gifstart[12]) { /* calc reasonably close value from gif header */
  333.       fileaspectratio = (64.0 / ((double)(gifstart[12]) + 15.0))
  334.               * (double)fileydots / (double)filexdots;
  335.       if ( fileaspectratio > SCREENASPECT-0.03
  336.     && fileaspectratio < SCREENASPECT+0.03)
  337.      fileaspectratio = SCREENASPECT;
  338.       }
  339.    else
  340.       if (fileydots * 4 == filexdots * 3) /* assume the common square pixels */
  341.      fileaspectratio = SCREENASPECT;
  342.  
  343.    if (resume_info != NULL) { /* free the prior area if there is one */
  344.       farmemfree(resume_info);
  345.       resume_info = NULL;
  346.       }
  347.  
  348.    /* Format of .gif extension blocks is:
  349.       1 byte    '!', extension block identifier
  350.       1 byte    extension block number, 255
  351.       1 byte    length of id, 11
  352.      11 bytes   alpha id, "fractintnnn" with fractint, nnn is secondary id
  353.        n * {
  354.       1 byte    length of block info in bytes
  355.       x bytes   block info
  356.        }
  357.       1 byte    0, extension terminator
  358.       To scan extension blocks, we first look in file at length of fractal_info
  359.       (the main extension block) from end of file, looking for a literal known
  360.       to be at start of our block info.  Then we scan forward a bit, in case
  361.       the file is from an earlier fractint vsn with shorter fractal_info.
  362.       If fractal_info is found and is from vsn>=14, it includes the total length
  363.       of all extension blocks; we then scan them all first to last to load
  364.       any optional ones which are present.
  365.       Defined extension blocks:
  366.     fractint001    header, always present
  367.     fractint002    resume info for interrupted resumable image
  368.     fractint003    additional formula type info
  369.    */
  370.  
  371.    memset(info,0,sizeof(FRACTAL_INFO));
  372.    fractinf_len = sizeof(FRACTAL_INFO) + (sizeof(FRACTAL_INFO)+254)/255;
  373.    fseek(fp,(long)(-1-fractinf_len),SEEK_END);
  374.    fread(info,1,sizeof(FRACTAL_INFO),fp);
  375.    if (strcmp(INFO_ID,info->info_id) == 0)
  376.       hdr_offset = -1-fractinf_len;
  377.    else {
  378.       /* didn't work 1st try, maybe an older vsn, maybe junk at eof, scan: */
  379.       int offset,i;
  380.       char tmpbuf[110];
  381.       hdr_offset = 0;
  382.       offset = 80; /* don't even check last 80 bytes of file for id */
  383.       while (offset < fractinf_len+513) { /* allow 512 garbage at eof */
  384.      offset += 100; /* go back 100 bytes at a time */
  385.      fseek(fp,(long)(0-offset),SEEK_END);
  386.      fread(tmpbuf,1,110,fp); /* read 10 extra for string compare */
  387.      for (i = 0; i < 100; ++i)
  388.         if (!strcmp(INFO_ID,&tmpbuf[i])) { /* found header? */
  389.            strcpy(info->info_id,INFO_ID);
  390.            fseek(fp,(long)(hdr_offset=i-offset),SEEK_END);
  391.            fread(info,1,sizeof(FRACTAL_INFO),fp);
  392.            offset = 10000; /* force exit from outer loop */
  393.            break;
  394.            }
  395.      }
  396.       }
  397.  
  398.    if (hdr_offset) { /* we found INFO_ID */
  399.  
  400.       if (info->version >= 4) {
  401.      /* first reload main extension block, reasons:
  402.           might be over 255 chars, and thus earlier load might be bad
  403.           find exact endpoint, so scan back to start of ext blks works
  404.         */
  405.      fseek(fp,(long)(hdr_offset-15),SEEK_END);
  406.      scan_extend = 1;
  407.      while (scan_extend) {
  408.         if (fgetc(fp) != '!' /* if not what we expect just give up */
  409.           || fread(temp1,1,13,fp) != 13
  410.           || strncmp(&temp1[2],"fractint",8))
  411.            break;
  412.         temp1[13] = 0;
  413.         block_type = atoi(&temp1[10]); /* e.g. "fractint002" */
  414.         switch (block_type) {
  415.            case 1: /* "fractint001", the main extension block */
  416.           if (scan_extend == 2) { /* we've been here before, done now */
  417.              scan_extend = 0;
  418.              break;
  419.              }
  420.           load_ext_blk((char far *)info,sizeof(FRACTAL_INFO));
  421.           scan_extend = 2;
  422.           /* now we know total extension len, back up to first block */
  423.           fseek(fp,0L-info->tot_extend_len,SEEK_CUR);
  424.           break;
  425.            case 2: /* resume info */
  426.           skip_ext_blk(&block_len,&data_len); /* once to get lengths */
  427.           if ((resume_info = farmemalloc((long)data_len)) == NULL)
  428.              info->calc_status = 3; /* not resumable after all */
  429.           else {
  430.              fseek(fp,(long)(0-block_len),SEEK_CUR);
  431.              load_ext_blk(resume_info,data_len);
  432.              resume_len = data_len;
  433.              }
  434.           break;
  435.            case 3: /* formula info */
  436.           load_ext_blk(FormName,40);
  437.           strcpy(LName,FormName);
  438.           /* perhaps in future add more here, check block_len for
  439.              backward compatibility */
  440.           break;
  441.  
  442.  
  443.            default:
  444.           skip_ext_blk(&block_len,&data_len);
  445.            }
  446.         }
  447.      }
  448.  
  449.       fclose(fp);
  450.       fileaspectratio = SCREENASPECT; /* if not >= v15, this is correct */
  451.       return(0);
  452.       }
  453.  
  454.    strcpy(info->info_id, "GIFFILE");
  455.    info->iterations = 150;
  456.    info->fractal_type = PLASMA;
  457.    info->xmin = -1;
  458.    info->xmax = 1;
  459.    info->ymin = -1;
  460.    info->ymax = 1;
  461.    info->x3rd = -1;
  462.    info->y3rd = -1;
  463.    info->creal = 0;
  464.    info->cimag = 0;
  465.    info->videomodeax=255;
  466.    info->videomodebx=255;
  467.    info->videomodecx=255;
  468.    info->videomodedx=255;
  469.    info->dotmode = 0;
  470.    info->xdots = filexdots;
  471.    info->ydots = fileydots;
  472.    info->colors = filecolors;
  473.    info->version = 0; /* this forces lots more init at calling end too */
  474.  
  475.    /* zero means we won */
  476.    fclose(fp);
  477.    return(0);
  478. }
  479.  
  480. static void load_ext_blk(char far *loadptr,int loadlen)
  481. {
  482.    int len;
  483.    while ((len = fgetc(fp)) > 0) {
  484.       while (--len >= 0) {
  485.      if (--loadlen >= 0)
  486.         *(loadptr++) = fgetc(fp);
  487.      else
  488.         fgetc(fp); /* discard excess characters */
  489.      }
  490.       }
  491. }
  492.  
  493. static void skip_ext_blk(int *block_len, int *data_len)
  494. {
  495.    int len;
  496.    *data_len = 0;
  497.    *block_len = 1;
  498.    while ((len = fgetc(fp)) > 0) {
  499.       fseek(fp,(long)len,SEEK_CUR);
  500.       *data_len += len;
  501.       *block_len += len + 1;
  502.       }
  503. }
  504.  
  505.  
  506. /* switch obsolete fractal types to new generalizations */
  507. static void backwardscompat(struct fractal_info *info)
  508. {
  509.    switch(fractype) {
  510.       case LAMBDASINE:
  511.      fractype = LAMBDATRIGFP;
  512.      trigndx[0] = SIN;
  513.      break;
  514.       case LAMBDACOS    :
  515.      fractype = LAMBDATRIGFP;
  516.      trigndx[0] = COS;
  517.      break;
  518.       case LAMBDAEXP    :
  519.      fractype = LAMBDATRIGFP;
  520.      trigndx[0] = EXP;
  521.      break;
  522.       case MANDELSINE    :
  523.      fractype = MANDELTRIGFP;
  524.      trigndx[0] = SIN;
  525.      break;
  526.       case MANDELCOS    :
  527.      fractype = MANDELTRIGFP;
  528.      trigndx[0] = COS;
  529.      break;
  530.       case MANDELEXP    :
  531.      fractype = MANDELTRIGFP;
  532.      trigndx[0] = EXP;
  533.      break;
  534.       case MANDELSINH    :
  535.      fractype = MANDELTRIGFP;
  536.      trigndx[0] = SINH;
  537.      break;
  538.       case LAMBDASINH    :
  539.      fractype = LAMBDATRIGFP;
  540.      trigndx[0] = SINH;
  541.      break;
  542.       case MANDELCOSH    :
  543.      fractype = MANDELTRIGFP;
  544.      trigndx[0] = COSH;
  545.      break;
  546.       case LAMBDACOSH    :
  547.      fractype = LAMBDATRIGFP;
  548.      trigndx[0] = COSH;
  549.      break;
  550.       case LMANDELSINE    :
  551.      fractype = MANDELTRIG;
  552.      trigndx[0] = SIN;
  553.      break;
  554.       case LLAMBDASINE    :
  555.      fractype = LAMBDATRIG;
  556.      trigndx[0] = SIN;
  557.      break;
  558.       case LMANDELCOS    :
  559.      fractype = MANDELTRIG;
  560.      trigndx[0] = COS;
  561.      break;
  562.       case LLAMBDACOS    :
  563.      fractype = LAMBDATRIG;
  564.      trigndx[0] = COS;
  565.      break;
  566.       case LMANDELSINH    :
  567.      fractype = MANDELTRIG;
  568.      trigndx[0] = SINH;
  569.      break;
  570.       case LLAMBDASINH    :
  571.      fractype = LAMBDATRIG;
  572.      trigndx[0] = SINH;
  573.      break;
  574.       case LMANDELCOSH    :
  575.      fractype = MANDELTRIG;
  576.      trigndx[0] = COSH;
  577.      break;
  578.       case LLAMBDACOSH    :
  579.      fractype = LAMBDATRIG;
  580.      trigndx[0] = COSH;
  581.      break;
  582.       case LMANDELEXP    :
  583.      fractype = MANDELTRIG;
  584.      trigndx[0] = EXP;
  585.      break;
  586.       case LLAMBDAEXP    :
  587.      fractype = LAMBDATRIG;
  588.      trigndx[0] = EXP;
  589.      break;
  590.       case DEMM     :
  591.      fractype = MANDELFP;
  592.      usr_distest = (info->ydots - 1) * 2;
  593.      break;
  594.       case DEMJ     :
  595.      fractype = JULIAFP;
  596.      usr_distest = (info->ydots - 1) * 2;
  597.      break;
  598.       case MANDELLAMBDA :
  599.      useinitorbit = 2;
  600.      break;
  601.       }
  602. }
  603.  
  604.  
  605.