home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / giflib11 / util / gifasm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-03  |  11.1 KB  |  314 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to assemble/disassemble GIF files: disassembles multi image file   *
  7. * into seperated files, or assembles few single image GIF files into one.    *
  8. * Options:                                     *
  9. * -a : assemble few files into one (default)                     *
  10. * -d name : disassmble given GIF file into seperate files derived from name. *
  11. * -h : on line help.                                 *
  12. ******************************************************************************
  13. * History:                                     *
  14. * 7 Jul 89 - Version 1.0 by Gershon Elber.                     *
  15. *****************************************************************************/
  16.  
  17. #ifdef __MSDOS__
  18. #include <stdlib.h>
  19. #include <alloc.h>
  20. #endif /* __MSDOS__ */
  21.  
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include <string.h>
  25. #include "gif_lib.h"
  26. #include "getarg.h"
  27.  
  28. #define PROGRAM_NAME    "GifAsm"
  29.  
  30. #ifdef __MSDOS__
  31. extern unsigned int
  32.     _stklen = 16384;                 /* Increase default stack size. */
  33. #endif /* __MSDOS__ */
  34.  
  35. #ifdef SYSV
  36. static char *VersionStr =
  37.         "Gif library module,\t\tGershon Elber\n\
  38.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  39. static char
  40.     *CtrlStr = "GifAsm a%- d%-OutFileName!s h%- GifFile(s)!*s";
  41. #else
  42. static char
  43.     *VersionStr =
  44.     PROGRAM_NAME
  45.     GIF_LIB_VERSION
  46.     "    Gershon Elber,    "
  47.     __DATE__ ",   " __TIME__ "\n"
  48.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  49. static char
  50.     *CtrlStr =
  51.     PROGRAM_NAME
  52.     " a%- d%-OutFileName!s h%- GifFile(s)!*s";
  53. #endif /* SYSV */
  54.  
  55. static int
  56.     AsmFlag = FALSE;
  57.  
  58. static void DoAssembly(int NumFiles, char **FileNames);
  59. static void DoDisassembly(char *InFileName, char *OutFileName);
  60. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
  61.  
  62. /******************************************************************************
  63. * Interpret the command line and scan the given GIF file.              *
  64. ******************************************************************************/
  65. void main(int argc, char **argv)
  66. {
  67.     int    Error, NumFiles, DisasmFlag = FALSE, HelpFlag = FALSE;
  68.     char **FileNames = NULL, *OutFileName;
  69.  
  70.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  71.         &AsmFlag, &DisasmFlag, &OutFileName,
  72.         &HelpFlag, &NumFiles, &FileNames)) != FALSE) {
  73.     GAPrintErrMsg(Error);
  74.     GAPrintHowTo(CtrlStr);
  75.     exit(1);
  76.     }
  77.  
  78.     if (HelpFlag) {
  79.     fprintf(stderr, VersionStr);
  80.     GAPrintHowTo(CtrlStr);
  81.     exit(0);
  82.     }
  83.  
  84.     if (!AsmFlag && !DisasmFlag) AsmFlag = TRUE; /* Make default - assemble. */
  85.     if (AsmFlag && NumFiles < 2) {
  86.     GIF_MESSAGE("At list two GIF files are required to assembly operation.");
  87.     GAPrintHowTo(CtrlStr);
  88.     exit(1);
  89.     }
  90.     if (!AsmFlag && NumFiles > 1) {
  91.     GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  92.     GAPrintHowTo(CtrlStr);
  93.     exit(1);
  94.     }
  95.  
  96.     if (AsmFlag)
  97.         DoAssembly(NumFiles, FileNames);
  98.     else
  99.     DoDisassembly(NumFiles == 1 ? *FileNames : NULL, OutFileName);
  100. }
  101.  
  102. /******************************************************************************
  103. * Perform the assembly operation - take few input files into one output.      *
  104. ******************************************************************************/
  105. static void DoAssembly(int NumFiles, char **FileNames)
  106. {
  107.     int    i, ExtCode, CodeSize;
  108.     GifRecordType RecordType;
  109.     GifByteType *Extension, *CodeBlock;
  110.     GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
  111.  
  112.     /* Open stdout for the output file: */
  113.     if ((GifFileOut = EGifOpenFileHandle(1)) == NULL)
  114.     QuitGifError(GifFileIn, GifFileOut);
  115.  
  116.     /* Scan the content of the GIF file and load the image(s) in: */
  117.     for (i = 0; i < NumFiles; i++) {
  118.     if ((GifFileIn = DGifOpenFileName(FileNames[i])) == NULL)
  119.         QuitGifError(GifFileIn, GifFileOut);
  120.  
  121.     /* And dump out screen descriptor iff its first image.    */
  122.     if (i == 0)
  123.         if (EGifPutScreenDesc(GifFileOut,
  124.         GifFileIn -> SWidth, GifFileIn -> SHeight,
  125.         GifFileIn -> SColorResolution, GifFileIn -> SBackGroundColor,
  126.         GifFileIn -> SBitsPerPixel, GifFileIn -> SColorMap) == GIF_ERROR)
  127.         QuitGifError(GifFileIn, GifFileOut);
  128.  
  129.     do {
  130.         if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
  131.         QuitGifError(GifFileIn, GifFileOut);
  132.  
  133.         switch (RecordType) {
  134.         case IMAGE_DESC_RECORD_TYPE:
  135.             if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
  136.             QuitGifError(GifFileIn, GifFileOut);
  137.             /* Put image descriptor to out file: */
  138.             if (EGifPutImageDesc(GifFileOut,
  139.             GifFileIn -> ILeft, GifFileIn -> ITop,
  140.             GifFileIn -> IWidth, GifFileIn -> IHeight,
  141.             GifFileIn -> IInterlace, GifFileIn -> IBitsPerPixel,
  142.             GifFileIn -> IColorMap) == GIF_ERROR)
  143.             QuitGifError(GifFileIn, GifFileOut);
  144.  
  145.             /* Now read image itself in decoded form as we dont      */
  146.             /* dont care what is there, and this is much faster.     */
  147.             if (DGifGetCode(GifFileIn, &CodeSize, &CodeBlock) == GIF_ERROR
  148.              || EGifPutCode(GifFileOut, CodeSize, CodeBlock) == GIF_ERROR)
  149.             QuitGifError(GifFileIn, GifFileOut);
  150.             while (CodeBlock != NULL)
  151.             if (DGifGetCodeNext(GifFileIn, &CodeBlock) == GIF_ERROR ||
  152.                 EGifPutCodeNext(GifFileOut, CodeBlock) == GIF_ERROR)
  153.                 QuitGifError(GifFileIn, GifFileOut);
  154.             break;
  155.         case EXTENSION_RECORD_TYPE:
  156.             /* Skip any extension blocks in file: */
  157.             if (DGifGetExtension(GifFileIn, &ExtCode, &Extension)
  158.             == GIF_ERROR)
  159.             QuitGifError(GifFileIn, GifFileOut);
  160.             if (EGifPutExtension(GifFileOut, ExtCode, Extension[0],
  161.                                Extension) == GIF_ERROR)
  162.             QuitGifError(GifFileIn, GifFileOut);
  163.  
  164.             /* No support to more than one extension blocks, discard.*/
  165.             while (Extension != NULL)
  166.             if (DGifGetExtensionNext(GifFileIn, &Extension)
  167.                 == GIF_ERROR)
  168.                 QuitGifError(GifFileIn, GifFileOut);
  169.             break;
  170.         case TERMINATE_RECORD_TYPE:
  171.             break;
  172.         default:        /* Should be traps by DGifGetRecordType. */
  173.             break;
  174.         }
  175.     }
  176.     while (RecordType != TERMINATE_RECORD_TYPE);
  177.  
  178.     if (DGifCloseFile(GifFileIn) == GIF_ERROR)
  179.         QuitGifError(GifFileIn, GifFileOut);
  180.     }
  181.  
  182.     if (EGifCloseFile(GifFileOut) == GIF_ERROR)
  183.     QuitGifError(GifFileIn, GifFileOut);
  184. }
  185.  
  186. /******************************************************************************
  187. * Perform the disassembly operation - take one input files into few output.   *
  188. ******************************************************************************/
  189. static void DoDisassembly(char *InFileName, char *OutFileName)
  190. {
  191.     int    i, ExtCode, CodeSize, FileNum = 0, FileEmpty;
  192.     GifRecordType RecordType;
  193.     char CrntFileName[80], *p;
  194.     GifByteType *Extension, *CodeBlock;
  195.     GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
  196.  
  197.     /* If name has type postfix, strip it out, and make sure name is less    */
  198.     /* or equal to 6 chars, so we will have 2 chars in name for numbers.     */
  199.     for (i = 0; i < strlen(OutFileName);  i++)/* Make sure all is upper case.*/
  200.     if (islower(OutFileName[i]))
  201.         OutFileName[i] = toupper(OutFileName[i]);
  202.  
  203.     if ((p = strrchr(OutFileName, '.')) != NULL && strlen(p) <= 4) p[0] = 0;
  204.     if ((p = strrchr(OutFileName, '/')) != NULL ||
  205.     (p = strrchr(OutFileName, '\\')) != NULL ||
  206.     (p = strrchr(OutFileName, ':')) != NULL) {
  207.     if (strlen(p) > 7) p[7] = 0;  /* p includes the '/', '\\', ':' char. */
  208.     }
  209.     else {
  210.     /* Only name is given for current directory: */
  211.     if (strlen(OutFileName) > 6) OutFileName[6] = 0;
  212.     }
  213.  
  214.     /* Open input file: */
  215.     if (InFileName != NULL) {
  216.     if ((GifFileIn = DGifOpenFileName(InFileName)) == NULL)
  217.         QuitGifError(GifFileIn, GifFileOut);
  218.     }
  219.     else {
  220.     /* Use the stdin instead: */
  221.     if ((GifFileIn = DGifOpenFileHandle(0)) == NULL)
  222.         QuitGifError(GifFileIn, GifFileOut);
  223.     }
  224.  
  225.     /* Scan the content of GIF file and dump image(s) to seperate file(s): */
  226.     do {
  227.     sprintf(CrntFileName, "%s%02d.gif", OutFileName, FileNum++);
  228.     if ((GifFileOut = EGifOpenFileName(CrntFileName, TRUE)) == NULL)
  229.         QuitGifError(GifFileIn, GifFileOut);
  230.     FileEmpty = TRUE;
  231.  
  232.     /* And dump out its exactly same screen information: */
  233.     if (EGifPutScreenDesc(GifFileOut,
  234.         GifFileIn -> SWidth, GifFileIn -> SHeight,
  235.         GifFileIn -> SColorResolution, GifFileIn -> SBackGroundColor,
  236.         GifFileIn -> SBitsPerPixel, GifFileIn -> SColorMap) == GIF_ERROR)
  237.         QuitGifError(GifFileIn, GifFileOut);
  238.  
  239.     do {
  240.         if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
  241.         QuitGifError(GifFileIn, GifFileOut);
  242.  
  243.         switch (RecordType) {
  244.         case IMAGE_DESC_RECORD_TYPE:
  245.             FileEmpty = FALSE;
  246.             if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
  247.             QuitGifError(GifFileIn, GifFileOut);
  248.             /* Put same image descriptor to out file: */
  249.             if (EGifPutImageDesc(GifFileOut,
  250.             GifFileIn -> ILeft, GifFileIn -> ITop,
  251.             GifFileIn -> IWidth, GifFileIn -> IHeight,
  252.             GifFileIn -> IInterlace, GifFileIn -> IBitsPerPixel,
  253.             GifFileIn -> IColorMap) == GIF_ERROR)
  254.             QuitGifError(GifFileIn, GifFileOut);
  255.  
  256.             /* Now read image itself in decoded form as we dont      */
  257.             /* really care what is there, and this is much faster.   */
  258.             if (DGifGetCode(GifFileIn, &CodeSize, &CodeBlock) == GIF_ERROR
  259.              || EGifPutCode(GifFileOut, CodeSize, CodeBlock) == GIF_ERROR)
  260.             QuitGifError(GifFileIn, GifFileOut);
  261.             while (CodeBlock != NULL)
  262.             if (DGifGetCodeNext(GifFileIn, &CodeBlock) == GIF_ERROR ||
  263.                 EGifPutCodeNext(GifFileOut, CodeBlock) == GIF_ERROR)
  264.             QuitGifError(GifFileIn, GifFileOut);
  265.             break;
  266.         case EXTENSION_RECORD_TYPE:
  267.             FileEmpty = FALSE;
  268.             /* Skip any extension blocks in file: */
  269.             if (DGifGetExtension(GifFileIn, &ExtCode, &Extension)
  270.             == GIF_ERROR)
  271.             QuitGifError(GifFileIn, GifFileOut);
  272.             if (EGifPutExtension(GifFileOut, ExtCode, Extension[0],
  273.                             Extension) == GIF_ERROR)
  274.             QuitGifError(GifFileIn, GifFileOut);
  275.  
  276.             /* No support to more than one extension blocks, discard.*/
  277.             while (Extension != NULL)
  278.             if (DGifGetExtensionNext(GifFileIn, &Extension)
  279.                 == GIF_ERROR)
  280.                 QuitGifError(GifFileIn, GifFileOut);
  281.             break;
  282.         case TERMINATE_RECORD_TYPE:
  283.             break;
  284.         default:        /* Should be traps by DGifGetRecordType. */
  285.             break;
  286.         }
  287.     }
  288.     while (RecordType != IMAGE_DESC_RECORD_TYPE &&
  289.            RecordType != TERMINATE_RECORD_TYPE);
  290.  
  291.     if (EGifCloseFile(GifFileOut) == GIF_ERROR)
  292.         QuitGifError(GifFileIn, GifFileOut);
  293.     if (FileEmpty) {
  294.         /* Might happen on last file - delete it if so: */
  295.         unlink(CrntFileName);
  296.     }
  297.    }
  298.     while (RecordType != TERMINATE_RECORD_TYPE);
  299.  
  300.     if (DGifCloseFile(GifFileIn) == GIF_ERROR)
  301.     QuitGifError(GifFileIn, GifFileOut);
  302. }
  303.  
  304. /******************************************************************************
  305. * Close both input and output file (if open), and exit.                  *
  306. ******************************************************************************/
  307. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
  308. {
  309.     PrintGifError();
  310.     if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  311.     if (GifFileOut != NULL) EGifCloseFile(GifFileOut);
  312.     exit(1);
  313. }
  314.