🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.msdos.programmer
Article #5468

VGA palette (long)

#5468
From: David Conrad
Date: Wed, 27 Mar 1991 09:07
188 lines
5554 bytes
Here are routines which modify the VGA palette, in response to a query
posted in comp.lang.c.  I've used the Pascal routines quite extensively,
but the C routines I translated from Pascal.  I did test them, but being
somewhat of a novice in C I'd appreciate it if anyone who is bilingual
would scan the two for any misteaks.  Compiled and tested under Turbo
Pascal 5.5 and Turbo C++ 1.0 (patched) under MS-DOS 3.30 on AutoComputer
VIP 286/12 IBM PC-AT Compatible with Paradise SVGA card.

File VGAPAL.PAS
unit vgapal;  (* release 3-91 pas *)

(* BIOS level routines to change the VGA palette registers from Turbo Pascal,
   should work on any VGA BIOS-compatible with IBM VGA (probably all of 'em).
   There is no SetRGBPalette corresponding to GetRGBPalette because this
   routine is already provided by the Graph unit, however if it is needed it
   should be easy to write given the SetDACBlock routine.
   (Get|Set)DACBlock were written by the chaps at Borland and came with the
   VGA256.BGI driver, GetRGBPalette was written and tested by me, David R.
   Conrad.  I hereby disclaim all warranties, both expressed and implied,
   as to the merchantability or fitness for a particular purpose of this
   program.  GetDACBlock and SetDACBlock are Copyright 1990 Borland Intl.,
   GetRGBPalette is Copyright 1991 David Conrad, all are Public Domain. *)

interface

uses dos;

type
  RGBcolor   = record
                 R, G, B : byte;
               end;
  VGApalette = array[0..255] of RGBColor;

(*
  example on how to get/set the entire palette:

  GetDACBlock(0, 256, VGAPal);
  SetDACBlock(0, 256, VGAPal);
*)

procedure GetRGBPalette (ColorNum : integer; var Color : RGBcolor);
procedure GetDACBlock (Start, Count : integer; var Pal : VGApalette);
procedure SetDACBlock (Start, Count : integer; var Pal : VGApalette);

implementation

procedure GetRGBPalette (ColorNum : integer; var Color : RGBcolor);
var
  Regs : Registers;
begin
  with Regs do
  begin
    AH := $10;
    AL := $17;
    BX := ColorNum;
    CX := 1;
    ES := Seg(Color);
    DX := Ofs(Color);
  end;
  Intr($10, Regs);
end;

procedure GetDACBlock (Start, Count : integer; var Pal : VGApalette);
var
  Regs : Registers;
begin
  with Regs do
  begin
    AH := $10;
    AL := $17;
    BX := Start;
    CX := Count;
    ES := Seg(Pal);
    DX := Ofs(Pal);
  end;
  Intr($10, Regs);
end;

procedure SetDACBlock (Start, Count : integer; var Pal : VGApalette);
var
  Regs : Registers;
begin
  with Regs do
  begin
    AH := $10;
    AL := $12;
    BX := Start;
    CX := Count;
    ES := Seg(Pal);
    DX := Ofs(Pal);
  end;
  Intr($10, Regs);
end;

end.

File VGAPAL.H
/* vgapal.h release 3-91 c */

/* BIOS level routines to change the VGA palette register from C.  Should
   work on any VGA which is BIOS-compatible with the IBM VGA (which should
   be all of them).  Tested under Turbo C++ 1.00 (with patches applied),
   may work under 1.01, TC 2.0, 1.5, 1.0, should be easily portable to
   other MS-DOS C compilers.  (The only problem I can forsee is with the
   interrupt calling mechanism, which varies from vendor to vendor.)
   There is no setrgbpalette() corresponding to getrgbpalette() because
   that is already provided by graphics.h.
   This code is based on code written by Borland Intl.; it is Copyright
   1991 David R. Conrad, Public Domain, and I disclaim all warranties,
   both expressed and implied, as to the merchantability or fitness for
   a particular purpose of this program.  */

#ifndef __VGAPAL_H
#define __VGAPAL_H

#include <dos.h>

struct rgbcolor {
  unsigned char rcolor;
  unsigned char gcolor;
  unsigned char bcolor;
};

typedef struct rgbcolor vgapalette[256];

/*
  example on how to get/set the entire palette:

  getdacblock (0, 256, &vgapal);
  setdacblock (0, 256, &vgapal);
*/

#ifdef __cplusplus
extern "C" {
#endif

void getrgbpalette (int colornum, struct rgbcolor *color)
{
  union REGS regs;
  struct SREGS sregs;

  regs.h.ah = 0x10;
  regs.h.al = 0x17;
  regs.x.bx = colornum;
  regs.x.cx = 1;
  sregs.es  = FP_SEG(color);
  regs.x.dx = FP_OFF(color);
  int86x (0x10, &regs, &regs, &sregs);
}

void getdacblock (int start, int count, vgapalette *pal)
{
  union REGS regs;
  struct SREGS sregs;

  regs.h.ah = 0x10;
  regs.h.al = 0x17;
  regs.x.bx = start;
  regs.x.cx = count;
  sregs.es  = FP_SEG(pal);
  regs.x.dx = FP_OFF(pal);
  int86x (0x10, &regs, &regs, &sregs);
}

void setdacblock (int start, int count, vgapalette *pal)
{
  union REGS regs;
  struct SREGS sregs;

  regs.h.ah = 0x10;
  regs.h.al = 0x12;
  regs.x.bx = start;
  regs.x.cx = count;
  sregs.es  = FP_SEG(pal);
  regs.x.dx = FP_OFF(pal);
  int86x (0x10, &regs, &regs, &sregs);
}

#ifdef __cplusplus
}
#endif

#endif
-- 
=  CAT-TALK Conferencing Network, Computer Conferencing and File Archive  =
-  1-313-343-0800, 300/1200/2400/9600 baud, 8/N/1. New users use 'new'    - 
=  as a login id.  AVAILABLE VIA PC-PURSUIT!!! (City code "MIDET")        =
   E-MAIL Address: dave%tygra@sharkey.cc.umich.edu
Technical Details
Message-ID: <1991Mar27.090742.1726@tygra.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!rphroy!cfctech!sharkey!tygra!dave

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!rphroy!cfctech!sharkey!tygra!dave
From: dave@tygra.UUCP (David Conrad)
Newsgroups: comp.os.msdos.programmer
Subject: VGA palette (long)
Message-ID: <1991Mar27.090742.1726@tygra.UUCP>
Date: 27 Mar 91 09:07:42 GMT
Organization: CAT-TALK Conferencing System, Detroit, MI
Lines: 188