home *** CD-ROM | disk | FTP | other *** search
/ Mega A/V / mega_av.zip / mega_av / GRAPHUTL / FRPOR172.ZIP / GIFVIEW.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  6KB  |  257 lines

  1. /*
  2.  *
  3.  * This GIF decoder is designed for use with Bert Tyler's FRACTINT
  4.  * program. It should be noted that the "FRACTINT" program only decodes
  5.  * GIF files FRACTINT creates, so this decoder code lacks full generality
  6.  * in the following respects: supports single image, non-interlaced GIF
  7.  * files with no local color maps and no extension blocks.
  8.  *
  9.  * GIF and 'Graphics Interchange Format' are trademarks (tm) of
  10.  * Compuserve, Incorporated, an H&R Block Company.
  11.  *
  12.  *                                            Tim Wegner
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #ifndef XFRACT
  18. #include <dos.h>
  19. #endif
  20. #include "fractint.h"
  21.  
  22. /* routines in this module    */
  23.  
  24. int  gifview(void);
  25. int  get_byte(void);        /* used only locally and by decoder.c */
  26. int  get_bytes(BYTE *,int);    /* used only by decoder.c  *mg*/
  27.  
  28. static void close_file(void);
  29.  
  30. #define MAXCOLORS    256
  31.  
  32. extern int timer(int timertype,int(*subrtn)(),...);
  33. extern int rowcount;        /* row counter for screen */
  34. extern char readname[];     /* file name          */
  35. static FILE *fpin = NULL;    /* FILE pointer       */
  36. unsigned int height;
  37. extern    char busy;
  38. unsigned numcolors;
  39.  
  40. extern char MAP_name[];
  41. extern int mapset;
  42. extern int colorstate;        /* comments in cmdfiles */
  43.  
  44. extern int glassestype;
  45. extern int display3d;
  46. extern int dotmode;        /* so we can detect disk-video */
  47. extern int calc_status;
  48. extern long calctime;
  49. extern long timer_interval;
  50. extern int pot16bit;        /* 16 bit values for continuous potential */
  51.  
  52. int bad_code_count = 0;     /* needed by decoder module */
  53.  
  54. int get_byte()
  55. {
  56.    return (getc(fpin)); /* EOF is -1, as desired */
  57. }
  58.  
  59. int get_bytes(BYTE *where,int how_many)
  60. {
  61.    return (fread((char *)where,1,how_many,fpin)); /* EOF is -1, as desired */
  62. }
  63.  
  64. extern BYTE dacbox[256][3];    /* Video-DAC (filled in by SETVIDEO) */
  65. #ifndef XFRACT
  66. extern BYTE decoderline[2049]; /* write-line routines use this */
  67. #else
  68. BYTE decoderline[2049]; /* write-line routines use this */
  69. #endif
  70.  
  71. /* Main entry decoder */
  72. int gifview()
  73. {
  74.    BYTE buffer[16];
  75.    unsigned width, finished;
  76.    char temp1[81];
  77.  
  78.    int status;
  79.    int i, j, k, planes;
  80.  
  81.    status = 0;
  82.  
  83.    /* initialize the row count for write-lines */
  84.    rowcount = 0;
  85.  
  86.    /* zero out the full write-line */
  87.    for (width = 0; width < 2049; width++) decoderline[width] = 0;
  88.  
  89.    /* Open the file */
  90.    strcpy(temp1,readname);
  91.    if (strchr(temp1,'.') == NULL) {
  92.       strcat(temp1,DEFAULTFRACTALTYPE);
  93.       if ((fpin = fopen(temp1,"rb")) != NULL) {
  94.      fclose(fpin);
  95.      }
  96.       else {
  97.      strcpy(temp1,readname);
  98.      strcat(temp1,ALTERNATEFRACTALTYPE);
  99.      }
  100.       }
  101.    if ((fpin = fopen(temp1, "rb")) == NULL)
  102.       return (-1);
  103.  
  104.    /* Get the screen description */
  105.    for (i = 0; i < 13; i++)
  106.    {
  107.       int tmp;
  108.  
  109.       buffer[i] = tmp = get_byte();
  110.       if (tmp < 0)
  111.       {
  112.      close_file();
  113.      return(-1);
  114.       }
  115.    }
  116.  
  117.    if(strncmp(buffer,"GIF87a",3) ||             /* use updated GIF specs */
  118.       buffer[3] < '0' || buffer[3] > '9' ||
  119.       buffer[4] < '0' || buffer[4] > '9' ||
  120.       buffer[5] < 'A' || buffer[5] > 'z' )
  121.    {
  122.       close_file();
  123.       return(-1);
  124.    }
  125.  
  126.    planes = (buffer[10] & 0x0F) + 1;
  127.  
  128.    if((buffer[10] & 0x80)==0)     /* color map (better be!) */
  129.    {
  130.       close_file();
  131.       return(-1);
  132.    }
  133.    numcolors = 1 << planes;
  134.  
  135.    for (i = 0; i < numcolors; i++)
  136.    {
  137.       for (j = 0; j < 3; j++) {
  138.      if ((k = get_byte()) < 0)
  139.      {
  140.         close_file();
  141.         return(-1);
  142.      }
  143.      if(!display3d || (glassestype != 1 && glassestype != 2))
  144.         dacbox[i][j] = k >> 2;
  145.       }
  146.    }
  147.    colorstate = 1; /* colors aren't default and not a known .map file */
  148.  
  149.    /* don't read if glasses */
  150.    if (display3d && mapset && glassestype!=1 && glassestype != 2)
  151.    {
  152.        ValidateLuts(MAP_name);    /* read the palette file */
  153.        spindac(0,1); /* load it, but don't spin */
  154.    }
  155.    if (dacbox[0][0] != 255)
  156.       spindac(0,1);      /* update the DAC */
  157.  
  158.    if (dotmode == 11) /* disk-video */
  159.        dvid_status(1,"...restoring...");
  160.  
  161.    /* Now display one or more GIF objects */
  162.    finished = 0;
  163.    while (!finished)
  164.    {
  165.       switch (get_byte())
  166.       {
  167.       case ';':
  168.      /* End of the GIF dataset */
  169.  
  170.      finished = 1;
  171.      status = 0;
  172.      break;
  173.  
  174.       case '!':                               /* GIF Extension Block */
  175.      get_byte();             /* read (and ignore) the ID */
  176.      while ((i = get_byte()) > 0)     /* get the data length */
  177.         for (j = 0; j < i; j++)
  178.            get_byte();     /* flush the data */
  179.      break;
  180.       case ',':
  181.      /*
  182.       * Start of an image object. Read the image description.
  183.       */
  184.  
  185.      for (i = 0; i < 9; i++)
  186.      {
  187.             int tmp;
  188.  
  189.             buffer[i] = tmp = get_byte();
  190.         if (tmp < 0)
  191.         {
  192.            status = -1;
  193.            break;
  194.         }
  195.      }
  196.      if(status < 0)
  197.      {
  198.         finished = 1;
  199.         break;
  200.      }
  201.  
  202.      width    = buffer[4] | (buffer[5] << 8);
  203.      if (pot16bit) width >>= 1;
  204.      height = buffer[6] | (buffer[7] << 8);
  205.  
  206.          /* Skip local color palette */
  207.          if((buffer[8] & 0x80)==0x80) {      /* local map? */
  208.              int numcolors;    /* make this local */
  209.              planes = (buffer[8] & 0x0F) + 1;
  210.              numcolors = 1 << planes;
  211.              /* skip local map */
  212.              for (i = 0; i < numcolors; i++) {
  213.                 for (j = 0; j < 3; j++) {
  214.                    if ((k = get_byte()) < 0) {
  215.                       close_file();
  216.                       return(-1);
  217.                       }
  218.                    }
  219.                 }
  220.              }
  221.  
  222.      if (calc_status == 1) /* should never be so, but make sure */
  223.         calc_status = 0;
  224.      busy = 1;    /* for slideshow CALCWAIT */
  225.      status = timer(1,NULL,width); /* call decoder(width) via timer */
  226.      busy = 0;    /* for slideshow CALCWAIT */
  227.      if (calc_status == 1) /* e.g., set by line3d */
  228.      {
  229.         calctime = timer_interval; /* note how long it took */
  230.         if (keypressed() != 0)
  231.            calc_status = 3; /* interrupted, not resumable */
  232.         else
  233.            calc_status = 4; /* complete */
  234.      }
  235.      finished = 1;
  236.      break;
  237.       default:
  238.      status = -1;
  239.      finished = 1;
  240.      break;
  241.       }
  242.    }
  243.    close_file();
  244.    if (dotmode == 11) { /* disk-video */
  245.       dvid_status(0,"Restore completed");
  246.       dvid_status(1,"");
  247.       }
  248.  
  249.    return(status);
  250. }
  251.  
  252. static void close_file()
  253. {
  254.    fclose(fpin);
  255.    fpin = NULL;
  256. }
  257.