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

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to convert RLE (utah raster toolkit) file to GIF format.         *
  7. * Options:                                     *
  8. * -c #colors : in power of two, i.e. 7 will allow upto 128 colors in output. *
  9. * -h : on line help.                                 *
  10. ******************************************************************************
  11. * History:                                     *
  12. * 5 Jan 90 - Version 1.0 by Gershon Elber.                     *
  13. *****************************************************************************/
  14.  
  15. #ifdef __MSDOS__
  16. #include <graphics.h>
  17. #include <stdlib.h>
  18. #include <alloc.h>
  19. #include <io.h>
  20. #include <dos.h>
  21. #include <bios.h>
  22. #endif /* __MSDOS__ */
  23.  
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include <fcntl.h>
  28. #include "gif_lib.h"
  29. #include "getarg.h"
  30.  
  31. #include "svfb_global.h"           /* The rle tool kit header files. */
  32.  
  33. #define PROGRAM_NAME    "Rle2Gif"
  34.  
  35. #ifdef __MSDOS__
  36. extern unsigned int
  37.     _stklen = 16384;                 /* Increase default stack size. */
  38. #endif /* __MSDOS__ */
  39.  
  40. #ifdef SYSV
  41. static char *VersionStr =
  42.         "Gif library module,\t\tGershon Elber\n\
  43.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  44. static char
  45.     *CtrlStr = "Rle2Gif c%-#Colors!d h%- RleFile!*s";
  46. #else
  47. static char
  48.     *VersionStr =
  49.     PROGRAM_NAME
  50.     GIF_LIB_VERSION
  51.     "    Gershon Elber,    "
  52.     __DATE__ ",   " __TIME__ "\n"
  53.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  54. static char
  55.     *CtrlStr =
  56.     PROGRAM_NAME
  57.     " c%-#Colors!d h%- RleFile!*s";
  58. #endif /* SYSV */
  59.  
  60. /* Make some variables global, so we could access them faster: */
  61. static int
  62.     ColorFlag = FALSE,
  63.     ExpNumOfColors = 8,
  64.     HelpFlag = FALSE,
  65.     ColorMapSize = 256;
  66.  
  67. static void LoadRle(char *FileName,
  68.             GifByteType **RedBuffer,
  69.             GifByteType **GreenBuffer,
  70.             GifByteType **BlueBuffer,
  71.             int *Width, int *Height);
  72. static void SaveGif(GifByteType *OutputBuffer,
  73.             GifColorType *OutputColorMap,
  74.             int ExpColorMapSize, int Width, int Height);
  75. static void QuitGifError(GifFileType *GifFile);
  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, Error, NumFiles, Width, Height;
  83.     char **FileName = NULL;
  84.     GifByteType *RedBuffer = NULL, *GreenBuffer = NULL, *BlueBuffer = NULL,
  85.         *OutputBuffer = NULL;
  86.     GifColorType *OutputColorMap = NULL;
  87.  
  88.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  89.         &ColorFlag, &ExpNumOfColors, &HelpFlag,
  90.         &NumFiles, &FileName)) != FALSE ||
  91.         (NumFiles > 1 && !HelpFlag)) {
  92.     if (Error)
  93.         GAPrintErrMsg(Error);
  94.     else if (NumFiles > 1)
  95.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  96.     GAPrintHowTo(CtrlStr);
  97.     exit(1);
  98.     }
  99.  
  100.     if (HelpFlag) {
  101.     fprintf(stderr, VersionStr);
  102.     GAPrintHowTo(CtrlStr);
  103.     exit(0);
  104.     }
  105.  
  106.     ColorMapSize = 1 << ExpNumOfColors;
  107.  
  108.     if (NumFiles == 1) {
  109.     LoadRle(*FileName,
  110.         &RedBuffer, &GreenBuffer, &BlueBuffer, &Width, &Height);
  111.     }
  112.     else {
  113.     LoadRle(NULL,
  114.         &RedBuffer, &GreenBuffer, &BlueBuffer, &Width, &Height);
  115.     }
  116.  
  117.     if ((OutputColorMap = (GifColorType *) malloc(ColorMapSize *
  118.                           sizeof(GifColorType))) == NULL ||
  119.     (OutputBuffer = (GifByteType *) malloc(Width * Height *
  120.                         sizeof(GifByteType))) == NULL)
  121.     GIF_EXIT("Failed to allocate memory required, aborted.");
  122.  
  123.     if (QuantizeBuffer(Width, Height, &ColorMapSize,
  124.                RedBuffer, GreenBuffer, BlueBuffer,
  125.                OutputBuffer, OutputColorMap) == GIF_ERROR)
  126.     QuitGifError(NULL);
  127.     free((char *) RedBuffer);
  128.     free((char *) GreenBuffer);
  129.     free((char *) BlueBuffer);
  130.  
  131.     SaveGif(OutputBuffer, OutputColorMap, ExpNumOfColors, Width, Height);
  132. }
  133.  
  134. /******************************************************************************
  135. * Load RLE file into internal frame buffer.                      *
  136. ******************************************************************************/
  137. static void LoadRle(char *FileName,
  138.             GifByteType **RedBuffer,
  139.             GifByteType **GreenBuffer,
  140.             GifByteType **BlueBuffer,
  141.             int *Width, int *Height)
  142. {
  143.     int i, j, k, Size;
  144.     GifByteType *OutputPtr[3];
  145.     struct sv_globals inglobals;
  146.     rle_pixel **rows, *ptr;
  147.     
  148.     if (FileName != NULL) {
  149.     if ((inglobals.svfb_fd = fopen(FileName, "r")) == NULL)
  150.         GIF_EXIT("Can't open input file name.");
  151.     }
  152.     else
  153.     inglobals.svfb_fd = stdin;
  154.  
  155.     rle_get_setup_ok( &inglobals, "rle2gif", FileName );
  156.  
  157.     *Width = inglobals.sv_xmax - inglobals.sv_xmin + 1;
  158.     *Height = inglobals.sv_ymax - inglobals.sv_ymin + 1;
  159.  
  160.     if (inglobals.sv_ncolors != 3)
  161.     GIF_EXIT("Input Rle file does not hold 3 (RGB) colors, aborted.");
  162.  
  163.     Size = *Width * *Height * sizeof(GifByteType);
  164.     if (rle_row_alloc(&inglobals, &rows) ||
  165.     (*RedBuffer = (GifByteType *) malloc(Size)) == NULL ||
  166.     (*GreenBuffer = (GifByteType *) malloc(Size)) == NULL ||
  167.     (*BlueBuffer = (GifByteType *) malloc(Size)) == NULL)
  168.     GIF_EXIT("Failed to allocate memory required, aborted.");
  169.  
  170.     OutputPtr[0] = *RedBuffer;
  171.     OutputPtr[1] = *GreenBuffer;
  172.     OutputPtr[2] = *BlueBuffer;
  173.  
  174.     for (i = 0; i < *Height; i++) {
  175.     rle_getrow(&inglobals, rows);       /* Get one scan line (3 colors). */
  176.  
  177.     for (j = 0; j < 3; j++) { /* Copy the 3 colors to the given buffers. */
  178.         ptr = &rows[j][inglobals.sv_xmin];
  179.  
  180.         for (k = 0; k < *Width; k++)
  181.         *OutputPtr[j]++ = *ptr++;
  182.     }
  183.     }
  184. }
  185.  
  186. /******************************************************************************
  187. * Save the GIF resulting image.                              *
  188. ******************************************************************************/
  189. static void SaveGif(GifByteType *OutputBuffer,
  190.             GifColorType *OutputColorMap,
  191.             int ExpColorMapSize, int Width, int Height)
  192. {
  193.     int i;
  194.     GifFileType *GifFile;
  195.     GifByteType *Ptr = OutputBuffer + Width * (Height - 1);
  196.  
  197.     /* Open stdout for the output file: */
  198.     if ((GifFile = EGifOpenFileHandle(1)) == NULL)
  199.     QuitGifError(GifFile);
  200.  
  201.     if (EGifPutScreenDesc(GifFile,
  202.               Width, Height, ExpColorMapSize, 0, ExpColorMapSize,
  203.               OutputColorMap) == GIF_ERROR ||
  204.     EGifPutImageDesc(GifFile,
  205.              0, 0, Width, Height, FALSE, ExpColorMapSize, NULL) ==
  206.                                                                  GIF_ERROR)
  207.     QuitGifError(GifFile);
  208.     
  209.     fprintf(stderr, "\n%s: Image 1 at (%d, %d) [%dx%d]:     ",
  210.             PROGRAM_NAME, GifFile -> ILeft, GifFile -> ITop,
  211.             GifFile -> IWidth, GifFile -> IHeight);
  212.  
  213.     for (i = 0; i < Height; i++) {
  214.     if (EGifPutLine(GifFile, Ptr, Width) == GIF_ERROR)
  215.         QuitGifError(GifFile);
  216.     fprintf(stderr, "\b\b\b\b%-4d", Height - i - 1);
  217.  
  218.     Ptr -= Width;
  219.     }
  220.  
  221.     if (EGifCloseFile(GifFile) == GIF_ERROR)
  222.     QuitGifError(GifFile);
  223. }
  224.  
  225. /******************************************************************************
  226. * Close output file (if open), and exit.                      *
  227. ******************************************************************************/
  228. static void QuitGifError(GifFileType *GifFile)
  229. {
  230.     PrintGifError();
  231.     if (GifFile != NULL) EGifCloseFile(GifFile);
  232.     exit(1);
  233. }
  234.