-
BMP to BGI 16 colors.   Turbo Pascal BGI-graph torso.

There were several questions about how to put a Windows Bitmap from a file to the graphics screen in TP.

Here is a quick and dirty procedure which will do the job for 16-color BMP files with EGAVGA.BGI - screens.

-------------
Program BmpLoad;   {TP 6}

USES Dos,Graph,Crt,MGraf;
(* Note: MGraf unit not included, it makes only the Initgraph stuff *)

Const CXlat : Array[0..15] of Byte =
(0,  4,  2,  6,  1,  5,  3, 7, 8,$0C,$0A,$0E,$09,$0D,$0B,$0F);
 {translate the RGB windows - colors to CGA - color # }

var
  Z : Array[0..3000] of Byte;
  I,M,j,T,y,w : Integer;
  f : File;
  r : Registers;
  H : Record
        L,b,h : Longint;   {L=28H, b=C1H, h=74H}
        d,a   : Word;      {d=74H, a=04H}
      End;
  SS : String; {unused}

(* This primitive procedure paints a Windows BMP file to the screen.
Screen = standard 640x480 VGA with 16 colours (EGAVGA.BGI)
  x.BMP must be 16-color type (4 bits per pixel)  *)

Procedure Load_BMP(BMP_Name : String;hx,hy : Integer);
Begin
  m := 479;
  Assign(f,BMP_name);
  Reset(f,1);
  BlockRead(f,Z,14); {ignore 14 header bytes}
  BlockRead(f,H,16);;
  BlockRead(f,Z,H.L-16); {40-16=remain 26}
  for j := 0 to 1 shl H.A-1 do   { 1 shl 4 = 16, -> 0 to 15}
    Begin
      BlockRead(f,Z,4); {palette, unused}
    End;
  y := M;
  w := 0;
  while H.A*H.B > w*8 do w := w+1;  {some calculation for hor. pixels}
  Repeat
    BlockRead(f,Z,w);
    For i := 0 TO w-1 do
      case H.A of
        4 : Begin
              PutPixel(I*2+hx,hy+y,CXlat[Z[I] shr 4]);
              PutPixel(I*2+hx+1,hy+y,CXlat[Z[I] and $0F]);
            End;
      end; {case}
    BlockRead(f,Z,(4-w MOD 4)MOD 4);
    y := y-1;
  until M-y >= H.H;
  Close(f);
(*   {diagnostic:}
  SetColor(14);
  SetTextStyle(DefaultFont,HorizDir,1);
  SetTextJustify(LeftText,CenterText);
  Str(W:1,SS); SS := 'W='+SS;    OutTextXY(10,20,SS);
  Str(H.B,SS); SS := 'H.B='+SS;  OutTextXY(50,20,SS);
  Str(H.A,SS); SS := 'H.A='+SS;  OutTextXY(120,20,SS);
  Str(M,SS); SS := 'M='+SS;      OutTextXY(10,40,SS);
  Str(H.D,SS); SS := 'H.D='+SS;  OutTextXY(120,40,SS);
  Str(H.H,SS); SS := 'H.H='+SS;  OutTextXY(10,60,SS);
  Str((4-w mod 4)mod 4,SS); SS := 'w mod 4='+SS;  OutTextXY(10,80,SS);
*)
END;

(*
Invoke with:  Load_BMP('ANYFILE.BMP',320,-300);
prior:
  InitGraph, SetGraphMode, Window must be setup properly!
*)
Begin  {TEST main}
{InitGraph is already performed by the MGraf unit at this point}
  DirectVideo := false;
  SetGraphMode(GetGraphMode);
  MainWindow('BMP-test',false); {is also in MGraf, painting a frame}
  Load_BMP(ParamStr(1)+'.BMP',320,-300);
  Readln;
  {DIRTY ENDING HERE!!!}
END.

(* DISCLAIMER
This example source file was proven to run on MSDOS with VGA 640x480x16.

There is NO GUARANTEE that it will run on your environment too.
Compiler used: TP 6
The procedure does not really check all BMP header entries, so do not use it with BMP files other than 16-color type.
I made the proc from another source, but i forgot where the orig was coming from several years ago.

  Franz Glaser, Austria,   meg-glaser@eunet.at
http://members.eunet.at/meg-glaser *)

back to TP links page graphics

This page hosted by GeoCities. Get your own Free Homepage