var
   n:integer;  {Global variable used by integration macros}


macro 'Print Video using TV-3 [P]';
begin
  CallExport('TV-3 Module');
end;


procedure ExtractEvenField(NewWindow:boolean);
{
Replaces odd scan lines with average of neighboring even lines. Can be used to improve the quality of images that have even and odd fields that are out of sync as the result of subject movement during capture.
}
var
  i,width,height,row1,row2:integer;
begin
  SaveState;
  if NewWindow then Duplicate('Even Field');
  GetPicSize(width,height);
  row1:=0; row2:=0;
  for i:=1 to height/2 do begin
    GetRow(0,row1,width);
    PutRow(0,row2,width);
    row1:=row1+2;
    row2:=row2+1;
  end;
  MakeRoi(0,0,width,height/2);
  Copy;
  MakeRoi(0,height/4-1,width,height/2);
  Paste;
  RestoreRoi;
  SetScaling('Bilinear; Same Window');
  ScaleAndRotate(1,2,0);
  RestoreState;
end;

macro 'Extract Even Field->New Window';
begin
  ExtractEvenField(true);
end;

macro 'Extract Even Field->Same Window';
begin
  ExtractEvenField(false);
end;


macro 'Camera and Light Source Test';
  {Test cameras and light sources for temporal stability.}
  {Maximum rate is about 2 frames/sec.}
var
  interval: real;
  nFrames,frames: integer;
  ticks,StartTicks,NextTicks: integer;
begin
   ResetCounter;
   nFrames:=trunc(GetNumber('Number of Frames:',10));
   interval:=GetNumber('Interval (seconds):',10);
   ticks := interval*60;
   SetCursor('watch');
   StartTicks := TickCount;
   NextTicks:=StartTicks+ticks;
   frames := 0;
   repeat
      Capture;
      Measure;
      SetCursor('Watch');
      repeat until TickCount>=NextTicks;
  				rUser1[rCount] := TickCount-StartTicks;
      NextTicks := NextTicks+ticks;
      frames := frames + 1;
   until frames=nFrames;
end;


macro 'Average Frames [F]';
begin
  AverageFrames;
end;


macro 'Average Frames on Trigger';
begin
  WaitForTrigger;
  AverageFrames;
end;


macro 'Dynamic 1-D Plot';
{
Displays a dynamic 1-d plot of a line in the image while the image
is being captured. You most first create a line selection in
Camera window. The macro works best if you first to a Plot Profile
and move the Plot window so it doesn't cover the Camera window. You
may have to resize the Camera window. Hold down the mouse button
to terminate.
}
var
  x1,y1,x2,y2,LineWidth:integer;
begin
  GetLine(x1,y1,x2,y2,LineWidth);
  if x1=-1 then begin
    PutMessage('Create a straight line selection in the Camera window');
    exit;
  end;
  SetPlotScale(0,255);
  repeat
    Capture;
    if button then exit;
    MakeLineRoi(x1,y1,x2,y2);
    PlotProfile;
  until button;
end;


macro 'Integrate Inverted';
{
Inverts captured video to allow more than 128 frames to be
integrated without overflow. For example, the sum of 256 pixels
with an average value of 200(very dark) is 51,200, which is
greater than the 32,767 maximum, but the sum of 256 pixels
with and average value of 55(200 inverted) is 14,080.
}
var
  nFrames:integer;
