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

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to reposition GIF file, by changing screen size and/or image(s)    *
  7. * position. Neither image(s) not colormap(s) are changed by any mean.         *
  8. * Options:                                     *
  9. * -s w h : set new screen size - width & height.                 *
  10. * -i t l : set new image position - top & left (for first image).         *
  11. * -n n w h : set new image position - top & left (for image number n).         *
  12. * -h : on line help.                                 *
  13. ******************************************************************************
  14. * History:                                     *
  15. * 6 Jul 89 - Version 1.0 by Gershon Elber.                     *
  16. *****************************************************************************/
  17.  
  18. #ifdef __MSDOS__
  19. #include <stdlib.h>
  20. #include <alloc.h>
  21. #endif /* __MSDOS__ */
  22.  
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26. #include "gif_lib.h"
  27. #include "getarg.h"
  28.  
  29. #define PROGRAM_NAME    "GifPos"
  30.  
  31. #ifdef __MSDOS__
  32. extern unsigned int
  33.     _stklen = 16384;                 /* Increase default stack size. */
  34. #endif /* __MSDOS__ */
  35.  
  36. #ifdef SYSV
  37. static char *VersionStr =
  38.         "Gif library module,\t\tGershon Elber\n\
  39.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  40. static char
  41.     *CtrlStr = "GifPos s%-Width|Height!d!d i%-Left|Top!d!d n%-n|Left|Top!d!d!d h%- GifFile!*s";
  42. #else
  43. static char
  44.     *VersionStr =
  45.     PROGRAM_NAME
  46.     GIF_LIB_VERSION
  47.     "    Gershon Elber,    "
  48.     __DATE__ ",   " __TIME__ "\n"
  49.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  50. static char
  51.     *CtrlStr =
  52.     PROGRAM_NAME
  53.     " s%-Width|Height!d!d i%-Left|Top!d!d n%-n|Left|Top!d!d!d h%- GifFile!*s";
  54. #endif /* SYSV */
  55.  
  56. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
  57.  
  58. /******************************************************************************
  59. * Interpret the command line and scan the given GIF file.              *
  60. ******************************************************************************/
  61. void main(int argc, char **argv)
  62. {
  63.     int    Error, NumFiles, ExtCode, CodeSize, ImageNum = 0,
  64.     ScreenFlag = FALSE, ScreenWidth, ScreenHeight,
  65.     ImageFlag = FALSE, ImageLeft, ImageTop,
  66.     ImageNFlag = FALSE, ImageN, ImageNLeft, ImageNTop,
  67.     HelpFlag = FALSE;
  68.     GifRecordType RecordType;
  69.     GifByteType *Extension, *CodeBlock;
  70.     char **FileName = NULL;
  71.     GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
  72.  
  73.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  74.         &ScreenFlag, &ScreenWidth, &ScreenHeight,
  75.         &ImageFlag, &ImageLeft, &ImageTop,
  76.         &ImageNFlag, &ImageN, &ImageNLeft, &ImageNTop,
  77.         &HelpFlag, &NumFiles, &FileName)) != FALSE ||
  78.         (NumFiles > 1 && !HelpFlag)) {
  79.     if (Error)
  80.         GAPrintErrMsg(Error);
  81.     else if (NumFiles > 1)
  82.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  83.     GAPrintHowTo(CtrlStr);
  84.     exit(1);
  85.     }
  86.  
  87.     if (HelpFlag) {
  88.     fprintf(stderr, VersionStr);
  89.     GAPrintHowTo(CtrlStr);
  90.     exit(0);
  91.     }
  92.  
  93.     if (NumFiles == 1) {
  94.     if ((GifFileIn = DGifOpenFileName(*FileName)) == NULL)
  95.         QuitGifError(GifFileIn, GifFileOut);
  96.     }
  97.     else {
  98.     /* Use the stdin instead: */
  99.     if ((GifFileIn = DGifOpenFileHandle(0)) == NULL)
  100.         QuitGifError(GifFileIn, GifFileOut);
  101.     }
  102.  
  103.     /* Open stdout for the output file: */
  104.     if ((GifFileOut = EGifOpenFileHandle(1)) == NULL)
  105.     QuitGifError(GifFileIn, GifFileOut);
  106.  
  107.     /* And dump out its new possible repositioned screen information: */
  108.     if (EGifPutScreenDesc(GifFileOut,
  109.     ScreenFlag ? ScreenWidth : GifFileIn -> SWidth,
  110.     ScreenFlag ? ScreenHeight : GifFileIn -> SHeight,
  111.     GifFileIn -> SColorResolution, GifFileIn -> SBackGroundColor,
  112.     GifFileIn -> SBitsPerPixel, GifFileIn -> SColorMap) == GIF_ERROR)
  113.     QuitGifError(GifFileIn, GifFileOut);
  114.  
  115.     /* Scan the content of the GIF file and load the image(s) in: */
  116.     do {
  117.     if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
  118.         QuitGifError(GifFileIn, GifFileOut);
  119.  
  120.     switch (RecordType) {
  121.         case IMAGE_DESC_RECORD_TYPE:
  122.         if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
  123.             QuitGifError(GifFileIn, GifFileOut);
  124.         /* Put possibly repositioned image descriptor to out file: */
  125.         if (++ImageNum == 1 && ImageFlag) {
  126.             /* First image should be repositioned: */
  127.             if (EGifPutImageDesc(GifFileOut,
  128.             ImageLeft, ImageTop,
  129.             GifFileIn -> IWidth, GifFileIn -> IHeight,
  130.             GifFileIn -> IInterlace, GifFileIn -> IBitsPerPixel,
  131.             GifFileIn -> IColorMap) == GIF_ERROR)
  132.             QuitGifError(GifFileIn, GifFileOut);
  133.         }
  134.         else if (ImageNum == ImageN && ImageNFlag) {
  135.             /* Image N should be repositioned: */
  136.             if (EGifPutImageDesc(GifFileOut,
  137.             ImageNLeft, ImageNTop,
  138.             GifFileIn -> IWidth, GifFileIn -> IHeight,
  139.             GifFileIn -> IInterlace, GifFileIn -> IBitsPerPixel,
  140.             GifFileIn -> IColorMap) == GIF_ERROR)
  141.             QuitGifError(GifFileIn, GifFileOut);
  142.         }
  143.         else {
  144.             /* No repositioning for this image: */
  145.             if (EGifPutImageDesc(GifFileOut,
  146.             GifFileIn -> ILeft, GifFileIn -> ITop,
  147.             GifFileIn -> IWidth, GifFileIn -> IHeight,
  148.             GifFileIn -> IInterlace, GifFileIn -> IBitsPerPixel,
  149.             GifFileIn -> IColorMap) == GIF_ERROR)
  150.             QuitGifError(GifFileIn, GifFileOut);
  151.         }
  152.  
  153.         /* Now read image itself in decoded form as we dont really   */
  154.         /* care what we have there, and this is much faster.         */
  155.         if (DGifGetCode(GifFileIn, &CodeSize, &CodeBlock) == GIF_ERROR ||
  156.             EGifPutCode(GifFileOut, CodeSize, CodeBlock) == GIF_ERROR)
  157.             QuitGifError(GifFileIn, GifFileOut);
  158.         while (CodeBlock != NULL) {
  159.             if (DGifGetCodeNext(GifFileIn, &CodeBlock) == GIF_ERROR ||
  160.             EGifPutCodeNext(GifFileOut, CodeBlock) == GIF_ERROR)
  161.             QuitGifError(GifFileIn, GifFileOut);
  162.         }
  163.         break;
  164.         case EXTENSION_RECORD_TYPE:
  165.         /* Skip any extension blocks in file: */
  166.         if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == GIF_ERROR)
  167.             QuitGifError(GifFileIn, GifFileOut);
  168.         if (EGifPutExtension(GifFileOut, ExtCode, Extension[0],
  169.                             Extension) == GIF_ERROR)
  170.             QuitGifError(GifFileIn, GifFileOut);
  171.  
  172.         /* No support to more than one extension blocks, so discard: */
  173.         while (Extension != NULL) {
  174.             if (DGifGetExtensionNext(GifFileIn, &Extension) == GIF_ERROR)
  175.             QuitGifError(GifFileIn, GifFileOut);
  176.         }
  177.         break;
  178.         case TERMINATE_RECORD_TYPE:
  179.         break;
  180.         default:            /* Should be traps by DGifGetRecordType. */
  181.         break;
  182.     }
  183.     }
  184.     while (RecordType != TERMINATE_RECORD_TYPE);
  185.  
  186.     if (DGifCloseFile(GifFileIn) == GIF_ERROR)
  187.     QuitGifError(GifFileIn, GifFileOut);
  188.     if (EGifCloseFile(GifFileOut) == GIF_ERROR)
  189.     QuitGifError(GifFileIn, GifFileOut);
  190. }
  191.  
  192. /******************************************************************************
  193. * Close both input and output file (if open), and exit.                  *
  194. ******************************************************************************/
  195. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
  196. {
  197.     PrintGifError();
  198.     if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  199.     if (GifFileOut != NULL) EGifCloseFile(GifFileOut);
  200.     exit(1);
  201. }
  202.