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