Exemplo n.º 1
0
  public static void writeOpenRaster(Composition comp, File outFile, boolean addMergedImage)
      throws IOException {
    FileOutputStream fos = new FileOutputStream(outFile);
    ZipOutputStream zos = new ZipOutputStream(fos);

    String stackXML =
        String.format(
            "<?xml version='1.0' encoding='UTF-8'?>\n"
                + "<image w=\"%d\" h=\"%d\">\n"
                + "<stack>\n",
            comp.getCanvasWidth(), comp.getCanvasHeight());

    int nrLayers = comp.getNrLayers();

    // Reverse iteration: in stack.xml the first element in a stack is the uppermost.
    for (int i = nrLayers - 1; i >= 0; i--) {
      Layer layer = comp.getLayer(i);
      stackXML += writeLayer(zos, i, layer);
    }

    if (addMergedImage) {
      zos.putNextEntry(new ZipEntry("mergedimage.png"));
      ImageIO.write(comp.getCompositeImage(), "PNG", zos);
      zos.closeEntry();
    }

    stackXML += "</stack>\n</image>";

    // write the stack.xml file
    zos.putNextEntry(new ZipEntry("stack.xml"));
    zos.write(stackXML.getBytes("UTF-8"));
    zos.closeEntry();

    // write the mimetype
    zos.putNextEntry(new ZipEntry("mimetype"));
    zos.write("image/openraster".getBytes("UTF-8"));
    zos.closeEntry();
    zos.close();
  }