uses crt, dos, screen, memory;



var
TunTex            : Pointer;
TunTexSeg         : Word;

Procedure LoadTga(TgaName : String);
Var
  InF   : File; Loop : Word;
  Dummy : Array[1..18] of Byte;
  Pal   : Array[1..768] of Byte;

Begin
  Assign(Inf,TgaName);
  Reset(Inf,1);
  BlockRead(Inf,Dummy,18);
  BlockRead(Inf,Pal,768);
  BlockRead(Inf,TunTex^,64000);
  Close(Inf);
  Port[$3C8]:=0;
  Loop:=1;
  While Loop<768 Do
    Begin
      Port[$3C9]:=Pal[Loop+2] Div 4;
      Port[$3C9]:=Pal[Loop+1] Div 4;
      Port[$3C9]:=Pal[Loop] Div 4;
      Inc(Loop,3);
    End;

End;


var
fw1:file;

xx,yy, c, x,y,n,m:word;
temp:word;
fr:file;
color, col,  b:byte;
size:word;

begin
asm
mov ax, 13h
int 10h
end;
SetUpVirtual(scr,vaddr);


GetMem(TunTex,64000); TunTexSeg:=Seg(TunTex^);
LoadTga('bitl2.tga');
for n:=0 to 63999 do mem[vga:n]:=mem[TunTexSeg:n];

n:=0;
color:=1;

for m:=0 to 255 do
for y:=27 to 34 do for x:=14 to 63 do
begin
  xx:=x*4+4;
  yy:=y*4;
  col:=mem[tuntexseg:xx+yy*320];
  if col<>0 then if col=m then
  begin
      memw[vaddr:n*4]   :=256;
      memw[vaddr:n*4+2]:=word(xx+yy*320);
      n:=n+1;
  end;

mem[vga:xx+yy*320]:=15;
end;


for m:=0 to n-1 do mem[vga: memw[vaddr:m*4+2] ]:=mem[vaddr:m*4+1];

Assign(fw1, 'codegfx.bin');
Rewrite(fw1,1);
for m:=0 to n*4-1 do BlockWrite(fw1, mem[vaddr:m], 1);
Close(fw1);


readkey;

asm
mov ax, 3h
int 10h
end;
writeln(n);
readkey;
end.