home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / graphics / gif-util.zip / GIFHISTO.C < prev    next >
C/C++ Source or Header  |  1989-08-01  |  9KB  |  256 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 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. #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    "GifHisto"
  30. #define VERSION        "ß Version 1.0, "
  31.  
  32. #define DEFAULT_HISTO_WIDTH    100           /* Histogram image diemnsions */
  33. #define DEFAULT_HISTO_HEIGHT    256
  34. #define HISTO_BITS_PER_PIXEL    2     /* Size of bitmap for histogram GIF */
  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.     " t%- s%-Width|Height!d!d n%-ImageNumber!d b%- h%- GifFile!*s";
  51. static char
  52.     *ProgramName;
  53. static int
  54.     ImageWidth = DEFAULT_HISTO_WIDTH,
  55.     ImageHeight = DEFAULT_HISTO_HEIGHT,
  56.     ImageN = 1;
  57. static GifColorType
  58.     HistoColorMap[] = {             /* Constant bit map for histograms: */
  59.     { 0, 0, 0 },
  60.     { 255,   0,   0 },
  61.     {   0, 255,   0 },
  62.     {   0,   0, 255 }
  63.     };
  64.  
  65. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
  66.  
  67. /******************************************************************************
  68. * Interpret the command line and scan the given GIF file.              *
  69. ******************************************************************************/
  70. void main(int argc, char **argv)
  71. {
  72.     int    i, j, Size, Error, NumFiles, ExtCode, CodeSize, NumColors, Color,
  73.     Count, ImageNum = 0, TextFlag = FALSE, SizeFlag = FALSE,
  74.     ImageNFlag = FALSE, BackGroundFlag = FALSE, HelpFlag = FALSE;
  75.     long Scaler, Histogram[256];
  76.     GifRecordType RecordType;
  77.     ByteType *Extension, *CodeBlock;
  78.     char **FileName = NULL;
  79.     RowType Line;
  80.     GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
  81.  
  82.     if (strlen(ProgramName = argv[0]) == 0)            /* DOS 3.x only! */
  83.     ProgramName = PROGRAM_NAME;      /* Do something reasonable for 2.x */
  84.  
  85.     /* Same image dimension vars for both Image & ImageN as only one allowed */
  86.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  87.         &TextFlag, &SizeFlag, &ImageWidth, &ImageHeight,
  88.         &ImageNFlag, &ImageN, &BackGroundFlag,
  89.         &HelpFlag, &NumFiles, &FileName)) != FALSE ||
  90.         (NumFiles > 1 && !HelpFlag)) {
  91.     if (Error) GAPrintErrMsg(Error);
  92.     else
  93.     if (NumFiles > 1)
  94.         MESSAGE("Error in command line parsing - one GIF file please\n");
  95.     GAPrintHowTo(CtrlStr);
  96.     exit(1);
  97.     }
  98.  
  99.     if (HelpFlag) {
  100.     fprintf(stderr, VersionStr);
  101.     GAPrintHowTo(CtrlStr);
  102.     exit(0);
  103.     }
  104.  
  105.     if (NumFiles == 1) {
  106.     if ((GifFileIn = DGifOpenFileName(*FileName)) == NULL)
  107.         QuitGifError(GifFileIn, GifFileOut);
  108.     }
  109.     else {
  110.     /* Use the stdin instead: */
  111.     if ((GifFileIn = DGifOpenFileHandle(0)) == NULL)
  112.         QuitGifError(GifFileIn, GifFileOut);
  113.     }
  114.  
  115.     for (i=0; i<256; i++) Histogram[i] = 0;          /* Reset counters. */
  116.  
  117.     /* Scan the content of the GIF file and load the image(s) in: */
  118.     do {
  119.     if (DGifGetRecordType(GifFileIn, &RecordType) == ERROR)
  120.         QuitGifError(GifFileIn, GifFileOut);
  121.  
  122.     switch (RecordType) {
  123.         case IMAGE_DESC_RECORD_TYPE:
  124.         if (DGifGetImageDesc(GifFileIn) == ERROR)
  125.             QuitGifError(GifFileIn, GifFileOut);
  126.  
  127.         if (GifFileIn -> IColorMap)
  128.             NumColors = (1 << GifFileIn -> IBitsPerPixel);
  129.         else
  130.         if (GifFileIn -> SColorMap)
  131.             NumColors = (1 << GifFileIn -> SBitsPerPixel);
  132.         else EXIT("Neither Screen nor Image color map exists\n");
  133.  
  134.         if ((ImageHeight / NumColors) * NumColors != ImageHeight)
  135.             EXIT("Image height specified not dividable by #colors\n");
  136.  
  137.         if (++ImageNum == ImageN) {
  138.             /* This is the image we should make histogram for:       */
  139.             Line = (RowType) malloc(GifFileIn -> IWidth *
  140.                             sizeof(PixelType));
  141.             fprintf(stderr, "\n%s: Image %d at (%d, %d) [%dx%d]:     ",
  142.             ProgramName, ImageNum,
  143.             GifFileIn -> ILeft, GifFileIn -> ITop,
  144.             GifFileIn -> IWidth, GifFileIn -> IHeight);
  145.  
  146.             for (i=0; i<GifFileIn -> IHeight; i++) {
  147.             if (DGifGetLine(GifFileIn, Line, GifFileIn -> IWidth)
  148.                 == ERROR)
  149.                 QuitGifError(GifFileIn, GifFileOut);
  150.             for (j=0; j<GifFileIn -> IWidth; j++)
  151.                 Histogram[Line[j]]++;
  152.             fprintf(stderr, "\b\b\b\b%-4d", i);
  153.             }
  154.  
  155.             free((char *) Line);
  156.         }
  157.         else {
  158.             /* Skip the image: */
  159.             /* Now read image itself in decoded form as we dont      */
  160.             /* really care what is there, and this is much faster.   */
  161.             if (DGifGetCode(GifFileIn, &CodeSize, &CodeBlock) == ERROR)
  162.             QuitGifError(GifFileIn, GifFileOut);
  163.             while (CodeBlock != NULL)
  164.             if (DGifGetCodeNext(GifFileIn, &CodeBlock) == ERROR)
  165.                 QuitGifError(GifFileIn, GifFileOut);
  166.         }
  167.         break;
  168.         case EXTENSION_RECORD_TYPE:
  169.         /* Skip any extension blocks in file: */
  170.         if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == ERROR)
  171.             QuitGifError(GifFileIn, GifFileOut);
  172.  
  173.         while (Extension != NULL) {
  174.             if (DGifGetExtensionNext(GifFileIn, &Extension) == ERROR)
  175.             QuitGifError(GifFileIn, GifFileOut);
  176.         }
  177.         break;
  178.         case TERMINATE_RECORD_TYPE:
  179.         break;
  180.         default:             /* Should be traps by DGifGetRecordType */
  181.         break;
  182.     }
  183.     }
  184.     while (RecordType != TERMINATE_RECORD_TYPE);
  185.  
  186.     /* We we requested to kill back ground count: */
  187.     if (BackGroundFlag) Histogram[GifFileIn -> SBackGroundColor] = 0;
  188.  
  189.     if (DGifCloseFile(GifFileIn) == ERROR)
  190.     QuitGifError(GifFileIn, GifFileOut);
  191.  
  192.  
  193.     /* We may required to dump out the histogram as text file: */
  194.     if (TextFlag) {
  195.     for (i=0; i<NumColors; i++) printf("%12ld  %3d\n", Histogram[i], i);
  196.     }
  197.     else {
  198.     /* Open stdout for the histogram output file: */
  199.     if ((GifFileOut = EGifOpenFileHandle(1)) == NULL)
  200.         QuitGifError(GifFileIn, GifFileOut);
  201.  
  202.     /* Dump out screen descriptor to fit histogram dimensions: */
  203.     if (EGifPutScreenDesc(GifFileOut,
  204.         ImageWidth, ImageHeight, HISTO_BITS_PER_PIXEL, 0,
  205.         HISTO_BITS_PER_PIXEL, HistoColorMap) == ERROR)
  206.         QuitGifError(GifFileIn, GifFileOut);
  207.  
  208.     /* Dump out image descriptor to fit histogram dimensions: */
  209.     if (EGifPutImageDesc(GifFileOut,
  210.         0, 0, ImageWidth, ImageHeight,
  211.         FALSE, HISTO_BITS_PER_PIXEL, NULL) == ERROR)
  212.         QuitGifError(GifFileIn, GifFileOut);
  213.  
  214.     /* Prepare scan line for histogram file, and find scaler to scale    */
  215.     /* histogram to be between 0 and ImageWidth:                 */
  216.     Line = (RowType) malloc(ImageWidth * sizeof(PixelType));
  217.     for (Scaler=0, i=0; i<NumColors; i++) if (Histogram[i] > Scaler)
  218.         Scaler = Histogram[i];
  219.     Scaler /= ImageWidth;
  220.     if (Scaler == 0) Scaler = 1;   /* In case maximum is less than width */
  221.  
  222.     /* Dump out the image itself: */
  223.     for (Count=ImageHeight, i=0, Color=1; i<NumColors; i++) {
  224.         if ((Size = Histogram[i] / Scaler) > ImageWidth) Size = ImageWidth;
  225.         for (j=0; j<Size; j++)
  226.         Line[j] = Color;
  227.         for (j=Size; j<ImageWidth; j++)
  228.         Line[j] = GifFileOut -> SBackGroundColor;
  229.  
  230.         /* Move to next color: */
  231.         if (++Color >= (1 << HISTO_BITS_PER_PIXEL)) Color = 1;
  232.  
  233.         /* Dump this histogram entry as many times as required: */
  234.         for (j=0; j<ImageHeight / NumColors; j++) {
  235.         if (EGifPutLine(GifFileOut, Line, ImageWidth) == ERROR)
  236.             QuitGifError(GifFileIn, GifFileOut);
  237.         fprintf(stderr, "\b\b\b\b%-4d", Count--);
  238.         }
  239.     }
  240.  
  241.     if (EGifCloseFile(GifFileOut) == ERROR)
  242.         QuitGifError(GifFileIn, GifFileOut);
  243.     }
  244. }
  245.  
  246. /******************************************************************************
  247. * Close both input and output file (if open), and exit.                  *
  248. ******************************************************************************/
  249. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
  250. {
  251.     PrintGifError();
  252.     if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  253.     if (GifFileOut != NULL) EGifCloseFile(GifFileOut);
  254.     exit(1);
  255. }
  256.