function [adjtext,adjpic] = checkadjacent(blocknum,intermimage,I,J)
%%takes a block number and the intermmediate image
%%returns ajtext=1 if it has an adjacent text block
%%returns adjpic=1 if it has an adjacent picture block
      above=blocknum-16;
      below=blocknum+16;
      left=blocknum-1;
      right=blocknum+1;
temp=intermimage;

global black
global white
global gray
global undetermined
%%make intermediate image into a 256 long vector
intermimage=[temp(1,:)];   
for row=2 : 16
  intermimage=[intermimage,temp(row,:)];
end

adjtext=0;
adjpic=0;


      if (I ~= 1) & (I ~= 16) & (J ~= 1) & (J ~= 16)  %%block not on edge
          if intermimage[above]==white
            adjpic=1;
          end
          if intermimage[below]==white
            adjpic=1;
          end
          if intermimage[left]==white
            adjpic=1;
          end
          if intermimage[right]==white
            adjpic=1;
          end
          if intermimage[above]==gray
            adjtext=1;
          end
          if intermimage[below]==gray
            adjtext=1;
          end
          if intermimage[left]==gray
            adjtext=1;
          end
          if intermimage[right]==gray
            adjtext=1;
          end
        end
      if (I == 1) & (J == 1) %%block on upper left hand corner
	    if intermimage[below]=white
            adjpic=1;
          end
          if intermimage[right]==white
            adjpic=1;
          end
          if intermimage[below]==gray
            adjtext=1;
          end
          if intermimage[right]==gray
            adjtext=1;
          end
      end
      if (I == 1) & (J==16)  %%block on upper right hand corner
          if intermimage[below]=white
            adjpic=1;
          end
          if intermimage[left]==white
            adjpic=1;
          end
          if intermimage[below]==gray
            adjtext=1;
          end
          if intermimage[left]==gray
            adjtext=1;
          end
      end

      if (I == 16) & (J==1)  %%block on lower left hand corner
          if intermimage[above]==white
            adjpic=1;
          end
          if intermimage[right]==white
            adjpic=1;
          end
          if intermimage[above]==gray
            adjtext=1;
          end
          if intermimage[right]==gray
            adjtext=1;
          end
        end
       if (I == 16) & (J==16)  %%block on lower right hand corner
          if intermimage[above]==white
            adjpic=1;
          end
          if intermimage[left]==white
            adjpic=1;
          end
          if intermimage[above]==gray
            adjtext=1;
          end
          if intermimage[left]==gray
            adjtext=1;
          end         
        end
       
      if (I == 1) & (J ~= 1) & (J ~= 16)  %%block on top row, but not on corner
          if intermimage[below]==white
            adjpic=1;
          end
          if intermimage[left]==white
            adjpic=1;
          end
          if intermimage[right]==white
            adjpic=1;
          end
          if intermimage[below]==gray
            adjtext=1;
          end
          if intermimage[left]==gray
            adjtext=1;
          end
          if intermimage[right]==gray
            adjtext=1;
          end
        end

      if (I == 16) & (J ~= 1) & (J ~= 16)  %%block on bottom row, but not on corner
          if intermimage[above]==white
            adjpic=1;
          end
          if intermimage[left]==white
            adjpic=1;
          end
          if intermimage[right]==white
            adjpic=1;
          end
          if intermimage[above]==gray
            adjtext=1;
          end
          if intermimage[left]==gray
            adjtext=1;
          end
          if intermimage[right]==gray
            adjtext=1;
          end
        end
      if (I ~= 1) & (I ~= 16) & (J == 1)  %%block in left col, but not on corner
          if intermimage[above]==white
            adjpic=1;
          end
          if intermimage[below]==white
            adjpic=1;
          end
          if intermimage[right]==white
            adjpic=1;
          end
          if intermimage[above]==gray
            adjtext=1;
          end
          if intermimage[below]==gray
            adjtext=1;
          end
          if intermimage[right]==gray
            adjtext=1;
          end
        end

      if (I ~= 1) & (I ~= 16) & (J == 16)  %%block in right col, but not on corner
          if intermimage[above]==white
            adjpic=1;
          end
          if intermimage[below]==white
            adjpic=1;
          end
          if intermimage[left]==white     
            adjpic=1;
          end
          if intermimage[above]==gray
            adjtext=1;
          end
          if intermimage[below]==gray
            adjtext=1;
          end
          if intermimage[left]==gray
            adjtext=1;
          end        
        end
intermimage=temp;



