uses screen, memory, crt, dos;

const
VGA=$a000;

procedure setonecolor(c,r,g,b:byte);assembler;
asm
  mov dx,3c8h
  mov al,c
  out dx,al
  inc dx
  mov al,r
  out dx,al
  mov al,g
  out dx,al
  mov al,b
  out dx,al
end;

procedure readonecolor(c:byte;var r,g,b:byte);assembler;
asm
  mov dx,3c7h
  mov al,c
  out dx,al
  add dx,2
  in  al,dx
  les di,r
  mov [es:di],al
  in  al,dx
  les di,g
  mov [es:di],al
  in  al,dx
  les di,b
  mov [es:di],al
end;


var palitra, palitra2:array[0..767] of byte;

Procedure LoadTga(TgaName : String; size:longint; where:word);
Var
  InF   : File of byte; Loop : Word;
  Dummy : Array[1..18] of Byte;
  Pal   : Array[1..768] of Byte;
  n,l:longint;
  tmp,r,g,b:byte;
Begin
  Assign(Inf,TgaName);
  Reset(Inf);
  for n:=0 to 17 do read(inf,tmp);
  for n:=1 to 768 do read(inf,pal[n]);
  l:=0;
  for n:=1 to size do begin
      read(inf, mem[where:l]);
      inc(l);
  end;
  Close(Inf);
{  Port[$3C8]:=0;}
  Loop:=1;
  n:=0;
  While Loop<768 Do
    Begin
      r:=Pal[Loop+2] Div 4;
      g:=Pal[Loop+1] Div 4;
      b:=Pal[Loop] Div 4;
      palitra[n]:=r;
      palitra[n+1]:=g;
      palitra[n+2]:=b;

      Port[$3C9]:=r;
      Port[$3C9]:=g;
      Port[$3C9]:=b;
      n:=n+3;
      Inc(Loop,3);
    End;
End;

procedure save_palitra(name:string);
var fw1:file;
i:word;
z:byte;
begin
Assign(fw1, name);
Rewrite(fw1,1);
Reset(fw1, 1);
z:=0;
for i:=0 to 767 do begin
   BlockWrite(fw1, z, 1);
   BlockWrite(fw1, palitra[i], 1);
end;
Close(fw1);
end;

procedure save_bitmap(name:string; size:longint; where:word);
var fw1:file;
i,n:longint;
begin
Assign(fw1, name);
Rewrite(fw1,1);
Reset(fw1, 1);
n:=0;
for i:=1 to size do begin
   BlockWrite(fw1, mem[where:n], 1);
   inc(n);
end;
Close(fw1);
end;


var
fw1,fw2:file;

c, i, x,y,n,m,yy,xx:word;
fr:file;
col, r,g,b:byte;
size:word;

begin
SetUpVirtual(scr,vaddr);
SetUpVirtual(scr2, vaddr2);
SetUpVirtual(scr3, vaddr3);

asm
mov ax, 13h
int 10h
end;

LoadTga('wizard2.tga', 320*200, vaddr);

for y:=0 to 199 do for x:=0 to 319 do mem[vga:x+y*320]:=mem[vaddr:x+y*320];

{spetial convert for wizard-scene}
for n:=0 to 63999 do begin
 if mem[vaddr:n]=4 then mem[vaddr:n]:=60; {hand}
 if mem[vaddr:n]=3 then mem[vaddr:n]:=38; {blink}
 if mem[vaddr:n]=2 then mem[vaddr:n]:=16; {hood}
 if mem[vaddr:n]=1 then mem[vaddr:n]:=255; {back}

end;


{save_palitra('wizard.pal');}
save_bitmap('wizard2.bin', 64000, vaddr);


readkey;

asm
mov ax, 3h
int 10h
end;

end.