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

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