home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / frasrc18.zip / LOADFDOS.C < prev    next >
C/C++ Source or Header  |  1993-04-10  |  15KB  |  408 lines

  1. /*
  2.     loadfdos.c - subroutine of loadfile.c (read_overlay) which sets
  3.          up video (mode, screen size).
  4.     This module is linked as an overlay, should only be called from loadfile.c
  5.  
  6.     This code was split to a separate module to isolate the DOS only aspects
  7.     of loading an image.  get_video_mode should return with:
  8.       return code 0 for ok, -1 for error or cancelled by user
  9.       video parameters setup for the mainline, in the dos case this means
  10.     setting initmode to video mode, based on this fractint.c will set up
  11.     for and call setvideomode
  12.       set viewwindow on if file going to be loaded into a view smaller than
  13.     physical screen, in this case also set viewreduction, viewxdots,
  14.     viewydots, and finalaspectratio
  15.       set skipxdots and skipydots, to 0 if all pixels are to be loaded,
  16.     to 1 for every 2nd pixel, 2 for every 3rd, etc
  17.  
  18.     In WinFract, at least initially, get_video_mode can do just the
  19.     following:
  20.       set overall image x & y dimensions (sxdots and sydots) to filexdots
  21.     and fileydots (note that filecolors is the number of colors in the
  22.     gif, not sure if that is of any use...)
  23.       if current window smaller than new sxdots and sydots, use scroll bars,
  24.     if larger perhaps reduce the window size? whatever
  25.       set viewwindow to 0 (no need? it always is for now in windows vsn?)
  26.       set finalaspectratio to .75 (ditto?)
  27.       set skipxdots and skipydots to 0
  28.       return 0
  29.  
  30. */
  31.  
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <math.h>
  36. #include "fractint.h"
  37. #include "helpdefs.h"
  38. #include "prototyp.h"
  39.  
  40. /* routines in this module    */
  41.  
  42. static int    vidcompare(VOIDCONSTPTR ,VOIDCONSTPTR );
  43. static void   format_vid_inf(int i,char *err,char *buf);
  44. static double vid_aspect(int tryxdots,int tryydots);
  45. static void   format_item(int,char *);
  46. static int    check_modekey(int,int);
  47.  
  48.  
  49. extern int    initmode;         /* initial video mode        */
  50. extern int    askvideo;
  51. extern int    initbatch;        /* 1 if batch run (no kbd)  */
  52. extern int    fractype;         /* fractal type         */
  53. extern int    viewwindow;        /* 0 for full screen, 1 for window */
  54. extern float  viewreduction;        /* window auto-sizing */
  55. extern float  finalaspectratio;     /* for view shape and rotation */
  56. extern int    viewxdots,viewydots;    /* explicit view sizing */
  57. extern int    fileydots, filexdots, filecolors;
  58. extern float  fileaspectratio;
  59. extern int    skipxdots,skipydots;    /* for decoder, when reducing image */
  60. extern char   readname[];        /* name of fractal input file */
  61. extern int    save_system,save_release;
  62. extern int    display3d;        /* 3D display flag: 0 = OFF */
  63. extern struct videoinfo far videotable[];
  64. extern struct videoinfo far *vidtbl;
  65. extern int    vidtbllen;
  66. extern float  screenaspect;
  67.  
  68.  
  69. struct vidinf {
  70.    int entnum;       /* videoentry subscript */
  71.    unsigned flags; /* flags for sort's compare, defined below */
  72.    };
  73. /* defines for flags; done this way instead of bit union to ensure ordering;
  74.    these bits represent the sort sequence for video mode list */
  75. #define VI_EXACT 0x8000 /* unless the one and only exact match */
  76. #define VI_NOKEY   512    /* if no function key assigned */
  77. #define VI_DISK1   256    /* disk video and size not exact */
  78. #define VI_SSMALL  128    /* screen smaller than file's screen */
  79. #define VI_SBIG     64    /* screen bigger than file's screen */
  80. #define VI_VSMALL   32    /* screen smaller than file's view */
  81. #define VI_VBIG     16    /* screen bigger than file's view */
  82. #define VI_CSMALL    8    /* mode has too few colors */
  83. #define VI_CBIG      4    /* mode has excess colors */
  84. #define VI_DISK2     2    /* disk video */
  85. #define VI_ASPECT    1    /* aspect ratio bad */
  86.  
  87.  
  88. static int vidcompare(VOIDCONSTPTR p1,VOIDCONSTPTR p2)
  89. {
  90.    struct vidinf *ptr1,*ptr2;
  91.    ptr1 = (struct vidinf *)p1;
  92.    ptr2 = (struct vidinf *)p2;
  93.    if (ptr1->flags < ptr2->flags) return(-1);
  94.    if (ptr1->flags > ptr2->flags) return(1);
  95.    if (vidtbl[ptr1->entnum].keynum < vidtbl[ptr2->entnum].keynum) return(-1);
  96.    if (vidtbl[ptr1->entnum].keynum > vidtbl[ptr2->entnum].keynum) return(1);
  97.    if (ptr1->entnum < ptr2->entnum) return(-1);
  98.    return(1);
  99. }
  100.  
  101. static void format_vid_inf(int i,char *err,char *buf)
  102. {
  103.    char kname[5];
  104.    far_memcpy((char far *)&videoentry,(char far *)&vidtbl[i],
  105.           sizeof(videoentry));
  106.    vidmode_keyname(videoentry.keynum,kname);
  107.    sprintf(buf,"%-5s %-25s %-4s %4d %4d %3d %-25s",  /* 76 chars */
  108.        kname, videoentry.name, err,
  109.        videoentry.xdots, videoentry.ydots,
  110.        videoentry.colors, videoentry.comment);
  111.    videoentry.xdots = 0; /* so tab_display knows to display nothing */
  112. }
  113.  
  114. static double vid_aspect(int tryxdots,int tryydots)
  115. {  /* calc resulting aspect ratio for specified dots in current mode */
  116.    return (double)tryydots / (double)tryxdots
  117.     * (double)videoentry.xdots / (double)videoentry.ydots
  118.     * screenaspect;
  119.    }
  120.  
  121.  
  122. static struct vidinf *vidptr;
  123.  
  124. int get_video_mode(struct fractal_info *info)
  125. {
  126.    static char far hdg2[]={"key...name......................err..xdot.ydot.clr.comment.................."};
  127.    static char far warning[]={"\nWARNING: non-standard aspect ratio; loading will change your <v>iew settings"};
  128.    static char far select_msg[]={"\
  129. Select a video mode.  Use the cursor keypad to move the pointer.\n\
  130. Press ENTER for selected mode, or use a video mode function key.\n\
  131. Press F1 for help, "};
  132.  
  133.    struct vidinf vid[MAXVIDEOMODES];
  134.    int i,j;
  135.    int gotrealmode;
  136.    double ftemp,ftemp2;
  137.    unsigned tmpflags;
  138.  
  139.    int tmpxdots,tmpydots;
  140.    float tmpreduce;
  141.    char *nameptr;
  142.    int    *attributes;
  143.    extern char temp1[256];
  144.    extern char dstack[]; /* for heading, avoid a 320 byte variable here  */
  145.              /* also, for attributes array, saves 600     */
  146.              /* both above necessary to avoid stack problems */
  147.    int oldhelpmode;
  148.    struct videoinfo far *vident;
  149.  
  150.    initmode = -1;
  151.    load_fractint_cfg(0); /* get fractint.cfg into *vidtbl (== extraseg) */
  152.  
  153.    /* try to find exact match for vid mode */
  154.    for (i = 0; i < vidtbllen; ++i) {
  155.       vident = &vidtbl[i];
  156.       if (info->xdots == vident->xdots && info->ydots == vident->ydots
  157.     && filecolors == vident->colors
  158.     && info->videomodeax == vident->videomodeax
  159.     && info->videomodebx == vident->videomodebx
  160.     && info->videomodecx == vident->videomodecx
  161.     && info->videomodedx == vident->videomodedx
  162.     && info->dotmode%100 == vident->dotmode%100) {
  163.      initmode = i;
  164.      break;
  165.      }
  166.       }
  167.  
  168.    if (initmode == -1) /* try to find very good match for vid mode */
  169.       for (i = 0; i < vidtbllen; ++i) {
  170.      vident = &vidtbl[i];
  171.      if (info->xdots == vident->xdots && info->ydots == vident->ydots
  172.        && filecolors == vident->colors) {
  173.         initmode = i;
  174.         break;
  175.         }
  176.      }
  177.  
  178.    /* setup table entry for each vid mode, flagged for how well it matches */
  179.    for (i = 0; i < vidtbllen; ++i) {
  180.       far_memcpy((char far *)&videoentry,(char far *)&vidtbl[i],
  181.          sizeof(videoentry));
  182.       tmpflags = VI_EXACT;
  183.       if (videoentry.keynum == 0)
  184.      tmpflags |= VI_NOKEY;
  185.       if (info->xdots > videoentry.xdots || info->ydots > videoentry.ydots)
  186.      tmpflags |= VI_SSMALL;
  187.       else if (info->xdots < videoentry.xdots || info->ydots < videoentry.ydots)
  188.      tmpflags |= VI_SBIG;
  189.       if (filexdots > videoentry.xdots || fileydots > videoentry.ydots)
  190.      tmpflags |= VI_VSMALL;
  191.       else if (filexdots < videoentry.xdots || fileydots < videoentry.ydots)
  192.      tmpflags |= VI_VBIG;
  193.       if (filecolors > videoentry.colors)
  194.      tmpflags |= VI_CSMALL;
  195.       if (filecolors < videoentry.colors)
  196.      tmpflags |= VI_CBIG;
  197.       if (i == initmode)
  198.      tmpflags -= VI_EXACT;
  199.       if (videoentry.dotmode%100 == 11) {
  200.      tmpflags |= VI_DISK2;
  201.      if ((tmpflags & (VI_SBIG+VI_SSMALL+VI_VBIG+VI_VSMALL)) != 0)
  202.         tmpflags |= VI_DISK1;
  203.      }
  204.       if (fileaspectratio != 0 && videoentry.dotmode%100 != 11
  205.     && (tmpflags & VI_VSMALL) == 0) {
  206.      ftemp = vid_aspect(filexdots,fileydots);
  207.      if ( ftemp < fileaspectratio * 0.98
  208.        || ftemp > fileaspectratio * 1.02)
  209.         tmpflags |= VI_ASPECT;
  210.      }
  211.       vid[i].entnum = i;
  212.       vid[i].flags  = tmpflags;
  213.       }
  214.  
  215. #ifndef XFRACT
  216.    gotrealmode = 0;
  217.    if (initmode < 0 || (askvideo && !initbatch)) {
  218.       /* no exact match or (askvideo=yes and batch=no), talk to user */
  219.  
  220.       qsort(vid,vidtbllen,sizeof(vid[0]),vidcompare); /* sort modes */
  221.  
  222.       attributes = (int *)&dstack[1000];
  223.       for (i = 0; i < vidtbllen; ++i)
  224.      attributes[i] = 1;
  225.       vidptr = &vid[0]; /* for format_item */
  226.  
  227.       /* format heading */
  228.       if (info->info_id[0] == 'G')
  229.      strcpy(temp1,"      Non-fractal GIF");
  230.       else {
  231.      nameptr = curfractalspecific->name;
  232.      if (*nameptr == '*') ++nameptr;
  233.      if (display3d) nameptr = "3D Transform";
  234.      sprintf(temp1,"Type: %s",nameptr);
  235.      }
  236.       sprintf(dstack,"File: %-44s  %d x %d x %d\n%-52s",
  237.          readname,filexdots,fileydots,filecolors,temp1);
  238.       if (info->info_id[0] != 'G') {
  239.      if (save_system)
  240.         strcat(dstack,"WinFract ");
  241.      sprintf(temp1,"v%d.%01d",save_release/100,(save_release%100)/10);
  242.      if (save_release%100) {
  243.         i = strlen(temp1);
  244.         temp1[i] = (save_release%10) + '0';
  245.         temp1[i+1] = 0;
  246.         }
  247.      if (save_system == 0 && save_release <= 1410)
  248.         strcat(temp1," or earlier");
  249.      strcat(dstack,temp1);
  250.      }
  251.       strcat(dstack,"\n");
  252.       if (info->info_id[0] != 'G' && save_system == 0)
  253.      if (initmode < 0)
  254.         strcat(dstack,"Saved in unknown video mode.");
  255.      else {
  256.         format_vid_inf(initmode,"",temp1);
  257.         strcat(dstack,temp1);
  258.         }
  259.       if (fileaspectratio != 0 && fileaspectratio != screenaspect)
  260.      far_strcat(dstack,warning);
  261.       strcat(dstack,"\n");
  262.       /* set up instructions */
  263.       far_strcpy(temp1,select_msg);
  264.       if (info->info_id[0] != 'G')
  265.      strcat(temp1,"TAB for fractal information, ");
  266.       strcat(temp1,"ESCAPE to back out.");
  267.  
  268.       oldhelpmode = helpmode;
  269.       helpmode = HELPLOADFILE;
  270.       i = fullscreen_choice(0,dstack,hdg2,temp1,vidtbllen,NULL,attributes,
  271.                      1,13,76,0,format_item,NULL,NULL,check_modekey);
  272.       helpmode = oldhelpmode;
  273.       if (i == -1)
  274.      return(-1);
  275.       if (i < 0) { /* returned -100 - videotable entry number */
  276.      initmode = -100 - i;
  277.      gotrealmode = 1;
  278.      }
  279.       else
  280.      initmode = vid[i].entnum;
  281.       }
  282. #else
  283.       initmode = 0;
  284.       j = vidtbl[0].keynum;
  285.       gotrealmode = 0;
  286. #endif
  287.  
  288.    if (gotrealmode == 0) { /* translate from temp table to permanent */
  289.       if ((j = vidtbl[i=initmode].keynum) != 0) {
  290.      for (initmode = 0; initmode < MAXVIDEOTABLE-1; ++initmode)
  291.         if (videotable[initmode].keynum == j) break;
  292.      if (initmode >= MAXVIDEOTABLE-1) j = 0;
  293.      }
  294.       if (j == 0) /* mode has no key, add to reserved slot at end */
  295.      far_memcpy((char far *)&videotable[initmode=MAXVIDEOTABLE-1],
  296.             (char far *)&vidtbl[i],sizeof(*vidtbl));
  297.       }
  298.  
  299.    /* ok, we're going to return with a video mode */
  300.  
  301.    far_memcpy((char far *)&videoentry,(char far *)&videotable[initmode],
  302.           sizeof(videoentry));
  303.  
  304.    skipxdots = skipydots = 0; /* set for no reduction */
  305.    if (videoentry.xdots < filexdots || videoentry.ydots < fileydots) {
  306.       /* set up to load only every nth pixel to make image fit */
  307.       skipxdots = skipydots = 1;
  308.       while (skipxdots * videoentry.xdots < filexdots) ++skipxdots;
  309.       while (skipydots * videoentry.ydots < fileydots) ++skipydots;
  310.       i = j = 0;
  311.       while (1) {
  312.      tmpxdots = (filexdots + skipxdots - 1) / skipxdots;
  313.      tmpydots = (fileydots + skipydots - 1) / skipydots;
  314.      if (fileaspectratio == 0 || videoentry.dotmode%100 == 11)
  315.         break;
  316.      /* reduce further if that improves aspect */
  317.      if ((ftemp = vid_aspect(tmpxdots,tmpydots)) > fileaspectratio) {
  318.         if (j) break; /* already reduced x, don't reduce y */
  319.         ftemp2 = vid_aspect(tmpxdots,(fileydots+skipydots)/(skipydots+1));
  320.         if (ftemp2 < fileaspectratio
  321.           && ftemp/fileaspectratio *0.9 <= fileaspectratio/ftemp2)
  322.            break; /* further y reduction is worse */
  323.         ++skipydots;
  324.         ++i;
  325.         }
  326.      else {
  327.         if (i) break; /* already reduced y, don't reduce x */
  328.         ftemp2 = vid_aspect((filexdots+skipxdots)/(skipxdots+1),tmpydots);
  329.         if (ftemp2 > fileaspectratio
  330.           && fileaspectratio/ftemp *0.9 <= ftemp2/fileaspectratio)
  331.            break; /* further x reduction is worse */
  332.         ++skipxdots;
  333.         ++j;
  334.         }
  335.      }
  336.       filexdots = tmpxdots;
  337.       fileydots = tmpydots;
  338.       --skipxdots;
  339.       --skipydots;
  340.       }
  341.  
  342.    if ((finalaspectratio = fileaspectratio) == 0) /* assume display correct */
  343.       finalaspectratio = vid_aspect(filexdots,fileydots);
  344.    if (finalaspectratio >= screenaspect-0.02
  345.      && finalaspectratio <= screenaspect+0.02)
  346.       finalaspectratio = screenaspect;
  347.    i = finalaspectratio * 1000.0 + 0.5;
  348.    finalaspectratio = (double)i/1000.0; /* chop precision to 3 decimals */
  349.  
  350.    /* setup view window stuff */
  351.    viewwindow = viewxdots = viewydots = 0;
  352.    if (filexdots != videoentry.xdots || fileydots != videoentry.ydots) {
  353.       /* image not exactly same size as screen */
  354.       viewwindow = 1;
  355.       ftemp = finalaspectratio
  356.         * (double)videoentry.ydots / (double)videoentry.xdots
  357.         / screenaspect;
  358.       if (finalaspectratio <= screenaspect) {
  359.      i = (double)videoentry.xdots / (double)filexdots * 20.0 + 0.5;
  360.      tmpreduce = (double)i/20.0; /* chop precision to nearest .05 */
  361.      i = (double)videoentry.xdots / tmpreduce + 0.5;
  362.      j = (double)i * ftemp + 0.5;
  363.      }
  364.       else {
  365.      i = (double)videoentry.ydots / (double)fileydots * 20.0 + 0.5;
  366.      tmpreduce = (double)i/20.0; /* chop precision to nearest .05 */
  367.      j = (double)videoentry.ydots / tmpreduce + 0.5;
  368.      i = (double)j / ftemp + 0.5;
  369.      }
  370.       if (i != filexdots || j != fileydots) { /* too bad, must be explicit */
  371.      viewxdots = filexdots;
  372.      viewydots = fileydots;
  373.      }
  374.       else
  375.      viewreduction = tmpreduce; /* ok, this works */
  376.       }
  377.    if (fabs(finalaspectratio - screenaspect) > .00001 || viewxdots != 0 ) {
  378.       static char far msg[] = {"\
  379. Warning: <V>iew parameters are being set to non-standard values.\n\
  380. Remember to reset them when finished with this image.1"};
  381.       stopmsg(4,msg);
  382.       }
  383.  
  384.    return(0);
  385. }
  386.  
  387. static void format_item(int choice,char *buf)
  388. {
  389.    char errbuf[10];
  390.    unsigned tmpflags;
  391.    errbuf[0] = 0;
  392.    tmpflags = vidptr[choice].flags;
  393.    if (tmpflags & (VI_VSMALL+VI_CSMALL+VI_ASPECT)) strcat(errbuf,"*");
  394.    if (tmpflags & VI_VSMALL) strcat(errbuf,"R");
  395.    if (tmpflags & VI_CSMALL) strcat(errbuf,"C");
  396.    if (tmpflags & VI_ASPECT) strcat(errbuf,"A");
  397.    if (tmpflags & VI_VBIG)   strcat(errbuf,"v");
  398.    if (tmpflags & VI_CBIG)   strcat(errbuf,"c");
  399.    format_vid_inf(vidptr[choice].entnum,errbuf,buf);
  400. }
  401.  
  402. static int check_modekey(int curkey,int choice)
  403. {
  404.    int i;
  405.    return (((i = check_vidmode_key(0,curkey)) >= 0) ? -100-i : 0);
  406. }
  407.  
  408.