/*
 * QuickTime for Java SDK Sample Code
 *
 * Usage subject to restrictions in SDK License Agreement
 * Copyright: © 1996-1999 Apple Computer, Inc.
 *
 */
 
import java.awt.*;
import java.awt.event.*;
import java.io.*;

import quicktime.*;
import quicktime.io.*;
import quicktime.qd.*;
import quicktime.std.StdQTConstants;
import quicktime.std.image.*;
import quicktime.std.movies.*;
import quicktime.app.image.*;
import quicktime.app.display.*;
import quicktime.util.*;

public class GraphicsExport extends Frame implements StdQTConstants, IOConstants,  Errors {	
	public static void main (String args[]) {
		try { 
			QTSession.open();
			GraphicsExport graphicsExport = new GraphicsExport("Graphics Export");
			graphicsExport.pack();
			graphicsExport.show();
			graphicsExport.toFront();
		} catch (QTException err) {
			if (err.errorCode() == userCanceledErr) 
				return;
			err.printStackTrace();
			QTSession.close();
		}
	}
	
	
	GraphicsExport (String title) throws QTException {
		super (title);
		openImage();
	
		theButton = new Button ("Export to File");
		theButton.addActionListener (new ActionListener () {
			public void actionPerformed (ActionEvent ae) {
				writeToFile();
			}
		});
		
		add (theButton, "North");

		addWindowListener(new WindowAdapter () {
			public void windowClosing (WindowEvent e) {
				QTSession.close();
				dispose();
			}

			public void windowClosed (WindowEvent e) { 
				System.exit(0);
			}
		});
	}
	
	private QTCanvas myQTCanvas;
	private QTImageDrawer qtImageDrawer;
	private Button theButton;
	
		// we use a QTImageDrawer in this case as the GraphicsExporter
		// requires a the java.awt.Image drawn into a QDGraphics (offscreen)
		// from which to do the export from
	void openImage () throws QTException {
		FileDialog loadFileDlg = new FileDialog (this, "Choose Image...", FileDialog.LOAD);
		loadFileDlg.show();
		if (loadFileDlg.getFile() == null)
			throw new QTException (Errors.userCanceledErr);

		MediaTracker tracker = new MediaTracker (this);
		Image image = Toolkit.getDefaultToolkit().getImage(loadFileDlg.getDirectory() + loadFileDlg.getFile());
		tracker.addImage (image, 0);
		
		// wait for them all to finish importing
		try { tracker.waitForAll(); }
		catch (InterruptedException e) { }
		
		if (tracker.isErrorAny()) throw new QTException ("Error in Media Tracker");
		
		//create a JavaImagePainter and pass it to the QTImageDrawer
		JImagePainter javaImage = new JImagePainter (image);
		javaImage.prepareImage (this);

		qtImageDrawer = new QTImageDrawer (image);
		
		myQTCanvas = new QTCanvas();
		add (myQTCanvas);
		myQTCanvas.setClient (qtImageDrawer, true);			
	}
	
	public void writeToFile() {
		try {
			//You can use functions that return information about the image format supported by a graphics exporter.
			FileDialog saveFileDlg = new FileDialog (this, "Export To...", FileDialog.SAVE);
			saveFileDlg.show();
			if (saveFileDlg.getFile() == null)
				return;
			QTFile exportFile = new QTFile (saveFileDlg.getDirectory() + saveFileDlg.getFile());
			
			// you know which file format you want to write "PICT"
			GraphicsExporter graphicsExporter = new GraphicsExporter(kQTFileTypePicture);
			// Displays a dialog for the user to configure graphics exporter settings, if applicable.
			graphicsExporter.requestSettings(); 			
			//By default, the graphics exporter's suggested file type and creator will be used for any newly
			//created file. To override the suggested file type or creator, call setOutputFileType() and setOutputFileCreator() .
			graphicsExporter.setOutputFile (exportFile); 		// sets the destination for image file
			
			
			int size = qtImageDrawer.export (graphicsExporter);
			System.out.println("done:wrote " + size + " bytes to output file.");
			
		} catch (QTException err) {
			err.printStackTrace();
		}
	}
}
