home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / newc_dev / fileid12.lha / GIF.c < prev    next >
C/C++ Source or Header  |  1990-11-25  |  9KB  |  337 lines

  1. /**************************************************
  2.  *
  3.  * GIF.c (GIF module of FileID)
  4.  *
  5.  *    Disassemble GIF file
  6.  *
  7.  *    Copyright(c), 1990 Lloyd B. Eldred
  8.  *                     & Fredrick R Homan
  9.  *
  10.  **************************************************/
  11. #include <stdio.h>
  12. #include <math.h>
  13. #include <string.h>
  14.  
  15. typedef unsigned char UBYTE;     /* 8 bits unsigned */
  16. typedef unsigned short UWORD;    /* 16 bits unsigned */
  17. typedef short WORD;         /* 16 bits signed */
  18. typedef long LONG;               /* 32 bits signed */
  19.  
  20. #define TRUE 1
  21. #define FALSE 0
  22.  
  23. void Gif_PlainText(picfile,PrintFlag)
  24. FILE *picfile;
  25. UBYTE PrintFlag;
  26. {
  27.     UBYTE bsize,c1,c2,c3,c4;
  28.     UBYTE ccw,cch,tbi,tfi;
  29.     char buf[256],coltext[512];
  30.     UWORD tgl,tgt;
  31.     int width,height,colwidth,tw;
  32.     void fi_getc(FILE *,char *);
  33.  
  34.     if (PrintFlag) printf(" Plain Text\n");
  35.  
  36.     fi_getc(picfile,&bsize);     /* Block size = 12 ? */
  37.  
  38.     if (fread(buf,12,1,picfile) !=  1) {
  39.       printf(" Unexpected end of file. (PT-2)\n");
  40.       fclose(picfile);
  41.       fi_exit();
  42.       }
  43.  
  44.     c1 = buf[0];
  45.     c2 = buf[1];
  46.     c3 = buf[2];
  47.     c4 = buf[3];
  48.  
  49.     tgl = (UWORD)(c1 + 256*c2);
  50.     tgt = (UWORD)(c3 + 256*c4);
  51.  
  52.     c1 = buf[4];
  53.     c2 = buf[5];
  54.     c3 = buf[6];
  55.     c4 = buf[7];
  56.  
  57.     width  = (int)(c1 + 256*c2);
  58.     height = (int)(c3 + 256*c4);
  59.  
  60.     ccw = buf[8];
  61.     cch = buf[9];
  62.     tfi = buf[10];
  63.     tbi = buf[11];
  64.  
  65.     colwidth = width / ccw;
  66.  
  67.     if (PrintFlag) printf("    Text left edge   : %d\n"
  68.            "    Text top         : %d\n"
  69.            "    Grid width       : %d\n"
  70.            "    Grid height      : %d\n"
  71.            "    Cell width       : %d\n"
  72.            "    Cell height      : %d\n"
  73.            "    Foreground color : %d\n"
  74.            "    Background color : %d\n"
  75.            ,tgl,tgt,width,height,ccw,cch,tfi,tbi);
  76.  
  77.     fi_getc(picfile,&bsize);        /* Block size */
  78.  
  79.     if (bsize != 0 && PrintFlag)
  80.       printf("    ********** Text reads: **********\n");
  81.  
  82.     while (bsize != 0) {
  83.       if (fread(buf, bsize, 1,picfile) != 1) {
  84.        fprintf(stderr, "Unexpected end of file. (PT-4)\n");
  85.        fclose(picfile);
  86.         fi_exit();
  87.       }
  88.  
  89.       buf[bsize]='\0';
  90.  
  91.       if (PrintFlag)
  92.           for (tw = 0; tw < bsize; tw += colwidth) {
  93.               strncpy (coltext, &buf[tw], colwidth);
  94.               coltext[colwidth] = '\0';
  95.               printf("%s\n",coltext);
  96.           }
  97.       fi_getc(picfile,&bsize);         /* Block size */
  98.  
  99.     } /* end of while() */
  100.  
  101.     if (PrintFlag) printf("\n");
  102.     return;
  103.  
  104. } /* End of Plain Text Section */
  105.  
  106. void Gif_Application(picfile,PrintFlag)
  107. FILE *picfile;
  108. UBYTE PrintFlag;
  109.  {
  110.     UBYTE bsize;
  111.     char buf[256];
  112.     void fi_getc(FILE *,char *);
  113.     
  114.     printf(" Application specific\n");
  115.     fi_getc(picfile,&bsize);     /* Block size = 11 ? */
  116.     if (fread(buf,bsize,1,picfile) !=  1) {
  117.       printf(" Unexpected end of file. (Appl-2)\n");
  118.       fclose(picfile);
  119.       fi_exit();
  120.       }
  121.                      
  122.     /* write Application Identifier */                     
  123.     if (PrintFlag) printf("    Application ID: %8s\n",buf);
  124.  
  125.     fi_getc(picfile,&bsize);
  126.  
  127.     while(bsize != 0){           
  128.       if (fread(buf,bsize,1,picfile) !=  1) {
  129.          printf(" Unexpected end of file. (Appl-4)\n");
  130.          fclose(picfile);
  131.          fi_exit();
  132.          }
  133.     fi_getc(picfile,&bsize);
  134.     } /* end of while */                      
  135.           
  136.     return;
  137.  } /* End of Application Specific Section */
  138.  
  139. void Gif_Comment(picfile,PrintFlag)
  140. FILE *picfile;
  141. UBYTE PrintFlag;
  142. {
  143.    UBYTE bsize;
  144.    char buf[256];
  145.    void fi_getc(FILE *,char *);
  146.    
  147.    fi_getc(picfile,&bsize);      /* Block size */
  148.   
  149.    if (bsize != 0 && PrintFlag) printf("   ********* Text reads: *********\n");
  150.    
  151.    while(bsize != 0){                 
  152.        if (fread(buf, bsize, 1,picfile) != 1) {
  153.            fprintf(stderr, "Unexpected end of file. (Com-2)\n");
  154.             fclose(picfile);
  155.             fi_exit();
  156.          }
  157.  
  158.       buf[bsize]='\0';
  159.                                           
  160.       if (PrintFlag) printf("%s",buf);
  161.  
  162.       fi_getc(picfile,&bsize);      /* Block size */
  163.  
  164.       
  165.    } /* end of while() */
  166.       
  167.    if (PrintFlag) printf("\n");
  168.    return;
  169. } /* end of dealing with comment */
  170.  
  171. void Gif_Image(picfile,PrintFlag)
  172. FILE *picfile;
  173. UBYTE PrintFlag;
  174. {
  175.    UBYTE c1,c2,c3,c4,bsize,LZW_size;
  176.    char buf[256];
  177.    UWORD ilp,itp;
  178.    int planes,color,offset,width,height,i;
  179.    void fi_getc(FILE *,char *);
  180.       
  181.    if (PrintFlag) printf("  Image Descriptor:\n");
  182.     if (fread(buf, 9, 1,picfile) != 1) {
  183.        fprintf(stderr, "Unexpected end of file. (I-1)\n");
  184.         fclose(picfile);
  185.         fi_exit();
  186.       }
  187.    c1 = buf[0];
  188.    c2 = buf[1];
  189.    c3 = buf[2];
  190.    c4 = buf[3];
  191.           
  192.    ilp = (UWORD)(c1 + 256*c2);
  193.    itp = (UWORD)(c3 + 256*c4);            
  194.             
  195.    c1 = buf[4];
  196.    c2 = buf[5];
  197.    c3 = buf[6];
  198.    c4 = buf[7];
  199.             
  200.    width  = (int)(c1 + 256*c2);
  201.    height = (int)(c3 + 256*c4);
  202.             
  203.    if (PrintFlag) printf("    Image left edge: %d\n"
  204.           "    Image top      : %d\n"
  205.           "    Image width    : %d\n"
  206.           "    Image height   : %d\n",ilp,itp,width,height);
  207.            
  208.    c1 = buf[8];
  209.    
  210.    if(PrintFlag){         
  211.       if(c1 & 0x40) 
  212.          printf("    Image is interlaced.\n");
  213.       else
  214.          printf("    Image is not interlaced.\n");
  215.                               
  216.       if(c1 & 0x18)
  217.          printf("    Uses bits reserved for future"
  218.                 " expansion as of the GIF89a standard.\n");
  219.       }
  220.             
  221.       if(c1 & 0x80){
  222.          if(c1 & 0x20)
  223.             if(PrintFlag)printf("    Uses Local Color Table (sorted)\n");
  224.          else
  225.             if(PrintFlag)printf("    Uses Local Color Table (unsorted)\n");
  226.                        
  227.      planes =(int)(c1 & 0x07) +1;
  228.       color=1;
  229.       for(i=0; i<planes ; i++) color = color * 2;
  230.    
  231.      if(PrintFlag) printf("       size: %d colors\n",color);
  232.      offset = 3 * color;
  233.                
  234.    /* Skip the Local Color Table */
  235.                             
  236.    if (fseek(picfile,offset,1) == -1){
  237.       printf(" Unexpected end of file. (I-2)\n");  
  238.       fclose(picfile);
  239.       fi_exit();
  240.    }
  241.                
  242.    } /* End of Local Color Table stuff */
  243.             
  244.    else
  245.       if (PrintFlag) printf("    Uses Global Color Table.\n");
  246.             
  247.    /* Skip over the actual image data */
  248.             
  249.     fi_getc(picfile,&LZW_size);      /* LZW Code Size */
  250.    fi_getc(picfile,&bsize);         /* Block size */                        
  251.             
  252.    while(bsize !=0){
  253.       if (fseek(picfile,bsize,1) == -1){
  254.          printf(" Unexpected end of file. (I-5)\n");  
  255.             fclose(picfile);
  256.             fi_exit();
  257.             }
  258.                
  259.       fi_getc(picfile,&bsize);      /* Next block size */
  260.                                  
  261.     } /* End of while() */
  262.     return;        
  263. }    /* End of Image Data */
  264.  
  265. void Gif_Graphic(picfile,PrintFlag)
  266. FILE *picfile;
  267. UBYTE PrintFlag;
  268. {
  269.    UBYTE c1,c2,bsize,fields,trans;
  270.    UWORD delay;
  271.    int disp;
  272.    void fi_getc(FILE *,char *);
  273.    
  274.    if (PrintFlag) printf(" Graphic Control\n");
  275.    
  276.    fi_getc(picfile,&bsize);         /* Block size = 4 ? */
  277.    
  278.    while(bsize != 0){
  279.       fi_getc(picfile,&fields);        /* Packed fields */
  280.       if(fields & 0xe0) 
  281.          if (PrintFlag) printf("    Bits reserved as of GIF89a standard are being used.\n");
  282.                
  283.       disp = (int)(fields & 0x1c)>>2;
  284.       if (PrintFlag) {
  285.          if(disp == 0)
  286.             printf("    No disposal information given for"
  287.                    " the following graphic.\n");
  288.          if(disp == 1)
  289.             printf("    Following graphic is not to be"
  290.                    " disposed of after being displayed.\n");
  291.          if(disp == 2)
  292.              printf("    The area used by the following graphic"
  293.                    " is to be restored to the background\n"
  294.                    "    color after being displayed.\n");
  295.          if(disp == 3)
  296.             printf("    The area used by the following graphic"
  297.                    " is to be restored to the previous\n"
  298.                    "    image after being displayed.\n");
  299.          if(disp >3 && disp <8)
  300.             printf("    The area used by the following graphic"
  301.                    " is to be disposed of in a method\n"
  302.                    "    NOT defined by the GIF89a standard.\n");
  303.       }
  304.       if(fields & 0x02)
  305.          if (PrintFlag) printf("    User Input is expected.\n");
  306.       else
  307.          if (PrintFlag) printf("    User Input is not expected.\n");
  308.                
  309.       trans = fields & 0x01;
  310.       
  311.       fi_getc(picfile,&c1);      /* Delay pt 1 */
  312.       fi_getc(picfile,&c2);      /* Delay pt 2 */
  313.       delay = (UWORD)(c1 + 256*c2);
  314.       if(delay != 0)
  315.          if (PrintFlag) printf("    Delay time: %d hundreths of a second"
  316.                                 ".\n",delay);
  317.       
  318.       fi_getc(picfile,&c1);      /* Transparent index */
  319.       if((trans) && (PrintFlag)) printf("    Color %d is to be treated as"
  320.                        " transparent.\n",c1);
  321.       
  322.       fi_getc(picfile,&bsize);       /* Next block size */
  323.       } /* End of while(bsize != 0) */               
  324.       return;
  325. } /* end of graphic control section */
  326.  
  327. void fi_getc(picfile,c)
  328. FILE *picfile;
  329. char *c;
  330. {
  331.       if (fread(c, 1, 1,picfile) != 1) { 
  332.          printf(" Unexpected end of file.\n");
  333.          fclose(picfile);  
  334.          fi_exit();
  335.       }
  336.    return;
  337. }