program image;
uses wincrt;
var
  fl,f2 : text;
  st : string;
  x,y,i,j,sw : integer;
  pic : array[1..32,1..30] of char;
  num : array[1..500] of integer;
  hp : array[1..100] of integer;
  vp : array[1..100] of integer;
  hsum,vsum : integer;
  referproj : array['A'..'Z',1..100] of integer;
  deviation : array['A'..'Z'] of integer;
  min : char;

procedure Read_File(st:string);                            {Q.1}
var
  temp : integer;
  i,cst,x : integer;

begin
  assign(fl,st);
  reset(fl);
  cst := 1;
  repeat
    for i := 1 to 32 do
      begin
        read(fl,x);
        if x = 1 then pic[i,cst] := '1'
        else if x = 0 then pic[i,cst] := '0';
      end;
    cst := cst + 1;
  until cst > 30;
  close(fl);
end;

function Count_Pixel(a : char):integer;
var
  i,j,total : integer;
begin
  total := 0;
  for i := 1 to 32 do
    for j := 1 to 30 do
      if pic[i,j] = a then total := total + 1;
  Count_Pixel := total;
end;

function Stroke_Width:integer;                             {Q.2}
var
  row,col,no,cst,temp,i,j : integer;
begin
  no := 1;
  for i := 1 to 500 do
    num[i] := 0;

  for row := 1 to 30 do
    begin
      cst := 0;
      col := 1;
      repeat
        col := col + 1;
        if pic[col,row] = '1' then
          begin
            cst := 0;
            repeat
              cst := cst + 1;
              col := col + 1;
            until pic[col,row] = '0';
            no := no + 1;
            num[no] := cst;
          end
      until col = 32;
    end;

  for col := 1 to 32 do
    begin
      cst := 0;
      row := 1;
      repeat
        row := row + 1;
        if pic[col,row] = '1' then
          begin
            cst := 0;
            repeat
              cst := cst + 1;
              row := row + 1;
            until pic[col,row] = '0';
            no := no + 1;
            num[no] := cst;
          end
      until
        row = 30;
    end;

  for i := 1 to no-1 do
    for j := 1 to no-i do
      if num[j] > num[j+1] then
        begin
          temp := num[j];
          num[j] := num[j+1];
          num[j+1] := temp;
        end;
  if (no mod 2 = 0) then
    Stroke_Width := num[no div 2]
  else Stroke_Width := num[(no+1) div 2]
end;

procedure Thinning;                                        {Q.3}
var
  row,col,cst,st,i,j : integer;
begin
  for row := 1 to 30 do
    begin
      cst := 0;
      col := 1;
      repeat
        col := col + 1;
        if pic[col,row] = '1' then
          begin
            cst := 0;
            st := col;
            repeat
              cst := cst + 1;
              col := col + 1;
            until pic[col,row] = '0';
              if cst = 1 then pic[st,row] := '#'
              else if (cst < 2*sw) and (cst mod 2 = 0) then
                pic[(cst div 2)+st,row] := '#'
              else if (cst < 2*sw) and (cst mod 2 <> 0 ) then
                pic[(cst div 2)+st,row] := '#';
          end
      until col = 32;
    end;

  for col := 1 to 32 do
    begin
      cst := 0;
      row := 1;
      repeat
        row := row + 1;
        if pic[col,row] = '1' then
          begin
            cst := 0;
            st := row;
            repeat
              cst := cst + 1;
              row := row + 1;
            until pic[col,row] = '0';
              if cst = 1 then pic[col,st] := '#'
              else if (cst < 2*sw) and (cst mod 2 = 0) then
                pic[col,(cst div 2)+st] := '#'
              else if (cst < 2*sw) and (cst mod 2 <> 0 ) then
                pic[col,(cst div 2)+st] := '#';
          end
      until
        row = 30;
    end;
  for j := 1 to 30 do
    for i := 1 to 32 do
      if pic[i,j] = '#' then pic[i,j] := '*';
  for i := 1 to 32 do
    for j := 1 to 30 do
      if pic[i,j] = '#' then pic[i,j] := '*'; 
end;

procedure Projection;                                      {Q.4}
var
  i,j : integer;
  sum : integer;

begin
  hsum := 0;
  for i := 1 to 100 do
    hp[i] := 0;
  for i := 1 to 32 do
    begin
      sum := 0;
      for j := 1 to 30 do
        if pic[i,j] = '*' then sum := sum + 1;
      hp[i] := sum;
      hsum := hsum + 1;
    end;

  vsum := 0;
  for i := 1 to 100 do
    vp[i] := 0;
  for j := 1 to 30 do
    begin
      sum := 0;
      for i := 1 to 32 do
        if pic[i,j] = '*' then sum := sum + 1;
      vp[j] := sum;
      vsum := vsum + 1;
    end;
