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

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to create histogram of the colors used by the given GIF file.      *
  7. * Dumps out GIF file of constants size GIF_WIDTH by GIF_HEIGHT.             *
  8. * Options:                                     *
  9. * -t : Dump out text instead of GIF - #Colors lines, each with #appearances. *
  10. * -i W H : size of GIF image to generate. Colors of input GIF file are         *
  11. *      spread homogeneously along Height, which better by dividable by the   *
  12. *      number of colors in input image.                         *
  13. * -n n : select image number to generate histogram to (1 by default).         *
  14. * -b : strip off background color count.                     *
  15. * -h : on line help                                 *
  16. ******************************************************************************
  17. * History:                                     *
  18. * 8 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    "GifHisto"
  33.  
  34. #define DEFAULT_HISTO_WIDTH    100          /* Histogram image diemnsions. */
  35. #define DEFAULT_HISTO_HEIGHT    256
  36. #define HISTO_BITS_PER_PIXEL    2    /* Size of bitmap for histogram GIF. */
  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 = "GifHisto t%- s%-Width|Height!d!d n%-ImageNumber!d b%- 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.     " t%- s%-Width|Height!d!d n%-ImageNumber!d b%- h%- GifFile!*s";
  61. #endif /* SYSV */
  62.  
  63. static int
  64.     ImageWidth = DEFAULT_HISTO_WIDTH,
  65.     ImageHeight = DEFAULT_HISTO_HEIGHT,
  66.     ImageN = 1;
  67. static GifColorType
  68.     HistoColorMap[] = {             /* Constant bit map for histograms: */
  69.     { 0, 0, 0 },
  70.     { 255,   0,   0 },
  71.     {   0, 255,   0 },
  72.     {   0,   0, 255 }
  73.     };
  74.  
  75. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
  76.  
  77. /******************************************************************************
  78. * Interpret the command line and scan the given GIF file.              *
  79. ******************************************************************************/
  80. void main(int argc, char **argv)
  81. {
  82.     int    i, j, Size, Error, NumFiles, ExtCode, CodeSize, NumColors = 2, Color,
  83.     Count, ImageNum = 0, TextFlag = FALSE, SizeFlag = FALSE,
  84.     ImageNFlag = FALSE, BackGroundFlag = FALSE, HelpFlag = FALSE;
  85.     long Scaler, Histogram[256];
  86.     GifRecordType RecordType;
  87.     GifByteType *Extension, *CodeBlock;
  88.     char **FileName = NULL;
  89.     GifRowType Line;
  90.     GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
  91.  
  92.     /* Same image dimension vars for both Image & ImageN as only one allowed */
  93.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  94.         &TextFlag, &SizeFlag, &ImageWidth, &ImageHeight,
  95.         &ImageNFlag, &ImageN, &BackGroundFlag,
  96.         &HelpFlag, &NumFiles, &FileName)) != FALSE ||
  97.         (NumFiles > 1 && !HelpFlag)) {
  98.     if (Error)
  99.         GAPrintErrMsg(Error);
  100.     else if (NumFiles > 1)
  101.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  102.     GAPrintHowTo(CtrlStr);
  103.     exit(1);
  104.     }
  105.  
  106.     if (HelpFlag) {
  107.     fprintf(stderr, VersionStr);
  108.     GAPrintHowTo(CtrlStr);
  109.     exit(0);
  110.     }
  111.  
  112.     if (NumFiles == 1) {
  113.     if ((GifFileIn = DGifOpenFileName(*FileName)) == NULL)
  114.         QuitGifError(GifFileIn, GifFileOut);
  115.     }
  116.     else {
  117.     /* Use the stdin instead: */
  118.     if ((GifFileIn = DGifOpenFileHandle(0)) == NULL)
  119.         QuitGifError(GifFileIn, GifFileOut);
  120.     }
  121.  
  122.     for (i = 0; i < 256; i++) Histogram[i] = 0;          /* Reset counters. */
  123.  
  124.     /* Scan the content of the GIF file and load the image(s) in: */
  125.     do {
  126.     if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
  127.         QuitGifError(GifFileIn, GifFileOut);
  128.  
  129.     switch (RecordType) {
  130.         case IMAGE_DESC_RECORD_TYPE:
  131.         if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
  132.             QuitGifError(GifFileIn, GifFileOut);
  133.  
  134.         if (GifFileIn -> IColorMap)
  135.             NumColors = (1 << GifFileIn -> IBitsPerPixel);
  136.         else if (GifFileIn -> SColorMap)
  137.             NumColors = (1 << GifFileIn -> SBitsPerPixel);
  138.         else
  139.             GIF_EXIT("Neither Screen nor Image color map exists.");
  140.  
  141.         if ((ImageHeight / NumColors) * NumColors != ImageHeight)
  142.             GIF_EXIT("Image height specified not dividable by #colors.");
  143.  
  144.         if (++ImageNum == ImageN) {
  145.             /* This is the image we should make histogram for:       */
  146.             Line = (GifRowType) malloc(GifFileIn -> IWidth *
  147.                             sizeof(GifPixelType));
  148.             fprintf(stderr, "\n%s: Image %d at (%d, %d) [%dx%d]:     ",
  149.             PROGRAM_NAME, ImageNum,
  150.             GifFileIn -> ILeft, GifFileIn -> ITop,
  151.             GifFileIn -> IWidth, GifFileIn -> IHeight);
  152.  
  153.             for (i = 0; i < GifFileIn -> IHeight; i++) {
  154.             if (DGifGetLine(GifFileIn, Line, GifFileIn -> IWidth)
  155.                 == GIF_ERROR)
  156.                 QuitGifError(GifFileIn, GifFileOut);
  157.             for (j = 0; j < GifFileIn -> IWidth; j++)
  158.                 Histogram[Line[j]]++;
  159.             fprintf(stderr, "\b\b\b\b%-4d", i);
  160.             }
  161.  
  162.             free((char *) Line);
  163.         }
  164.         else {
  165.             /* Skip the image: */
  166.             /* Now read image itself in decoded form as we dont      */
  167.             /* really care what is there, and this is much faster.   */
  168.             if (DGifGetCode(GifFileIn, &CodeSize, &CodeBlock) == GIF_ERROR)
  169.             QuitGifError(GifFileIn, GifFileOut);
  170.             while (CodeBlock != NULL)
  171.             if (DGifGetCodeNext(GifFileIn, &CodeBlock) == GIF_ERROR)
  172.                 QuitGifError(GifFileIn, GifFileOut);
  173.         }
  174.         break;
  175.         case EXTENSION_RECORD_TYPE:
  176.         /* Skip any extension blocks in file: */
  177.         if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == GIF_ERROR)
  178.             QuitGifError(GifFileIn, GifFileOut);
  179.  
  180.         while (Extension != NULL) {
  181.             if (DGifGetExtensionNext(GifFileIn, &Extension) == GIF_ERROR)
  182.             QuitGifError(GifFileIn, GifFileOut);
  183.         }
  184.         break;
  185.         case TERMINATE_RECORD_TYPE:
  186.         break;
  187.         default:            /* Should be traps by DGifGetRecordType. */
  188.         break;
  189.     }
  190.     }
  191.     while (RecordType != TERMINATE_RECORD_TYPE);
  192.  
  193.     /* We we requested to kill back ground count: */
  194.     if (BackGroundFlag) Histogram[GifFileIn -> SBackGroundColor] = 0;
  195.  
  196.     if (DGifCloseFile(GifFileIn) == GIF_ERROR)
  197.     QuitGifError(GifFileIn, GifFileOut);
  198.  
  199.  
  200.     /* We may required to dump out the histogram as text file: */
  201.     if (TextFlag) {
  202.     for (i = 0; i < NumColors; i++)
  203.         printf("%12ld  %3d\n", Histogram[i], i);
  204.     }
  205.     else {
  206.     /* Open stdout for the histogram output file: */
  207.     if ((GifFileOut = EGifOpenFileHandle(1)) == NULL)
  208.         QuitGifError(GifFileIn, GifFileOut);
  209.  
  210.     /* Dump out screen descriptor to fit histogram dimensions: */
  211.     if (EGifPutScreenDesc(GifFileOut,
  212.         ImageWidth, ImageHeight, HISTO_BITS_PER_PIXEL, 0,
  213.         HISTO_BITS_PER_PIXEL, HistoColorMap) == GIF_ERROR)
  214.         QuitGifError(GifFileIn, GifFileOut);
  215.  
  216.     /* Dump out image descriptor to fit histogram dimensions: */
  217.     if (EGifPutImageDesc(GifFileOut,
  218.         0, 0, ImageWidth, ImageHeight,
  219.         FALSE, HISTO_BITS_PER_PIXEL, NULL) == GIF_ERROR)
  220.         QuitGifError(GifFileIn, GifFileOut);
  221.  
  222.     /* Prepare scan line for histogram file, and find scaler to scale    */
  223.     /* histogram to be between 0 and ImageWidth:                 */
  224.     Line = (GifRowType) malloc(ImageWidth * sizeof(GifPixelType));
  225.     for (Scaler = 0, i = 0; i < NumColors; i++) if (Histogram[i] > Scaler)
  226.         Scaler = Histogram[i];
  227.     Scaler /= ImageWidth;
  228.     if (Scaler == 0) Scaler = 1;  /* In case maximum is less than width. */
  229.  
  230.     /* Dump out the image itself: */
  231.     for (Count = ImageHeight, i = 0, Color = 1; i < NumColors; i++) {
  232.         if ((Size = Histogram[i] / Scaler) > ImageWidth) Size = ImageWidth;
  233.         for (j = 0; j < Size; j++)
  234.         Line[j] = Color;
  235.         for (j = Size; j < ImageWidth; j++)
  236.         Line[j] = GifFileOut -> SBackGroundColor;
  237.  
  238.         /* Move to next color: */
  239.         if (++Color >= (1 << HISTO_BITS_PER_PIXEL)) Color = 1;
  240.  
  241.         /* Dump this histogram entry as many times as required: */
  242.         for (j = 0; j < ImageHeight / NumColors; j++) {
  243.         if (EGifPutLine(GifFileOut, Line, ImageWidth) == GIF_ERROR)
  244.             QuitGifError(GifFileIn, GifFileOut);
  245.         fprintf(stderr, "\b\b\b\b%-4d", Count--);
  246.         }
  247.     }
  248.  
  249.     if (EGifCloseFile(GifFileOut) == GIF_ERROR)
  250.         QuitGifError(GifFileIn, GifFileOut);
  251.     }
  252. }
  253.  
  254. /******************************************************************************
  255. * Close both input and output file (if open), and exit.                  *
  256. ******************************************************************************/
  257. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
  258. {
  259.     PrintGifError();
  260.     if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  261.     if (GifFileOut != NULL) EGifCloseFile(GifFileOut);
  262.     exit(1);
  263. }
  264.