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

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to clip an image and dump out only portion of it.             *
  7. * Options:                                     *
  8. * -i left top width bottom : clipping information for first image.         *
  9. * -n n left top width bottom : clipping information for nth image.         *
  10. * -h : on line help                                 *
  11. ******************************************************************************
  12. * History:                                     *
  13. * 8 Jul 89 - Version 1.0 by Gershon Elber.                     *
  14. *****************************************************************************/
  15.  
  16. #ifdef __MSDOS__
  17. #include <stdlib.h>
  18. #include <alloc.h>
  19. #endif /* __MSDOS__ */
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include "gif_lib.h"
  25. #include "getarg.h"
  26.  
  27. #define PROGRAM_NAME    "GifClip"
  28.  
  29. #ifdef __MSDOS__
  30. extern unsigned int
  31.     _stklen = 16384;                 /* Increase default stack size. */
  32. #endif /* __MSDOS__ */
  33.  
  34. #ifdef SYSV
  35. static char *VersionStr =
  36.         "Gif library module,\t\tGershon Elber\n\
  37.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  38. static char
  39.     *CtrlStr = "GifClip i%-Xmin|Ymin|Xmax|Ymax!d!d!d!d n%-n|Xmin|Ymin|Xmax|Ymax!d!d!d!d!d h%- GifFile!*s";
  40. #else
  41. static char
  42.     *VersionStr =
  43.     PROGRAM_NAME
  44.     GIF_LIB_VERSION
  45.     "    Gershon Elber,    "
  46.     __DATE__ ",   " __TIME__ "\n"
  47.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  48. static char
  49.     *CtrlStr =
  50.     PROGRAM_NAME
  51.     " i%-Xmin|Ymin|Xmax|Ymax!d!d!d!d n%-n|Xmin|Ymin|Xmax|Ymax!d!d!d!d!d h%- GifFile!*s";
  52. #endif /* SYSV */
  53.  
  54. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
  55.  
  56. /******************************************************************************
  57. * Interpret the command line and scan the given GIF file.              *
  58. ******************************************************************************/
  59. void main(int argc, char **argv)
  60. {
  61.     int    i, Error, NumFiles, ExtCode, CodeSize, ImageNum = 0,
  62.     ImageFlag = FALSE, ImageNFlag = FALSE, ImageN, ImageX1, ImageY1,
  63.     ImageX2, ImageY2, ImageWidth, HelpFlag = FALSE;
  64.     GifRecordType RecordType;
  65.     GifByteType *Extension, *CodeBlock;
  66.     char **FileName = NULL;
  67.     GifRowType Line;
  68.     GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
  69.  
  70.     /* Same image dimension vars for both Image & ImageN as only one allowed.*/
  71.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  72.         &ImageFlag, &ImageX1, &ImageY1, &ImageX2, &ImageY2,
  73.         &ImageNFlag, &ImageN, &ImageX1, &ImageY1, &ImageX2, &ImageY2,
  74.         &HelpFlag, &NumFiles, &FileName)) != FALSE ||
  75.         (NumFiles > 1 && !HelpFlag)) {
  76.     if (Error)
  77.         GAPrintErrMsg(Error);
  78.     else if (NumFiles > 1)
  79.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  80.     GAPrintHowTo(CtrlStr);
  81.     exit(1);
  82.     }
  83.  
  84.     if (HelpFlag) {
  85.     fprintf(stderr, VersionStr);
  86.     GAPrintHowTo(CtrlStr);
  87.     exit(0);
  88.     }
  89.  
  90.     /* Test to make sure exactly one of ImageFlag & ImageNFlag is set: */
  91.     if ((ImageFlag && ImageNFlag) || (!ImageFlag && !ImageNFlag)) {
  92.     GIF_MESSAGE("Exactly one of [-i ...] && [-n ...] please.");
  93.     GAPrintHowTo(CtrlStr);
  94.     exit(1);
  95.     }
  96.     if (ImageFlag) ImageN = 1;            /* Its first image we are after. */
  97.  
  98.     /* Make sure the first coordinates of clipping box are smaller: */
  99.     if (ImageX1 > ImageX2) {
  100.     i = ImageX1;
  101.     ImageX1 = ImageX2;
  102.     ImageX2 = i;
  103.     }
  104.     if (ImageY1 > ImageY2) {
  105.     i = ImageX1;
  106.     ImageY1 = ImageY2;
  107.     ImageY2 = i;
  108.     }
  109.     ImageWidth = ImageX2 - ImageX1 + 1;           /* Width of clipped image. */
  110.  
  111.     if (NumFiles == 1) {
  112.     if ((GifFileIn = DGifOpenFileName(*FileName)) == NULL)
  113.         QuitGifError(GifFileIn, GifFileOut);
  114.     }
  115.     else {
  116.     /* Use the stdin instead: */
  117.     if ((GifFileIn = DGifOpenFileHandle(0)) == NULL)
  118.         QuitGifError(GifFileIn, GifFileOut);
  119.     }
  120.  
  121.     /* Open stdout for the output file: */
  122.     if ((GifFileOut = EGifOpenFileHandle(1)) == NULL)
  123.     QuitGifError(GifFileIn, GifFileOut);
  124.  
  125.     /* And dump out exactly same screen information: */
  126.     if (EGifPutScreenDesc(GifFileOut,
  127.     GifFileIn -> SWidth, GifFileIn -> SHeight,
  128.     GifFileIn -> SColorResolution, GifFileIn -> SBackGroundColor,
  129.     GifFileIn -> SBitsPerPixel, GifFileIn -> SColorMap) == GIF_ERROR)
  130.     QuitGifError(GifFileIn, GifFileOut);
  131.  
  132.     /* Scan the content of the GIF file and load the image(s) in: */
  133.     do {
  134.     if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
  135.         QuitGifError(GifFileIn, GifFileOut);
  136.  
  137.     switch (RecordType) {
  138.         case IMAGE_DESC_RECORD_TYPE:
  139.         if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
  140.             QuitGifError(GifFileIn, GifFileOut);
  141.         if (++ImageNum == ImageN) {
  142.             /* We can handle only non interlaced images here: */
  143.             if (GifFileIn -> IInterlace)
  144.             GIF_EXIT("Image to clip is interlaced - use GifInter first.");
  145.  
  146.             /* This is the image we should clip - test sizes and     */
  147.             /* dump out new clipped screen descriptor if o.k.         */
  148.             if (GifFileIn -> IWidth <= ImageX2 ||
  149.             GifFileIn -> IHeight <= ImageY2)
  150.             GIF_EXIT("Image is smaller than given clip dimensions.");
  151.  
  152.             /* Put the image descriptor to out file: */
  153.             if (EGifPutImageDesc(GifFileOut,
  154.             GifFileIn -> ILeft, GifFileIn -> ITop,
  155.             ImageWidth, ImageY2 - ImageY1 + 1,
  156.             FALSE, GifFileIn -> IBitsPerPixel,
  157.             GifFileIn -> IColorMap) == GIF_ERROR)
  158.             QuitGifError(GifFileIn, GifFileOut);
  159.  
  160.             /* o.k. - read the image and clip it: */
  161.             Line = (GifRowType) malloc(GifFileIn -> IWidth *
  162.                             sizeof(GifPixelType));
  163.             fprintf(stderr, "\n%s: Image %d at (%d, %d) [%dx%d]:     ",
  164.             PROGRAM_NAME, ImageNum,
  165.             GifFileIn -> ILeft, GifFileIn -> ITop,
  166.             GifFileIn -> IWidth, GifFileIn -> IHeight);
  167.  
  168.             /* Skip lines below ImageY1: */
  169.             for (i = 0; i < ImageY1; i++) {
  170.             if (DGifGetLine(GifFileIn, Line, GifFileIn -> IWidth)
  171.                 == GIF_ERROR)
  172.                 QuitGifError(GifFileIn, GifFileOut);
  173.             fprintf(stderr, "\b\b\b\b%-4d", i);
  174.             }
  175.  
  176.             /* Clip the lines from ImageY1 to ImageY2 (to X1 - X2): */
  177.             for (i = ImageY1; i <= ImageY2; i++) {
  178.             if (DGifGetLine(GifFileIn, Line, GifFileIn -> IWidth)
  179.                 == GIF_ERROR)
  180.                 QuitGifError(GifFileIn, GifFileOut);
  181.             if (EGifPutLine(GifFileOut, &Line[ImageX1],
  182.                             ImageWidth) == GIF_ERROR)
  183.                 QuitGifError(GifFileIn, GifFileOut);
  184.             fprintf(stderr, "\b\b\b\b%-4d", i);
  185.             }
  186.  
  187.             /* Skip lines above ImageY2: */
  188.             for (i = ImageY2 + 1; i < GifFileIn -> IHeight; i++) {
  189.             if (DGifGetLine(GifFileIn, Line, GifFileIn -> IWidth)
  190.                 == GIF_ERROR)
  191.                 QuitGifError(GifFileIn, GifFileOut);
  192.             fprintf(stderr, "\b\b\b\b%-4d", i);
  193.             }
  194.  
  195.             free((char *) Line);
  196.         }
  197.         else {
  198.             /* Copy the image as is (we dont modify this one): */
  199.             if (EGifPutImageDesc(GifFileOut,
  200.             GifFileIn -> ILeft, GifFileIn -> ITop,
  201.             GifFileIn -> IWidth, GifFileIn -> IHeight,
  202.             GifFileIn -> IInterlace, GifFileIn -> IBitsPerPixel,
  203.             GifFileIn -> IColorMap) == GIF_ERROR)
  204.             QuitGifError(GifFileIn, GifFileOut);
  205.  
  206.             /* Now read image itself in decoded form as we dont      */
  207.             /* really care what is there, and this is much faster.   */
  208.             if (DGifGetCode(GifFileIn, &CodeSize, &CodeBlock) == GIF_ERROR
  209.              || EGifPutCode(GifFileOut, CodeSize, CodeBlock) == GIF_ERROR)
  210.             QuitGifError(GifFileIn, GifFileOut);
  211.             while (CodeBlock != NULL)
  212.             if (DGifGetCodeNext(GifFileIn, &CodeBlock) == GIF_ERROR ||
  213.                 EGifPutCodeNext(GifFileOut, CodeBlock) == GIF_ERROR)
  214.                 QuitGifError(GifFileIn, GifFileOut);
  215.         }
  216.         break;
  217.         case EXTENSION_RECORD_TYPE:
  218.         /* Skip any extension blocks in file: */
  219.         if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == GIF_ERROR)
  220.             QuitGifError(GifFileIn, GifFileOut);
  221.         if (EGifPutExtension(GifFileOut, ExtCode, Extension[0],
  222.                             Extension) == GIF_ERROR)
  223.             QuitGifError(GifFileIn, GifFileOut);
  224.  
  225.         /* No support to more than one extension blocks, so discard: */
  226.         while (Extension != NULL) {
  227.             if (DGifGetExtensionNext(GifFileIn, &Extension) == GIF_ERROR)
  228.             QuitGifError(GifFileIn, GifFileOut);
  229.         }
  230.         break;
  231.         case TERMINATE_RECORD_TYPE:
  232.         break;
  233.         default:            /* Should be traps by DGifGetRecordType. */
  234.         break;
  235.     }
  236.     }
  237.     while (RecordType != TERMINATE_RECORD_TYPE);
  238.  
  239.     if (DGifCloseFile(GifFileIn) == GIF_ERROR)
  240.     QuitGifError(GifFileIn, GifFileOut);
  241.     if (EGifCloseFile(GifFileOut) == GIF_ERROR)
  242.     QuitGifError(GifFileIn, GifFileOut);
  243. }
  244.  
  245. /******************************************************************************
  246. * Close both input and output file (if open), and exit.                  *
  247. ******************************************************************************/
  248. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
  249. {
  250.     PrintGifError();
  251.     if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  252.     if (GifFileOut != NULL) EGifCloseFile(GifFileOut);
  253.     exit(1);
  254. }
  255.  
  256.