/**
  * Creates a new data sink for the specified set of pixels.
  *
  * @param pixels The pixels set the data sink is for.
  * @return See above.
  */
 public static DataSink createDataSink(PixelsData pixels) {
   if (pixels == null) throw new IllegalArgumentException("Pixels cannot be null.");
   if (singleton.pixelsSource != null && singleton.pixelsSource.isSame(pixels.getId()))
     return singleton.pixelsSource;
   registry.getCacheService().clearAllCaches();
   int size = getCacheSize();
   if (size <= 0) size = 0;
   singleton.pixelsSource = DataSink.makeNew(pixels, registry, size);
   return singleton.pixelsSource;
 }
  /**
   * Creates a new image with one channel from a source image.
   *
   * @param info The information about the data to handle.
   */
  private void CreateNewImage(ConfigurationInfo info) throws Exception {
    PixelsData pixels = image.getDefaultPixels();
    int sizeZ = pixels.getSizeZ();
    int sizeT = pixels.getSizeT();
    int sizeC = pixels.getSizeC();
    int sizeX = pixels.getSizeX();
    int sizeY = pixels.getSizeY();
    long pixelsId = pixels.getId();
    if (sizeC <= 1) throw new Exception("The image must have at least 2 channels.");
    RawPixelsStorePrx store = null;
    // Create a new image.
    Map<Integer, byte[]> map = new LinkedHashMap<Integer, byte[]>();
    try {
      store = connector.getRawPixelsStore();
      store.setPixelsId(pixelsId, false);
      for (int z = 0; z < sizeZ; z++) {
        for (int t = 0; t < sizeT; t++) {
          byte[] planeC1 = store.getPlane(z, 0, t);
          byte[] planeC2 = store.getPlane(z, 1, t);
          byte[] newPlane = new byte[planeC1.length];
          for (int i = 0; i < planeC1.length; i++) {
            newPlane[i] = (byte) ((planeC1[i] + planeC2[i]) / 2);
          }
          map.put(linearize(z, t, sizeZ), newPlane);
        }
      }

    } catch (Exception e) {
      throw new Exception("Cannot retrieve the plane", e);
    } finally {
      if (store != null) store.close();
    }

    // Now we are going to create the new image.
    IPixelsPrx proxy = connector.getPixelsService();
    List<IObject> l = proxy.getAllEnumerations(PixelsType.class.getName());
    Iterator<IObject> i = l.iterator();
    PixelsType type = null;
    String original = pixels.getPixelType();
    while (i.hasNext()) {
      PixelsType o = (PixelsType) i.next();
      String value = o.getValue().getValue();
      if (value.equals(original)) {
        type = o;
        break;
      }
    }
    if (type == null) throw new Exception("Pixels Type not valid.");

    String name = "newImageFrom" + image.getId();
    RLong idNew =
        proxy.createImage(
            sizeX,
            sizeY,
            sizeZ,
            sizeT,
            Arrays.asList(0),
            type,
            name,
            "From Image ID: " + image.getId());
    if (idNew == null) throw new Exception("New image could not be created.");
    ImageData newImage = loadImage(idNew.getValue());

    // link the new image and the dataset hosting the source image.
    DatasetImageLink link = new DatasetImageLinkI();
    link.setParent(new DatasetI(info.getDatasetId(), false));
    link.setChild(new ImageI(newImage.getId(), false));
    connector.getUpdateService().saveAndReturnObject(link);

    // Write the data.
    try {
      store = connector.getRawPixelsStore();
      store.setPixelsId(newImage.getDefaultPixels().getId(), false);
      int index = 0;
      for (int z = 0; z < sizeZ; z++) {
        for (int t = 0; t < sizeT; t++) {
          index = linearize(z, t, sizeZ);
          store.setPlane(map.get(index), z, 0, t);
        }
      }
      store.save();
      System.err.println("image created");
    } catch (Exception e) {
      throw new Exception("Cannot set the plane", e);
    } finally {
      if (store != null) store.close();
    }
  }