Bitmap display in VGA mode

A program I wrote back in 1998 to read a bitmap image (uncompressed) file and display it in the VGA mode screen (DOS mode).

bmp.pas
  1. uses crt,dos;
  2.  
  3. var curpos,x,y,count,mx,my:word;
  4.     fbmp:text;
  5.     srec:searchrec;
  6.     c,cold,num:byte;
  7.     bmpfactor:shortint;
  8.     ch:char;
  9.     fname:string;
  10.  
  11. {$r-}
  12. procedure swapbyte(var num:byte);
  13. var tmp,c,b:byte;
  14. begin
  15.   tmp:=0;
  16.   for c:=1 to 8 do
  17.     begin
  18.       b:=0;
  19.       b:=num and 1;
  20.       tmp:=tmp shl 1;
  21.       tmp:=tmp+b;
  22.       num:=num shr 1;
  23.     end;
  24.   num:=tmp;
  25. end;
  26. {$r+}
  27.  
  28. procedure loadgui(mode:byte);assembler;
  29. asm
  30.   push ax
  31.   xor ax,ax
  32.   mov al,mode
  33.   int 10h
  34.   pop ax
  35. end;
  36.  
  37. procedure writedot(x,y:word;col:byte);assembler;
  38. asm
  39.   push ax
  40.   push dx
  41.   push cx
  42.   mov ah,0ch
  43.   mov al,col
  44.   mov dx,y
  45.   mov cx,x
  46.   int 10h
  47.   pop cx
  48.   pop dx
  49.   pop ax
  50. end;
  51.  
  52. begin
  53.   writeln('BMP Display,  Version 1.00 (Beta Version)');
  54.   writeln;
  55.  
  56.   if paramcount<1 then
  57.     begin
  58.       writeln('USAGE: BMPDISP filename');
  59.       writeln;
  60.       writeln('filename        Path and filename of the bitmap you want to view.');
  61.       writeln;
  62.       halt(1);
  63.     end;
  64.  
  65.   fname:=paramstr(1);
  66.   findfirst(fname,$3f,srec);
  67.   if srec.name='' then
  68.     begin
  69.       writeln('File not found ',fname);
  70.       halt(1);
  71.     end;
  72.  
  73.   assign(fbmp,fname);
  74.   {$i-}reset(fbmp);{$i+}
  75.   if ioresult<>0 then
  76.     begin
  77.       writeln('I/O failure on opening ',fname);
  78.       halt(1);
  79.     end;
  80.  
  81.   for count:=1 to 19 do
  82.     begin
  83.       read(fbmp,ch);
  84.     end;
  85.  
  86.   bmpfactor:=0;
  87.   count:=ord(ch);
  88.   read(fbmp,ch);
  89.   num:=ord(ch);
  90.   mx:=count+(num shl 8);
  91.   read(fbmp,ch);
  92.   read(fbmp,ch);
  93.   read(fbmp,ch);
  94.   count:=ord(ch);
  95.   read(fbmp,ch);
  96.   num:=ord(ch);
  97.   my:=count+(num shl 8);
  98.  
  99.   if(mx>800)or(my>600)then
  100.     begin
  101.       writeln('Resolution ',mx,'x',my,' is not supported.');
  102.       halt(1);
  103.     end;
  104.  
  105.   for count:=1 to 5 do
  106.     begin
  107.       read(fbmp,ch);
  108.     end;
  109.  
  110.   cold:=ord(ch);
  111.   if((mx=640)or(mx=800))then
  112.     begin
  113.       bmpfactor:=1;
  114.     end;
  115.  
  116.   if mx&lt;640 then
  117.     begin
  118.       case cold of
  119.         8:bmpfactor:=1;
  120.         4:bmpfactor:=3;
  121.         1:bmpfactor:=-3;
  122.         end;
  123.     end;
  124.  
  125.   if (mx>640)and(mx&lt;800) then
  126.     begin
  127.       bmpfactor:=0;
  128.     end;
  129.  
  130.   if(mx>640)or(my>480)then
  131.     begin
  132.       loadgui($ea);
  133.     end
  134.   else
  135.     begin
  136.       loadgui($12);
  137.     end;
  138.  
  139.   case cold of
  140.     1:begin
  141.         for count:=1 to 33 do
  142.           begin
  143.             read(fbmp,ch);
  144.           end;
  145.  
  146.         for y:=my-1 downto 0 do
  147.           begin
  148.             for x:=0 to mx-bmpfactor do
  149.               begin
  150.                 if keypressed then
  151.                   begin
  152.                     loadgui($3);
  153.                     exit;
  154.                   end;
  155.  
  156.                 read(fbmp,ch);
  157.                 num:=ord(ch);
  158.                 swapbyte(num);
  159.  
  160.                 for count:=1 to 8 do
  161.                   begin
  162.                     c:=num and 1;
  163.                     if c=1 then
  164.                       begin
  165.                         c:=15;
  166.                       end;
  167.  
  168.                     writedot(x,y,c);
  169.                     num:=num shr 1;
  170.                     if count&lt;8 then
  171.                       begin
  172.                         inc(x);
  173.                       end;
  174.  
  175.                   end;
  176.  
  177.                 end;
  178.  
  179.             end;
  180.  
  181.       end;
  182.  
  183.     4:begin
  184.         for count:=1 to 89 do
  185.           begin
  186.             read(fbmp,ch);
  187.           end;
  188.  
  189.         for y:=my-1 downto 0 do
  190.           begin
  191.             for x:=0 to mx+bmpfactor do
  192.               begin
  193.                 if keypressed then
  194.                   begin
  195.                     exit;
  196.                   end;
  197.  
  198.                 read(fbmp,ch);
  199.                 num:=ord(ch);
  200.                 writedot(x,y,num div 16);
  201.                 inc(x);
  202.                 writedot(x,y,num mod 16);
  203.               end;
  204.  
  205.           end;
  206.  
  207.       end;
  208.  
  209.     8:begin
  210.         for count:=1 to 1049 do
  211.           begin
  212.             read(fbmp,ch);
  213.           end;
  214.  
  215.         for y:=my-1 downto 0 do
  216.           begin
  217.             for x:=0 to mx-bmpfactor do
  218.               begin
  219.                 if keypressed then
  220.                   begin
  221.                     loadgui($3);
  222.                     close(fbmp);
  223.                     halt(1);
  224.                   end;
  225.  
  226.                 read(fbmp,ch);
  227.                 num:=ord(ch);
  228.                 writedot(x,y,num);
  229.               end;
  230.  
  231.           end;
  232.  
  233.       end;
  234.     else
  235.       begin
  236.         loadgui($3);
  237.         writeln;
  238.         writeln(cold,'-bit format is not spported.');
  239.         halt(1);
  240.       end;
  241.  
  242.     end;
  243.  
  244.   readkey;
  245.   loadgui($3);
  246.   close(fbmp);
  247.  
  248. end.