program raster;  {converts raster files to plots of intensity}
uses graph,crt;
var  i,j,row,column,L,xMax,yMax,xMid,yMid,t,radius,cc:integer;
     fileName,dataLine,dataWord,title:string;
     ch:char;
     inFile, outFile:text;
     motorX,motorY,intensity,iris,meanSum,meanIris,gain,dx,dy,
	dxOld,dyOld:real;
     color:byte;
     plottingsum,plottingIris:boolean;
     message:string;

{ data columns: 
1	CycleCount/n	
2	A_PsdX	
3	A_PsdY	
4	A_PsdSum	
5	A_Iris	
6	B_PsdX	
7	B_PsdY	
8	B_PsdSum	
9	B_Iris	
10	Motor_X	
11	Motor_Y		}

procedure getMeans;
begin
repeat
   readln(infile,dataLine);
   i:=1; column:=1; L:=length(dataLine);
      repeat
      if dataLine[i]=chr(9) then
         begin
         inc(column);
         if column=cc then        {column 4 for trapA, column 8 for trapB}
            begin
            dataWord:='';
            repeat
            inc(i);
            dataWord:=dataWord + dataLine[i];
            until (i=L) or (dataLine[i+1]=chr(9));
            val(dataword,intensity,j);
            end;      
         if column=cc+1 then        {column 5 for trapA, column 9 for trapB}
            begin
            dataWord:='';
            repeat
            inc(i);
            dataWord:=dataWord + dataLine[i];
            until (i=L) or (dataLine[i+1]=chr(9));
            val(dataword,iris,j);
            end;      
        end;
     inc(i);
   until (i>L) or keypressed;
   meanSum:=meanSum+intensity;
   meanIris:=meanIris+iris;
   inc(row);
   until eof(infile) or keypressed;
   meanSum:=meanSum/row;
   meanIris:=meanIris/row;
   reset(infile);
end;

procedure openDataFile;
begin
clearDevice;
outTextXY(xMid, yMid,'enter_file_to_open');
readln(filename);
assign(infile,filename+'.txt'); 
meanSum:=0;meanIris:=0;row:=0;{signal to get new meanSum and meanIris}
reset(infile);
outTextXY(xMid, yMid+20,'enter_which_trap___A_or_B ');
repeat
ch:=readkey;
until ch in ['a','A','b','B'];
if ch in ['a','A'] then cc:=4 else cc:=8;
end;


procedure writeBitmap;
{to make a color bitmap from 640*480 graphics screen}
{declare  var  outFile:text; also assign filename , rewrite, and close file}
var i,j,k:integer;
begin
write(outFile,'BM'); {bitmaps start with "BM"}
write(outFile,chr(54),chr(180),chr(4),chr(0)); {size of file = 54+(4*16)+(640*480)  }
write(outFile,chr(0),chr(0),chr(0),chr(0)); {identifies our application}
write(outFile,chr(118),chr(0),chr(0),chr(0)); {offset to start = 54 + 4*16}
write(outFile,chr(40),chr(0),chr(0),chr(0)); {header size}
write(outFile,chr(128),chr(2),chr(0),chr(0)); {width = 640}
write(outFile,chr(224),chr(1),chr(0),chr(0)); {height = 480}
write(outFile,chr(1),chr(0)); {color planes}
write(outFile,chr(4),chr(0)); {color depth in bits}
write(outFile,chr(0),chr(0),chr(0),chr(0)); {compression method = none}
write(outFile,chr(0),chr(88),chr(2),chr(0)); {image size = 640*480/2-pix/byte }
write(outFile,chr(194),chr(30),chr(0),chr(0));{horizontal resolution ???}
write(outFile,chr(194),chr(30),chr(0),chr(0)); {verticle res.???}
write(outFile,chr(16),chr(0),chr(0),chr(0)); {colors used = 16}
write(outFile,chr(0),chr(0),chr(0),chr(0)); {impotant colors}
{color palette}
write(outFile,chr(0),chr(0),chr(0),chr(0)); {color0 will be 0blue, 0green and 0red}
write(outFile,chr(128),chr(0),chr(0),chr(0));{color1 = blue}
write(outFile,chr(0),chr(128),chr(0),chr(0));{color2 = green}
write(outFile,chr(128),chr(128),chr(0),chr(0));{color3 = cyan}
write(outFile,chr(0),chr(0),chr(128),chr(0));{color4 = red}
write(outFile,chr(128),chr(0),chr(128),chr(0)); {color5 = magenta}
write(outFile,chr(0),chr(128),chr(128),chr(0)); {color6 = brown}
write(outFile,chr(192),chr(192),chr(192),chr(0)); {color7 = lightGray}
write(outFile,chr(128),chr(128),chr(128),chr(0)); {color8 = darkGray}
write(outFile,chr(255),chr(0),chr(0),chr(0)); {color9 = lightBlue}
write(outFile,chr(0),chr(255),chr(0),chr(0)); {color10 = lightGreen}
write(outFile,chr(255),chr(255),chr(0),chr(0)); {color11 = lightCyan}
write(outFile,chr(0),chr(0),chr(255),chr(0)); {color12 = lightRed}
write(outFile,chr(255),chr(0),chr(255),chr(0)); {color13 = lightMagenta}
write(outFile,chr(0),chr(255),chr(255),chr(0)); {color14 = yellow}
write(outFile,chr(255),chr(255),chr(255),chr(0)); {color15 = white}

