// This macro implements the Image>HyperStacks>Split Channels command.

  batch = is("batch mode");
  Stack.getDimensions(w, h, channels, slices, frames);
  if (channels==1)
       exit("Multi-channel stack required");
  if (!batch) setBatchMode(true);
  Dialog.create("Split");
  Dialog.addCheckbox("Split All "+channels+" Channels", false);
  Dialog.show;
  all = Dialog.getCheckbox;
  if (all) {
      first =1;
      last = channels;
  } else {
      Stack.getPosition(c, z, t);
      first = c;
      last = c;
  }
  type = bitDepth;
  id1 = getImageID;
  Stack.getPosition(c1, z1, t1);
  for (c=first; c<=last; c++) {
      title = "Channel "+c;
      newImage(title, type, w, h, slices*frames);
      showStatus("Splitting channel "+c);
      if (slices>1 || frames>1)
          Stack.setDimensions(1, slices, frames);
      id2 = getImageID;
      for (z=1; z<=slices; z++) {
          for (t=1; t<=frames; t++) {
              selectImage(id1);
              Stack.setPosition(c, z, t);
              if (z==1 && t==1) {
                  getVoxelSize(vw, vh, vd, unit);
                  getLut(reds, greens, blues);
                  getMinAndMax(min, max);
              }
              run("Copy");
              selectImage(id2);
              Stack.setPosition(1, z, t);
              run("Paste");
              if (z==1 && t==1) {
                  setVoxelSize(vw, vh, vd, unit);
                  setLut(reds, greens, blues);
                  setMinAndMax(min, max);
              }
              if (z==slices && t==frames)
                  run("Select None");
          }
      }
  }
  selectImage(id1);
  Stack.setPosition(c1, z1, t1);
  if (!batch)  setBatchMode("exit and display");
