home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / os2 / hpgl312.zip / TO_VGA.C < prev    next >
C/C++ Source or Header  |  1993-04-18  |  5KB  |  203 lines

  1. /*
  2.    Copyright (c) 1991 - 1993 Heinz W. Werntges.  All rights reserved.
  3.    Distributed by Free Software Foundation, Inc.
  4.  
  5. This file is part of HP2xx.
  6.  
  7. HP2xx is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  9. to anyone for the consequences of using it or for whether it serves any
  10. particular purpose or works at all, unless he says so in writing.  Refer
  11. to the GNU General Public License, Version 2 or later, for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. HP2xx, but only under the conditions described in the GNU General Public
  15. License.  A copy of this license is supposed to have been
  16. given to you along with HP2xx so you can know your rights and
  17. responsibilities.  It should be in a file named COPYING.  Among other
  18. things, the copyright notice and this notice must be preserved on all
  19. copies.
  20.  
  21. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  22. */
  23.  
  24. /** to_vga.c: VGA preview part of project "hp2xx" (PC only)
  25.  **
  26.  ** 91/06/16  V 1.00  HWW  Originating
  27.  ** 91/10/15  V 1.01  HWW  ANSI_C
  28.  ** 91/11/23  V 1.01b HWW  Echo off
  29.  ** 92/01/29  V 1.02b HWW  More flexible screen placement
  30.  ** 92/05/24  V 2.00b HWW  Color supported
  31.  ** 92/07/12  V 2.01a HWW  REGPACK --> REGS, intr() --> int86()
  32.  **
  33.  ** NOTES:
  34.  **  1)   Use PicBuf_to_VGA() as a reference for access
  35.  **       to the picture buffer (b/w & color) in other modules.
  36.  **
  37.  **  2)   Color bug: Somehow, I can set all colors EXCEPT magenta. Instead
  38.  **       of magenta I always end up with brown. Does anybody know why???
  39.  **
  40.  **  2)   to_vga.c may be a misnomer. See R. Emmerich's "showit" for
  41.  **       previewing on a Hercules card. I also suspect that this code here
  42.  **       is sufficient for EGA previews as well (just change the mode byte
  43.  **       and adjust for DPI and screen sizes), but cannot test it (no EGA
  44.  **       card around amymore).
  45.  **/
  46.  
  47.  
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <ctype.h>
  51. #include <dos.h>
  52. #include <conio.h>
  53. #include "bresnham.h"
  54. #include "hp2xx.h"
  55.  
  56.  
  57. Byte        buf[256][3], linebuf[1024];
  58. unsigned    bufaddr_lo,  bufaddr_hi;
  59.  
  60.  
  61.  
  62. Byte    get_VGAmode (void)
  63. {
  64. union    REGS    inregs;
  65. union    REGS    outregs;
  66.  
  67.   inregs.x.ax = 0x0f00;    /* get VGA mode    */
  68.   int86 (0x10, &inregs, &outregs);
  69.   return (outregs.x.ax & 0xff);
  70. }
  71.  
  72.  
  73. void    set_VGAmode (Byte mode)
  74. {
  75. union    REGS    inregs;
  76. union    REGS    outregs;
  77.  
  78.   bufaddr_lo = (unsigned) ((long) buf        & 0xffff);
  79.   bufaddr_hi = (unsigned) (((long)buf >>16)    & 0xffff);
  80.  
  81.   inregs.x.ax = 0x0000 | mode;    /* set VGA mode    */
  82.   int86 (0x10, &inregs, &outregs);
  83. }
  84.  
  85.  
  86.  
  87. void    get_color_regs (short codenum,
  88.             Byte *p_red,
  89.             Byte *p_green,
  90.             Byte *p_blue)
  91. {
  92. union    REGS    inregs;
  93. union    REGS    outregs;
  94.  
  95.   inregs.x.ax = 0x1015;
  96.   inregs.x.bx = codenum;
  97.   int86 (0x10, &inregs, &outregs);    /* get color codes */
  98.   *p_red   = outregs.x.dx >> 8;
  99.   *p_green = outregs.x.cx >> 8;
  100.   *p_blue  = outregs.x.cx & 0xff;
  101. }
  102.  
  103.  
  104.  
  105. void    set_color_regs (short codenum,
  106.             Byte red,
  107.             Byte green,
  108.             Byte blue)
  109. {
  110. union    REGS    inregs;
  111. union    REGS    outregs;
  112.  
  113.   inregs.x.ax = 0x1010;
  114.   inregs.x.bx = codenum;
  115.   inregs.x.dx =  red << 8;
  116.   inregs.x.cx = (green << 8) | blue;
  117.   int86 (0x10, &inregs, &outregs);
  118. }
  119.  
  120.  
  121.  
  122.  
  123. void    set_pixel (unsigned x, unsigned y, Byte colorcode)
  124. {
  125. union    REGS    inregs;
  126. union    REGS    outregs;
  127.  
  128.   inregs.x.ax = 0x0c00 | colorcode;    /* Write dot */
  129.   inregs.x.bx = 0;
  130.   inregs.x.cx = x;
  131.   inregs.x.dx = y;
  132.   int86 (0x10, &inregs, &outregs);
  133. }
  134.  
  135.  
  136.  
  137.  
  138. void    PicBuf_to_VGA (PicBuf *picbuf, PAR *p)
  139. {
  140. int    row_c, x, y, xoff, yoff, color_index;
  141. RowBuf    *row;
  142. Byte    orig_mode;
  143. short    i;
  144.  
  145.   if (!p->quiet)
  146.   {
  147.     fprintf(stderr, "\nVGA preview follows.\n");
  148.     fprintf(stderr, "Press <return> to start and end graphics mode\n");
  149.     SilentWait();
  150.   }
  151.  
  152.   xoff = p->xoff * p->dpi_x / 25.4;
  153.   yoff = p->yoff * p->dpi_y / 25.4;
  154.  
  155.   if ((!p->quiet) &&
  156.       (((picbuf->nb << 3) + xoff > 639) || (picbuf->nr + yoff > 480)) )
  157.   {
  158.     fprintf(stderr, "\n\007WARNING: Picture won't fit on a standard VGA!\n");
  159.     fprintf(stderr, "Current range: (%d..%d) x (%d..%d) pels\n",
  160.         xoff, (picbuf->nb << 3) + xoff, yoff, picbuf->nr + yoff);
  161.     fprintf(stderr, "Continue anyway (y/n)?: ");
  162.     if (toupper(getchar()) == 'N')
  163.         return;
  164.   }
  165.  
  166.   orig_mode = get_VGAmode();
  167.   set_VGAmode (p->vga_mode);
  168.  
  169.  
  170. /**
  171.  ** CLUT setting & special VGA adjustment
  172.  **/
  173.   if (p->is_color)    /* Darker background for higher color contrast    */
  174.   {
  175.     for (i=xxBackground; i <= xxYellow; i++)
  176.         set_color_regs (i, p->Clut[i][0],p->Clut[i][1],p->Clut[i][2]);
  177.     set_color_regs((short) xxBackground, 160, 160, 160); /* GRAY    */
  178.   }
  179.   else
  180.   {
  181.     set_color_regs((short) xxBackground, 180, 180, 180); /* LIGHT GRAY*/
  182.     set_color_regs((short) xxForeground,   0,   0,   0); /* BLACK    */
  183.   }
  184.  
  185.   for (row_c=0, y=picbuf->nr+yoff-1; row_c < picbuf->nr; row_c++, y--)
  186.   {
  187.     row = get_RowBuf (picbuf, row_c);
  188.     for (x=0; x < picbuf->nc; x++)
  189.     {
  190.         color_index = index_from_RowBuf(row, x, picbuf);
  191.         if (color_index != xxBackground)
  192.             set_pixel(x+xoff, y, (Byte) color_index);
  193.     }
  194.   }
  195.  
  196.   while (kbhit())
  197.     getch();
  198.   getch();
  199.  
  200.   set_VGAmode (orig_mode);
  201. }
  202.  
  203.