GRAPH.C
上传用户:sunrenlu
上传日期:2022-06-13
资源大小:1419k
文件大小:18k
源码类别:

操作系统开发

开发平台:

DOS

  1. /*
  2.  * Copyright (c) 1990, 1999 Erick Engelke
  3.  */
  4. #include <rtos.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <dos.h>
  8. #include <stdlib.h>
  9. #include <net.h>
  10. #include <mem.h>
  11. #include <graph.h>
  12. #include <values.h>
  13. void *gr_alloc( int width, int height )
  14. {
  15.     DWORD bytes;
  16.     int i;
  17.     graph_x *g;
  18.     BYTE *bp;
  19.     bytes = (DWORD)width * (DWORD)height;
  20.     i = bytes;
  21.     if ( i < bytes ) return( NULL );
  22.     bp = kcalloc( i, 1 );
  23.     if ( bp == NULL ) return( NULL );
  24.     g = kcalloc( sizeof( graph_x ), 1 );
  25.     if ( g == NULL ) {
  26.         kfree( bp );
  27.         return( NULL );
  28.     }
  29.     g->g_data = bp;
  30.     g->g_width = width;
  31.     g->g_height = height;
  32.     return( g );
  33. }
  34. void gr_free( void *x )
  35. {
  36.     graph_x *g = x;
  37.     if ( g != NULL ) {
  38.         kfree( g->g_data );
  39.         kfree( g );
  40.     }
  41. }
  42. void gr_background( void *z, int col )
  43. {
  44.     graph_x *g = z;
  45.     g->g_background = col;
  46.     memset( g->g_data, col, g->g_width * g->g_height );
  47. }
  48. void gr_setpixel( void *z, int x, int y, int cols )
  49. {
  50.     graph_x *g = z;
  51.     if (( x >= 0 ) && ( x < g->g_width ) && ( y >= 0 ) && ( y < g->g_height ))
  52.         g->g_data[ x + ( y * g->g_width) ] = cols;
  53. }
  54. int gr_getpixel( void *z, int x, int y )
  55. {
  56.     graph_x *g = z;
  57.     static int oldy = 0, ymult = 0;
  58.     /* avoid multiplications when we can */
  59.     if ( oldy != y ) {
  60.         ymult = y * g->g_width;
  61.         oldy = y;
  62.     }
  63.     if (( x >= 0 ) && ( x < g->g_width ) && ( y >= 0 ) && ( y < g->g_height ))
  64.         return( g->g_data[ x + ymult  ]  );
  65.     else
  66.         return( g->g_background );
  67. }
  68. static void swapxy( int *x1, int *y1, int *x2, int *y2 )
  69. {
  70.     int temp;
  71.     temp = *x1; *x1 = *x2; *x2 = temp;
  72.     temp = *y1; *y1 = *y2; *y2 = temp;
  73. }
  74. void gr_line( void *z, int x1, int y1, int x2, int y2, int color )
  75. {
  76.     int difx, dify;
  77.     int temp;
  78.     difx = x2 - x1;
  79.     if ( difx < 0 ) {
  80.         swapxy( &x1, &y1, &x2, &y2 );
  81.         difx = x2 - x1;
  82.     }
  83.     /* now x1 < x2 */
  84.     dify = y2 - y1;
  85.     temp = dify;
  86.     if ( temp < 0 ) temp = -temp;
  87.     if ( difx > temp ) {
  88.         for ( temp = 0 ; temp <= difx ; ++temp )
  89.             gr_setpixel( z, temp + x1, temp*dify/difx + y1,color);
  90.     } else {
  91.         if ( dify < 0 ) {
  92.             swapxy( &x1, &y1, &x2, &y2 );
  93.             difx = -difx;
  94.             dify = -dify;
  95.         }
  96.         for ( temp = 0 ; temp < dify ; ++temp )
  97.             gr_setpixel( z, temp*difx/dify + x1, temp + y1, color);
  98.     }
  99. }
  100. #define HLEN 9
  101. #if defined(__TURBOC__) || defined(__BORLANDC__)
  102. static char font[96][HLEN] = {
  103.         0000,0000,0000,0000,0000,0000,0000,0000,0000,   /* sp */
  104.         0010,0010,0010,0010,0010,0000,0010,0000,0000,   /* ! */
  105.         0024,0024,0024,0000,0000,0000,0000,0000,0000,   /* QUOTE */
  106.         0024,0024,0076,0024,0076,0024,0024,0000,0000,   /* # */
  107.         0010,0036,0050,0034,0012,0074,0010,0000,0000,   /* $ */
  108.         0060,0062,0004,0010,0020,0046,0006,0000,0000,   /* % */
  109.         0010,0024,0024,0030,0052,0044,0032,0000,0000,   /* & */
  110.         0010,0010,0020,0000,0000,0000,0000,0000,0000,   /* ' */
  111.         0004,0010,0020,0020,0020,0010,0004,0000,0000,   /* ( */
  112.         0020,0010,0004,0004,0004,0010,0020,0000,0000,   /* ) */
  113.         0000,0010,0052,0034,0052,0010,0000,0000,0000,   /* * */
  114.         0000,0010,0010,0076,0010,0010,0000,0000,0000,   /* + */
  115.         0000,0000,0000,0000,0000,0030,0030,0010,0020,   /* , */
  116.         0000,0000,0000,0076,0000,0000,0000,0000,0000,   /* - */
  117.         0000,0000,0000,0000,0000,0030,0030,0000,0000,   /* . */
  118.         0001,0002,0004,0010,0020,0040,0100,0000,0000,   /* / */
  119.         0034,0042,0046,0052,0062,0042,0034,0000,0000,   /* 0 */
  120.         0010,0030,0010,0010,0010,0010,0034,0000,0000,   /* 1 */
  121.         0034,0042,0002,0004,0010,0020,0076,0000,0000,   /* 2 */
  122.         0076,0004,0010,0004,0002,0042,0034,0000,0000,   /* 3 */
  123.         0004,0014,0024,0044,0076,0004,0004,0000,0000,   /* 4 */
  124.         0076,0040,0074,0002,0002,0042,0034,0000,0000,   /* 5 */
  125.         0014,0020,0040,0074,0042,0042,0034,0000,0000,   /* 6 */
  126.         0076,0002,0004,0010,0020,0020,0020,0000,0000,   /* 7 */
  127.         0034,0042,0042,0034,0042,0042,0034,0000,0000,   /* 8 */
  128.         0034,0042,0042,0036,0002,0004,0030,0000,0000,   /* 9 */
  129.         0000,0030,0030,0000,0030,0030,0000,0000,0000,   /* : */
  130.         0000,0030,0030,0000,0030,0030,0010,0020,0000,   /* ; */
  131.         0002,0004,0010,0020,0010,0004,0002,0000,0000,   /* < */
  132.         0000,0000,0076,0000,0076,0000,0000,0000,0000,   /* = */
  133.         0040,0020,0010,0004,0010,0020,0040,0000,0000,   /* > */
  134.         0034,0042,0002,0004,0010,0000,0010,0000,0000,   /* ? */
  135.         0014,0022,0056,0052,0056,0040,0036,0000,0000,   /* @ */
  136.         0034,0042,0042,0076,0042,0042,0042,0000,0000,   /* A */
  137.         0074,0042,0042,0074,0042,0042,0074,0000,0000,   /* B */
  138.         0034,0042,0040,0040,0040,0042,0034,0000,0000,   /* C */
  139.         0070,0044,0042,0042,0042,0044,0070,0000,0000,   /* D */
  140.         0076,0040,0040,0074,0040,0040,0076,0000,0000,   /* E */
  141.         0076,0040,0040,0074,0040,0040,0040,0000,0000,   /* F */
  142.         0036,0040,0040,0046,0042,0042,0036,0000,0000,   /* G */
  143.         0042,0042,0042,0076,0042,0042,0042,0000,0000,   /* H */
  144.         0034,0010,0010,0010,0010,0010,0034,0000,0000,   /* I */
  145.         0034,0010,0010,0010,0010,0050,0020,0000,0000,   /* J */
  146.         0042,0044,0050,0060,0050,0044,0042,0000,0000,   /* K */
  147.         0040,0040,0040,0040,0040,0040,0076,0000,0000,   /* L */
  148.         0042,0066,0052,0052,0042,0042,0042,0000,0000,   /* M */
  149.         0042,0042,0062,0052,0046,0042,0042,0000,0000,   /* N */
  150.         0034,0042,0042,0042,0042,0042,0034,0000,0000,   /* O */
  151.         0074,0042,0042,0074,0040,0040,0040,0000,0000,   /* P */
  152.         0034,0042,0042,0042,0052,0044,0032,0000,0000,   /* Q */
  153.         0074,0042,0042,0074,0050,0044,0042,0000,0000,   /* R */
  154.         0034,0042,0040,0034,0002,0042,0034,0000,0000,   /* S */
  155.         0076,0010,0010,0010,0010,0010,0010,0000,0000,   /* T */
  156.         0042,0042,0042,0042,0042,0042,0034,0000,0000,   /* U */
  157.         0042,0042,0042,0024,0024,0010,0010,0000,0000,   /* V */
  158.         0042,0042,0042,0052,0052,0052,0024,0000,0000,   /* W */
  159.         0042,0042,0024,0010,0024,0042,0042,0000,0000,   /* X */
  160.         0042,0042,0024,0010,0010,0010,0010,0000,0000,   /* Y */
  161.         0076,0002,0004,0010,0020,0040,0076,0000,0000,   /* Z */
  162.         0034,0020,0020,0020,0020,0020,0034,0000,0000,   /* [ */
  163.         0100,0040,0020,0010,0004,0002,0001,0000,0000,   /*  */
  164.         0034,0004,0004,0004,0004,0004,0034,0000,0000,   /* ] */
  165.         0010,0024,0042,0000,0000,0000,0000,0000,0000,   /* ^ */
  166.         0000,0000,0000,0000,0000,0000,0076,0000,0000,   /* _ */
  167.         0010,0010,0004,0000,0000,0000,0000,0000,0000,   /* ` */
  168.         0000,0000,0034,0002,0036,0042,0036,0000,0000,   /* a */
  169.         0040,0040,0074,0042,0042,0042,0074,0000,0000,   /* b */
  170.         0000,0000,0030,0044,0040,0044,0030,0000,0000,   /* c */
  171.         0002,0002,0036,0042,0042,0042,0036,0000,0000,   /* d */
  172.         0000,0000,0034,0042,0076,0040,0034,0000,0000,   /* e */
  173.         0014,0022,0070,0020,0020,0020,0020,0000,0000,   /* f */
  174.         0000,0000,0032,0046,0042,0046,0032,0002,0034,   /* g */
  175.         0040,0040,0074,0042,0042,0042,0042,0000,0000,   /* h */
  176.         0010,0000,0030,0010,0010,0010,0034,0000,0000,   /* i */
  177.         0004,0000,0004,0004,0004,0004,0004,0044,0030,   /* j */
  178.         0040,0040,0044,0050,0064,0042,0042,0000,0000,   /* k */
  179.         0030,0010,0010,0010,0010,0010,0034,0000,0000,   /* l */
  180.         0000,0000,0064,0052,0052,0052,0052,0000,0000,   /* m */
  181.         0000,0000,0074,0042,0042,0042,0042,0000,0000,   /* n */
  182.         0000,0000,0034,0042,0042,0042,0034,0000,0000,   /* o */
  183.         0000,0000,0054,0062,0042,0062,0054,0040,0040,   /* p */
  184.         0000,0000,0032,0046,0042,0046,0032,0002,0002,   /* q */
  185.         0000,0000,0054,0062,0040,0040,0040,0000,0000,   /* r */
  186.         0000,0000,0036,0040,0034,0002,0074,0000,0000,   /* s */
  187.         0020,0020,0070,0020,0020,0022,0014,0000,0000,   /* t */
  188.         0000,0000,0042,0042,0042,0046,0032,0000,0000,   /* u */
  189.         0000,0000,0042,0042,0042,0024,0010,0000,0000,   /* v */
  190.         0000,0000,0042,0042,0052,0052,0024,0000,0000,   /* w */
  191.         0000,0000,0042,0024,0010,0024,0042,0000,0000,   /* x */
  192.         0000,0000,0042,0042,0042,0046,0032,0002,0034,   /* y */
  193.         0000,0000,0076,0004,0010,0020,0076,0000,0000,   /* z */
  194.         0014,0020,0020,0040,0020,0020,0014,0000,0000,   /* { */
  195.         0010,0010,0010,0000,0010,0010,0010,0000,0000,   /* | */
  196.         0030,0004,0004,0002,0004,0004,0030,0000,0000,   /* } */
  197.         0020,0052,0004,0000,0000,0000,0000,0000,0000,   /* ~ */
  198.         0177,0177,0177,0177,0177,0177,0177,0177,0177,   /* del */
  199. };
  200. #elif defined(__DJGPP__)
  201. static char font[96][HLEN] = {
  202.       { 0000,0000,0000,0000,0000,0000,0000,0000,0000 },   /* sp */
  203.       { 0010,0010,0010,0010,0010,0000,0010,0000,0000},  /* ! */
  204.       { 0024,0024,0024,0000,0000,0000,0000,0000,0000},  /* QUOTE */
  205.       { 0024,0024,0076,0024,0076,0024,0024,0000,0000},  /* # */
  206.       { 0010,0036,0050,0034,0012,0074,0010,0000,0000},  /* $ */
  207.       { 0060,0062,0004,0010,0020,0046,0006,0000,0000},  /* % */
  208.       { 0010,0024,0024,0030,0052,0044,0032,0000,0000},  /* & */
  209.       { 0010,0010,0020,0000,0000,0000,0000,0000,0000},  /* ' */
  210.       { 0004,0010,0020,0020,0020,0010,0004,0000,0000},  /* ( */
  211.       { 0020,0010,0004,0004,0004,0010,0020,0000,0000},  /* ) */
  212.       { 0000,0010,0052,0034,0052,0010,0000,0000,0000},  /* * */
  213.       { 0000,0010,0010,0076,0010,0010,0000,0000,0000},  /* + */
  214.       { 0000,0000,0000,0000,0000,0030,0030,0010,0020},  /* , */
  215.       { 0000,0000,0000,0076,0000,0000,0000,0000,0000},  /* - */
  216.       { 0000,0000,0000,0000,0000,0030,0030,0000,0000},  /* . */
  217.       { 0001,0002,0004,0010,0020,0040,0100,0000,0000},  /* / */
  218.       { 0034,0042,0046,0052,0062,0042,0034,0000,0000},  /* 0 */
  219.       { 0010,0030,0010,0010,0010,0010,0034,0000,0000},  /* 1 */
  220.       { 0034,0042,0002,0004,0010,0020,0076,0000,0000},  /* 2 */
  221.       { 0076,0004,0010,0004,0002,0042,0034,0000,0000},  /* 3 */
  222.       { 0004,0014,0024,0044,0076,0004,0004,0000,0000},  /* 4 */
  223.       { 0076,0040,0074,0002,0002,0042,0034,0000,0000},  /* 5 */
  224.       { 0014,0020,0040,0074,0042,0042,0034,0000,0000},  /* 6 */
  225.       { 0076,0002,0004,0010,0020,0020,0020,0000,0000},  /* 7 */
  226.       { 0034,0042,0042,0034,0042,0042,0034,0000,0000},  /* 8 */
  227.       { 0034,0042,0042,0036,0002,0004,0030,0000,0000},  /* 9 */
  228.       { 0000,0030,0030,0000,0030,0030,0000,0000,0000},  /* : */
  229.       { 0000,0030,0030,0000,0030,0030,0010,0020,0000},  /* ; */
  230.       { 0002,0004,0010,0020,0010,0004,0002,0000,0000},  /* < */
  231.       { 0000,0000,0076,0000,0076,0000,0000,0000,0000},  /* = */
  232.       { 0040,0020,0010,0004,0010,0020,0040,0000,0000},  /* > */
  233.       { 0034,0042,0002,0004,0010,0000,0010,0000,0000},  /* ? */
  234.       { 0014,0022,0056,0052,0056,0040,0036,0000,0000},  /* @ */
  235.       { 0034,0042,0042,0076,0042,0042,0042,0000,0000},  /* A */
  236.       { 0074,0042,0042,0074,0042,0042,0074,0000,0000},  /* B */
  237.       { 0034,0042,0040,0040,0040,0042,0034,0000,0000},  /* C */
  238.       { 0070,0044,0042,0042,0042,0044,0070,0000,0000},  /* D */
  239.       { 0076,0040,0040,0074,0040,0040,0076,0000,0000},  /* E */
  240.       { 0076,0040,0040,0074,0040,0040,0040,0000,0000},  /* F */
  241.       { 0036,0040,0040,0046,0042,0042,0036,0000,0000},  /* G */
  242.       { 0042,0042,0042,0076,0042,0042,0042,0000,0000},  /* H */
  243.       { 0034,0010,0010,0010,0010,0010,0034,0000,0000},  /* I */
  244.       { 0034,0010,0010,0010,0010,0050,0020,0000,0000},  /* J */
  245.       { 0042,0044,0050,0060,0050,0044,0042,0000,0000},  /* K */
  246.       { 0040,0040,0040,0040,0040,0040,0076,0000,0000},  /* L */
  247.       { 0042,0066,0052,0052,0042,0042,0042,0000,0000},  /* M */
  248.       { 0042,0042,0062,0052,0046,0042,0042,0000,0000},  /* N */
  249.       { 0034,0042,0042,0042,0042,0042,0034,0000,0000},  /* O */
  250.       { 0074,0042,0042,0074,0040,0040,0040,0000,0000},  /* P */
  251.       { 0034,0042,0042,0042,0052,0044,0032,0000,0000},  /* Q */
  252.       { 0074,0042,0042,0074,0050,0044,0042,0000,0000},  /* R */
  253.       { 0034,0042,0040,0034,0002,0042,0034,0000,0000},  /* S */
  254.       { 0076,0010,0010,0010,0010,0010,0010,0000,0000},  /* T */
  255.       { 0042,0042,0042,0042,0042,0042,0034,0000,0000},  /* U */
  256.       { 0042,0042,0042,0024,0024,0010,0010,0000,0000},  /* V */
  257.       { 0042,0042,0042,0052,0052,0052,0024,0000,0000},  /* W */
  258.       { 0042,0042,0024,0010,0024,0042,0042,0000,0000},  /* X */
  259.       { 0042,0042,0024,0010,0010,0010,0010,0000,0000},  /* Y */
  260.       { 0076,0002,0004,0010,0020,0040,0076,0000,0000},  /* Z */
  261.       { 0034,0020,0020,0020,0020,0020,0034,0000,0000},  /* [ */
  262.       { 0100,0040,0020,0010,0004,0002,0001,0000,0000},  /*  */
  263.       { 0034,0004,0004,0004,0004,0004,0034,0000,0000},  /* ] */
  264.       { 0010,0024,0042,0000,0000,0000,0000,0000,0000},  /* ^ */
  265.       { 0000,0000,0000,0000,0000,0000,0076,0000,0000},  /* _ */
  266.       { 0010,0010,0004,0000,0000,0000,0000,0000,0000},  /* ` */
  267.       { 0000,0000,0034,0002,0036,0042,0036,0000,0000},  /* a */
  268.       { 0040,0040,0074,0042,0042,0042,0074,0000,0000},  /* b */
  269.       { 0000,0000,0030,0044,0040,0044,0030,0000,0000},  /* c */
  270.       { 0002,0002,0036,0042,0042,0042,0036,0000,0000},  /* d */
  271.       { 0000,0000,0034,0042,0076,0040,0034,0000,0000},  /* e */
  272.       { 0014,0022,0070,0020,0020,0020,0020,0000,0000},  /* f */
  273.       { 0000,0000,0032,0046,0042,0046,0032,0002,0034},  /* g */
  274.       { 0040,0040,0074,0042,0042,0042,0042,0000,0000},  /* h */
  275.       { 0010,0000,0030,0010,0010,0010,0034,0000,0000},  /* i */
  276.       { 0004,0000,0004,0004,0004,0004,0004,0044,0030},  /* j */
  277.       { 0040,0040,0044,0050,0064,0042,0042,0000,0000},  /* k */
  278.       { 0030,0010,0010,0010,0010,0010,0034,0000,0000},  /* l */
  279.       { 0000,0000,0064,0052,0052,0052,0052,0000,0000},  /* m */
  280.       { 0000,0000,0074,0042,0042,0042,0042,0000,0000},  /* n */
  281.       { 0000,0000,0034,0042,0042,0042,0034,0000,0000},  /* o */
  282.       { 0000,0000,0054,0062,0042,0062,0054,0040,0040},  /* p */
  283.       { 0000,0000,0032,0046,0042,0046,0032,0002,0002},  /* q */
  284.       { 0000,0000,0054,0062,0040,0040,0040,0000,0000},  /* r */
  285.       { 0000,0000,0036,0040,0034,0002,0074,0000,0000},  /* s */
  286.       { 0020,0020,0070,0020,0020,0022,0014,0000,0000},  /* t */
  287.       { 0000,0000,0042,0042,0042,0046,0032,0000,0000},  /* u */
  288.       { 0000,0000,0042,0042,0042,0024,0010,0000,0000},  /* v */
  289.       { 0000,0000,0042,0042,0052,0052,0024,0000,0000},  /* w */
  290.       { 0000,0000,0042,0024,0010,0024,0042,0000,0000},  /* x */
  291.       { 0000,0000,0042,0042,0042,0046,0032,0002,0034},  /* y */
  292.       { 0000,0000,0076,0004,0010,0020,0076,0000,0000},  /* z */
  293.       { 0014,0020,0020,0040,0020,0020,0014,0000,0000},  /* { */
  294.       { 0010,0010,0010,0000,0010,0010,0010,0000,0000},  /* | */
  295.       { 0030,0004,0004,0002,0004,0004,0030,0000,0000},  /* } */
  296.       { 0020,0052,0004,0000,0000,0000,0000,0000,0000},  /* ~ */
  297.       { 0177,0177,0177,0177,0177,0177,0177,0177,0177 }  /* del */
  298. };
  299. #endif
  300. void gr_char_at( void *z , char ch, int x, int y, int color )
  301. {
  302.     graph_x *g;
  303.     int xx, yy;
  304.     char *cp;
  305.     BYTE b, mask;
  306.     g = z;
  307.     cp = font[ ch - ' ' ];
  308.     for ( yy = 8 ; yy > 0 ; --yy ) {
  309.         b = *cp++;
  310.         for ( xx = 0 , mask = 0x80 ; mask ; ++xx, mask >>= 1 ) {
  311.             if ( b & mask )
  312.                 gr_setpixel( g, x + xx, y + yy, color );
  313.         }
  314.     }
  315. }
  316. void gr_text_at( void *z , char *s, int x, int y, int color )
  317. {
  318.     int i;
  319.     while (*s) {
  320.         gr_char_at( z, *s++, x, y, color );
  321.         x += 9;
  322.     }
  323. }
  324. void gr_linegraph( void *z, int numpoints, int *ys,
  325.     char *x1, char *x2, int textcolor, int color )
  326. {
  327.     int i;
  328.     int max = -MAXINT;
  329.     int min = MAXINT;
  330.     graph_x *g;
  331.     char buffer[ 16 ];
  332.     int prevx, curx, cury, dif;
  333.     g = z;
  334.     for ( i = 0 ; i < numpoints ; ++i ) {
  335.         cury = ys[i];
  336.         if ( cury > max ) max = cury;
  337.         if ( cury < min ) min = cury;
  338.     }
  339.     if ( max == min ) max++;
  340.     dif = max - min;
  341.     gr_line( g, 0, 9, g->g_width -1, 9, 0 );
  342.     gr_line( g, 0, 9, 0, g->g_height-1, 0 );
  343.     itoa( max, buffer, 10 );
  344.     gr_text_at( g, buffer, 1, g->g_height - 10, textcolor );
  345.     itoa( min, buffer, 10 );
  346.     gr_text_at( g, buffer, 1, 10, textcolor );
  347.     if ( x1 != NULL ) gr_text_at( g, x1, 1, 0, textcolor );
  348.     if ( x2 != NULL ) gr_text_at( g, x2,
  349.             g->g_width - strlen( x2 ) * 9, 0,  textcolor );
  350. #define SCALEX( x ) (int)( (DWORD)(x) * g->g_width / numpoints )
  351. #define SCALEY( y ) ((int)( (DWORD)(y-min) * (g->g_height - 10) / dif )+9)
  352.     curx = 0;
  353.     for ( i = 1 ; i < numpoints ; ++i ) {
  354.         prevx = curx;
  355.         curx = SCALEX( i );
  356.         gr_setpixel( g, curx, 8, 0 );
  357.         gr_line( g, prevx, // SCALEX(i - 1),
  358.                     SCALEY( ys[i-1] ),
  359.                     curx, // SCALEX( i ),
  360.                     SCALEY( ys[ i ] ),
  361.                     color );
  362.     }
  363. }
  364. /*
  365.  * Gif Conversion - this section must be single threaded
  366.  */
  367. static graph_x *cur = NULL;
  368. #include "gifsave.h"
  369. static int gpixel(int x, int y)
  370. {
  371.     return gr_getpixel(cur, x, cur->g_height - y);
  372. }
  373. void gr_gif( tcp_Socket *s, void *z )
  374. {
  375.     graph_x *g;
  376. #define NUMCOLORS          8  /* Number of different colors */
  377. #define BITS_PR_PRIM_COLOR 1  /* Two bits pr primary color */
  378.     int q;                      /* Counter */
  379.     sock_mode( s, TCP_MODE_BINARY | TCP_MODE_NONAGLE );
  380.     g = z;
  381.     cur = g;
  382.     GIF_Create(
  383.         s,
  384.         g->g_width, g->g_height-1, NUMCOLORS, BITS_PR_PRIM_COLOR );
  385.     /*
  386.      *  Set each color according to the values extracted from
  387.      *  the palette
  388.      */
  389.     for (q = 0; q < NUMCOLORS; q++)
  390.         /*   Red, Green, Blue */
  391.         GIF_SetColor(q, q & 1, (q >> 1) &1, (q>>2) & 1 );
  392.     /*
  393.      *  Store the entire screen as an image using the user defined
  394.      *  callback function gpixel() to get pixel values from the screen
  395.      */
  396.     GIF_CompressImage(z, 0, 0, -1, -1, gr_getpixel);
  397.     /*
  398.      *  Finish it all and close the file
  399.      */
  400.     GIF_Close();
  401. }