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

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jun. 1989   *
  5. ******************************************************************************
  6. * Module to conver raw image into a GIF file.                     *
  7. * Options:                                                                   *
  8. * -s Width Height : specifies size of raw image.                             *
  9. * -p ColorMapFile : specifies color map for ray image (see gifclrmp).        *
  10. * -h : on line help.                                                         *
  11. ******************************************************************************
  12. * History:                                     *
  13. * 15 Oct 89 - Version 1.0 by Gershon Elber.                     *
  14. *****************************************************************************/
  15.  
  16. #ifdef __MSDOS__
  17. #include <dos.h>
  18. #include <alloc.h>
  19. #include <stdlib.h>
  20. #include <graphics.h>
  21. #include <io.h>
  22. #endif /* __MSDOS__ */
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <fcntl.h>
  27. #include "getarg.h"
  28. #include "gif_lib.h"
  29.  
  30. #define PROGRAM_NAME    "Raw2Gif"
  31.  
  32. #ifdef __MSDOS__
  33. extern unsigned int
  34.     _stklen = 16384;                 /* Increase default stack size. */
  35. #endif /* __MSDOS__ */
  36.  
  37. #ifdef SYSV
  38. static char *VersionStr =
  39.         "Gif library module,\t\tGershon Elber\n\
  40.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  41. static char
  42.     *CtrlStr = "Raw2Gif s!-Width|Height!d!d p%-ColorMapFile!s h%- RawFile!*s";
  43. #else
  44. static char
  45.     *VersionStr =
  46.     PROGRAM_NAME
  47.     GIF_LIB_VERSION
  48.     "    Gershon Elber,    "
  49.     __DATE__ ",   " __TIME__ "\n"
  50.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  51. static char
  52.     *CtrlStr =
  53.     PROGRAM_NAME
  54.     " s!-Width|Height!d!d p%-ColorMapFile!s h%- RawFile!*s";
  55. #endif /* SYSV */
  56.  
  57. static GifColorType EGAPallete[] =      /* Default color map is EGA pallete. */
  58. {
  59.     {   0,   0,   0 },   /* 0. Black */
  60.     {   0,   0, 170 },   /* 1. Blue */
  61.     {   0, 170,   0 },   /* 2. Green */
  62.     {   0, 170, 170 },   /* 3. Cyan */
  63.     { 170,   0,   0 },   /* 4. Red */
  64.     { 170,   0, 170 },   /* 5. Magenta */
  65.     { 170, 170,   0 },   /* 6. Brown */
  66.     { 170, 170, 170 },   /* 7. LightGray */
  67.     {  85,  85,  85 },   /* 8. DarkGray */
  68.     {  85,  85, 255 },   /* 9. LightBlue */
  69.     {  85, 255,  85 },   /* 10. LightGreen */
  70.     {  85, 255, 255 },   /* 11. LightCyan */
  71.     { 255,  85,  85 },   /* 12. LightRed */
  72.     { 255,  85, 255 },   /* 13. LightMagenta */
  73.     { 255, 255,  85 },   /* 14. Yellow */
  74.     { 255, 255, 255 },   /* 15. White */
  75. };
  76. #define EGA_PALLETE_SIZE (sizeof(EGAPallete) / sizeof(GifColorType))
  77.  
  78. int Raw2Gif(int ImagwWidth, int ImagwHeight,
  79.                 GifColorType *ColorMap, int ColorMapSize);
  80. static int HandleGifError(GifFileType *GifFile);
  81.  
  82. /******************************************************************************
  83. * Interpret the command line, prepar global data and call the Gif routines.   *
  84. ******************************************************************************/
  85. void main(int argc, char **argv)
  86. {
  87.     int    Error, NumFiles, ImageWidth, ImageHeight, Dummy, Red, Green, Blue,
  88.     ColorMapSize, InFileHandle,
  89.     ImageSizeFlag = FALSE, ColorMapFlag = FALSE, HelpFlag = FALSE;
  90.     char **FileName = NULL, *ColorMapFile;
  91.     GifColorType *ColorMap;
  92.     FILE *InColorMapFile;
  93.  
  94.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  95.         &ImageSizeFlag, &ImageWidth, &ImageHeight,
  96.         &ColorMapFlag, &ColorMapFile,
  97.         &HelpFlag,
  98.         &NumFiles, &FileName)) != FALSE ||
  99.         (NumFiles > 1 && !HelpFlag)) {
  100.     if (Error)
  101.         GAPrintErrMsg(Error);
  102.     else if (NumFiles > 1)
  103.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  104.     GAPrintHowTo(CtrlStr);
  105.     exit(1);
  106.     }
  107.  
  108.     if (HelpFlag) {
  109.     fprintf(stderr, VersionStr);
  110.     GAPrintHowTo(CtrlStr);
  111.     exit(0);
  112.     }
  113.  
  114.     if (ColorMapFlag) {
  115.     /* Read color map from given file: */
  116.     if ((InColorMapFile = fopen(ColorMapFile, "rt")) == NULL) {
  117.         GIF_MESSAGE("Failed to open COLOR MAP file (not exists!?).");
  118.         exit(2);
  119.     }
  120.     if ((ColorMap = (GifColorType *) 
  121.                     malloc(sizeof(GifColorType) * 255))  /* Biggest map. */
  122.         == NULL) {
  123.         GIF_MESSAGE("Failed to allocate bitmap, aborted.");
  124.         exit(3);
  125.     }
  126.  
  127.     for (ColorMapSize = 0;
  128.          ColorMapSize < 256 && !feof(InColorMapFile);
  129.          ColorMapSize++) {
  130.         fscanf(InColorMapFile, "%3d %3d %3d %3d\n",
  131.                         &Dummy, &Red, &Green, &Blue);
  132.         ColorMap[ColorMapSize].Red = Red;
  133.         ColorMap[ColorMapSize].Green = Green;
  134.         ColorMap[ColorMapSize].Blue = Blue;
  135.     }
  136.     }
  137.     else {
  138.     ColorMap = EGAPallete;
  139.     ColorMapSize = EGA_PALLETE_SIZE;
  140.     }
  141.  
  142.     if (NumFiles == 1) {
  143. #ifdef __MSDOS__
  144.     if ((InFileHandle = open(*FileName, O_RDONLY | O_BINARY)) == -1) {
  145. #else
  146.     if ((InFileHandle = open(*FileName, O_RDONLY)) == -1) {
  147. #endif /* __MSDOS__ */
  148.         GIF_MESSAGE("Failed to open RAW image file (not exists!?).");
  149.         exit(2);
  150.     }
  151.     dup2(InFileHandle, 0);               /* Make stdin from this file. */
  152.     }
  153.     else {
  154. #ifdef __MSDOS__
  155.     setmode(0, O_BINARY);          /* Make sure it is in binary mode. */
  156. #endif /* __MSDOS__ */
  157.     }
  158.  
  159. #ifdef __MSDOS__
  160.     setvbuf(stdin, NULL, _IOFBF, GIF_FILE_BUFFER_SIZE);
  161. #endif /* __MSDOS__ */
  162.  
  163.     /* Conver Raw image from stdin to Gif file in stdout: */
  164.     Raw2Gif(ImageWidth, ImageHeight, ColorMap, ColorMapSize);
  165. }
  166.  
  167. /******************************************************************************
  168. * Convert Raw image (One byte per pixel) into Gif file. Raw data is read from *
  169. * stdin, and Gif is dumped to stdout. ImagwWidth times ImageHeight bytes are  *
  170. * read. Color map is dumped from ColorMap.                      *
  171. ******************************************************************************/
  172. int Raw2Gif(int ImageWidth, int ImageHeight,
  173.                 GifColorType *ColorMap, int ColorMapSize)
  174. {
  175.     static int BitsPerPixelArray[] = { 2, 4 ,8, 16, 32, 64, 128, 256 };
  176.     int i, j, BitsPerPixel;
  177.     static GifPixelType *ScanLine;
  178.     GifFileType *GifFile;
  179.  
  180.     for (BitsPerPixel = 0;
  181.      BitsPerPixel < 8 && BitsPerPixelArray[BitsPerPixel] != ColorMapSize;
  182.      BitsPerPixel++);
  183.     if (++BitsPerPixel > 8) {
  184.     GIF_MESSAGE("Number of color map is NOT power of 2 up to 256.");
  185.     exit(3);
  186.     }
  187.  
  188.     if ((ScanLine = (GifPixelType *) malloc(sizeof(GifPixelType) * ImageWidth))
  189.                                 == NULL) {
  190.     GIF_MESSAGE("Failed to allocate scan line, aborted.");
  191.     exit(3);
  192.     }
  193.  
  194.     if ((GifFile = EGifOpenFileHandle(1)) == NULL) {       /* Gif to stdout. */
  195.     free((char *) ScanLine);
  196.     return HandleGifError(GifFile);
  197.     }
  198.  
  199.     if (EGifPutScreenDesc(GifFile, ImageWidth, ImageHeight, BitsPerPixel,
  200.               0, BitsPerPixel, ColorMap) == GIF_ERROR) {
  201.     free((char *) ScanLine);
  202.     return HandleGifError(GifFile);
  203.     }
  204.  
  205.     if (EGifPutImageDesc(GifFile, 0, 0, ImageWidth, ImageHeight, FALSE, 1,
  206.              NULL) == GIF_ERROR) {
  207.     free((char *) ScanLine);
  208.     return HandleGifError(GifFile);
  209.     }
  210.  
  211.     /* Here it is - get one raw line from stdin, and dump to stdout Gif: */
  212.     fprintf(stderr, "\n%s: Image 1 at (0, 0) [%dx%d]:     ",
  213.     PROGRAM_NAME, ImageWidth, ImageHeight);
  214.     for (i = 0; i < ImageHeight; i++) {
  215.     /* Note we assume here PixelSize == Byte, which is not necessarily   */
  216.     /* so. If not - must read one byte at a time, and coerce to pixel.   */
  217.     if (fread(ScanLine, 1, ImageWidth, stdin) != ImageWidth) {
  218.         GIF_MESSAGE("RAW input file ended prematurely.");
  219.         exit(3);
  220.     }
  221.  
  222.     for (j = 0; j < ImageWidth; j++)
  223.         if (ScanLine[j] >= ColorMapSize)
  224.         GIF_MESSAGE("Warning: RAW data color > maximum color map entry.");
  225.  
  226.     if (EGifPutLine(GifFile, ScanLine, ImageWidth) == GIF_ERROR) {
  227.         free((char *) ScanLine);
  228.         return HandleGifError(GifFile);
  229.     }
  230.     fprintf(stderr, "\b\b\b\b%-4d", i);
  231.     }
  232.  
  233.     if (EGifCloseFile(GifFile) == GIF_ERROR) {
  234.     free((char *) ScanLine);
  235.     return HandleGifError(GifFile);
  236.     }
  237.  
  238.     free((char *) ScanLine);
  239.     return 0;
  240. }
  241.  
  242. /******************************************************************************
  243. * Handle last GIF error. Try to close the file and free all allocated memory. *
  244. ******************************************************************************/
  245. static int HandleGifError(GifFileType *GifFile)
  246. {
  247.     int i = GifLastError();
  248.  
  249.     if (EGifCloseFile(GifFile) == GIF_ERROR) {
  250.     GifLastError();
  251.     }
  252.     return i;
  253. }
  254.