//
// WriteRGBMovie.java
//

import loci.common.services.ServiceFactory;
import loci.formats.*;
import loci.formats.meta.IMetadata;
import loci.formats.services.OMEXMLService;

import ome.xml.model.enums.DimensionOrder;
import ome.xml.model.enums.EnumerationException;
import ome.xml.model.enums.PixelType;
import ome.xml.model.primitives.PositiveInteger;

/**
 * Demonstrates writing multiple RGB image planes to a movie.
 *
 * <dl><dt><b>Source code:</b></dt>
 * <dd><a href="http://trac.openmicroscopy.org.uk/ome/browser/bioformats.git/components/bio-formats/utils/WriteRGBMovie.java">Trac</a>,
 * <a href="http://git.openmicroscopy.org/?p=bioformats.git;a=blob;f=components/bio-formats/utils/WriteRGBMovie.java;hb=HEAD">Gitweb</a></dd></dl>
 */
public class WriteRGBMovie {

  public static void main(String[] args) throws Exception {
    if (args.length < 1) {
      System.out.println("Please specify an output file name.");
      System.exit(1);
    }
    String id = args[0];

    // create 20 blank 512x512 image planes
    System.out.println("Creating random image planes...");
    int w = 511, h = 507, numFrames = 20, numChannels = 3;
    int pixelType = FormatTools.UINT8;
    int bpp = FormatTools.getBytesPerPixel(pixelType);
    int planeSize = h * w * numChannels * bpp;
    byte[][] img = new byte[numFrames][planeSize];

    // fill with random data
    for (int t=0; t<numFrames; t++) {
      for (int i=0; i<img[t].length; i+=numChannels) {
        for (int c=0; c<numChannels; c++) {
          img[t][i + c] = (byte) (256 * Math.random());
        }
      }
    }

    // create metadata object with required metadata fields
    System.out.println("Populating metadata...");
    ServiceFactory factory = new ServiceFactory();
    OMEXMLService service = factory.getInstance(OMEXMLService.class);
    IMetadata meta = service.createOMEXMLMetadata();
    meta.createRoot();

    meta.setImageID("Image:0", 0);
    meta.setPixelsID("Pixels:0", 0);
    meta.setPixelsBinDataBigEndian(Boolean.TRUE, 0, 0);
    meta.setPixelsDimensionOrder(DimensionOrder.XYZCT, 0);
    meta.setPixelsType(
      PixelType.fromString(FormatTools.getPixelTypeString(pixelType)), 0);
    meta.setPixelsSizeX(new PositiveInteger(w), 0);
    meta.setPixelsSizeY(new PositiveInteger(h), 0);
    meta.setPixelsSizeZ(new PositiveInteger(1), 0);
    meta.setPixelsSizeC(new PositiveInteger(numChannels), 0);
    meta.setPixelsSizeT(new PositiveInteger(numFrames), 0);
    meta.setChannelID("Channel:0:0", 0, 0);
    meta.setChannelSamplesPerPixel(new PositiveInteger(numChannels), 0, 0);

    // write image planes to disk
    System.out.print("Writing planes to '" + id + "'");
    IFormatWriter writer = new ImageWriter();
    writer.setMetadataRetrieve(meta);
    writer.setId(id);
    for (int t=0; t<numFrames; t++) {
      System.out.print(".");
      writer.saveBytes(t, img[t]);
    }
    writer.close();

    System.out.println("Done.");
  }

}

@@ -1868,270 +1868,81 @@
 ();%0A
+%0A
     
-meta.createRoot();%0A%0A    meta.setImageID(%22Image:0%22, 0);%0A    meta.setPixelsID(%22Pixels:0%22, 0);%0A    meta.setPixelsBinDataBigEndian(Boolean.TRUE, 0, 0);%0A    meta.setPixelsDimensionOrder(DimensionOrder.XYZCT, 0);%0A    meta.setPixelsType(%0A      PixelType.fromString(
+MetadataTools.populateMetadata(meta, 0, null, false, %22XYZCT%22,%0A      
 Form
@@ -1982,277 +1982,38 @@
 ype)
-)
 , 
-0);%0A    meta.setPixelsSizeX(new PositiveInteger(w), 0);%0A    meta.setPixelsSizeY(new PositiveInteger(h), 0);%0A    meta.setPixelsSizeZ(new PositiveInteger(1), 0);%0A    meta.setPixelsSizeC(new PositiveInteger(numChannels), 0);%0A    meta.setPixelsSizeT(new PositiveInteger(
+w, h, 1, numChannels,%0A      
 numF
@@ -2021,114 +2021,10 @@
 ames
-)
 , 
-0);%0A    meta.setChannelID(%22Channel:0:0%22, 0, 0);%0A    meta.setChannelSamplesPerPixel(new PositiveInteger(
 numC
@@ -2035,15 +2035,8 @@
 els)
-, 0, 0)
 ;%0A%0A 

Use populateMetadata method in WriteRGBMovie.