home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / dos / imdisp / doc / gif_lib.doc < prev    next >
Encoding:
Text File  |  1994-04-28  |  18.7 KB  |  454 lines

  1.                              GIF library document
  2.                              --------------------
  3.  
  4.                           Gershon Elber, August 1989
  5.                           --------------------------
  6.  
  7.                                  Version 1.0
  8.                                  -----------
  9.  
  10.        This library was written once I didnt find anything similar and I
  11. wanted one. I was inspired from the rle Utah tool kit, which I hoped to port
  12. to an IBM PC, but found it to be too machine specific, and its compression
  13. ratio too low. I compromised on the GIF format while I am not sure how long 8
  14. bits per pixel will be enough.
  15.  
  16.        This document explains the GIF library kernel on directory GIF/LIB. The
  17. kernel is built to the gif_libl.lib which is used in all the utilities on
  18. GIF/UTIL, or can be used in any application needs to read/write GIF file
  19. format. This document does NOT explain the GIF file format and assumes it is
  20. known, at list to the level of the GIF file structure.
  21.  
  22.        When a GIF file is opened, a GIF file descriptor is maintained which is
  23. a pointer to GifFileType structure as follows:
  24.  
  25. typedef struct GifFileType {
  26.     int SWidth, SHeight,                 /* Screen dimensions */
  27.         SColorResolution, SBitsPerPixel; /* How many colors can we generate? */
  28.         SBackGroundColor,                /* I hope you understand this one... */
  29.         ILeft, ITop, IWidth, IHeight,    /* Current image dimensions */
  30.         IInterlace,                      /* Sequential/Interlaced lines */
  31.         IBitsPerPixel;                   /* How many colors this image has? */
  32.     GifColorType *SColorMap, *IColorMap; /* NULL if not exists */
  33.     void *Private;      /* The regular user should not mess with this one! */
  34. } GifFileType;
  35.  
  36.        This structure was copied from gif_lib.h - the header file for the GIF
  37. library. Any application program that uses the gif_libl.lib library should
  38. include it. All items begin with S refer to GIF Screen, while the ones with I
  39. to current image (note GIF file may have more than one image). The user NEVER
  40. writes into this structure, but can read any of these items at any time it is
  41. proper (image information is invalid until first image was read/write).
  42.  
  43.        As the library needs to save its own internal data also, a Private
  44. pointer to internal structure is also saved there. Applications should ignore
  45. this item.
  46.  
  47.        The library has no static data. This means that it is fully reentrant
  48. and any number of GIF files (up to memory limits) can be opened for
  49. read/write. Instead of the static data, internal structure pointed by the
  50. Private pointer is used.
  51.  
  52.        The library do allocates its own memory dynamically, on opening of
  53. file, and releases that once closed. The user is NEVER requires to allocate
  54. any memory for any of the functions of this library (unless the provided
  55. parameters, such as image line, were prefered to be allocated dynammically by
  56. the user) nor to free them directly. In order to reduce disk access, the file
  57. buffer is increased to FILE_BUFFER_SIZE (defined in gif_lib.h). The library
  58. was compiled in large model as the memory allocated per file is quite big:
  59. about 17k for decoding (DGIF_LIB.C), and 32k for encoding (EGIF_LIB.C),
  60. excluding the FILE_BUFFER_SIZE.
  61.  
  62.        We now can see what the library contains (directory GIF/LIB):
  63.  
  64. 1. EGIF_LIB.C - Encoding routines, all prefixed with E.
  65. 2. DGIF_LIB.C - Decoding routines, all prefixed with D.
  66. 3. DEV2GIF.C - Routines to convert specific device buffers into GIF files.
  67. 4. GIF_ERR.C - Error handler for the library.
  68.  
  69.        The library has fifth hashing table file in which is accessed
  70. internally only.
  71.  
  72.        Major part of the routines returns ERROR (see gif_lib.h) if something
  73. went wrong or OK otherwise. Once ERROR received, GIF_ERR.C module can be used
  74. to do something about it.
  75.  
  76.        In addition a module to scan the command line arguments was added. This
  77. module is called GETARG.C and its headers are in GETARG.H. see header of
  78. GETARG.C for details on its usage.
  79.  
  80.  
  81. ENCODING (EGIF_LIB.C)
  82. ---------------------
  83.  
  84. GifFileType *EGifOpenFileName(char *GifFileName, int GifTestExistance);
  85.  
  86.        Open a new GIF file using the given GifFileName. If GifTestExistance is
  87.     TRUE, and file exists, the file is not destroyed, and NULL returned. If
  88.     any error occurs, NULL is returned and Error handler can be used to get
  89.     the exact error (see GIF_ERR.C). The file is opened in binary mode, and
  90.     its buffer size is set to FILE_BUFFER_SIZE bytes.
  91.  
  92.  
  93. GifFileType *EGifOpenFileHandle(int GifFileHandle);
  94.  
  95.        Open a new GIF file using the given GifFileHandle. If any error occurs,
  96.     NULL is returned and Error handler can be used to get the exact error (see
  97.     GIF_ERR.C) The file is opened in binary mode, and its buffer size is set
  98.     to FILE_BUFFER_SIZE bytes.
  99.  
  100.  
  101. int EGifPutScreenDesc(GifFileType *GifFile, int GifWidth, int GifHeight,
  102.            int GifColorRes, int GifBackGround, int GifBitsPerPixel,
  103.            GifColorType *GifColorMap);
  104.  
  105.        Update GifFile Screen parameters, in GifFile structure and in real
  106.     file. if error occurs returns ERROR (see gif_lib.h), otherwise OK. This
  107.     routine should be called immediately after the GIF file was opened.
  108.  
  109.  
  110. int EGifPutImageDesc(GifFileType *GifFile, int GifLeft, int GifTop, int Width,
  111.            int GifHeight, int GifInterlace, int GifBitsPerPixel,
  112.            GifColorType *GifColorMap);
  113.  
  114.        Update GifFile Image parameters, in GifFile structure and in real file.
  115.     if error occurs returns ERROR (see gif_lib.h), otherwise OK. This routine
  116.     should be called each time a new image should be dumped to the file.
  117.  
  118.  
  119. int EGifPutLine(GifFileType *GifFile, PixelType *GifLine, int GifLineLen);
  120.  
  121.        Dumps block of pixels out to the GIF file. The line length can be of
  122.     any length. More than that, this routine may be interleaved with
  123.     EGifPutPixel, until all pixels were sent. Returns ERROR if something went
  124.     wrong, OK otherwise.
  125.  
  126.  
  127. int EGifPutPixel(GifFileType *GifFile, PixelType GifPixel);
  128.  
  129.        Dumps one pixel to the GIF file. This routine may be interleaved with
  130.     EGifPutLine, until all pixels were sent. Because of the overhead per each
  131.     call, the usage of this routine is not recommended. Returns ERROR if
  132.     something went wrong, OK otherwise.
  133.  
  134.  
  135. int EGifPutComment(GifFileType *GifFile, char *GifComment);
  136.  
  137.        Uses extension GIF records to save a string as a comment is the file.
  138.     The extension code is 'C' (for Comment). This is optional in GIF file.
  139.     Returns ERROR if something went wrong, OK otherwise.
  140.  
  141.  
  142. int EGifPutExtension(GifFileType *GifFile, int GifExtCode, int GifExtLen,
  143.            void *GifExtension);
  144.  
  145.        Dumps the given extension block into the GIF file. Extension blocks are
  146.     optional in GIF file. Extension blocks of more than 255 bytes or more than
  147.     one block are not supported. Returns ERROR if something went wrong, OK
  148.     otherwise.
  149.  
  150.  
  151. int EGifPutCode(GifFileType *GifFile, int *GifCodeSize,
  152.            ByteType **GifCodeBlock);
  153.  
  154.        It sometimes may be desired to write the compressed code as is without
  155.     decoding it. For example a filter for GIF file that change only screen
  156.     size (GifPos), does not need the exact pixel values and pipes out the
  157.     compressed image as is, make this process much faster.
  158.        This routine do exactly that (with EGifPutCodeNext), and can be used
  159.     instead of EGifPutLine. This usually works with the
  160.     DGifGetCode/DgifGetCodeNext routines, which reads the compressed code,
  161.     while EGifPutCode/EGifPutCodeNext write it out. See GifPos.c for example.
  162.        Returns ERROR if something went wrong, OK otherwise.
  163.  
  164.  
  165. int EGifPutCodeNext(GifFileType *GifFile, ByteType **GifCodeBlock);
  166.  
  167.        See EGifPutCode above.
  168.  
  169.  
  170. int EGifCloseFile(GifFileType *GifFile);
  171.  
  172.        Close GIF file and free all memory allocated for it. GifFile should not
  173.     be used, once this routine was called.
  174.        Returns ERROR if something went wrong, OK otherwise.
  175.  
  176.  
  177. DECODING (DGIF_LIB.C)
  178. ---------------------
  179.  
  180. GifFileType *DGifOpenFileName(char *GifFileName);
  181.  
  182.        Open a new GIF file using the given GifFileName, and read its Screen
  183.     information. If any error occurs, NULL is returned and Error handler can
  184.     be used to get the exact error (see GIF_ERR.C). The file is opened in
  185.     binary mode, and its buffer size is set to FILE_BUFFER_SIZE bytes.
  186.  
  187.  
  188. GifFileType *DGifOpenFileHandle(int GifFileHandle);
  189.  
  190.        Open a new GIF file using the given GifFileHandle, and read its Screen
  191.     information. If any error occurs, NULL is returned and Error handler can
  192.     be used to get the exact error (see GIF_ERR.C) The file is opened in
  193.     binary mode, and its buffer size is set to FILE_BUFFER_SIZE bytes.
  194.  
  195.  
  196. int DGifGetScreenDesc(GifFileType *GifFile);
  197.  
  198.        Reads the screen information into the GifFile structure. Note this
  199.     routine is automatically called once a file is opened, and therefore
  200.     usually not needed. Returns ERROR if something went wrong, OK otherwise.
  201.  
  202.  
  203. int DGifGetRecordType(GifFileType *GifFile, GifRecordType *GifType);
  204.  
  205.        As the GIF file can have different records in arbitrary order, this
  206.     routine should be called once the file was open to detect the next record
  207.     type, and act upon it. Few types might be returned in GifType:
  208.      1. UndefinedRecordType - something is wrong!
  209.      2. ScreenDescRecordType - screen information. As the screen information
  210.         is automatically read in when the file is open, this usually would not
  211.         happen.
  212.      3. ImageDescRecordType - next record is Image.
  213.      4. ExtensionRecordType - next record is extension block.
  214.      5. TerminateRecordType - last record reached, can close the file.
  215.         The first Two types can usually be ignored.
  216.         Returns ERROR if something went wrong, OK otherwise.
  217.  
  218.  
  219. int DGifGetImageDesc(GifFileType *GifFile);
  220.  
  221.        Reads the image information into the GifFile structure.
  222.        Returns ERROR if something went wrong, OK otherwise.
  223.  
  224.  
  225. int DGifGetLine(GifFileType *GifFile, PixelType *GifLine, int GifLineLen);
  226.  
  227.        Load block of pixels from the GIF file. The line length can be of any
  228.     length. More than that, this routine may be interleaved with DGifGetPixel,
  229.     until all pixels were read.
  230.        Returns ERROR if something went wrong, OK otherwise.
  231.  
  232. int DGifGetPixel(GifFileType *GifFile, PixelType GifPixel);
  233.  
  234.        Loads one pixel from the GIF file. This routine may be interleaved with
  235.     DGifGetLine, until all pixels were read. Because of the overhead per each
  236.     call, the usage of this routine is not recommended.
  237.        Returns ERROR if something went wrong, OK otherwise.
  238.  
  239. int DGifGetComment(GifFileType *GifFile, char *GifComment);
  240.  
  241.        Load comment from the GIF file. Because DGifGetRecordType will only
  242.     tell this records is of type extension, this routine should be called iff
  243.     it is known %100 that is must be a comment. For definition of comment, see
  244.     EGifPutComment.
  245.        Returns ERROR if something went wrong, OK otherwise.
  246.  
  247.  
  248. int DGifGetExtension(GifFileType *GifFile, int *GifExtCode,
  249.            ByteType **GifExtension);
  250.  
  251.        Loads the given extension block from the GIF file. Extension blocks are
  252.     optional in GIF file. This routine should be follows by
  253.     DGifGetExtensionNext - see below
  254.        Returns ERROR if something went wrong, OK otherwise.
  255.  
  256.  
  257. int DGifGetExtensionNext(GifFileType *GifFile, ByteType **GifExtension);
  258.  
  259.        As extensions may contain more than one block, use this routine to
  260.     continue after DGifGetExtension, until *GifExtension is NULL.
  261.        Returns ERROR if something went wrong, OK otherwise.
  262.  
  263.  
  264. int DGifGetCode(GifFileType *GifFile, int *GifCodeSize,
  265.            ByteType **GifCodeBlock);
  266.  
  267.        It sometimes may be desired to read the compressed code as is without
  268.     decoding it. This routine do exactly that (with DGifGetCodeNext), and can
  269.     be used instead of DGifGetLine. This compressed code information can be
  270.     written out using the EGifPutCode/EGifPutCodeNext sequence (see GifPos.c
  271.     for example).
  272.        Returns ERROR if something went wrong, OK otherwise.
  273.  
  274.  
  275. int DGifGetCodeNext(GifFileType *GifFile, ByteType **GifCodeBlock);
  276.  
  277.        See DGifGetCode above.
  278.  
  279.  
  280. int DGifGetLZCodes(GifFileType *GifFile, int *GifCode);
  281.  
  282.        This routine can be called instead of DGifGetLine/DGifGetPixel or
  283.     DGifGetCode/DGifGetCodeNext to get the 12 bits LZ codes of the images. It
  284.     may be used mainly for debugging purposes (see GifText.c for example).
  285.        Returns ERROR if something went wrong, OK otherwise.
  286.  
  287.  
  288. int DGifCloseFile(GifFileType *GifFile);
  289.  
  290.        Close GIF file and free all memory allocated for it. GifFile should not
  291.     be used, once this routine was called.
  292.        Returns ERROR if something went wrong, OK otherwise.
  293.  
  294.  
  295.  
  296. ERROR HANDLING (EGIF_LIB.C)
  297. ---------------------------
  298.  
  299. void PrintGifError(void)
  300.  
  301.        Print one line diagnostic on the last gif_lib error to stderr.
  302.  
  303.  
  304. int GifLastError(void)
  305.  
  306.        Return last gif_lib error, and clear the error. Note it is the user
  307.     responsibility to call the file closing routine, so the file will be
  308.     closed (if was opened), and memory will be released (if was allocated).
  309.        The different error types are defined in gif_lib.h.
  310.  
  311.  
  312. DEVICE SPECIFIC (XXX2GIF.C)
  313. ---------------------------
  314.  
  315. int DumpScreen(char *FileName, int ReqGraphDriver, int ReqGraphMode);
  316.  
  317.        Dumps the whole device buffer as specified by GraphDriver and GraphMode
  318.     (as defined in TC 2.0 graphics.h) into FileName as GIF file.
  319.        Current devices supported:
  320.        1. Hercules.
  321.        Returns ERROR if something went wrong, OK otherwise.
  322.  
  323.  
  324. COMMAND LINE PARSING (GETARG.C)
  325. -------------------------------
  326.  
  327. int GAGetArgs(int argc, char **argv, char *CtrlStr, ...);
  328.  
  329.        Main routine of this module. Given the argc & argv as received by the
  330.     main procedure, the command line CtrlStr, and the addresses of all
  331.     parameters, parse the command line, and update the parameters. The CtrlStr
  332.     defines what types of variables should follow. Look at the beginning of
  333.     getarg.c for exact usage.
  334.        Returns 0 if successful, error number (as defined by getarg.h)
  335.     otherwise.
  336.  
  337.  
  338. void GAPrintErrMsg(int Error);
  339.  
  340.        If error occurred in GAGetARgs, this routine may be used to print one
  341.     line diagnostic to stderr.
  342.  
  343.  
  344. void GAPrintHowTo(char *CtrlStr);
  345.  
  346.        Given same CtrlStr as for GAGetArgs, can be used to print a one line
  347.     'how to use'
  348.  
  349.  
  350. Skeleton of GIF filter
  351. ----------------------
  352.  
  353.        This completes the functions, application can access. An application
  354. skeleton usually will look like (assuming it is a filter - read GIF file,
  355. modifies it, and write new GIF file) the following example, which only copy a
  356. GIF file from stdin to stdout. Please give a pick to the utilities on the util
  357. directory to get more idea once you fill comfortable with this skeleton. Also
  358. try to follow the coding standards of this package if you want me to
  359. officially add your new utility to it.
  360.  
  361. #include "getarg.h"
  362.  
  363. main(... )
  364. {
  365.     GifFile *GifFileIn, *GifFileOut;
  366.  
  367.     GAGetArgs( argc, argv, CtrlStr, ... );       /* Process command line */
  368.  
  369.     /* Use the stdin as input (note this also read screen descriptor in: */
  370.     if ((GifFileIn = DGifOpenFileHandle(0)) == NULL)
  371.        QuitGifError(GifFileIn, GifFileOut);
  372.  
  373.     /* Use the stdout as output: */
  374.     if ((GifFileOut = EGifOpenFileHandle(1)) == NULL)
  375.        QuitGifError(GifFileIn, GifFileOut);
  376.     /* And dump out its screen information: */
  377.     if (EGifPutScreenDesc(GifFileOut,
  378.        GifFileIn -> SWidth, GifFileIn -> SHeight,
  379.        GifFileIn -> SColorResolution, GifFileIn -> SBackGroundColor,
  380.        GifFileIn -> SBitsPerPixel, GifFileIn -> SColorMap) == ERROR)
  381.        QuitGifError(GifFileIn, GifFileOut);
  382.  
  383.     /* Scan the content of the input GIF file and load the image(s) in: */
  384.     do
  385.     {
  386.        if (DGifGetRecordType(GifFileIn, &RecordType) == ERROR)
  387.            QuitGifError(GifFileIn, GifFileOut);
  388.  
  389.        switch (RecordType)
  390.        {
  391.        case IMAGE_DESC_RECORD_TYPE:
  392.            if (DGifGetImageDesc(GifFileIn) == ERROR)
  393.                QuitGifError(GifFileIn, GifFileOut);
  394.            /* Put image descriptor to out file: */
  395.            if (EGifPutImageDesc(GifFileOut,
  396.                GifFileIn -> ILeft, GifFileIn -> ITop,
  397.                GifFileIn -> IWidth, GifFileIn -> IHeight,
  398.                GifFileIn -> IInterlace, GifFileIn -> IBitsPerPixel,
  399.                GifFileIn -> IColorMap) == ERROR)
  400.                QuitGifError(GifFileIn, GifFileOut);
  401.  
  402.            /* Now read image itself in decoded form as we dont really   */
  403.            /* care what we have there, and this is much faster.         */
  404.            if (DGifGetCode(GifFileIn, &CodeSize, &CodeBlock) == ERROR ||
  405.                EGifPutCode(GifFileOut, CodeSize, CodeBlock) == ERROR)
  406.                QuitGifError(GifFileIn, GifFileOut);
  407.            while (CodeBlock != NULL)
  408.            {
  409.                if (DGifGetCodeNext(GifFileIn, &CodeBlock) == ERROR ||
  410.                    EGifPutCodeNext(GifFileOut, CodeBlock) == ERROR)
  411.                QuitGifError(GifFileIn, GifFileOut);
  412.            }
  413.            break;
  414.        case EXTENSION_RECORD_TYPE:
  415.            /* Skip any extension blocks in file: */
  416.            if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == ERROR)
  417.                QuitGifError(GifFileIn, GifFileOut);
  418.            if (EGifPutExtension(GifFileOut, ExtCode, Extension[0],
  419.                                                    Extension) == ERROR)
  420.                QuitGifError(GifFileIn, GifFileOut);
  421.  
  422.            /* No support to more than one extension blocks, so discard: */
  423.            while (Extension != NULL)
  424.            {
  425.                if (DGifGetExtensionNext(GifFileIn, &Extension) == ERROR)
  426.                    QuitGifError(GifFileIn, GifFileOut);
  427.            }
  428.            break;
  429.        case TERMINATE_RECORD_TYPE:
  430.            break;
  431.        default:                 /* Should be traps by DGifGetRecordType */
  432.            break;
  433.        }
  434.     }
  435.     while (RecordType != TERMINATE_RECORD_TYPE);
  436.  
  437.     if (DGifCloseFile(GifFileIn) == ERROR)
  438.        QuitGifError(GifFileIn, GifFileOut);
  439.     if (EGifCloseFile(GifFileOut) == ERROR)
  440.        QuitGifError(GifFileIn, GifFileOut);
  441. }
  442.  
  443.  
  444. /******************************************************************************
  445. * Close both input and output file (if open), and exit.                          *
  446. ******************************************************************************/
  447. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
  448. {
  449.     PrintGifError();
  450.     if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  451.     if (GifFileOut != NULL) EGifCloseFile(GifFileOut);
  452.     exit(1);
  453. }
  454.