end;

procedure Recognition;                                     {Q.5}
var
  ch : char;
  i,j : integer;
  ref : text;
begin
  assign(ref,'asc.txt');
  reset(ref);
  while not eof(ref) do
    begin
      read(ref,ch);
      i := 0;
      while not eoln(ref) do
        begin
          i := i + 1;
          read(ref,referproj[ch,i])
        end;
      readln(ref);
    end;
  close(ref);
  for i := (hsum+1) to (hsum+vsum) do
    hp[i] := vp[i-hsum];
  for ch := 'A' to 'Z' do
    deviation[ch] := 0;
  for ch := 'A' to 'Z' do
    for i := 1 to 100 do
      deviation[ch] := deviation[ch] + sqr(referproj[ch,i]-hp[i]);
  min := 'A';
  for ch := 'A' to 'Z' do
    if deviation[ch] < deviation[min] then
      min := ch
    else if deviation[ch] = deviation[min] then
      min := ch;
end;

function NCP(x,y : integer) : integer;                     {Calculate NCP}
var
  i,j,sum : integer;
begin
  sum := 0;
  for i := x-1 to x+1 do
    for j := y-1 to y+1 do
      if (i > 0) and (j > 0) and (pic[i,j] = '*') then
        sum := sum + 1;
      if pic[x,y] = '*' then sum := sum - 1;
  NCP := sum;
end;

function NCS(x,y : integer) : integer;                     {Calculate NCS}
var
  i,j,a,b,p,q,sum : integer;
  check : array[1..32,1..30] of boolean;
begin
  sum := 0;
  for i := 1 to 32 do
    for j := 1 to 30 do
      check[i,j] := false;
  check[x,y] := true;

  for i := x-1 to x+1 do
    for j := y-1 to y+1 do
      if (i > 0) and (j > 0) and (pic[i,j] = '*') and (check[i,j] = false) then
        begin
          a := i;
          b := j;
          check[a,b] := true;
          while (pic[a,b] = '*') and (check[a,b] = false) do 
            begin
              p := a;
              q := b;
              for p := p to p + 2 do
                begin
                  check[p,b] := true;
                  if pic[p,b] <> '*' then halt;
                end;
      
              for q := q to q + 2 do
                begin
                  check[a,q] := true;
                  if pic[a,q] <> '*' then halt; 
                end;
      
              p := a + 3;
              q := b;
              for q := q to q + 2 do
                begin
                  check[p,q] := true;
                  if pic[p,q] <> '*' then halt;
                end;

              p := a;
              q := b + 3;
              for p := p to p + 2 do
                begin
                  check[p,q] := true;
                  if pic[p,q] <> '*' then halt;
                end;
              sum := sum + 1;
            end;
        end;
  NCS := sum;

end;

procedure Enhancement;                                     {Q.6}
var
  i,j : integer;
begin
  for i := 1 to 32 do
    for j := 1 to 30 do
      if pic[i,j] = '*' then










end;



{----------------------Main Program--------------------------}
begin
  {Q.1}
  Read_File('cs2.txt');
  for j := 1 to 30 do
    begin
      writeln;
      for i := 1 to 32 do
        write(pic[i,j]);
    end;
  writeln;

  {Q.2}
  writeln('Total number of black pixels : ',Count_Pixel('1'));
  writeln('Stroke width : ',Stroke_Width);

  {Q.3}
  sw := Stroke_Width;
  Thinning;
  assign(f2,'new.txt');
  rewrite(f2);
  for j := 1 to 30 do
    begin
      for i := 1 to 32 do
        write(f2,pic[i,j]);
      writeln(f2);
    end;
  close(f2);
  writeln('Number of skeleton pixels : ',Count_Pixel('*'));

  {Q.4}
  projection;
  write('The first five numbers of horizontal projection : ');
  for i := 1 to 4 do
    write(hp[i],',');
  writeln(hp[5]);

  write('The first five numbers of vertical projection : ');
  for i := 1 to 4 do
    write(vp[i],',');
  writeln(vp[5]);

  {Q.5}
  Recognition;
  writeln('The character recognised is ',min);

  {Q.6}
  Enhancement;
  writeln('The number of skeleton pixels remaining is ',Count_Pixel('*'));

end.