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

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to modify GIF file color map(s). Images are not modified at all.   *
  7. * Options:                                     *
  8. * -s : save screen color map (unless -i - see below), to stdout.         *
  9. * -l colormap.file : substitute given colormap.file colormap into screen     *
  10. *    colormap (unless -i - see below).                         *
  11. * -g correction : apply gamma correction to the screen colormap (unless -i - *
  12. *    see below).                                 *
  13. * -i n : select image number n color map instead of screen map for above     *
  14. *    operations (n = 1 for first image).                     *
  15. * -h : on line help.                                 *
  16. *   All color maps are ascii text files, with one line per color of the      *
  17. * form: index, Red color, Green color, Blue color - all in the range 0..255. *
  18. *   All color maps should be in the exact same length.                 *
  19. ******************************************************************************
  20. * History:                                     *
  21. * 17 Jul 89 - Version 1.0 by Gershon Elber.                     *
  22. *****************************************************************************/
  23.  
  24. #ifdef __MSDOS__
  25. #include <stdlib.h>
  26. #include <alloc.h>
  27. #endif /* __MSDOS__ */
  28.  
  29. #include <math.h>
  30. #include <stdio.h>
  31. #include <ctype.h>
  32. #include <string.h>
  33. #include "gif_lib.h"
  34. #include "getarg.h"
  35.  
  36. #define PROGRAM_NAME    "GifClrMp"
  37.  
  38. #ifdef __MSDOS__
  39. extern unsigned int
  40.     _stklen = 16384;                 /* Increase default stack size. */
  41. #endif /* __MSDOS__ */
  42.  
  43. #ifdef SYSV
  44. static char *VersionStr =
  45.         "Gif library module,\t\tGershon Elber\n\
  46.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  47. static char
  48.     *CtrlStr = "GifClrMp s%- l%-ColorMapFile!s g%-Gamma!F i%-Image#!d h%- GifFile!*s";
  49. #else
  50. static char
  51.     *VersionStr =
  52.     PROGRAM_NAME
  53.     GIF_LIB_VERSION
  54.     "    Gershon Elber,    "
  55.     __DATE__ ",   " __TIME__ "\n"
  56.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  57. static char
  58.     *CtrlStr =
  59.     PROGRAM_NAME
  60.     " s%- l%-ColorMapFile!s g%-Gamma!F i%-Image#!d h%- GifFile!*s";
  61. #endif /* SYSV */
  62.  
  63. static int
  64.     SaveFlag = FALSE,
  65.     LoadFlag = FALSE,
  66.     GammaFlag = FALSE;
  67. static
  68.     double Gamma = 1.0;
  69. static
  70.     FILE *ColorFile = NULL;
  71.  
  72.  
  73. static void ModifyColorMap(GifColorType *ColorMap, int BitsPerPixel);
  74. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
  75.  
  76. /******************************************************************************
  77. * Interpret the command line and scan the given GIF file.              *
  78. ******************************************************************************/
  79. void main(int argc, char **argv)
  80. {
  81.     int    Error, NumFiles, ExtCode, CodeSize, ImageNum = 0,
  82.     ImageNFlag = FALSE, ImageN, HelpFlag = FALSE, HasGIFOutput;
  83.     GifRecordType RecordType;
  84.     GifByteType *Extension, *CodeBlock;
  85.     char **FileName = NULL, *ColorFileName;
  86.     GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
  87.  
  88.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  89.         &SaveFlag, &LoadFlag, &ColorFileName,
  90.         &GammaFlag, &Gamma, &ImageNFlag, &ImageN,
  91.         &HelpFlag, &NumFiles, &FileName)) != FALSE ||
  92.         (NumFiles > 1 && !HelpFlag)) {
  93.     if (Error)
  94.         GAPrintErrMsg(Error);
  95.     else if (NumFiles > 1)
  96.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  97.     GAPrintHowTo(CtrlStr);
  98.     exit(1);
  99.     }
  100.  
  101.     if (HelpFlag) {
  102.     fprintf(stderr, VersionStr);
  103.     GAPrintHowTo(CtrlStr);
  104.     exit(0);
  105.     }
  106.  
  107.     if (SaveFlag && LoadFlag || SaveFlag && GammaFlag || LoadFlag && GammaFlag)
  108.     GIF_EXIT("Can not handle more than one of -s -l or -g at the same time.");
  109.  
  110.     if (NumFiles == 1) {
  111.     if ((GifFileIn = DGifOpenFileName(*FileName)) == NULL)
  112.         QuitGifError(GifFileIn, GifFileOut);
  113.     }
  114.     else {
  115.     /* Use the stdin instead: */
  116.     if ((GifFileIn = DGifOpenFileHandle(0)) == NULL)
  117.         QuitGifError(GifFileIn, GifFileOut);
  118.     }
  119.  
  120.     if (SaveFlag) {
  121.     /* We are dumping out the color map as text file to stdout: */
  122.     ColorFile = stdout;
  123.     }
  124.     else if (LoadFlag) {
  125.     /* We are loading new color map from specified file: */
  126.     if ((ColorFile = fopen(ColorFileName, "rt")) == NULL)
  127.         GIF_EXIT("Failed to open specified color map file.");
  128.     }
  129.  
  130.     if ((HasGIFOutput = (LoadFlag || GammaFlag)) != 0) {
  131.     /* Open stdout for GIF output file: */
  132.     if ((GifFileOut = EGifOpenFileHandle(1)) == NULL)
  133.         QuitGifError(GifFileIn, GifFileOut);
  134.     }
  135.  
  136.     if (!ImageNFlag) {
  137.     /* We are suppose to modify the screen color map, so do it: */
  138.     ModifyColorMap(GifFileIn -> SColorMap, GifFileIn -> SBitsPerPixel);
  139.     if (!HasGIFOutput) {
  140.         /* We can quit here, as we have the color map: */
  141.         if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  142.         fclose(ColorFile);
  143.         exit(0);
  144.     }
  145.     }
  146.     /* And dump out its new possible repositioned screen information: */
  147.     if (HasGIFOutput)
  148.     if (EGifPutScreenDesc(GifFileOut,
  149.         GifFileIn -> SWidth, GifFileIn -> SHeight,
  150.         GifFileIn -> SColorResolution, GifFileIn -> SBackGroundColor,
  151.         GifFileIn -> SBitsPerPixel, GifFileIn -> SColorMap) == GIF_ERROR)
  152.         QuitGifError(GifFileIn, GifFileOut);
  153.  
  154.     /* Scan the content of the GIF file and load the image(s) in: */
  155.     do {
  156.     if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
  157.         QuitGifError(GifFileIn, GifFileOut);
  158.  
  159.     switch (RecordType) {
  160.         case IMAGE_DESC_RECORD_TYPE:
  161.         if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
  162.             QuitGifError(GifFileIn, GifFileOut);
  163.         if (++ImageNum == ImageN && ImageNFlag) {
  164.             /* We are suppose to modify this image color map, do it: */
  165.             ModifyColorMap(GifFileIn -> SColorMap,
  166.                                                   GifFileIn -> SBitsPerPixel);
  167.             if (!HasGIFOutput) {
  168.             /* We can quit here, as we have the color map: */
  169.             if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  170.             fclose(ColorFile);
  171.             exit(0);
  172.             }
  173.         }
  174.         if (HasGIFOutput)
  175.             if (EGifPutImageDesc(GifFileOut,
  176.             GifFileIn -> ILeft, GifFileIn -> ITop,
  177.             GifFileIn -> IWidth, GifFileIn -> IHeight,
  178.             GifFileIn -> IInterlace, GifFileIn -> IBitsPerPixel,
  179.             GifFileIn -> IColorMap) == GIF_ERROR)
  180.             QuitGifError(GifFileIn, GifFileOut);
  181.  
  182.         /* Now read image itself in decoded form as we dont really   */
  183.         /* care what we have there, and this is much faster.         */
  184.         if (DGifGetCode(GifFileIn, &CodeSize, &CodeBlock) == GIF_ERROR)
  185.             QuitGifError(GifFileIn, GifFileOut);
  186.         if (HasGIFOutput)
  187.             if (EGifPutCode(GifFileOut, CodeSize, CodeBlock) == GIF_ERROR)
  188.             QuitGifError(GifFileIn, GifFileOut);
  189.         while (CodeBlock != NULL) {
  190.             if (DGifGetCodeNext(GifFileIn, &CodeBlock) == GIF_ERROR)
  191.             QuitGifError(GifFileIn, GifFileOut);
  192.             if (HasGIFOutput)
  193.             if (EGifPutCodeNext(GifFileOut, CodeBlock) == GIF_ERROR)
  194.                 QuitGifError(GifFileIn, GifFileOut);
  195.         }
  196.         break;
  197.         case EXTENSION_RECORD_TYPE:
  198.         /* Skip any extension blocks in file: */
  199.         if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == GIF_ERROR)
  200.             QuitGifError(GifFileIn, GifFileOut);
  201.         if (HasGIFOutput)
  202.             if (EGifPutExtension(GifFileOut, ExtCode, Extension[0],
  203.                             Extension) == GIF_ERROR)
  204.             QuitGifError(GifFileIn, GifFileOut);
  205.  
  206.         /* No support to more than one extension blocks, so discard: */
  207.         while (Extension != NULL) {
  208.             if (DGifGetExtensionNext(GifFileIn, &Extension) == GIF_ERROR)
  209.             QuitGifError(GifFileIn, GifFileOut);
  210.         }
  211.         break;
  212.         case TERMINATE_RECORD_TYPE:
  213.         break;
  214.         default:            /* Should be traps by DGifGetRecordType. */
  215.         break;
  216.     }
  217.     }
  218.     while (RecordType != TERMINATE_RECORD_TYPE);
  219.  
  220.     if (DGifCloseFile(GifFileIn) == GIF_ERROR)
  221.     QuitGifError(GifFileIn, GifFileOut);
  222.     if (HasGIFOutput)
  223.     if (EGifCloseFile(GifFileOut) == GIF_ERROR)
  224.         QuitGifError(GifFileIn, GifFileOut);
  225. }
  226.  
  227. /******************************************************************************
  228. * Modify the given colormap according to global variables setting.          *
  229. ******************************************************************************/
  230. static void ModifyColorMap(GifColorType *ColorMap, int BitsPerPixel)
  231. {
  232.     int i, Dummy, Red, Green, Blue;
  233.     double Gamma1;
  234.  
  235.     if (SaveFlag) {
  236.     /* Save this color map to ColorFile: */
  237.     for (i = 0; i < (1 << BitsPerPixel); i++)
  238.         fprintf(ColorFile, "%3d %3d %3d %3d\n", i,
  239.         ColorMap[i].Red, ColorMap[i].Green, ColorMap[i].Blue);
  240.     }
  241.     else if (LoadFlag) {
  242.     /* Read the color map in ColorFile into this color map: */
  243.     for (i = 0; i < (1 << BitsPerPixel); i++) {
  244.         if (feof(ColorFile))
  245.         GIF_EXIT("Color file to load color map from, too small.");
  246.         fscanf(ColorFile, "%3d %3d %3d %3d\n", &Dummy, &Red, &Green, &Blue);
  247.         ColorMap[i].Red = Red;
  248.         ColorMap[i].Green = Green;
  249.         ColorMap[i].Blue = Blue;
  250.     }
  251.     }
  252.     else if (GammaFlag) {
  253.     /* Apply gamma correction to this color map: */
  254.     Gamma1 = 1.0 / Gamma;
  255.     for (i = 0; i < (1 << BitsPerPixel); i++) {
  256.         ColorMap[i].Red =
  257.         ((int) (255 * pow(ColorMap[i].Red / 255.0, Gamma1)));
  258.         ColorMap[i].Green =
  259.         ((int) (255 * pow(ColorMap[i].Green / 255.0, Gamma1)));
  260.         ColorMap[i].Blue =
  261.         ((int) (255 * pow(ColorMap[i].Blue / 255.0, Gamma1)));
  262.     }
  263.     }
  264.     else
  265.     GIF_EXIT("Nothing to do!");
  266. }
  267.  
  268. /******************************************************************************
  269. * Close both input and output file (if open), and exit.                  *
  270. ******************************************************************************/
  271. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
  272. {
  273.     PrintGifError();
  274.     if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  275.     if (GifFileOut != NULL) EGifCloseFile(GifFileOut);
  276.     exit(1);
  277. }
  278.  
  279.