home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Web / Utilities / wwwcount-2.3 / combine / getsize.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-02  |  3.2 KB  |  206 lines

  1. /*
  2.  *    GetGIFsize - reads the image size in a GIF file
  3.  *
  4.  *    RCS:
  5.  *        $Revision: 2.3 $
  6.  *        $Date: 1996/05/03 02:21:34 $
  7.  *
  8.  *    Security:
  9.  *        Unclassified
  10.  *
  11.  *    Description:
  12.  *      This functions returns after reading the size params in a GIF file
  13.  *
  14.  *    Input Parameters:
  15.  *        type    identifier    description
  16.  *
  17.  *        char    *filename
  18.  *      unsigned int *width;
  19.  *      unsigned int *height;
  20.  *
  21.  *    Output Parameters:
  22.  *        type    identifier    description
  23.  *
  24.  *        unsigned int *width
  25.  *      unsigned int *height
  26.  *
  27.  *    Return Values:
  28.  *        value    description
  29.  *      0 on success
  30.  *
  31.  *    Side Effects:
  32.  *        text
  33.  *
  34.  *    Limitations and Comments:
  35.  *        text
  36.  *
  37.  *    Development History:
  38.  *        when    who        why
  39.  *  02/18/96    muquit  first cut
  40.  */
  41.  
  42. #include "combine.h"
  43. #include "defines.h"
  44. #include "errcds.h"
  45.  
  46.  
  47. int GetGIFsize(filename,width,height)
  48. char
  49.     *filename;
  50.  
  51. unsigned int
  52.     *width,
  53.     *height;
  54. {
  55.  
  56. #define BitSet(byte, bit) (((byte) & (bit)) == (bit))
  57. #define LSBFirstOrder(x, y)    (((y) << 8) | (x))
  58.  
  59.     int
  60.         rc = 0;
  61.  
  62.     short int
  63.         transparency_index;
  64.  
  65.     unsigned char
  66.         c,
  67.         *global_colormap,
  68.         header[MaxTextLength],
  69.         type[12];
  70.  
  71.     unsigned int
  72.         global_colors;
  73.  
  74.     FILE
  75.         *fp=(FILE *) NULL;
  76.     
  77.     *width=0;
  78.     *height=0;
  79.  
  80.     fp = fopen (filename, "rb");
  81.  
  82.     if (fp == (FILE *) NULL)
  83.     {
  84.         fprintf (stderr, "could not open file: %s\n",filename);
  85.         rc = OPEN_FAILED;
  86.         goto ExitProcessing;
  87.     }
  88.  
  89.  
  90.     rc = Read_Data ((char *) type, 1, 6, fp);
  91.  
  92.     if ((rc == 1) || ((strncmp((char *) type, "GIF87", 5) != 0) &&
  93.         (strncmp ((char *) type, "GIF89", 5) != 0)))
  94.     {
  95.         fprintf (stderr, "Not a GIF Image: [%s]\n",filename);
  96.         rc = NOT_A_GIF_FILE;
  97.         goto ExitProcessing;
  98.     }
  99.  
  100.     rc = Read_Data ((char *) header, 1, 7, fp);
  101.  
  102.     if (rc == 1)
  103.     {
  104.         rc = FAILED_TO_READ_SCREEND;
  105.         goto ExitProcessing;
  106.     }
  107.  
  108.     global_colors = 0;
  109.     global_colormap = (unsigned char *) NULL;
  110.  
  111.     if (BitSet(header[4], 0x80))
  112.     {
  113.         global_colors = 1 << ((header[4] & 0x07) + 1);
  114.         fseek(fp,3*global_colors,1);
  115.     }
  116.  
  117.     transparency_index = (-1);
  118.  
  119.     for ( ; ; )
  120.     {
  121.         rc = Read_Data ((char *) &c, 1, 1, fp);
  122.  
  123.         if (rc == 1)
  124.         {
  125.             rc = 0;
  126.             break;
  127.         }
  128.  
  129.         if (c == ';')    /* terminator */
  130.             break;
  131.         
  132.         if (c == '!')    /* EXTENSION ! */
  133.         {
  134.             rc = Read_Data ((char *) &c, 1, 1, fp);
  135.  
  136.             if (rc == 1)
  137.             {
  138.                 rc = READ_ERR_EXT_BLOCK;
  139.                 goto ExitProcessing;
  140.             }
  141.  
  142.             switch (c)
  143.             {
  144.                 case 0xf9:    /* Transperency */
  145.                 {
  146.                     while (ReadDataBlock ((char *) header, fp) !=0)
  147.                     if ((header[0] * 0x01) == 1)
  148.                     {
  149.                         transparency_index = header[3];
  150.                     }
  151.                     break;
  152.                 
  153.                 }
  154.                 case 0xfe:    /* comment extension */
  155.                 {
  156.                     int
  157.                         length;
  158.                     
  159.                     for ( ; ; )
  160.                     {
  161.                         length = ReadDataBlock ((char *) header, fp);
  162.                         if (length <= 0)
  163.                             break;
  164.                     }
  165.                     break;
  166.                 }    
  167.  
  168.                 default:
  169.                 {
  170.                     while (ReadDataBlock ((char *) header, fp) != 0);
  171.                     break;
  172.                 }
  173.             }
  174.         }
  175.  
  176.  
  177.         if (c != ',')
  178.             continue;
  179.  
  180.  
  181.         /*
  182.         ** read Header Now
  183.         */
  184.  
  185.         rc = Read_Data ((char *) header, 1, 9, fp);
  186.  
  187.         if (rc == 1)
  188.         {
  189.             rc = READ_ERR_WIDTH_HEIGHT;
  190.             goto ExitProcessing;
  191.         }
  192.  
  193.         *width = LSBFirstOrder (header[4], header[5]);
  194.         *height = LSBFirstOrder (header[6], header[7]);
  195.         break;
  196.     }/* for (;;) */
  197.  
  198.         
  199. ExitProcessing:
  200.     if (fp != (FILE *) NULL)
  201.         (void) fclose ((FILE *) fp);
  202.     return (rc);
  203.  
  204. }
  205.     
  206.