{
        My number is 17
    Used modeles :
           PCMOUSE.TPU    -     I wrote this module to use mouse device in my
                                   program;
           G320x200.TPU   -     This module gives possibility to use graphics
                                  mode 320x200x256 in my program;

}
program MapViewer;

uses G320x200, PCMouse, Crt;

const
  HighLightedColor = 240;
  ScreenColor      = 241;

var
  InputFile: text; {Map file for input}
  i, j: integer;
  s: string;
  c, x1, y1, x2, y2, maxx, maxy, minx, miny: integer;
  ch: char;

procedure SetRGB(c: integer);
  begin
  SetRGBPalette(c, ((c shr 5) * 63) div 7, (((c shr 2) and 7) * 63) div 7, ((c and 3) * 63) div 3);
  end;

procedure ShowPart(col: integer);
  var
    i, j, k, l: integer;
    flag: boolean;
  begin
  if col <> ScreenColor then
    for i := minx to maxx do
      for j := miny to maxy do
        if GetPixel(i, j) = col then
          begin
          k := -1;
          l := -1;
          flag := true;
          while flag and (l < 2) do
            begin
            if (GetPixel(i + k, j + l) <> col) and (GetPixel(i + k, j + l) <> HighLightedColor) then
              flag := false;
            if k = 2 then
              begin
              k := -1;
              inc(l);
              end;
            inc(k);
            end;
          if not flag then
            PutPixel(i, j, HighLightedColor);
          end;
  end;

procedure BackToNormal(col: integer);
  var
    i, j: integer;
  begin
  for i := minx to maxx do
    for j := miny to maxy do
      if GetPixel(i, j) = HighLightedColor then
        PutPixel(i, j, col);
  end;

begin

  if paramcount <> 1 then {analizing arguments}
    begin
    writeln('Error: Invalid arguments');
    writeln('You should specify input file name. Example:');
    writeln;
    writeln('17mview.exe icp.mdf');
    writeln;
    exit;
    end;

  assign(InputFile, paramstr(1)); {opening file}
  reset(InputFile);

  Init320x200;
  ResetMouse;

  SetRGBPalette(HighLightedColor, 60, 60, 0);
  SetRGBPalette(ScreenColor, 0, 0, 0);

  for i := 0 to 199 do
    for j := 0 to 319 do
      PutPixel(j, i, ScreenColor);

  minx := 320;
  miny := 200;
  maxx := 0;
  maxy := 0;

  readln(InputFile, s);
  while not EOF(InputFile) do
    begin
    readln(InputFile, c, x1, y1, x2, y2);
    if x1 < minx then
      minx := x1;
    if y1 < miny then
      miny := y1;
    if x2 > maxx then
      maxx := x2;
    if y2 > maxy then
      maxy := y2;
    SetRGB(c);
    Bar(x1, y1, x2, y2, c);
    end;
  ShowMouse;

  i := ScreenColor;
  repeat
    if AnyButton then
      begin
      if i <> ScreenColor then
        begin
        HideMouse;
        BackToNormal(i);
        ShowMouse;
        end;
      i := GetPixel(GetMouseX div 2, GetMouseY);
      HideMouse;
      ShowPart(i);
      ShowMouse;
      end;
    while keypressed do ch := readkey;
  until ch = #27;

  HideMouse;
  ResetMouse;
  Deinit320x200;
  close(InputFile);

end.