macro 'Color Merge Two Images';
{
Merges a "red" image and a "green" image to
create a composite color image by creating a
temporary 24-bit image and converted to 8-bits.
Assumes dark objects on a light background.
Remove the two Invert commands if this is not
the case. Requires a lot of memory.
}
var
  i,w1,w2,h1,h2,rgb:integer;
begin
  RequiresVersion(1.50);
  SaveState;
  if nPics<>2 then begin
     PutMessage('This macro operates on exactly two images.');
     exit;
  end;
  SelectPic(1);
  GetPicSize(w1,h1);
  SelectPic(2);
  GetPicSize(w2,h2);
  if (w1<>w2) or (h1<>h2) then begin
     PutMessage('The two images must have the same width and height.');
    exit;
  end;
  SetNewSize(w1,h2);
  SetBackground(255);
  MakeNewStack('RGB');
  AddSlice;
  AddSlice;
  rgb:=PicNumber;
  SelectPic(1);
  SelectAll;
  Copy;
  SelectPic(rgb);
  SelectSlice(1);
  Paste;
  Invert;
  SelectPic(2);
  SelectAll;
  Copy;
  SelectPic(rgb);
  SelectSlice(2);
  Paste;
  Invert;
  RGBToIndexed('Custom, Dither');
  SelectPic(rgb);
  Dispose;
  SelectPic(3);
  RestoreState;
end;


macro 'Color Merge Two Stacks';
{
Merges a "red" stack and a "green" stack to
create a new composite color stack.
}
var
  i,w1,w2,h1,h2,d1,d2,d3:integer;
  rgb,merged:integer;
begin
  RequiresVersion(1.50);
  SaveState;
  if nPics<>2 then begin
    PutMessage('This macro operates on exactly two stacks.');
    exit;
  end;
  SelectPic(1);
  GetPicSize(w1,h1);
  d1:=nSlices;
  SelectPic(2);
  GetPicSize(w2,h2);
  d2:=nSlices;
  if (d1=0) or (d2=0) then begin
    PutMessage('Both images must be stacks.');
    exit;
  end;
  if d1>=d2
    then d3:=d2
    else d3:=d1;
  if (w1<>w2) or (h1<>h2) then begin
    PutMessage('The two stacks must have the same width and height.');
    exit;
  end;
  SetNewSize(w1,h2);
  SetBackground(255);
  MakeNewStack('RGB');
  AddSlice;
  AddSlice;
  rgb:=PicNumber;
  SetPalette('System');
  MakeNewStack('Merged');
  merged:=PicNumber;
  for i:=1 to d3 do begin
    SelectPic(1);
    SelectSlice(i);
    SelectAll;
    Copy;
    {DeleteSlice;}
    SelectPic(rgb);
    SelectSlice(1);
    SelectAll;
    Paste;
    Invert;
    SelectPic(2);
    SelectSlice(i);
    SelectAll;
    Copy;
    {DeleteSlice;}
    SelectPic(rgb);
    SelectSlice(2);
    SelectAll;
    Paste;
    Invert;
    SelectPic(rgb);
    RGBToIndexed('System');
    SelectAll;
    Copy;
    Dispose;
    SelectPic(merged);
    Paste;
    if i<>d3 then AddSlice;
   end;
  SelectPic(rgb);
  Dispose;
{
  SelectPic(1);
  Dispose;
  SelectPic(1);
  Dispose;
}
  RestoreState;
end;

procedure CheckForRGBStack;
begin
   if (nPics=0) or (nSlices<>3) then begin
    PutMessage('This macro requires an RGB stack.');
    exit
  end;
end;

macro 'Enhance Contrast';
var
  i:integer;
begin
  CheckForRGBStack;
  for i:=1 to 3 do begin
     SelectSlice(i);
     EnhanceContrast;
     ApplyLUT;
  end;
  RGBToIndexed('Custom LUT, Dither');
end;

macro 'RGB to 8-Bit Color';
begin
  RGBToIndexed('Custom LUT, Dither');
end;

macro '8-Bit Color to RGB';
begin
  IndexedToRGB;
end;


macro '8-Bit Color to Grayscale';
begin
  ApplyLUT;
end;


macro 'Sort LUT by Hue';
begin
  SortPalette;
end;

