home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / graf / plotfrac.zip / GRAFIX.C next >
Text File  |  1987-10-26  |  10KB  |  364 lines

  1. /*
  2.  
  3.         G R A F I X . C
  4.  
  5.         VERSION     :     3.0
  6.         EDIT-DATE   :     25-Oct-87
  7.  
  8.  
  9.     My own Int. 10h driven BIOS ROM graphics routines.  These will only
  10.     work on the IBM-PC or compatable; and are recommended for use only
  11.     with Microsoft 'C' Version 4.00 or later under MS-DOS 3.10 or later.
  12.  
  13.         M O D I F I C A T I O N   H I S T O R Y
  14.  
  15.     VERSION                 DESCRIPTION
  16.     -------                 -----------
  17.       1.0        Creation, supports CGA modes 4, 5 and 6 only.  No EGA
  18.                  modes supported.
  19.  
  20.       2.0        Added support for EGA modes 0Dh and 0Eh (13 and 14)...
  21.                  320 X 200 with 16 colors and 640 X 200 with 16 colors.
  22.  
  23.       2.1        Changed the save_pic() and load_pic()| functions to make
  24.                  them "smarter"; as well as save additional information.
  25.                  Although this information may be somewhat specific to the
  26.                  PLOTFRAC program, the information may be useful to other
  27.                  applications.
  28.  
  29.       3.0        Added the support for EGA mode 10h (16)...
  30.                  640 X 350.  This mode requires not only an EGA card, but
  31.                  also an Enhanced Color Display (ECD) -- a.k.a. EGA monitor
  32.                  or a Multi-Sync monitor.
  33.  
  34. */
  35.  
  36. #include <dos.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <math.h>
  40. #include <float.h>
  41.  
  42. #define ROM_BIOS       0x10     /* INT 10H calls the IBM-PC ROM
  43.                                    BIOS video driver services       */
  44.  
  45. #define DOS_INT        0x21     /* MS-DOS Int. 21h service call */
  46.  
  47. #define PRN_OUTPUT     0x05     /* Function 05h sends output to PRN: */
  48.  
  49. #define SET_VI_MODE    0x00     /* Function 00h sets the video mode
  50.                                    calls.                           */
  51.  
  52. #define SET_PALETTE    0x0B     /* Function 0Bh sets the color
  53.                                    palette for 320 X 200 medium-res */
  54.  
  55. #define WRITE_PIXEL    0x0C     /* Function 0Ch writes a pixel at
  56.                                    location specified by CX & DX    */
  57.  
  58. #define READ_PIXEL     0x0D     /* Function 0Dh reads a pixel at
  59.                                    location specified by CX & DX    */
  60.  
  61. #define GET_DIS_MODE   0x0F     /* Function 0Fh gets the current
  62.                                    display mode and saves all the
  63.                                    relevant information.            */
  64.  
  65. #define SELECT_PAGE    0x05     /* Function 05h selects the current
  66.                                    active display page              */
  67.  
  68. extern void get_mode (void);
  69. extern void set_mode (int);
  70. extern void set_palette (int);
  71. extern void set_background (int);
  72. extern void plot_point (int, int, int);
  73. extern void set_page (int);
  74. extern void save_pic (char *, double *, int *);
  75. extern void load_pic (char *);
  76. extern int read_point (int, int);
  77. extern void print_pic (void);
  78. extern void prn_out (int);
  79.  
  80. FILE *PIC_STREAM;
  81.  
  82. union REGS inregs, outregs;
  83. struct SREGS segregs;
  84.  
  85. char P_BUFFER[350];
  86. int bits[8] = {1,2,4,8,16,32,64,128};
  87.  
  88. int CURRENT_PAGE = 0, CURRENT_MODE = 0, PALETTE = 0, BACKGROUND = 0,
  89.     USE_MODE = 0;
  90.  
  91. void get_mode()
  92. {
  93.     register int i;
  94.     int result;
  95.  
  96.     for (i=0;i<350;i++)
  97.         P_BUFFER[i] = 0;
  98.     inregs.h.ah = GET_DIS_MODE;
  99.     result = int86x(ROM_BIOS, &inregs, &outregs, &segregs);
  100.     CURRENT_PAGE = outregs.h.bh;
  101.     CURRENT_MODE = outregs.h.al;
  102.  
  103. } /* End of GET_MODE */
  104.  
  105. void set_mode(mode_val)
  106. int mode_val;
  107. {
  108.     int result;
  109.  
  110.     USE_MODE = mode_val;
  111.     inregs.h.ah = SET_VI_MODE;
  112.     inregs.h.al = mode_val;
  113.     result = int86x(ROM_BIOS, &inregs, &outregs, &segregs);
  114.  
  115. } /* End of SET_MODE */
  116.  
  117. void set_palette(palette_val)
  118. int palette_val;
  119. {
  120.     int result;
  121.  
  122.     if (USE_MODE == 0x06 || USE_MODE == 0x0D || USE_MODE == 0x0E)
  123.     return;
  124.     PALETTE = palette_val;
  125.     inregs.h.ah = SET_PALETTE;
  126.     inregs.h.bh = 1;
  127.     inregs.h.bl = palette_val;
  128.     result = int86x(ROM_BIOS, &inregs, &outregs, &segregs);
  129.  
  130. } /* End of SET_PALETTE */
  131.  
  132. void set_background(background_val)
  133. int background_val;
  134. {
  135.     int result;
  136.  
  137.     if (USE_MODE == 0x06 || USE_MODE == 0x0D || USE_MODE == 0x0E)
  138.     return;
  139.     BACKGROUND = background_val;
  140.     inregs.h.ah = SET_PALETTE;
  141.     inregs.h.bh = 0;
  142.     inregs.h.bl = background_val;
  143.     result = int86x(ROM_BIOS, &inregs, &outregs, &segregs);
  144.  
  145. } /* End of SET_BACKGROUND */
  146.  
  147. void plot_point(x_val, y_val, plot_color)
  148. int x_val, y_val, plot_color;
  149. {
  150.     int result, max_col, max_row;
  151.  
  152.     max_row = 199;
  153.     switch (USE_MODE) {
  154.     case 4:              /* 320 X 200, 4 color */
  155.     case 5:              /* 320 X 200, 4 color (burst off) */
  156.     case 13:             /* 320 X 200, 16 color (EGA) */
  157.         max_col = 319;
  158.         break;
  159.     case 6:              /* 640 X 200, 2 color */
  160.     case 14:             /* 640 X 200, 16 color (EGA) */
  161.     case 16:             /* 640 X 350, 16 color (EGA) */
  162.         max_col = 639;
  163.         if (USE_MODE == 16)
  164.             max_row = 349;
  165.         break;
  166.     }
  167.     if (x_val < 0 )
  168.     x_val = 0;
  169.     if (x_val > max_col)
  170.     x_val = max_col;
  171.     if (y_val < 0)
  172.     y_val = 0;
  173.     if (y_val > max_row)
  174.     y_val = max_row;
  175.  
  176.     inregs.h.ah = WRITE_PIXEL;
  177.     inregs.h.al = plot_color;
  178.     inregs.x.cx = x_val;
  179.     inregs.x.dx = y_val;
  180.     result = int86x(ROM_BIOS, &inregs, &outregs, &segregs);
  181.  
  182. } /* End of PLOT_POINT */
  183.  
  184. void set_page(page_num)
  185. int page_num;
  186. {
  187.     int result;
  188.  
  189.     inregs.h.al = SELECT_PAGE;
  190.     inregs.h.al = page_num;
  191.     result = int86x(ROM_BIOS, &inregs, &outregs, &segregs);
  192.  
  193. } /* End of SET_PAGE */
  194.  
  195. int read_point(n_col,n_row)
  196. int n_col, n_row;
  197. {
  198.     int result;
  199.  
  200.     inregs.h.ah = READ_PIXEL;
  201.     inregs.x.cx = n_col;
  202.     inregs.x.dx = n_row;
  203.     result = int86x(ROM_BIOS, &inregs, &outregs, &segregs);
  204.     return (outregs.h.al);
  205.  
  206. } /* End of READ_POINT */
  207.  
  208. void save_pic(save_fname, xy_data, gr_data)
  209. char *save_fname;
  210. double *xy_data;
  211. int *gr_data;
  212. {
  213.     int wr_result, row_num, col_num, max_col, p_num, max_row;
  214.  
  215.     max_row = 199;
  216.     switch (USE_MODE) {
  217.     case 4:
  218.     case 5:
  219.     case 13:
  220.         max_col = 320;
  221.         break;
  222.     case 6:
  223.     case 14:
  224.     case 16:
  225.         max_col = 640;
  226.         if (USE_MODE == 16)
  227.             max_row = 349;
  228.         break;
  229.     }
  230.  
  231.     PIC_STREAM = fopen(save_fname,"w+b");
  232.  
  233. /*
  234.     First let us save the following information:
  235.     xy_data - The minimum and maximum X-Y or P-Q values
  236.     gr_data - The resolution of the plot (320 or 640, and 200 or 350) and
  237.           the grafix mode used
  238. */
  239.     wr_result = fwrite((char*)xy_data, sizeof(double),4,PIC_STREAM);
  240.     wr_result = fwrite((char*)gr_data, sizeof(int),3,PIC_STREAM);
  241.  
  242. /*
  243.     Now save the picture, one column at a time.
  244. */
  245.     for (col_num=0;col_num<max_col;col_num++) {
  246.     for (row_num=0;row_num<max_row+1;row_num++) {
  247.         p_num = (read_point(col_num,row_num) + 32);
  248.         P_BUFFER[row_num] = p_num;
  249.     }
  250.     wr_result = fwrite((char *)P_BUFFER,sizeof(char),max_row+1,PIC_STREAM);
  251.     }
  252.     fclose(PIC_STREAM);
  253.  
  254. } /* End of SAVE_PIC */
  255.  
  256. void load_pic(save_fname)
  257. char *save_fname;
  258. {
  259.     int seek_result, row_num, col_num, max_col, max_row;
  260.     int gr_stuf[3];
  261.     double xy_stuf[4];
  262.  
  263.     max_row = 199;
  264.     switch (USE_MODE) {
  265.     case 4:
  266.     case 5:
  267.     case 13:
  268.         max_col = 320;
  269.         break;
  270.     case 6:
  271.     case 14:
  272.     case 16:
  273.         max_col = 640;
  274.         if (USE_MODE == 16)
  275.             max_row = 349;
  276.         break;
  277.     }
  278.  
  279.     PIC_STREAM = fopen(save_fname,"r+b");
  280.     seek_result = fseek(PIC_STREAM,0L,SEEK_SET);
  281.  
  282. /*
  283.     This routine reads the "file heading" info saved by save_pic(); but
  284.     doesn't do anything with it since all of that is handled currently
  285.     within the PLOTFRAC main routine restore_picture().  This will be
  286.     changing with the next version of GRAFIX.
  287. */
  288.     seek_result = fread((char*)xy_stuf,sizeof(double),4,PIC_STREAM);
  289.     seek_result = fread((char*)gr_stuf,sizeof(int),3,PIC_STREAM);
  290.  
  291. /*
  292.      Now read the picture one column at a time and restore it.
  293. */
  294.     for (col_num=0;col_num<max_col;col_num++) {
  295.     seek_result = fread((char *)P_BUFFER,sizeof(char),max_row+1,PIC_STREAM);
  296.     for (row_num=0;row_num<=max_row;row_num++) {
  297.         plot_point(col_num,row_num,((int)P_BUFFER[row_num] - 32));
  298.     }
  299.     }
  300.  
  301.     fclose(PIC_STREAM);
  302.  
  303. } /* End of LOAD_PIC */
  304.  
  305. void print_pic()
  306. {
  307.     int pr_num, row_num, col_num, max_col, pix_val, row_offset, max_offset,
  308.         max_row;
  309.  
  310.     max_row = 199;
  311.     if (USE_MODE == 4 || USE_MODE == 5 || USE_MODE == 13)
  312.         max_col = 320;
  313.     else {
  314.         max_col = 640;
  315.         if (USE_MODE == 16)
  316.             max_row = 349;
  317.     }
  318.     max_offset = 4;
  319.  
  320.     prn_out(27);        /* <ESC> */
  321.     prn_out((int)'A');  /* "A" */
  322.     prn_out(8);         /* <BS> */
  323.     for (row_num=0;row_num<max_row+1;row_num+=max_offset) {
  324.     prn_out(27);        /* <ESC> */
  325.     prn_out((int)'*');  /* "*" */
  326.     prn_out(4);         /* chr$(4) */
  327.     prn_out(128);   /* n1 = 128 */
  328.     prn_out(2);     /* n2 = 2  Graphic number = 128 + 512 = 640 */
  329.     /* Set Graphic Print Mode  1:0.9 Vert.:Horiz. */
  330.     for (col_num=0;col_num<max_col;col_num++) {
  331.         pr_num = 0;
  332.         for (row_offset=0;row_offset<max_offset;row_offset++) {
  333.         pix_val = read_point(col_num, (row_num+row_offset));
  334.         if (pix_val == 0) {
  335.             pr_num = (pr_num | bits[7 - (2 * row_offset)]);
  336.             pr_num = (pr_num | bits[6 - (2 * row_offset)]);
  337.         }
  338.  
  339.         }  /* Next row_offset */
  340.         prn_out(pr_num);  /* Print the graphics block needed */
  341.         if (USE_MODE == 4 || USE_MODE == 5 || USE_MODE == 13)
  342.         prn_out(pr_num);   /* Print it again if we're in 320 x 200 */
  343.  
  344.     }      /* Next col_num */
  345.     prn_out(10);  /* <LF> */
  346.  
  347.     }          /* Next row_num */
  348.     prn_out(12);      /* <FF> */
  349.  
  350. } /* End of PRINT_PIC */
  351.  
  352. void prn_out(out_char)
  353. int out_char;
  354. {
  355.     int result;
  356.  
  357.     inregs.h.ah = PRN_OUTPUT;
  358.     inregs.h.dl = (char)out_char;
  359.  
  360.     result = int86x(DOS_INT, &inregs, &outregs, &segregs);
  361.  
  362. }  /* End of PRN_OUT */
  363.  
  364.