home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TIERRA40.ZIP / BEAGLE / GIF / DEV2GIF.C next >
C/C++ Source or Header  |  1992-05-02  |  16KB  |  514 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber            IBM PC Ver 1.1,    Jun. 1989    *
  5. ******************************************************************************
  6. * Module to dump graphic devices into a GIF file. Current supported devices: *
  7. * 1. EGA, VGA, SVGA (800x600), Hercules on the IBM PC (#define __MSDOS__).   *
  8. * 2. SGI 4D Irix using gl library (#define __SGI_GL__).                 *
  9. * 3. X11 using libX.a (#define __X11__).                     *
  10. ******************************************************************************
  11. * History:                                     *
  12. * 22 Jun 89 - Version 1.0 by Gershon Elber.                     *
  13. * 12 Aug 90 - Version 1.1 by Gershon Elber (added devices).             *
  14. *****************************************************************************/
  15. #define __MSDOS__
  16.  
  17. #ifdef __MSDOS__
  18. #include <dos.h>
  19. #include <alloc.h>
  20. #include <graphics.h>
  21. #endif /* __MSDOS__ */
  22.  
  23. #ifdef __SGI_GL__
  24. #include <gl/gl.h>
  25. #endif /* __SGI_GL__ */
  26.  
  27. #ifdef __X11__
  28. #include <X11/Xlib.h>
  29. #endif /* __X11__ */
  30.  
  31. #include <stdio.h>
  32. #include "gif\gif_lib.h"
  33.  
  34. #define PROGRAM_NAME    "GIF_LIBRARY"
  35.  
  36. #define SVGA_SPECIAL    999               /* 800 by 600 Super VGA mode. */
  37.  
  38. static int GraphDriver = -1,       /* Device parameters - reasonable values. */
  39.        GraphMode = -1,
  40.        ScreenColorBits = 1;
  41. static long ScreenXMax = 100,
  42.         ScreenYMax = 100;
  43. static unsigned int ScreenBase;
  44.  
  45. #ifdef SYSV
  46. static char *VersionStr =
  47.         "Gif library module,\t\tGershon Elber\n\
  48.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  49. #else
  50. static char *VersionStr =
  51.     PROGRAM_NAME
  52.     "    IBMPC "
  53.     GIF_LIB_VERSION
  54.     "    Gershon Elber,    "
  55.     __DATE__ ",   " __TIME__ "\n"
  56.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  57. #endif /* SYSV */
  58.  
  59. #if defined(__SGI_GL__) || defined(__X11__)
  60.     GifByteType *GlblGifBuffer = NULL, *GlblGifBufferPtr = NULL;
  61. #endif /* __SGI_GL__ || __X11__ */
  62.  
  63. #ifdef __SGI_GL__
  64. static int QuantizeRGBBuffer(int Width, int Height, long *RGBBuffer,
  65.                  GifColorType *ColorMap, GifByteType *GIFBuffer);
  66. #endif /* __SGI_GL__ */
  67.  
  68. static void GetScanLine(GifPixelType *ScanLine, int Y);
  69. static int HandleGifError(GifFileType *GifFile);
  70.  
  71. /******************************************************************************
  72. * Dump the given Device, into given File as GIF format:                  *
  73. * Return 0 on success, -1 if device not supported, or GIF-LIB error number.   *
  74. * Device is selected via the ReqGraphDriver. Device mode is selected via      *
  75. * ReqGraphMode1/2 as follows:                              *
  76. * 1. IBM PC Hercules card: HERCMONO (one mode only) in ReqGraphMode1,          *
  77. *    ReqGraphMode2/3 are ignored.                          *
  78. * 2. IBM PC EGA card: EGALO/EGAHI in ReqGraphMode1,                  *
  79. *    ReqGraphMode2/3 are ignored.                          *
  80. * 3. IBM PC EGA64 card: EGA64LO/EGA64HI in ReqGraphMode1,              *
  81. *    ReqGraphMode2/3 are ignored.                          *
  82. * 4. IBM PC EGAMONO card: EGAMONOHI (one mode only) in ReqGraphMode1,          *
  83. *    ReqGraphMode2/3 are ignored.                          *
  84. * 5. IBM PC VGA card: VGALO/VGAMED/VGAHI in ReqGraphMode1,              *
  85. *    ReqGraphMode2/3 are ignored.                          *
  86. * 6. IBM PC SVGA card: ReqGraphMode1/2 are both ignored. Fixed mode (800x600  *
  87. *    16 colors) is assumed.                              *
  88. * 7. SGI 4D using GL: window id to dump (as returned by winget()) in          *
  89. *    ReqGraphMode1, ReqGraphMode2/3 are ignored.                  *
  90. * 8. X11: Window id in ReqGraphMode1, Display id in ReqGraphMode2, Color      *
  91. *    map id in  ReqGraphMode3.                              *
  92. ******************************************************************************/
  93. int DumpScreen2Gif(char *FileName, int ReqGraphDriver, int ReqGraphMode1,
  94.                                       int ReqGraphMode2,
  95.                                       int ReqGraphMode3)
  96. {
  97.     int i, j, k;
  98.     GifPixelType *ScanLine;
  99.     GifFileType *GifFile;
  100.     GifColorType *ColorMap = NULL;
  101. #ifdef __MSDOS__
  102.     static GifColorType MonoChromeColorMap[] = {
  103.         { 0, 0, 0 },
  104.         { 255, 255, 255 }
  105.     };
  106.     /* I have no idea what default EGA64 (4 colors) should be (I guessed...).*/
  107.     static GifColorType EGA64ColorMap[] = {
  108.         {   0,   0,   0 },   /* 0. Black */
  109.         { 255,   0,   0 },   /* 1. Red */
  110.         {   0, 255,   0 },   /* 2. Green */
  111.         {   0,   0, 255 },   /* 3. Blue */
  112.     };
  113.     static GifColorType EGAColorMap[] = {
  114.         {   0,   0,   0 },   /* 0. Black */
  115.         {   0,   0, 170 },   /* 1. Blue */
  116.         {   0, 170,   0 },   /* 2. Green */
  117.         {   0, 170, 170 },   /* 3. Cyan */
  118.         { 170,   0,   0 },   /* 4. Red */
  119.         { 170,   0, 170 },   /* 5. Magenta */
  120.         { 170, 170,   0 },   /* 6. Brown */
  121.         { 170, 170, 170 },   /* 7. LightGray */
  122.         {  85,  85,  85 },   /* 8. DarkGray */
  123.         {  85,  85, 255 },   /* 9. LightBlue */
  124.         {  85, 255,  85 },   /* 10. LightGreen */
  125.         {  85, 255, 255 },   /* 11. LightCyan */
  126.         { 255,  85,  85 },   /* 12. LightRed */
  127.         { 255,  85, 255 },   /* 13. LightMagenta */
  128.         { 255, 255,  85 },   /* 14. Yellow */
  129.         { 255, 255, 255 },   /* 15. White */
  130.     };
  131. #endif /* __MSDOS__ */
  132. #if defined(__SGI_GL__) || defined(__X11__)
  133.     long *RGBBuffer;
  134.     GifColorType ColorMap256[256];
  135. #endif
  136. #ifdef __X11__
  137.     XImage *XImg;
  138.     unsigned long XPixel;
  139.     XColor XColorTable[256];               /* Up to 256 colors in X. */
  140.     XWindowAttributes WinAttr;
  141. #endif /* __X11__ */
  142.  
  143.     switch (ReqGraphDriver) {         /* Return on non supported screens. */
  144. #ifdef __MSDOS__
  145.         case HERCMONO:
  146.         ScreenXMax = 720;
  147.         ScreenYMax = 350;
  148.         ScreenColorBits = 1;
  149.         ScreenBase = 0xb000;
  150.         ColorMap = MonoChromeColorMap;
  151.         break;
  152.     case EGA:
  153.         switch (ReqGraphMode1) {
  154.                 case EGALO:
  155.             ScreenYMax = 200;
  156.             break;
  157.                 case EGAHI:
  158.             ScreenYMax = 350;
  159.             break;
  160.         default:
  161.                     return -1;
  162.         }
  163.         ScreenXMax = 640;
  164.         ScreenColorBits = 4;
  165.         ScreenBase = 0xa000;
  166.         ColorMap = EGAColorMap;
  167.         break;
  168.     case EGA64:
  169.         switch (ReqGraphMode1) {
  170.                 case EGA64LO:
  171.             ScreenYMax = 200;
  172.             break;
  173.                 case EGA64HI:
  174.             ScreenYMax = 350;
  175.             break;
  176.         default:
  177.                     return -1;
  178.         }
  179.         ScreenXMax = 640;
  180.         ScreenColorBits = 2;
  181.         ScreenBase = 0xa000;
  182.         ColorMap = EGA64ColorMap;
  183.         break;
  184.     case EGAMONO:
  185.         switch (ReqGraphMode1) {
  186.                 case EGAMONOHI:
  187.             ScreenYMax = 350;
  188.             break;
  189.         default:
  190.                     return -1;
  191.         }
  192.         ScreenXMax = 640;
  193.         ScreenColorBits = 1;
  194.         ScreenBase = 0xa000;
  195.         ColorMap = MonoChromeColorMap;
  196.         break;
  197.     case VGA:
  198.         switch (ReqGraphMode1) {
  199.                 case VGALO:
  200.             ScreenYMax = 200;
  201.             break;
  202.                 case VGAMED:
  203.             ScreenYMax = 350;
  204.             break;
  205.                 case VGAHI:
  206.             ScreenYMax = 480;
  207.             break;
  208.         default:
  209.                     return -1;
  210.         }
  211.         ScreenXMax = 640;
  212.         ScreenColorBits = 4;
  213.         ScreenBase = 0xa000;
  214.         ColorMap = EGAColorMap;
  215.         break;
  216.         case SVGA_SPECIAL:
  217.         ScreenXMax = 800;
  218.         ScreenYMax = 600;
  219.         ScreenColorBits = 4;
  220.         ScreenBase = 0xa000;
  221.         ColorMap = EGAColorMap;
  222.         break;
  223. #endif /* __MSDOS__ */
  224. #ifdef __SGI_GL__
  225.     case GIF_DUMP_SGI_WINDOW:
  226.         winset(ReqGraphMode1);        /* Select window as active window. */
  227.         getsize(&ScreenXMax, &ScreenYMax);
  228.  
  229.         RGBBuffer = (long *) malloc(sizeof(long) * ScreenXMax * ScreenYMax);
  230.         readsource(SRC_FRONT);
  231.         if (lrectread((short) 0,
  232.               (short) 0,
  233.               (short) (ScreenXMax - 1),
  234.               (short) (ScreenYMax - 1), RGBBuffer) !=
  235.         ScreenXMax * ScreenYMax) {                /* Get data. */
  236.         free(RGBBuffer);
  237.         return -1;
  238.         }
  239.         GlblGifBuffer = (GifByteType *) malloc(sizeof(GifByteType) *
  240.                         ScreenXMax * ScreenYMax);
  241.         i = QuantizeRGBBuffer(ScreenXMax, ScreenYMax, RGBBuffer,
  242.                   ColorMap256, GlblGifBuffer);
  243.         /* Find minimum color map size to hold all quantized colors. */
  244.         for    (ScreenColorBits = 1;
  245.          (1 << ScreenColorBits) < i && ScreenColorBits < 8;
  246.          ScreenColorBits++);
  247.  
  248.         /* Start to dump with top line as GIF expects it. */
  249.         GlblGifBufferPtr = GlblGifBuffer + ScreenXMax * (ScreenYMax - 1);
  250.         ColorMap = ColorMap256;
  251.         free(RGBBuffer);
  252.         break;
  253. #endif /* __SGI_GL__ */
  254. #ifdef __X11__
  255.     case GIF_DUMP_X_WINDOW:
  256.         XGetWindowAttributes((Display *) ReqGraphMode2,
  257.                  (Window) ReqGraphMode1,
  258.                  &WinAttr);
  259.         ScreenXMax = WinAttr.width;
  260.         ScreenYMax = WinAttr.height;
  261.  
  262.         XImg = XGetImage((Display *) ReqGraphMode2,
  263.                  (Window) ReqGraphMode1,
  264.                  0, 0, ScreenXMax - 1, ScreenYMax - 1,
  265.                  AllPlanes, XYPixmap);
  266.  
  267.         GlblGifBuffer = (GifByteType *) malloc(sizeof(GifByteType) *
  268.                         ScreenXMax * ScreenYMax);
  269.  
  270.         /* Scan the image for all different colors exists. */
  271.         for (i = 0; i < 256; i++) XColorTable[i].pixel = 0;
  272.         k = FALSE;
  273.          for (i = 0; i < ScreenXMax; i++)
  274.         for (j = 0; j < ScreenYMax; j++) {
  275.             XPixel = XGetPixel(XImg, i, j);
  276.             if (XPixel > 255) {
  277.             if (!k) {
  278.                 /* Make sure we state it once only.*/
  279.                 fprintf(stderr, "X Color table - truncated.\n");
  280.                 k = TRUE;
  281.             }
  282.             XPixel = 255;
  283.             }
  284.             XColorTable[XPixel].pixel = XPixel;
  285.         }
  286.         /* Find the RGB representation of the colors. */
  287.         XQueryColors((Display *) ReqGraphMode2,
  288.              (Colormap) ReqGraphMode3,
  289.              XColorTable,
  290.              256);
  291.         /* Count number of active colors (Note color 0 is always in)    */
  292.         /* and create the Gif color map from it.                */
  293.         ColorMap = ColorMap256;
  294.         ColorMap[0].Red = ColorMap[0].Green = ColorMap[0].Blue = 0;
  295.         for (i = j = 1; i < 256; i++)
  296.         if (XColorTable[i].pixel) {
  297.             ColorMap[j].Red = XColorTable[i].red / 256;
  298.             ColorMap[j].Green = XColorTable[i].green / 256;
  299.             ColorMap[j].Blue = XColorTable[i].blue / 256;
  300.             /* Save the X color index into the Gif table: */
  301.             XColorTable[i].pixel = j++;
  302.         }
  303.         /* and set the number of colors in the Gif color map. */
  304.         for    (ScreenColorBits = 1;
  305.          (1 << ScreenColorBits) < j && ScreenColorBits < 8;
  306.          ScreenColorBits++);
  307.  
  308.         /* Prepare the Gif image buffer as indices into the Gif color   */
  309.         /* map from the X image.                        */
  310.         GlblGifBufferPtr = GlblGifBuffer;
  311.         for (i = 0; i < ScreenXMax; i++)
  312.         for (j = 0; j < ScreenYMax; j++)
  313.             *GlblGifBufferPtr++ =
  314.             XColorTable[XGetPixel(XImg, j, i) & 0xff].pixel;
  315.         XDestroyImage(XImg);
  316.  
  317.         GlblGifBufferPtr = GlblGifBuffer;
  318.         ColorMap = ColorMap256;
  319.         break;
  320. #endif /* __X11__ */
  321.     default:
  322.         return -1;
  323.     }
  324.  
  325.     ScanLine = (GifPixelType *) malloc(sizeof(GifPixelType) * ScreenXMax);
  326.  
  327.     GraphDriver = ReqGraphDriver;
  328.     GraphMode = ReqGraphMode1;
  329.  
  330.     if ((GifFile = EGifOpenFileName(FileName, FALSE)) == NULL ||
  331.     EGifPutScreenDesc(GifFile, ScreenXMax, ScreenYMax, ScreenColorBits,
  332.               0, ScreenColorBits, ColorMap) == GIF_ERROR ||
  333.     EGifPutImageDesc(GifFile, 0, 0, ScreenXMax, ScreenYMax, FALSE, 1,
  334.              NULL) == GIF_ERROR) {
  335.     free((char *) ScanLine);
  336. #if defined(__SGI_GL__) || defined(__X11__)
  337.     free((char *) GlblGifBuffer);
  338. #endif
  339.     return HandleGifError(GifFile);
  340.     }
  341.  
  342.     for (i = 0; i < ScreenYMax; i++) {
  343.     GetScanLine(ScanLine, i);
  344.     if (EGifPutLine(GifFile, ScanLine, ScreenXMax) == GIF_ERROR) {
  345.         free((char *) ScanLine);
  346. #if defined(__SGI_GL__) || defined(__X11__)
  347.         free((char *) GlblGifBuffer);
  348. #endif
  349.         return HandleGifError(GifFile);
  350.     }
  351.     }
  352.  
  353.     if (EGifCloseFile(GifFile) == GIF_ERROR) {
  354.     free((char *) ScanLine);
  355. #if defined(__SGI_GL__) || defined(__X11__)
  356.     free((char *) GlblGifBuffer);
  357. #endif
  358.     return HandleGifError(GifFile);
  359.     }
  360.  
  361.     free((char *) ScanLine);
  362. #if defined(__SGI_GL__) || defined(__X11__)
  363.     free((char *) GlblGifBuffer);
  364. #endif
  365.     return 0;
  366. }
  367.  
  368. #ifdef __SGI_GL__
  369. /******************************************************************************
  370. * Quantize the given 24 bit (8 per RGB) into 256 colors.              *
  371. ******************************************************************************/
  372. static int QuantizeRGBBuffer(int Width, int Height, long *RGBBuffer,
  373.                   GifColorType *ColorMap, GifByteType *GIFBuffer)
  374. {
  375.     int i;
  376.     GifByteType *RedInput, *GreenInput, *BlueInput;
  377.  
  378.     /* Convert the RGB Buffer into 3 seperated buffers: */
  379.     RedInput = (GifByteType *) malloc(sizeof(GifByteType) * Width * Height);
  380.     GreenInput = (GifByteType *) malloc(sizeof(GifByteType) * Width * Height);
  381.     BlueInput = (GifByteType *) malloc(sizeof(GifByteType) * Width * Height);
  382.  
  383.     for (i = 0; i < Width * Height; i++) {
  384.     RedInput[i] = RGBBuffer[i] & 0xff;
  385.     GreenInput[i] = (RGBBuffer[i] >> 8) & 0xff;
  386.     BlueInput[i] = (RGBBuffer[i] >> 16) & 0xff;
  387.     }
  388.     for (i = 0; i < 256; i++)
  389.     ColorMap[i].Red = ColorMap[i].Green = ColorMap[i].Blue = 0;
  390.  
  391.     i = 256;
  392.     QuantizeBuffer(Width, Height, &i,
  393.            RedInput, GreenInput, BlueInput,
  394.            GIFBuffer, ColorMap);
  395.  
  396.     free(RedInput);
  397.     free(GreenInput);
  398.     free(BlueInput);
  399.  
  400.     return i;               /* Real number of colors in color table. */
  401. }
  402. #endif /* __SGI_GL__ */
  403.  
  404. /******************************************************************************
  405. * Update the given scan line buffer with the pixel levels of the Y line.      *
  406. * This routine is device specific, so make sure you know was you are doing    *
  407. ******************************************************************************/
  408. static void GetScanLine(GifPixelType *ScanLine, int Y)
  409. {
  410.     unsigned char ScreenByte;
  411.     int i, j, k;
  412.     unsigned int BufferOffset, Bit;
  413. #ifdef __MSDOS__
  414.     union REGS InRegs, OutRegs;
  415. #endif /* __MSDOS__ */
  416.  
  417.     switch (GraphDriver) {
  418. #ifdef __MSDOS__
  419.     case HERCMONO:
  420.         BufferOffset = 0x2000 * (Y % 4) + (Y / 4) * (ScreenXMax / 8);
  421.         /* In one scan lines we have ScreenXMax / 8 bytes: */
  422.         for (i = 0, k = 0; i < ScreenXMax / 8; i++) {
  423.         ScreenByte = (unsigned char) peekb(ScreenBase, BufferOffset++);
  424.         for (j = 0, Bit = 0x80; j < 8; j++) {
  425.             ScanLine[k++] = (ScreenByte & Bit ? 1 : 0);
  426.             Bit >>= 1;
  427.         }
  428.         }
  429.         break;
  430.     case EGA:
  431.     case EGA64:
  432.     case EGAMONO:
  433.     case VGA:
  434.     case SVGA_SPECIAL:
  435.         InRegs.x.dx = Y;
  436.         InRegs.h.bh = 0;
  437.             InRegs.h.ah = 0x0d;                   /* BIOS Read dot. */
  438.         for (i = 0; i < ScreenXMax; i++) {
  439.             InRegs.x.cx = i;
  440.         int86(0x10, &InRegs, &OutRegs);
  441.             ScanLine[i] = OutRegs.h.al;
  442.         }
  443.  
  444.         /* Makr this line as done by putting a xored dot on the left. */
  445.         InRegs.x.dx = Y;
  446.         InRegs.h.bh = 0;
  447.         InRegs.h.ah = 0x0c;               /* BIOS Write dot (xor mode). */
  448.         InRegs.h.al = 0x81;                    /* Xor with color 1. */
  449.         InRegs.x.cx = 0;
  450.         int86(0x10, &InRegs, &OutRegs);
  451.         InRegs.x.dx = Y;
  452.         InRegs.h.bh = 0;
  453.         InRegs.h.ah = 0x0c;               /* BIOS Write dot (xor mode). */
  454.         InRegs.h.al = 0x81;                    /* Xor with color 1. */
  455.         InRegs.x.cx = 1;
  456.         int86(0x10, &InRegs, &OutRegs);
  457.  
  458.         if (Y == ScreenYMax - 1) {/* Last row - clear all marks we made. */
  459.         for (i = 0; i < ScreenYMax; i++) {
  460.             InRegs.h.bh = 0;
  461.             InRegs.h.ah = 0x0c;           /* BIOS Write dot (xor mode). */
  462.             InRegs.h.al = 0x81;           /* Xor back with color 1. */
  463.             InRegs.x.dx = i;
  464.             InRegs.x.cx = 0;
  465.             int86(0x10, &InRegs, &OutRegs);
  466.             InRegs.h.bh = 0;
  467.             InRegs.h.ah = 0x0c;           /* BIOS Write dot (xor mode). */
  468.             InRegs.h.al = 0x81;           /* Xor back with color 1. */
  469.             InRegs.x.dx = i;
  470.             InRegs.x.cx = 1;
  471.             int86(0x10, &InRegs, &OutRegs);
  472.                 }
  473.         }
  474.         break;
  475. #endif /* __MSDOS__ */
  476. #ifdef __SGI_GL__
  477.     case GIF_DUMP_SGI_WINDOW:
  478.         memcpy(ScanLine, GlblGifBufferPtr, ScreenXMax * sizeof(GifPixelType));
  479.         GlblGifBufferPtr -= ScreenXMax;
  480.         break;
  481. #endif /* __SGI_GL__ */
  482. #ifdef __X11__
  483.     case GIF_DUMP_X_WINDOW:
  484.         memcpy(ScanLine, GlblGifBufferPtr, ScreenXMax * sizeof(GifPixelType));
  485.         GlblGifBufferPtr += ScreenXMax;
  486.         break;
  487. #endif /* __X11__ */
  488.     default:
  489.         break;
  490.     }
  491. }
  492.  
  493. /******************************************************************************
  494. * Handle last GIF error. Try to close the file and free all allocated memory. *
  495. ******************************************************************************/
  496. static int HandleGifError(GifFileType *GifFile)
  497. {
  498.     int i = GifLastError();
  499.  
  500.     if (EGifCloseFile(GifFile) == GIF_ERROR) {
  501.     GifLastError();
  502.     }
  503.     return i;
  504. }
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.