home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / graphics / gif-util.zip / GIFCLRMP.C < prev    next >
C/C++ Source or Header  |  1989-08-01  |  10KB  |  274 lines

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