for j:=479 downto 0 do for i:=0 to 639 div 2 do
    write(outFile,chr(16*getPixel(2*i,j)+ getPixel(2*i+1,j)));
end;

begin  {main}
i:=detect;
initgraph(i,j,'');
xMax:=getMaxX; yMax:=getMaxY; xMid:=xMax div 2; yMid:=yMax div 2;
plottingSum:=true; color:=red;  title:='   Sum';
gain:=100;
openDataFile;
repeat
clearDevice;
reset(infile);
readln(infile,dataLine);
if meanSum=0 then getMeans;
   repeat
   readln(infile,dataLine);
   i:=1; column:=1; L:=length(dataLine);
      repeat
      if dataLine[i]=chr(9) then
         begin
         inc(column);
         if column=cc-2 then       
            begin
            dataWord:='';
            repeat
            inc(i);
            dataWord:=dataWord + dataLine[i];
            until (i=L) or (dataLine[i+1]=chr(9));
            val(dataword,dx,j);
            end;      
         if column=cc-1 then       
            begin
            dataWord:='';
            repeat
            inc(i);
            dataWord:=dataWord + dataLine[i];
            until (i=L) or (dataLine[i+1]=chr(9));
            val(dataword,dy,j);
            end;      
         if column=cc then        {column 4 for trapA, column 8 for trapB}
            begin
            dataWord:='';
            repeat
            inc(i);
            dataWord:=dataWord + dataLine[i];
            until (i=L) or (dataLine[i+1]=chr(9));
            val(dataword,intensity,j);
            end;      
         if column=cc+1 then        {column 5 for trapA, column 9 for trapB}
            begin
            dataWord:='';
            repeat
            inc(i);
            dataWord:=dataWord + dataLine[i];
            until (i=L) or (dataLine[i+1]=chr(9));
            val(dataword,iris,j);
            end;      
         if column=10 then
            begin
            dataWord:='';
            repeat
            inc(i);
            dataWord:=dataWord + dataLine[i];
            until (i=L) or (dataLine[i+1]=chr(9));
            val(dataword,motorX,j);
            end;
        if column=11 then
            begin
            dataWord:='';
            repeat
            inc(i);
            dataWord:=dataWord + dataLine[i];
            until (i=L) or (dataLine[i+1]=chr(9));
            val(dataword,motorY,j);
            end;
        end;
     inc(i);
   until (i>L) or keypressed;
  setColor(black);
  if plottingSum  and (row>2) then
     begin
     radius:= round((intensity-meanSum)/meanSum*100);
     if radius>0 then setColor(red);
     if radius<0 then setColor(blue);
     {circle(round(xMid*(1+motorX/3000)),round(yMid*(1+motorY/3000)),abs(radius));}
     end;
  if plottingIris and (row>2) then
     begin
     radius:=round((iris-meanIris)/meanIris*100);
     if radius>0 then setColor(red);
     if radius<0 then setColor(blue);
     {circle(round(xMid*(1+motorX/3000)),round(yMid*(1+motorY/3000)),abs(radius));}
     end;
  if abs(dx) < 0.01*meanSum then setColor(yellow);
  dxOld:=dx;
  if abs(dy) < 0.015*meanSum then setColor(yellow);
  dyOld:=dy;
  if plottingSum then circle(round(xMid*(1+motorX/3000)), 
		round(yMid*(1+motorY/3000)),round(abs(intensity-meanSum)/meanSum*gain+2));
  if plottingIris then circle(round(xMid*(1+motorX/3000)), 
		round(yMid*(1+motorY/3000)),round(abs(iris-meanIris)/meanIris*gain+2));
   delay(t);
   until eof(infile) or keypressed;
str(gain:0:0,message);
outTextXY (5,5, fileName + title+'______GAIN='+message);
for i:=-5 to 5 do
    begin
    if i<0 then setColor(blue);
    if i=0 then setColor(0);
    if i>0 then setColor(red);
    circle(xMid+i*10, 5, abs(i));
    end;
rectangle(0,0,xMax,yMax);
ch:=readkey;
case ch of
      's','S':  begin  clearDevice; 
		color:=red;
		plottingSum:=true; plottingIris:=false;
		title:=' Sum';
		end;
       'i','I': begin
		clearDevice;    color:=cyan; 
		plottingSum:=false; plottingIris:=true;
		title :=' Iris';
		end; 
	   't': begin dec(t); if t<0 then t:=0; end;
     	   'T': inc(t);
       'f','F': begin openDataFile; getMeans; end;
	'g':gain:=gain/2;
	'G':gain:=gain*2;
       'p','P': begin
                assign(outFile,filename+title+'.bmp');
                rewrite(outFile);
                writeBitmap;
                close(outFile);
                end;
  end; {case}
until ch='q';
end.