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

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to generate GIF image page from a given text by drawing the chars  *
  7. * using 8 by 8 fixed font.                             *
  8. * Options:                                     *
  9. * -s ColorMapSize : in bits, i.e. 6 bits for 64 colors.                 *
  10. * -f ForeGroundIndex : by default foreground is 1. Must be in range 0..255.  *
  11. * -c R G B : set the foregound color values. By default it is white.         *
  12. * -t "Text" : Make one line given file (8 pixel high) from the given Text.   *
  13. * -h : on line help.                                 *
  14. ******************************************************************************
  15. * History:                                     *
  16. * 3 May 90 - Version 1.0 by Gershon Elber.                     *
  17. *****************************************************************************/
  18.  
  19. #ifdef __MSDOS__
  20. #include <stdlib.h>
  21. #include <alloc.h>
  22. #endif /* __MSDOS__ */
  23.  
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include "gif_lib.h"
  28. #include "getarg.h"
  29.  
  30. #define PROGRAM_NAME    "Text2Gif"
  31.  
  32. #define MAX_NUM_TEXT_LINES    100     /* Maximum number of lines in file. */
  33.  
  34. #define LINE_LEN        256     /* Maximum length of one text line. */
  35.  
  36. #define DEFAULT_FG_INDEX    1           /* Text foreground index. */
  37.  
  38. #define DEFAULT_COLOR_RED    255           /* Text foreground color. */
  39. #define DEFAULT_COLOR_GREEN    255
  40. #define DEFAULT_COLOR_BLUE    255
  41.  
  42. #ifdef __MSDOS__
  43. extern unsigned int
  44.     _stklen = 16384;                 /* Increase default stack size. */
  45. #endif /* __MSDOS__ */
  46.  
  47. #ifdef SYSV
  48. static char *VersionStr =
  49.         "Gif library module    \t\tGershon Elber\n\
  50.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  51. static char
  52.     *CtrlStr = "Text2Gif s%-ClrMapSize!d f%-FGClr!d c%-R|G|B!d!d!d t%-\"Text\"!s h%-";
  53. #else
  54. static char
  55.     *VersionStr =
  56.     PROGRAM_NAME
  57.     GIF_LIB_VERSION
  58.     "    Gershon Elber,    "
  59.     __DATE__ ",   " __TIME__ "\n"
  60.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  61. static char
  62.     *CtrlStr =
  63.     PROGRAM_NAME
  64.     " s%-ClrMapSize!d f%-FGClr!d c%-R|G|B!d!d!d t%-\"Text\"!s h%-";
  65. #endif /* SYSV */
  66.  
  67. static unsigned int
  68.     RedColor = DEFAULT_COLOR_RED,
  69.     GreenColor = DEFAULT_COLOR_GREEN,
  70.     BlueColor = DEFAULT_COLOR_BLUE;
  71.  
  72. /*****************************************************************************
  73. * Ascii 8 by 8 regular font - only first 128 characters are supported.         *
  74. *****************************************************************************/
  75. static unsigned char AsciiTable[][8] = {
  76.     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* Ascii 0  */
  77.     { 0x3c, 0x42, 0xa5, 0x81, 0xbd, 0x42, 0x3c, 0x00 }, /* Ascii 1  */
  78.     { 0x3c, 0x7e, 0xdb, 0xff, 0xc3, 0x7e, 0x3c, 0x00 }, /* Ascii 2  */
  79.     { 0x00, 0xee, 0xfe, 0xfe, 0x7c, 0x38, 0x10, 0x00 }, /* Ascii 3  */
  80.     { 0x10, 0x38, 0x7c, 0xfe, 0x7c, 0x38, 0x10, 0x00 }, /* Ascii 4  */
  81.     { 0x00, 0x3c, 0x18, 0xff, 0xff, 0x08, 0x18, 0x00 }, /* Ascii 5  */
  82.     { 0x10, 0x38, 0x7c, 0xfe, 0xfe, 0x10, 0x38, 0x00 }, /* Ascii 6  */
  83.     { 0x00, 0x00, 0x18, 0x3c, 0x18, 0x00, 0x00, 0x00 }, /* Ascii 7  */
  84.     { 0xff, 0xff, 0xe7, 0xc3, 0xe7, 0xff, 0xff, 0xff }, /* Ascii 8  */
  85.     { 0x00, 0x3c, 0x42, 0x81, 0x81, 0x42, 0x3c, 0x00 }, /* Ascii 9  */
  86.     { 0xff, 0xc3, 0xbd, 0x7e, 0x7e, 0xbd, 0xc3, 0xff }, /* Ascii 10 */
  87.     { 0x1f, 0x07, 0x0d, 0x7c, 0xc6, 0xc6, 0x7c, 0x00 }, /* Ascii 11 */
  88.     { 0x00, 0x7e, 0xc3, 0xc3, 0x7e, 0x18, 0x7e, 0x18 }, /* Ascii 12 */
  89.     { 0x04, 0x06, 0x07, 0x04, 0x04, 0xfc, 0xf8, 0x00 }, /* Ascii 13 */
  90.     { 0x0c, 0x0a, 0x0d, 0x0b, 0xf9, 0xf9, 0x1f, 0x1f }, /* Ascii 14 */
  91.     { 0x00, 0x92, 0x7c, 0x44, 0xc6, 0x7c, 0x92, 0x00 }, /* Ascii 15 */
  92.     { 0x00, 0x00, 0x60, 0x78, 0x7e, 0x78, 0x60, 0x00 }, /* Ascii 16 */
  93.     { 0x00, 0x00, 0x06, 0x1e, 0x7e, 0x1e, 0x06, 0x00 }, /* Ascii 17 */
  94.     { 0x18, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x7e, 0x18 }, /* Ascii 18 */
  95.     { 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x66, 0x00 }, /* Ascii 19 */
  96.     { 0xff, 0xb6, 0x76, 0x36, 0x36, 0x36, 0x36, 0x00 }, /* Ascii 20 */
  97.     { 0x7e, 0xc1, 0xdc, 0x22, 0x22, 0x1f, 0x83, 0x7e }, /* Ascii 21 */
  98.     { 0x00, 0x00, 0x00, 0x7e, 0x7e, 0x00, 0x00, 0x00 }, /* Ascii 22 */
  99.     { 0x18, 0x7e, 0x18, 0x18, 0x7e, 0x18, 0x00, 0xff }, /* Ascii 23 */
  100.     { 0x18, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00 }, /* Ascii 24 */
  101.     { 0x18, 0x18, 0x18, 0x18, 0x18, 0x7e, 0x18, 0x00 }, /* Ascii 25 */
  102.     { 0x00, 0x04, 0x06, 0xff, 0x06, 0x04, 0x00, 0x00 }, /* Ascii 26 */
  103.     { 0x00, 0x20, 0x60, 0xff, 0x60, 0x20, 0x00, 0x00 }, /* Ascii 27 */
  104.     { 0x00, 0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xff, 0x00 }, /* Ascii 28 */
  105.     { 0x00, 0x24, 0x66, 0xff, 0x66, 0x24, 0x00, 0x00 }, /* Ascii 29 */
  106.     { 0x00, 0x00, 0x10, 0x38, 0x7c, 0xfe, 0x00, 0x00 }, /* Ascii 30 */
  107.     { 0x00, 0x00, 0x00, 0xfe, 0x7c, 0x38, 0x10, 0x00 }, /* Ascii 31 */
  108.     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /*   */
  109.     { 0x30, 0x30, 0x30, 0x30, 0x30, 0x00, 0x30, 0x00 }, /* ! */
  110.     { 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* " */
  111.     { 0x6c, 0x6c, 0xfe, 0x6c, 0xfe, 0x6c, 0x6c, 0x00 }, /* # */
  112.     { 0x10, 0x7c, 0xd2, 0x7c, 0x86, 0x7c, 0x10, 0x00 }, /* $ */
  113.     { 0xf0, 0x96, 0xfc, 0x18, 0x3e, 0x72, 0xde, 0x00 }, /* % */
  114.     { 0x30, 0x48, 0x30, 0x78, 0xce, 0xcc, 0x78, 0x00 }, /* & */
  115.     { 0x0c, 0x0c, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* ' */
  116.     { 0x10, 0x60, 0xc0, 0xc0, 0xc0, 0x60, 0x10, 0x00 }, /* ( */
  117.     { 0x10, 0x0c, 0x06, 0x06, 0x06, 0x0c, 0x10, 0x00 }, /* ) */
  118.     { 0x00, 0x54, 0x38, 0xfe, 0x38, 0x54, 0x00, 0x00 }, /* * */
  119.     { 0x00, 0x18, 0x18, 0x7e, 0x18, 0x18, 0x00, 0x00 }, /* + */
  120.     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x70 }, /* , */
  121.     { 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00 }, /* - */
  122.     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00 }, /* . */
  123.     { 0x02, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0x00 }, /* / */
  124.     { 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00 }, /* 0x */
  125.     { 0x18, 0x38, 0x78, 0x18, 0x18, 0x18, 0x3c, 0x00 }, /* 1 */
  126.     { 0x7c, 0xc6, 0x06, 0x0c, 0x30, 0x60, 0xfe, 0x00 }, /* 2 */
  127.     { 0x7c, 0xc6, 0x06, 0x3c, 0x06, 0xc6, 0x7c, 0x00 }, /* 3 */
  128.     { 0x0e, 0x1e, 0x36, 0x66, 0xfe, 0x06, 0x06, 0x00 }, /* 4 */
  129.     { 0xfe, 0xc0, 0xc0, 0xfc, 0x06, 0x06, 0xfc, 0x00 }, /* 5 */
  130.     { 0x7c, 0xc6, 0xc0, 0xfc, 0xc6, 0xc6, 0x7c, 0x00 }, /* 6 */
  131.     { 0xfe, 0x06, 0x0c, 0x18, 0x30, 0x60, 0x60, 0x00 }, /* 7 */
  132.     { 0x7c, 0xc6, 0xc6, 0x7c, 0xc6, 0xc6, 0x7c, 0x00 }, /* 8 */
  133.     { 0x7c, 0xc6, 0xc6, 0x7e, 0x06, 0xc6, 0x7c, 0x00 }, /* 9 */
  134.     { 0x00, 0x30, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00 }, /* : */
  135.     { 0x00, 0x30, 0x00, 0x00, 0x00, 0x30, 0x20, 0x00 }, /* }, */
  136.     { 0x00, 0x1c, 0x30, 0x60, 0x30, 0x1c, 0x00, 0x00 }, /* < */
  137.     { 0x00, 0x00, 0x7e, 0x00, 0x7e, 0x00, 0x00, 0x00 }, /* = */
  138.     { 0x00, 0x70, 0x18, 0x0c, 0x18, 0x70, 0x00, 0x00 }, /* > */
  139.     { 0x7c, 0xc6, 0x0c, 0x18, 0x30, 0x00, 0x30, 0x00 }, /* ? */
  140.     { 0x7c, 0x82, 0x9a, 0xaa, 0xaa, 0x9e, 0x7c, 0x00 }, /* @ */
  141.     { 0x38, 0x6c, 0xc6, 0xc6, 0xfe, 0xc6, 0xc6, 0x00 }, /* A */
  142.     { 0xfc, 0xc6, 0xc6, 0xfc, 0xc6, 0xc6, 0xfc, 0x00 }, /* B */
  143.     { 0x7c, 0xc6, 0xc6, 0xc0, 0xc0, 0xc6, 0x7c, 0x00 }, /* C */
  144.     { 0xf8, 0xcc, 0xc6, 0xc6, 0xc6, 0xcc, 0xf8, 0x00 }, /* D */
  145.     { 0xfe, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xfe, 0x00 }, /* E */
  146.     { 0xfe, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0x00 }, /* F */
  147.     { 0x7c, 0xc6, 0xc0, 0xce, 0xc6, 0xc6, 0x7e, 0x00 }, /* G */
  148.     { 0xc6, 0xc6, 0xc6, 0xfe, 0xc6, 0xc6, 0xc6, 0x00 }, /* H */
  149.     { 0x78, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00 }, /* I */
  150.     { 0x1e, 0x06, 0x06, 0x06, 0xc6, 0xc6, 0x7c, 0x00 }, /* J */
  151.     { 0xc6, 0xcc, 0xd8, 0xf0, 0xd8, 0xcc, 0xc6, 0x00 }, /* K */
  152.     { 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfe, 0x00 }, /* L */
  153.     { 0xc6, 0xee, 0xfe, 0xd6, 0xc6, 0xc6, 0xc6, 0x00 }, /* M */
  154.     { 0xc6, 0xe6, 0xf6, 0xde, 0xce, 0xc6, 0xc6, 0x00 }, /* N */
  155.     { 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00 }, /* O */
  156.     { 0xfc, 0xc6, 0xc6, 0xfc, 0xc0, 0xc0, 0xc0, 0x00 }, /* P */
  157.     { 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x06 }, /* Q */
  158.     { 0xfc, 0xc6, 0xc6, 0xfc, 0xc6, 0xc6, 0xc6, 0x00 }, /* R */
  159.     { 0x78, 0xcc, 0x60, 0x30, 0x18, 0xcc, 0x78, 0x00 }, /* S */
  160.     { 0xfc, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x00 }, /* T */
  161.     { 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00 }, /* U */
  162.     { 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x6c, 0x38, 0x00 }, /* V */
  163.     { 0xc6, 0xc6, 0xc6, 0xd6, 0xfe, 0xee, 0xc6, 0x00 }, /* W */
  164.     { 0xc6, 0xc6, 0x6c, 0x38, 0x6c, 0xc6, 0xc6, 0x00 }, /* X */
  165.     { 0xc3, 0xc3, 0x66, 0x3c, 0x18, 0x18, 0x18, 0x00 }, /* Y */
  166.     { 0xfe, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0xfe, 0x00 }, /* Z */
  167.     { 0x3c, 0x30, 0x30, 0x30, 0x30, 0x30, 0x3c, 0x00 }, /* [ */
  168.     { 0xc0, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x03, 0x00 }, /* \ */
  169.     { 0x3c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x3c, 0x00 }, /* ] */
  170.     { 0x00, 0x38, 0x6c, 0xc6, 0x00, 0x00, 0x00, 0x00 }, /* ^ */
  171.     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff }, /* _ */
  172.     { 0x30, 0x30, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* ` */
  173.     { 0x00, 0x00, 0x7c, 0x06, 0x7e, 0xc6, 0x7e, 0x00 }, /* a */
  174.     { 0xc0, 0xc0, 0xfc, 0xc6, 0xc6, 0xe6, 0xdc, 0x00 }, /* b */
  175.     { 0x00, 0x00, 0x7c, 0xc6, 0xc0, 0xc0, 0x7e, 0x00 }, /* c */
  176.     { 0x06, 0x06, 0x7e, 0xc6, 0xc6, 0xce, 0x76, 0x00 }, /* d */
  177.     { 0x00, 0x00, 0x7c, 0xc6, 0xfe, 0xc0, 0x7e, 0x00 }, /* e */
  178.     { 0x1e, 0x30, 0x7c, 0x30, 0x30, 0x30, 0x30, 0x00 }, /* f */
  179.     { 0x00, 0x00, 0x7e, 0xc6, 0xce, 0x76, 0x06, 0x7c }, /* g */
  180.     { 0xc0, 0xc0, 0xfc, 0xc6, 0xc6, 0xc6, 0xc6, 0x00 }, /*  */
  181.     { 0x18, 0x00, 0x38, 0x18, 0x18, 0x18, 0x3c, 0x00 }, /* i */
  182.     { 0x18, 0x00, 0x38, 0x18, 0x18, 0x18, 0x18, 0xf0 }, /* j */
  183.     { 0xc0, 0xc0, 0xcc, 0xd8, 0xf0, 0xd8, 0xcc, 0x00 }, /* k */
  184.     { 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x00 }, /* l */
  185.     { 0x00, 0x00, 0xcc, 0xfe, 0xd6, 0xc6, 0xc6, 0x00 }, /* m */
  186.     { 0x00, 0x00, 0xfc, 0xc6, 0xc6, 0xc6, 0xc6, 0x00 }, /* n */
  187.     { 0x00, 0x00, 0x7c, 0xc6, 0xc6, 0xc6, 0x7c, 0x00 }, /* o */
  188.     { 0x00, 0x00, 0xfc, 0xc6, 0xc6, 0xe6, 0xdc, 0xc0 }, /* p */
  189.     { 0x00, 0x00, 0x7e, 0xc6, 0xc6, 0xce, 0x76, 0x06 }, /* q */
  190.     { 0x00, 0x00, 0x6e, 0x70, 0x60, 0x60, 0x60, 0x00 }, /* r */
  191.     { 0x00, 0x00, 0x7c, 0xc0, 0x7c, 0x06, 0xfc, 0x00 }, /* s */
  192.     { 0x30, 0x30, 0x7c, 0x30, 0x30, 0x30, 0x1c, 0x00 }, /* t */
  193.     { 0x00, 0x00, 0xc6, 0xc6, 0xc6, 0xc6, 0x7e, 0x00 }, /* u */
  194.     { 0x00, 0x00, 0xc6, 0xc6, 0xc6, 0x6c, 0x38, 0x00 }, /* v */
  195.     { 0x00, 0x00, 0xc6, 0xc6, 0xd6, 0xfe, 0x6c, 0x00 }, /* w */
  196.     { 0x00, 0x00, 0xc6, 0x6c, 0x38, 0x6c, 0xc6, 0x00 }, /* x */
  197.     { 0x00, 0x00, 0xc6, 0xc6, 0xce, 0x76, 0x06, 0x7c }, /* y */
  198.     { 0x00, 0x00, 0xfc, 0x18, 0x30, 0x60, 0xfc, 0x00 }, /* z */
  199.     { 0x0e, 0x18, 0x18, 0x70, 0x18, 0x18, 0x0e, 0x00 }, /* { */
  200.     { 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00 }, /* | */
  201.     { 0xe0, 0x30, 0x30, 0x1c, 0x30, 0x30, 0xe0, 0x00 }, /* } */
  202.     { 0x00, 0x00, 0x70, 0x9a, 0x0e, 0x00, 0x00, 0x00 }, /* ~ */
  203.     { 0x00, 0x00, 0x18, 0x3c, 0x66, 0xff, 0x00, 0x00 } /* Ascii 127 */
  204. };
  205.  
  206. static void QuitGifError(GifFileType *GifFile);
  207. static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
  208.                     int BufferWidth, int ForeGroundIndex);
  209.  
  210. /******************************************************************************
  211. * Interpret the command line and generate the given GIF file.              *
  212. ******************************************************************************/
  213. void main(int argc, char **argv)
  214. {
  215.     int    i, j, l, Error, ImageWidth, ImageHeight, NumOfLines, LogNumLevels,
  216.     NumLevels, ClrMapSizeFlag = FALSE, ColorMapSize = 1, ColorFlag = FALSE,
  217.     ForeGroundIndex = DEFAULT_FG_INDEX, ForeGroundFlag = FALSE,
  218.     TextLineFlag = FALSE, HelpFlag = FALSE;
  219.     char *TextLines[MAX_NUM_TEXT_LINES], Line[LINE_LEN];
  220.     GifRowType RasterBuffer[8];
  221.     GifColorType *ColorMap;
  222.     GifFileType *GifFile;
  223.  
  224.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  225.         &ClrMapSizeFlag, &ColorMapSize,
  226.         &ForeGroundFlag, &ForeGroundIndex,
  227.         &ColorFlag, &RedColor, &GreenColor, &BlueColor,
  228.         &TextLineFlag, &TextLines[0],
  229.         &HelpFlag)) != FALSE) {
  230.     GAPrintErrMsg(Error);
  231.     GAPrintHowTo(CtrlStr);
  232.     exit(1);
  233.     }
  234.  
  235.     if (HelpFlag) {
  236.     fprintf(stderr, VersionStr);
  237.     GAPrintHowTo(CtrlStr);
  238.     exit(0);
  239.     }
  240.  
  241.     if (ForeGroundIndex > 255 || ForeGroundIndex < 1)
  242.     GIF_EXIT("Foregound (-f) should be in the range 1..255, aborted.");
  243.  
  244.     if (ColorMapSize > 8 || ColorMapSize < 1)
  245.     GIF_EXIT("ColorMapSize (-s) should be in the range 1..8, aborted.");
  246.  
  247.     if (TextLineFlag) {
  248.     NumOfLines = 1;
  249.     ImageHeight = 8;
  250.     ImageWidth = 8 * strlen(TextLines[0]);
  251.     }
  252.     else {
  253.     NumOfLines = l = 0;
  254.     while (fgets(Line, LINE_LEN - 1, stdin)) {
  255.         for (i = strlen(Line); i > 0 && Line[i-1] <= ' '; i--);
  256.         Line[i] = 0;
  257.         if (l < i) l = i;
  258.         TextLines[NumOfLines++] = strdup(Line);
  259.         if (NumOfLines == MAX_NUM_TEXT_LINES)
  260.         GIF_EXIT("Input file has too many lines, aborted.");
  261.     }
  262.     if (NumOfLines == 0)
  263.         GIF_EXIT("No input text, aborted.");
  264.     ImageHeight = 8 * NumOfLines;
  265.     ImageWidth = 8 * l;
  266.     }
  267.  
  268.     /* Allocate the raster buffer for 8 scan lines (one text line). */
  269.     for (i = 0; i < 8; i++)
  270.     if ((RasterBuffer[i] = (GifRowType) malloc(sizeof(GifPixelType) *
  271.                             ImageWidth)) == NULL)
  272.         GIF_EXIT("Failed to allocate memory required, aborted.");
  273.  
  274.     /* Open stdout for the output file: */
  275.     if ((GifFile = EGifOpenFileHandle(1)) == NULL)
  276.     QuitGifError(GifFile);
  277.  
  278.     /* Dump out screen description with given size and generated color map: */
  279.     for (LogNumLevels = 1, NumLevels = 2;
  280.      NumLevels < ForeGroundIndex;
  281.      LogNumLevels++, NumLevels <<= 1);
  282.     if (NumLevels < (1 << ColorMapSize)) {
  283.         NumLevels = (1 << ColorMapSize);
  284.     LogNumLevels = ColorMapSize;
  285.     }
  286.  
  287.     if ((ColorMap = (GifColorType *) malloc(NumLevels * sizeof(GifColorType)))
  288.     == NULL) GIF_EXIT("Failed to allocate memory required, aborted.");
  289.  
  290.     for (i = 0; i < NumLevels; i++)
  291.     ColorMap[i].Red = ColorMap[i].Green = ColorMap[i].Blue = 0;
  292.     ColorMap[ForeGroundIndex].Red = RedColor;
  293.     ColorMap[ForeGroundIndex].Green = GreenColor;
  294.     ColorMap[ForeGroundIndex].Blue = BlueColor;
  295.  
  296.     if (EGifPutScreenDesc(GifFile,
  297.     ImageWidth, ImageHeight, LogNumLevels, 0, LogNumLevels, ColorMap)
  298.     == GIF_ERROR)
  299.     QuitGifError(GifFile);
  300.  
  301.     /* Dump out the image descriptor: */
  302.     if (EGifPutImageDesc(GifFile,
  303.     0, 0, ImageWidth, ImageHeight, FALSE, LogNumLevels, NULL) == GIF_ERROR)
  304.     QuitGifError(GifFile);
  305.  
  306.     fprintf(stderr, "\n%s: Image 1 at (%d, %d) [%dx%d]:     ",
  307.             PROGRAM_NAME, GifFile -> ILeft, GifFile -> ITop,
  308.             GifFile -> IWidth, GifFile -> IHeight);
  309.  
  310.     for (i = l = 0; i < NumOfLines; i++) {
  311.     GenRasterTextLine(RasterBuffer, TextLines[i], ImageWidth,
  312.                             ForeGroundIndex);
  313.     for (j = 0; j < 8; j++) {
  314.         if (EGifPutLine(GifFile, RasterBuffer[j], ImageWidth) == GIF_ERROR)
  315.         QuitGifError(GifFile);
  316.         fprintf(stderr, "\b\b\b\b%-4d", l++);
  317.     }
  318.     }
  319.  
  320.     if (EGifCloseFile(GifFile) == GIF_ERROR)
  321.     QuitGifError(GifFile);
  322. }
  323.  
  324. /******************************************************************************
  325. * Close output file (if open), and exit.                      *
  326. ******************************************************************************/
  327. static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
  328.                     int BufferWidth, int ForeGroundIndex)
  329. {
  330.     char c;
  331.     unsigned char Byte, Mask;
  332.     int i, j, k, CharPosX, Len = strlen(TextLine);
  333.  
  334.     for (i = 0; i < BufferWidth; i++)
  335.         for (j = 0; j < 8; j++) RasterBuffer[j][i] = 0;
  336.  
  337.     for (i = CharPosX = 0; i < Len; i++, CharPosX += 8) {
  338.     c = TextLine[i];
  339.     for (j = 0; j < 8; j++) {
  340.         Byte = AsciiTable[c][j];
  341.         for (k = 0, Mask = 128; k < 8; k++, Mask >>= 1)
  342.         if (Byte & Mask)
  343.             RasterBuffer[j][CharPosX + k] = ForeGroundIndex;
  344.     }
  345.     }
  346. }
  347.  
  348. /******************************************************************************
  349. * Close output file (if open), and exit.                      *
  350. ******************************************************************************/
  351. static void QuitGifError(GifFileType *GifFile)
  352. {
  353.     PrintGifError();
  354.     if (GifFile != NULL) EGifCloseFile(GifFile);
  355.     exit(1);
  356. }
  357.