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