home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / graphics / gif-util.zip / DEV2GIF.C < prev    next >
C/C++ Source or Header  |  1989-08-01  |  5KB  |  142 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber            IBM PC Ver 0.1,    Jun. 1989    *
  5. ******************************************************************************
  6. * Module to dump graphic devices into a GIF file. Current supported devices: *
  7. * 1. Hercules.                                     *
  8. ******************************************************************************
  9. * History:                                     *
  10. * 22 Jun 89 - Version 1.0 by Gershon Elber.                     *
  11. *****************************************************************************/
  12.  
  13. #include <dos.h>
  14. #include <alloc.h>
  15. #include <stdio.h>
  16. #include <graphics.h>
  17. #include "gif_lib.h"
  18.  
  19. #define PROGRAM_NAME    "GIF_LIBRARY"
  20. #define VERSION        "ß Version 1.0, "
  21.  
  22. static int GraphDriver = HERCMONO,            /* Device parameters */
  23.        GraphMode = HERCMONOHI,
  24.        ScreenXMax = 720,
  25.        ScreenYMax = 348,
  26.        ScreenColorBits = 1;
  27. static unsigned int ScreenBase;
  28.  
  29. static char *VersionStr =
  30.     PROGRAM_NAME
  31.     "    IBMPC "
  32.     VERSION
  33.     "    Gershon Elber,    "
  34.     __DATE__ ",   " __TIME__ "\n"
  35.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  36.  
  37. static void GetScanLine(PixelType *ScanLine, int Y);
  38. static int HandleGifError(GifFileType *GifFile);
  39.  
  40. /******************************************************************************
  41. * Dump the given Device, into given File as GIF format:                  *
  42. * Return 0 on success, -1 if device not supported, or GIF-LIB error number.   *
  43. * See graphics.h for exact definition of GraphDriver/Mode.              *
  44. ******************************************************************************/
  45. int DumpScreen(char *FileName, int ReqGraphDriver, int ReqGraphMode)
  46. {
  47.     int i;
  48.     static PixelType *ScanLine;
  49.     static GifColorType HerculesColorMap[2] = { { 0, 0, 0 },
  50.                         { 255, 255, 255 } };
  51.     GifFileType *GifFile;
  52.  
  53.     switch (ReqGraphDriver) {          /* return on non supported screens */
  54.     case HERCMONO:
  55.         ScreenXMax = 720;
  56.         ScreenYMax = 350;
  57.         ScreenColorBits = 1;
  58.         ScreenBase = 0xb000;
  59.         break;
  60.     default:
  61.         return -1;
  62.     }
  63.  
  64.     ScanLine = (PixelType *) malloc(sizeof(PixelType) * ScreenXMax);
  65.  
  66.     GraphDriver = ReqGraphDriver;
  67.     GraphMode = ReqGraphMode;
  68.  
  69.     if ((GifFile = EGifOpenFileName(FileName, FALSE)) == NULL) {
  70.     free((char *) ScanLine);
  71.     return HandleGifError(GifFile);
  72.     }
  73.  
  74.     if (EGifPutScreenDesc(GifFile, ScreenXMax, ScreenYMax, ScreenColorBits,
  75.               0, ScreenColorBits, HerculesColorMap) == ERROR) {
  76.     free((char *) ScanLine);
  77.     return HandleGifError(GifFile);
  78.     }
  79.  
  80.     if (EGifPutImageDesc(GifFile, 0, 0, ScreenXMax, ScreenYMax, FALSE, 1,
  81.              NULL) == ERROR) {
  82.     free((char *) ScanLine);
  83.     return HandleGifError(GifFile);
  84.     }
  85.  
  86.     for (i=0; i<ScreenYMax; i++) {
  87.     GetScanLine(ScanLine, i);
  88.     if (EGifPutLine(GifFile, ScanLine, ScreenXMax) == ERROR) {
  89.         free((char *) ScanLine);
  90.         return HandleGifError(GifFile);
  91.     }
  92.     }
  93.  
  94.     if (EGifCloseFile(GifFile) == ERROR) {
  95.     free((char *) ScanLine);
  96.     return HandleGifError(GifFile);
  97.     }
  98.  
  99.     free((char *) ScanLine);
  100.     return 0;
  101. }
  102.  
  103. /******************************************************************************
  104. * Update the given scan line buffer with the pixel levels of the Y line.      *
  105. * This routine is device specific, so make sure you know was you are doing    *
  106. ******************************************************************************/
  107. static void GetScanLine(PixelType *ScanLine, int Y)
  108. {
  109.     unsigned char ScreenByte;
  110.     int i, j, k;
  111.     unsigned int Offset, Bit;
  112.  
  113.     switch (GraphDriver) {
  114.     case HERCMONO:
  115.         Offset = 0x2000 * (Y % 4) + (Y / 4) * (ScreenXMax / 8);
  116.         /* In one scan lines we have ScreenXMax / 8 bytes: */
  117.         for (i=0, k=0; i<ScreenXMax / 8; i++) {
  118.         ScreenByte = (unsigned char) peekb(ScreenBase, Offset++);
  119.         for (j=0, Bit = 0x80; j<8; j++) {
  120.             ScanLine[k++] = (ScreenByte & Bit ? 1 : 0);
  121.             Bit >>= 1;
  122.         }
  123.         }
  124.         break;
  125.     default:
  126.         break;
  127.     }
  128. }
  129.  
  130. /******************************************************************************
  131. * Handle last GIF error. Try to close the file and free all allocated memory. *
  132. ******************************************************************************/
  133. static int HandleGifError(GifFileType *GifFile)
  134. {
  135.     int i = GifLastError();
  136.  
  137.     if (EGifCloseFile(GifFile) == ERROR) {
  138.     GifLastError();
  139.     }
  140.     return i;
  141. }
  142.