home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / graphics / gif-util.zip / GIFBG.C < prev    next >
C/C++ Source or Header  |  1989-08-01  |  11KB  |  352 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 generate back ground image that can be used to replace constant *
  7. * background.                                     *
  8. * Options:                                     *
  9. * -d direction : set direction image should increase intensity.             *
  10. * -l levels : number of color levels.                         *
  11. * -c r g b : colors of the back ground.                         *
  12. * -m min : minimin intensity in percent.                     *
  13. * -M max : maximum intensity in percent.                     *
  14. * -s width height : size of image to create.                     *
  15. * -h : on line help.                                 *
  16. ******************************************************************************
  17. * History:                                     *
  18. * 9 Jul 89 - Version 1.0 by Gershon Elber.                     *
  19. *****************************************************************************/
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24. #include <alloc.h>
  25. #include <string.h>
  26. #include "gif_lib.h"
  27. #include "getarg.h"
  28.  
  29. #define PROGRAM_NAME    "GifBG"
  30. #define VERSION        "ß Version 1.0, "
  31.  
  32. #define DEFAULT_WIDTH    640
  33. #define DEFAULT_HEIGHT    350
  34.  
  35. #define DEFAULT_COLOR_RED    0
  36. #define DEFAULT_COLOR_GREEN    0
  37. #define DEFAULT_COLOR_BLUE    255
  38.  
  39. #define DEFAULT_MIN_INTENSITY    10                   /* In percent */
  40. #define DEFAULT_MAX_INTENSITY    100
  41.  
  42. #define DEFAULT_NUM_LEVELS    16    /* Number of colors to gen the image */
  43.  
  44. enum {                     /* Direction the levels can be changed: */
  45.     DIR_NONE,
  46.     DIR_TOP,
  47.     DIR_TOP_RIGHT,
  48.     DIR_RIGHT,
  49.     DIR_BOT_RIGHT,
  50.     DIR_BOT,
  51.     DIR_BOT_LEFT,
  52.     DIR_LEFT,
  53.     DIR_TOP_LEFT
  54. };
  55.  
  56. #define DEFAULT_DIR    "T"                /* TOP (North) direction */
  57.  
  58. extern unsigned int
  59.     _stklen = 16384;                  /* Increase default stack size */
  60.  
  61. static char
  62.     *VersionStr =
  63.     PROGRAM_NAME
  64.     "    IBMPC "
  65.     VERSION
  66.     "    Gershon Elber,    "
  67.     __DATE__ ",   " __TIME__ "\n"
  68.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  69. static char
  70.     *CtrlStr =
  71.     PROGRAM_NAME
  72.     " d%-Dir!s l%-#Lvls!d c%-R|G|B!d!d!d m%-MinI!d M%-MaxI!d s%-W|H!d!d h%-";
  73. static char
  74.     *ProgramName;
  75. static int
  76.     MaximumIntensity = DEFAULT_MAX_INTENSITY,               /* In percent */
  77.     MinimumIntensity = DEFAULT_MIN_INTENSITY,
  78.     NumLevels = DEFAULT_NUM_LEVELS,
  79.     ImageWidth = DEFAULT_WIDTH,
  80.     ImageHeight = DEFAULT_HEIGHT,
  81.     Direction;
  82. static unsigned int
  83.     RedColor = DEFAULT_COLOR_RED,
  84.     GreenColor = DEFAULT_COLOR_GREEN,
  85.     BlueColor = DEFAULT_COLOR_BLUE;
  86.  
  87. static void QuitGifError(GifFileType *GifFile);
  88.  
  89. /******************************************************************************
  90. * Interpret the command line and scan the given GIF file.              *
  91. ******************************************************************************/
  92. void main(int argc, char **argv)
  93. {
  94.     unsigned int Ratio;
  95.     int    i, j, l, LevelHeight, LevelWidth, Error, LogNumLevels, FlipDir,
  96.     Accumulator, StartX, StepX, Count = 0, DoAllMaximum = FALSE,
  97.     DirectionFlag = FALSE, LevelsFlag = FALSE, ColorFlag = FALSE,
  98.     MinFlag = FALSE, MaxFlag = FALSE, SizeFlag = FALSE, HelpFlag = FALSE;
  99.     PixelType Color;
  100.     char *DirectionStr = DEFAULT_DIR;
  101.     RowType Line;
  102.     GifColorType *ColorMap;
  103.     GifFileType *GifFile;
  104.  
  105.     if (strlen(ProgramName = argv[0]) == 0)            /* DOS 3.x only! */
  106.     ProgramName = PROGRAM_NAME;      /* Do something reasonable for 2.x */
  107.  
  108.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  109.         &DirectionFlag, &DirectionStr, &LevelsFlag, &NumLevels,
  110.         &ColorFlag, &RedColor, &GreenColor, &BlueColor,
  111.         &MinFlag, &MinimumIntensity, &MaxFlag, &MaximumIntensity,
  112.         &SizeFlag, &ImageWidth, &ImageHeight,
  113.         &HelpFlag)) != FALSE) {
  114.     GAPrintErrMsg(Error);
  115.     GAPrintHowTo(CtrlStr);
  116.     exit(1);
  117.     }
  118.  
  119.     if (HelpFlag) {
  120.     fprintf(stderr, VersionStr);
  121.     GAPrintHowTo(CtrlStr);
  122.     exit(0);
  123.     }
  124.  
  125.     /* Make sure intensities are in the right range: */
  126.     if (MinimumIntensity < 0 || MinimumIntensity > 100 ||
  127.     MaximumIntensity < 0 || MaximumIntensity > 100)
  128.     EXIT("Intensities (-m or -M options) are not in [0..100] range (percent)\n");
  129.  
  130.     /* Convert DirectionStr to our local representation: */
  131.     Direction = DIR_NONE;
  132.     FlipDir = FALSE;
  133.     strupr(DirectionStr);
  134.     switch(DirectionStr[0]) {
  135.     case 'T': /* Top or North */
  136.     case 'N':
  137.         if (strlen(DirectionStr) < 2) Direction = DIR_TOP;
  138.         else
  139.         switch(DirectionStr[1]) {
  140.         case 'R':
  141.         case 'E':
  142.             Direction = DIR_TOP_RIGHT;
  143.             break;
  144.         case 'L':
  145.         case 'W':
  146.             Direction = DIR_TOP_LEFT;
  147.             FlipDir = TRUE;
  148.             break;
  149.         }
  150.         break;
  151.     case 'R': /* Right or East */
  152.     case 'E':
  153.         Direction = DIR_RIGHT;
  154.         break;
  155.     case 'B': /* Bottom or South */
  156.     case 'S':
  157.         if (strlen(DirectionStr) < 2) {
  158.         Direction = DIR_BOT;
  159.         FlipDir = TRUE;
  160.         }
  161.         else
  162.         switch(DirectionStr[1]) {
  163.         case 'R':
  164.         case 'E':
  165.             Direction = DIR_BOT_RIGHT;
  166.             break;
  167.         case 'L':
  168.         case 'W':
  169.             Direction = DIR_BOT_LEFT;
  170.             FlipDir = TRUE;
  171.             break;
  172.         }
  173.         break;
  174.     case 'L': /* Left or West */
  175.     case 'W':
  176.         Direction = DIR_LEFT;
  177.         FlipDir = TRUE;
  178.         break;
  179.     }
  180.     if (Direction == DIR_NONE)
  181.     EXIT("Direction requested (-d option) is wierd!\n");
  182.  
  183.     /* We are going to handle only TOP, TOP_RIGHT, RIGHT, BOT_RIGHT  so flip */
  184.     /* the complement cases (TOP <-> BOT for example) by flipping the         */
  185.     /* Color i with color (NumLevels - i - 1).                     */
  186.     if (FlipDir) {
  187.     switch (Direction) {
  188.         case DIR_BOT:
  189.         Direction = DIR_TOP;
  190.         break;
  191.         case DIR_BOT_LEFT:
  192.         Direction = DIR_TOP_RIGHT;
  193.         break;
  194.         case DIR_LEFT:
  195.         Direction = DIR_RIGHT;
  196.         break;
  197.         case DIR_TOP_LEFT:
  198.         Direction = DIR_BOT_RIGHT;
  199.         break;
  200.     }
  201.     }
  202.  
  203.     /* If binary mask is requested (special case): */
  204.     if (MinimumIntensity == 100 && MaximumIntensity == 100 && NumLevels == 2) {
  205.     MinimumIntensity = 0;
  206.     DoAllMaximum = TRUE;
  207.     Direction = DIR_RIGHT;
  208.     }
  209.  
  210.     /* Make sure colors are in the right range: */
  211.     if (RedColor > 255 || GreenColor > 255 || BlueColor > 255)
  212.     EXIT("Colors are not in the ragne [0..255]\n");
  213.  
  214.     /* Make sure the number of levels is power of 2 (up to 8 bits per pixel) */
  215.     for (i=1; i<8; i++) if (NumLevels == (1 << i)) break;
  216.     if (i == 8) EXIT("#Lvls (-l option) is not power of 2\n");
  217.     LogNumLevels = i;
  218.  
  219.     /* Open stdout for the output file: */
  220.     if ((GifFile = EGifOpenFileHandle(1)) == NULL)
  221.     QuitGifError(GifFile);
  222.  
  223.     /* Dump out screen description with given size and generated color map: */
  224.     if ((ColorMap = (GifColorType *) malloc(NumLevels * sizeof(GifColorType)))
  225.     == NULL) EXIT("Failed to allocate memory required, aborted");
  226.  
  227.     for (i=1; i<=NumLevels; i++) {
  228.     /* Ratio will be in the range of 0..100 for required intensity: */
  229.     Ratio = (MaximumIntensity * (i * (256 / NumLevels)) +
  230.          MinimumIntensity * ((NumLevels - i) * (256 / NumLevels))) /
  231.          256;
  232.     ColorMap[i-1].Red   = (RedColor * Ratio) / 100;
  233.     ColorMap[i-1].Green = (GreenColor * Ratio) / 100;
  234.     ColorMap[i-1].Blue  = (BlueColor * Ratio) / 100;
  235.     }
  236.     if (EGifPutScreenDesc(GifFile,
  237.     ImageWidth, ImageHeight, LogNumLevels, 0, LogNumLevels, ColorMap)
  238.     == ERROR)
  239.     QuitGifError(GifFile);
  240.  
  241.     /* Dump out the image descriptor: */
  242.     if (EGifPutImageDesc(GifFile,
  243.     0, 0, ImageWidth, ImageHeight, FALSE, LogNumLevels, NULL) == ERROR)
  244.     QuitGifError(GifFile);
  245.  
  246.     fprintf(stderr, "\n%s: Image 1 at (%d, %d) [%dx%d]:     ",
  247.             ProgramName, GifFile -> ILeft, GifFile -> ITop,
  248.             GifFile -> IWidth, GifFile -> IHeight);
  249.  
  250.     /* Allocate one scan line twice as big as image is as we are going to    */
  251.     /* shift along it, while we dump the scan lines:                 */
  252.     if ((Line = (RowType) malloc(sizeof(PixelType) * ImageWidth * 2)) == NULL)
  253.     EXIT("Failed to allocate memory required, aborted");
  254.  
  255.     if (Direction == DIR_TOP) {
  256.     /* We must evaluate the line each time level is changing: */
  257.     LevelHeight = ImageHeight / NumLevels;
  258.     for (Color=NumLevels, i=l=0; i<ImageHeight; i++) {
  259.         if (i == l) {
  260.         /* Time to update the line to next color level: */
  261.         if (Color != 0) Color--;
  262.         for (j=0; j<ImageWidth; j++)
  263.             Line[j] = (FlipDir ? NumLevels - Color - 1 : Color);
  264.         l += LevelHeight;
  265.         }
  266.         if (EGifPutLine(GifFile, Line, ImageWidth) == ERROR)
  267.         QuitGifError(GifFile);
  268.         fprintf(stderr, "\b\b\b\b%-4d", Count++);
  269.     }
  270.     }
  271.     else if (Direction == DIR_RIGHT) {
  272.     /* We pre-prepare the scan lines as going from color zero to maximum */
  273.     /* color and dump the same scan line Height times:             */
  274.     /* Note this case should handle the Boolean Mask special case.         */
  275.     LevelWidth = ImageWidth / NumLevels;
  276.     if (DoAllMaximum) {
  277.         /* Special case - do all in maximum color: */
  278.         for (i=0; i<ImageWidth; i++) Line[i] = 1;
  279.     }
  280.     else {
  281.         for (Color=i=0, l=LevelWidth; i<ImageWidth; i++, l--) {
  282.         if (l == 0) {
  283.             l = LevelWidth;
  284.             if (Color < NumLevels - 1) Color++;
  285.         }
  286.         Line[i] = (FlipDir ? NumLevels - Color - 1 : Color);
  287.         }
  288.     }
  289.  
  290.     for (i=0; i<ImageHeight; i++) {
  291.         if (EGifPutLine(GifFile, Line, ImageWidth) == ERROR)
  292.         QuitGifError(GifFile);
  293.         fprintf(stderr, "\b\b\b\b%-4d", Count++);
  294.     }
  295.     }
  296.     else {
  297.     /* We are in one of the TOP_RIGHT, BOT_RIGHT cases: we will          */
  298.     /* initialize the Line with its double ImageWidth length from the    */
  299.     /* minimum intensity to the maximum intensity and shift along it     */
  300.     /* while we go along the image height.                     */
  301.     LevelWidth = ImageWidth * 2 / NumLevels;
  302.     for (Color=i=0, l=LevelWidth; i<ImageWidth * 2; i++, l--) {
  303.         if (l == 0) {
  304.         l = LevelWidth;
  305.         if (Color < NumLevels - 1) Color++;
  306.         }
  307.         Line[i] = (FlipDir ? NumLevels - Color - 1 : Color);
  308.     }
  309.     /* We need to implement a DDA to know how much to shift Line while   */
  310.     /* we go down along image height. we set the parameters for it now:  */
  311.     Accumulator = 0;
  312.     switch(Direction) {
  313.         case DIR_TOP_RIGHT:
  314.         StartX = ImageWidth;
  315.         StepX = -1;
  316.         break;
  317.         case DIR_BOT_RIGHT:
  318.         StartX = 0;
  319.         StepX = 1;
  320.         break;
  321.     }
  322.  
  323.     /* Time to dump information out: */
  324.     for (i=0; i<ImageHeight; i++) {
  325.         if (EGifPutLine(GifFile, &Line[StartX], ImageWidth) == ERROR)
  326.         QuitGifError(GifFile);
  327.         fprintf(stderr, "\b\b\b\b%-4d", Count++);
  328.         if ((Accumulator += ImageWidth) > ImageHeight) {
  329.         while (Accumulator > ImageHeight) {
  330.             Accumulator -= ImageHeight;
  331.             StartX += StepX;
  332.         }
  333.         if (Direction < 0) Direction = 0;
  334.         if (Direction > ImageWidth) Direction = ImageWidth;
  335.         }
  336.     }
  337.     }
  338.  
  339.     if (EGifCloseFile(GifFile) == ERROR)
  340.     QuitGifError(GifFile);
  341. }
  342.  
  343. /******************************************************************************
  344. * Close output file (if open), and exit.                      *
  345. ******************************************************************************/
  346. static void QuitGifError(GifFileType *GifFile)
  347. {
  348.     PrintGifError();
  349.     if (GifFile != NULL) DGifCloseFile(GifFile);
  350.     exit(1);
  351. }
  352.