begin
  nFrames:=GetNumber('Number of Frames:', 200);
  SetVideo('Invert');
  AverageFrames('Integrate', nFrames);
  SetVideo(''); {Don't invert}
  Invert;
end;


macro 'Acquire AV Video [V]';
begin
   Acquire('Plug-in Digitizer');
end;


macro 'Acquire AV with Calibration';
{Plug-in Digitiser always calibrates captured images to
72 dots per inch. To get around this problem, this macro
saves the current spatial scale, calls Plug-in Digitizer 1.1,
and then applies the saved scale to the captured image. The
name used in the Acquire command will have to be changed
if you use a different version of Plug-in Digitizer.
}
var
   scale, AspectRatio:real;
   unit:string;
begin
   GetScale(scale, unit, AspectRatio);
   Acquire('Plug-in Digitizer 1.1');
   SetScale(scale, unit, AspectRatio);
end;


macro 'Correct Aspect Ratio';
var
   AspectRatio: real;
begin
  AspectRatio := GetNumber('Aspect Ratio:' , 0.95);
  SelectAll;
  SetScaling('Bilinear, Same Window');
  ScaleAndRotate(AspectRatio, 1, 0);
end;


macro 'Capture with "Live" Histogram';
begin
  StartCapturing;
  ShowHistogram;
end;

macro 'Set Offset and gain...';
var
  offset, gain: integer;
begin
  offset := GetNumber('Offset:', get('offset'), 0);;
  gain := GetNumber('Gain:', get('gain'), 0);;
  SetVideo('', gain, offset);
end;


macro '(-' begin end; {Menu divider}
{
These two macros continuously integrate and display frames either off-chip, using the Scion AG-5, or on-clip, using the Scion LG-3 and a Coho 4910 series camera. Press and hold the mouse button near the top of the Camera window to decrease the number of frames integrated. Press near the bottom to increase the number of frames integrated. Press above or to the left of the Camera window to stop integrating.
}

procedure Integrate (mode:string);
var
   x,y,delta:integer;
begin
   if n=0 then n:=6;
   repeat
      if button then begin
         GetMouse(x,y);
         if (x<0) or (y<0) then exit;
         delta:=round(0.333*n);
         if delta<1 then delta:=1;
         if y<220 then begin
            n:=n-delta;
            if n<1 then n:=1;
         end else begin
             n:=n+delta;
             if n>127 then n:=127;
         end;
      end;
      AverageFrames(mode, n);
   until false;
end;

macro 'Integrate Using AG-5 [5]';
begin
  Integrate('integrate video rate');
end;

macro 'Integrate On-chip Using Cohu [C]';
begin
  Integrate('integrate on-chip');
end;


macro '(-' begin end; {Menu divider}

macro 'Paste Live [L]';
{
Pastes live from Camera window into a rectangular
selection in another window. Use Paste Control to
switch to various semi-transparent transfer modes.
}
begin
  PasteLive;
end;

macro 'Paste Averaged [A]';
{
Captures an averaged or integrated selection into a window
other than the Camera window. Use in conjunction with "PasteLive". Useful for making montages of different focal
planes of fluorescent specimens.
}
var
  x,y,width,height,pid:integer;
begin
  RequiresVersion(1.53);
  if WindowTitle='Camera' then begin
    PutMessage('The active window cannot be "Camera".');
    exit;
  end;
  GetRoi(x,y,width,height);
   if width=0 then begin
    PutMessage('Rectangular selection required.');
    exit;
  end;
  pid:=PidNumber;
  SelectWindow('Camera');
  MakeRoi(x,y,width,height);
  AverageFrames;
  Copy;
  SelectPic(pid);
  MakeRoi(x,y,width,height);
  Paste;
end;

macro 'Paste Live OR [O]';
begin
  PasteLive;
  SetOption; DoOr;
end;

macro 'Paste Live XOR [X]';
begin
  PasteLive;
  SetOption; DoXor;
end;

macro 'Add Duplicate Slice [D]';
{Use with "Paste Live OR" or "Paste Live XOR"}
{to register series of sections into a stack.}
begin
  SetOption; DoCopy;
  KillRoi;
  SelectAll;
  Copy;
  AddSlice;
  Paste;
end;

macro '(-' begin end; {Menu divider}

{Note: keyboard shortcuts do not work when the Video}
{Control dialog box is the active window.}

macro 'SetChannel 1 [1]'; begin SetChannel(1) end;
macro 'SetChannel 2 [2]'; begin SetChannel(2) end;
macro 'SetChannel 3 [3]'; begin SetChannel(3) end;
macro 'SetChannel 4 [4]'; begin SetChannel(4) end;

macro '(-' begin end; {Menu divider}

macro 'Generate Pulse Train'
{Outputs a 30Hz pulse train on pin 1(Data Output bit 3)}
{of the Scion LG-3's utility connector.}
var
  NextTicks,inc:integer;
begin
  inc:=1; {1/60 sec.}
  SetCursor('watch');
  NextTicks:=TickCount+inc;
  repeat
    scion[4]:=BitOr(scion[4],8);
    repeat until TickCount>=NextTicks;
    NextTicks:=NextTicks+inc;
    scion[4]:=BitAnd(scion[4],7);
    repeat until TickCount>=NextTicks;
    NextTicks:=NextTicks+inc;
  until button;
end;


macro 'Set LG-3 DAC A'; begin scion[1]:=GetNumber('DAC A(0-255):',scion[1]); end;
macro 'Set LG-3 DAC B'; begin scion[2]:=GetNumber('DAC B(0-255):',scion[2]); end;
macro 'Set LG-3 Data Out'; begin scion[4]:=GetNumber('Data Out(0-15):',scion[4]); end;
macro 'Read LG-3 Data In'; begin PutMessage('Data In=',BitAnd(scion[3],15):1); end;





