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

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to flip interlaced and non interlaced images in GIF files.         *
  7. * Options:                                     *
  8. * -i : Force all images to be intelaced.                     *
  9. * -s : Force all images to be sequencial (non interlaced). This is default.  *
  10. * -h : on line help                                 *
  11. ******************************************************************************
  12. * History:                                     *
  13. * 5 Jul 89 - Version 1.0 by Gershon Elber.                     *
  14. * 21 Dec 89 - Fix problems with -i and -s flags (Version 1.1).               *
  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    "GifInter"
  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 = "GifInter i%- s%- h%- GifFile!*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.     " i%- s%- h%- GifFile!*s";
  53. #endif /* SYSV */
  54.  
  55. /* Make some variables global, so we could access them faster: */
  56. static int
  57.     ImageNum = 0,
  58.     SequencialFlag = FALSE,
  59.     InterlacedFlag = FALSE,
  60.     HelpFlag = FALSE,
  61.     InterlacedOffset[] = { 0, 4, 2, 1 }, /* The way Interlaced image should. */
  62.     InterlacedJumps[] = { 8, 8, 4, 2 };    /* be read - offsets and jumps... */
  63.  
  64. static int LoadImage(GifFileType *GifFile, GifRowType **ImageBuffer);
  65. static int DumpImage(GifFileType *GifFile, GifRowType *ImageBuffer);
  66. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
  67.  
  68. /******************************************************************************
  69. * Interpret the command line and scan the given GIF file.              *
  70. ******************************************************************************/
  71. void main(int argc, char **argv)
  72. {
  73.     int    Error, NumFiles, ExtCode;
  74.     GifRecordType RecordType;
  75.     GifByteType *Extension;
  76.     char **FileName = NULL;
  77.     GifRowType *ImageBuffer;
  78.     GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
  79.  
  80.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  81.         &InterlacedFlag, &SequencialFlag, &HelpFlag,
  82.         &NumFiles, &FileName)) != FALSE ||
  83.         (NumFiles > 1 && !HelpFlag)) {
  84.     if (Error)
  85.         GAPrintErrMsg(Error);
  86.     else if (NumFiles > 1)
  87.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  88.     GAPrintHowTo(CtrlStr);
  89.     exit(1);
  90.     }
  91.  
  92.     if (HelpFlag) {
  93.     fprintf(stderr, VersionStr);
  94.     GAPrintHowTo(CtrlStr);
  95.     exit(0);
  96.     }
  97.  
  98.     if (NumFiles == 1) {
  99.     if ((GifFileIn = DGifOpenFileName(*FileName)) == NULL)
  100.         QuitGifError(GifFileIn, GifFileOut);
  101.     }
  102.     else {
  103.     /* Use the stdin instead: */
  104.     if ((GifFileIn = DGifOpenFileHandle(0)) == NULL)
  105.         QuitGifError(GifFileIn, GifFileOut);
  106.     }
  107.  
  108.     /* Open stdout for the output file: */
  109.     if ((GifFileOut = EGifOpenFileHandle(1)) == NULL)
  110.     QuitGifError(GifFileIn, GifFileOut);
  111.  
  112.     /* And dump out exactly same screen information: */
  113.     if (EGifPutScreenDesc(GifFileOut,
  114.     GifFileIn -> SWidth, GifFileIn -> SHeight,
  115.     GifFileIn -> SColorResolution, GifFileIn -> SBackGroundColor,
  116.     GifFileIn -> SBitsPerPixel, GifFileIn -> SColorMap) == GIF_ERROR)
  117.     QuitGifError(GifFileIn, GifFileOut);
  118.  
  119.     /* Scan the content of the GIF file and load the image(s) in: */
  120.     do {
  121.     if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
  122.         QuitGifError(GifFileIn, GifFileOut);
  123.  
  124.     switch (RecordType) {
  125.         case IMAGE_DESC_RECORD_TYPE:
  126.         if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
  127.             QuitGifError(GifFileIn, GifFileOut);
  128.  
  129.         /* Put the image descriptor to out file: */
  130.         if (EGifPutImageDesc(GifFileOut,
  131.             GifFileIn -> ILeft, GifFileIn -> ITop,
  132.             GifFileIn -> IWidth, GifFileIn -> IHeight,
  133.             InterlacedFlag, GifFileIn -> IBitsPerPixel,
  134.             GifFileIn -> IColorMap) == GIF_ERROR)
  135.             QuitGifError(GifFileIn, GifFileOut);
  136.  
  137.         /* Load the image (either Interlaced or not), and dump it as */
  138.         /* defined in GifFileOut -> IInterlaced.             */
  139.         if (LoadImage(GifFileIn, &ImageBuffer) == GIF_ERROR)
  140.             QuitGifError(GifFileIn, GifFileOut);
  141.         if (DumpImage(GifFileOut, ImageBuffer) == GIF_ERROR)
  142.             QuitGifError(GifFileIn, GifFileOut);
  143.         break;
  144.         case EXTENSION_RECORD_TYPE:
  145.         /* Skip any extension blocks in file: */
  146.         if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == GIF_ERROR)
  147.             QuitGifError(GifFileIn, GifFileOut);
  148.         if (EGifPutExtension(GifFileOut, ExtCode, Extension[0],
  149.                             Extension) == GIF_ERROR)
  150.             QuitGifError(GifFileIn, GifFileOut);
  151.  
  152.         /* No support to more than one extension blocks, so discard: */
  153.         while (Extension != NULL) {
  154.             if (DGifGetExtensionNext(GifFileIn, &Extension) == GIF_ERROR)
  155.             QuitGifError(GifFileIn, GifFileOut);
  156.         }
  157.         break;
  158.         case TERMINATE_RECORD_TYPE:
  159.         break;
  160.         default:            /* Should be traps by DGifGetRecordType. */
  161.         break;
  162.     }
  163.     }
  164.     while (RecordType != TERMINATE_RECORD_TYPE);
  165.  
  166.     if (DGifCloseFile(GifFileIn) == GIF_ERROR)
  167.     QuitGifError(GifFileIn, GifFileOut);
  168.     if (EGifCloseFile(GifFileOut) == GIF_ERROR)
  169.     QuitGifError(GifFileIn, GifFileOut);
  170. }
  171.  
  172. /******************************************************************************
  173. * Routine to read Image out. The image can be Interlaced or None interlaced.  *
  174. * The memory required to hold the image is allocate by the routine itself.    *
  175. * The image is always loaded sequencially into the buffer.              *
  176. * Return GIF_OK if succesful, GIF_ERROR otherwise.                  *
  177. ******************************************************************************/
  178. static int LoadImage(GifFileType *GifFile, GifRowType **ImageBufferPtr)
  179. {
  180.     int Size, i, j, Count;
  181.     GifRowType *ImageBuffer;
  182.  
  183.     /* Allocate the image as vector of column of rows. We cannt allocate     */
  184.     /* the all screen at once, as this broken minded CPU can allocate up to  */
  185.     /* 64k at a time and our image can be bigger than that:             */
  186.     if ((ImageBuffer = (GifRowType *)
  187.     malloc(GifFile -> IHeight * sizeof(GifRowType *))) == NULL)
  188.         GIF_EXIT("Failed to allocate memory required, aborted.");
  189.  
  190.     Size = GifFile -> IWidth * sizeof(GifPixelType);/* One row size in bytes.*/
  191.     for (i = 0; i < GifFile -> IHeight; i++) {
  192.     /* Allocate the rows: */
  193.     if ((ImageBuffer[i] = (GifRowType) malloc(Size)) == NULL)
  194.         GIF_EXIT("Failed to allocate memory required, aborted.");
  195.     }
  196.  
  197.     *ImageBufferPtr = ImageBuffer;
  198.  
  199.     fprintf(stderr, "\n%s: Image %d at (%d, %d) [%dx%d]:     ",
  200.     PROGRAM_NAME, ++ImageNum, GifFile -> ILeft, GifFile -> ITop,
  201.                  GifFile -> IWidth, GifFile -> IHeight);
  202.     if (GifFile -> IInterlace) {
  203.     /* Need to perform 4 passes on the images: */
  204.     for (Count = i = 0; i < 4; i++)
  205.         for (j = InterlacedOffset[i]; j < GifFile -> IHeight;
  206.                          j += InterlacedJumps[i]) {
  207.         fprintf(stderr, "\b\b\b\b%-4d", Count++);
  208.         if (DGifGetLine(GifFile, ImageBuffer[j], GifFile -> IWidth)
  209.             == GIF_ERROR) return GIF_ERROR;
  210.         }
  211.     }
  212.     else {
  213.     for (i = 0; i < GifFile -> IHeight; i++) {
  214.         fprintf(stderr, "\b\b\b\b%-4d", i);
  215.         if (DGifGetLine(GifFile, ImageBuffer[i], GifFile -> IWidth)
  216.         == GIF_ERROR) return GIF_ERROR;
  217.     }
  218.     }
  219.  
  220.     return GIF_OK;
  221. }
  222.  
  223. /******************************************************************************
  224. * Routine to dump image out. The given Image buffer should always hold the    *
  225. * image sequencially. Image will be dumped according to IInterlaced flag in   *
  226. * GifFile structure. Once dumped, the memory holding the image is freed.      *
  227. * Return GIF_OK if succesful, GIF_ERROR otherwise.                  *
  228. ******************************************************************************/
  229. static int DumpImage(GifFileType *GifFile, GifRowType *ImageBuffer)
  230. {
  231.     int i, j, Count;
  232.  
  233.     if (GifFile -> IInterlace) {
  234.     /* Need to perform 4 passes on the images: */
  235.     for (Count = GifFile -> IHeight, i = 0; i < 4; i++)
  236.         for (j = InterlacedOffset[i]; j < GifFile -> IHeight;
  237.                          j += InterlacedJumps[i]) {
  238.         fprintf(stderr, "\b\b\b\b%-4d", Count--);
  239.         if (EGifPutLine(GifFile, ImageBuffer[j], GifFile -> IWidth)
  240.             == GIF_ERROR) return GIF_ERROR;
  241.         }
  242.     }
  243.     else {
  244.     for (Count = GifFile -> IHeight, i = 0; i < GifFile -> IHeight; i++) {
  245.         fprintf(stderr, "\b\b\b\b%-4d", Count--);
  246.         if (EGifPutLine(GifFile, ImageBuffer[i], GifFile -> IWidth)
  247.         == GIF_ERROR) return GIF_ERROR;
  248.     }
  249.     }
  250.  
  251.     /* Free the m emory used for this image: */
  252.     for (i = 0; i < GifFile -> IHeight; i++) free((char *) ImageBuffer[i]);
  253.     free((char *) ImageBuffer);
  254.  
  255.     return GIF_OK;
  256. }
  257.  
  258. /******************************************************************************
  259. * Close both input and output file (if open), and exit.                  *
  260. ******************************************************************************/
  261. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
  262. {
  263.     PrintGifError();
  264.     if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  265.     if (GifFileOut != NULL) EGifCloseFile(GifFileOut);
  266.     exit(1);
  267. }
  268.