/**
   * Performs a deep copy of the writer, including nested wrapped writers. Most of the writer state
   * is preserved as well, including:
   *
   * <ul>
   *   <li>{@link #isInterleaved()}
   *   <li>{@link #getColorModel()}
   *   <li>{@link #getFramesPerSecond()}
   *   <li>{@link #getCompression()}
   * </ul>
   *
   * @param imageWriterClass If non-null, any {@link ImageWriter}s in the writer stack will be
   *     replaced with instances of the given class.
   * @throws FormatException If something goes wrong during the duplication.
   */
  public WriterWrapper duplicate(Class<? extends IFormatWriter> imageWriterClass)
      throws FormatException {
    WriterWrapper wrapperCopy = duplicateRecurse(imageWriterClass);

    // sync top-level configuration with original writer
    boolean interleaved = isInterleaved();
    ColorModel cm = getColorModel();
    int rate = getFramesPerSecond();
    String compress = getCompression();
    wrapperCopy.setInterleaved(interleaved);
    wrapperCopy.setColorModel(cm);
    wrapperCopy.setFramesPerSecond(rate);
    wrapperCopy.setCompression(compress);
    return wrapperCopy;
  }