1402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll/*******************************************************************************
2402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * Copyright (c) 2000, 2009 IBM Corporation and others.
3402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * All rights reserved. This program and the accompanying materials
4402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * are made available under the terms of the Eclipse Public License v1.0
5402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * which accompanies this distribution, and is available at
6402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * http://www.eclipse.org/legal/epl-v10.html
7402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll *
8402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * Contributors:
9402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll *     IBM Corporation - initial API and implementation
10402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll *******************************************************************************/
11402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollpackage org.eclipse.test.performance.ui;
12402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
13402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport java.io.BufferedOutputStream;
14402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport java.io.File;
15402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport java.io.FileNotFoundException;
16402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport java.io.FileOutputStream;
17402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport java.io.IOException;
18402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport java.io.OutputStream;
19402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport java.io.PrintStream;
20402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport java.util.ArrayList;
21402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport java.util.List;
22402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
23402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport org.eclipse.swt.SWT;
24402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport org.eclipse.swt.graphics.GC;
25402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport org.eclipse.swt.graphics.Image;
26402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport org.eclipse.swt.graphics.ImageData;
27402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport org.eclipse.swt.graphics.ImageLoader;
28402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport org.eclipse.swt.widgets.Display;
29402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport org.eclipse.test.internal.performance.results.db.ConfigResults;
30402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport org.eclipse.test.internal.performance.results.db.DB_Results;
31402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport org.eclipse.test.internal.performance.results.db.PerformanceResults;
32402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport org.eclipse.test.internal.performance.results.db.ScenarioResults;
33402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
34402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll/**
35402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * Class used to create scenario fingerprint.
36402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll */
37402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollpublic class FingerPrint {
38402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
39402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollprivate static final int GRAPH_WIDTH = 1000;
40402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
41402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	String component;
42402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	PrintStream stream;
43402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	File outputDir;
44402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
45402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollpublic FingerPrint(String name, PrintStream ps, File outputDir) {
46402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	if (!name.startsWith("global")) this.component = name;
47402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream = ps;
48402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.outputDir = outputDir;
49402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll}
50402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
51402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll/**
52402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * Create and save fingerprints as image and print their reference in the current stream.
53402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll *
54402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * @param performanceResults The performance results used to print the fingerprints
55402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll */
56402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollpublic void print(final PerformanceResults performanceResults) {
57402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	String buildName = performanceResults.getName();
58402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
59402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	// Compute fingerprint output file name prefix
60402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	int currentUnderscoreIndex = buildName.indexOf('_');
61402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	if  (currentUnderscoreIndex != -1){
62402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		buildName = buildName.substring(0, currentUnderscoreIndex);
63402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	}
64402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	StringBuffer buffer = new StringBuffer("FP_");
65402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	if (this.component != null) {
66402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		buffer.append(this.component);
67402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		buffer.append('_');
68402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	}
69402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	buffer.append(DB_Results.getDbBaselineRefVersion());
70402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	buffer.append('_');
71402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	buffer.append(buildName);
72402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	String filePrefix = buffer.toString();
73402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
74402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	// Print the legend
75402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("The following fingerprints show results for the most representative tests of the ");
76402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	if (this.component == null) {
77402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		this.stream.print("current build.<br>\n");
78402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	} else {
79402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		this.stream.print(this.component);
80402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		this.stream.print(" component.<br>\n");
81402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	}
82402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("<table border=\"0\">\n");
83402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("<tr><td valign=\"top\">Select which kind of scale you want to use:</td>\n");
84402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("<td valign=\"top\">\n");
85402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("  <form>\n");
86402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("    <select onChange=\"toggleFingerprints();\">\n");
87402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("      <option>percentage</option>\n");
88402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("      <option>time (linear)</option>\n");
89402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("      <option>time (log)</option>\n");
90402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("    </select>\n");
91402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("  </form>\n");
92402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("</td>\n");
93402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll//	this.stream.print("<td valign=\"top\">\n");
94402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll//	this.stream.print("<a href=\"help.html\"><img hspace=\"10\" border=\"0\" src=\""+Utils.LIGHT+"\" title=\"Some tips on fingerprints\"/></a>\n");
95402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll//	this.stream.print("</td></tr></table>\n");
96402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("</tr></table>\n");
97402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("<img hspace=\"10\" border=\"0\" src=\""+Utils.LIGHT+"\"><a href=\""+Utils.HELP+"\">Help on fingerprints</a>\n");
98402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
99402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	// Print script to reset dropdown list selection
100402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("<script type=\"text/javascript\">\n");
101402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("	setFingerprintsType();\n");
102402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	this.stream.print("</script>\n");
103402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
104402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	// Create each fingerprint and save it
105402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	String[] configNames = performanceResults.getConfigNames(false/* not sorted*/);
106402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	String[] configBoxes = performanceResults.getConfigBoxes(false/* not sorted*/);
107402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	int length = configNames.length;
108402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	for (int c=0; c<length; c++) {
109402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		String configName  = configNames[c];
110402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		List scenarios = performanceResults.getComponentSummaryScenarios(this.component, configName);
111402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		if (scenarios == null) continue;
112402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
113402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		// Create BarGraph
114402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		// TODO use FingerPrintGraph instead
115402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		BarGraph barGraph = null;
116402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		List allResults = new ArrayList();
117402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		String defaultDimName = DB_Results.getDefaultDimension().getName();
118402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		for (int i=0, size=scenarios.size(); i<size; i++) {
119402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			ScenarioResults scenarioResults = (ScenarioResults) scenarios.get(i);
120402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			ConfigResults configResults = scenarioResults.getConfigResults(configName);
121402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			if (configResults == null || !configResults.isValid()) continue;
122402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			double[] results = configResults.getCurrentBuildDeltaInfo();
123402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			double percent = -results[0] * 100.0;
124402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			if (results != null && Math.abs(percent) < 200) {
125402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				String name = scenarioResults.getLabel() + " (" + defaultDimName + ")";
126402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				if (!configResults.getCurrentBuildName().equals(buildName)) {
127402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll					continue; // the test didn't run on last build, skip it
128402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				}
129402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				if (!configResults.isBaselined()) {
130402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll					name = "*" + name + " (" + configResults.getBaselineBuildName() + ")";
131402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				}
132402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				if (barGraph == null) {
133402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll					barGraph = new BarGraph(null);
134402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				}
135402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				barGraph.addItem(name,
136402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				    results,
137402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				    configName + "/" + scenarioResults.getFileName() + ".html",
138402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				    configResults.getCurrentBuildResults().getComment(),
139402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				    (Utils.confidenceLevel(results) & Utils.ERR) == 0);
140402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
141402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				// add results
142402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				allResults.add(configResults);
143402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			}
144402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		}
145402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		if (barGraph == null) continue;
146402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
147402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		// Save image file
148402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		String fileName = filePrefix + '.' + configName ;
149402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		File outputFile = new File(this.outputDir, fileName+".gif");
150402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		save(barGraph, outputFile);
151402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
152402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		// Print image file reference in stream
153402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		String boxName = configBoxes[c];
154402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		if (outputFile.exists()) {
155402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			String areas = barGraph.getAreas();
156402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			if (areas == null) areas = "";
157402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print("<h4>");
158402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print(boxName);
159402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print("</h4>\n");
160402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print("<?php\n");
161402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print("	$type=$_SERVER['QUERY_STRING'];\n");
162402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print("	if ($type==\"\" || $type==\"fp_type=0\") {\n");
163402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print("		echo '<img src=\"");
164402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print(fileName);
165402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print(".gif\" usemap=\"#");
166402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print(fileName);
167402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print("\" name=\"");
168402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print(configName);
169402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print("\">';\n");
170402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print("		echo '<map name=\"");
171402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print(fileName);
172402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print("\">';\n");
173402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print(areas);
174402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print("		echo '</map>';\n");
175402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print("	}\n");
176402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		} else {
177402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print("<br><br>There is no fingerprint for ");
178402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print(boxName);
179402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			this.stream.print("<br><br>\n");
180402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		}
181402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
182402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		// Create, paint and print the time bars graph
183402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		FingerPrintGraph graph = new FingerPrintGraph(this.outputDir, fileName, GRAPH_WIDTH, allResults);
184402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		graph.paint(this.stream);
185402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		this.stream.print("?>\n");
186402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	}
187402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll}
188402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
189402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll/*
190402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * Save the computed bar graph.
191402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll */
192402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollprivate void save(BarGraph barGraph, File outputFile) {
193402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
194402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	// Create and paint image
195402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	Display display = Display.getDefault();
196402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	int height = barGraph.getHeight();
197402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	Image image = new Image(display, GRAPH_WIDTH, height);
198402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	GC gc = new GC(image);
199402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	barGraph.paint(display, GRAPH_WIDTH, height, gc);
200402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	gc.dispose();
201402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
202402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	saveImage(outputFile, image);
203402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll}
204402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
205402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll/**
206402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * @param outputFile
207402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * @param image
208402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll */
209402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollprivate void saveImage(File outputFile, Image image) {
210402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	// Save image
211402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	ImageData data = Utils.downSample(image);
212402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	ImageLoader imageLoader = new ImageLoader();
213402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	imageLoader.data = new ImageData[] { data };
214402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
215402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	OutputStream out = null;
216402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	try {
217402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		out = new BufferedOutputStream(new FileOutputStream(outputFile));
218402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		imageLoader.save(out, SWT.IMAGE_GIF);
219402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	} catch (FileNotFoundException e) {
220402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		e.printStackTrace();
221402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	} finally {
222402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		image.dispose();
223402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		if (out != null) {
224402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			try {
225402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				out.close();
226402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			} catch (IOException e1) {
227402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll				// silently ignored
228402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			}
229402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		}
230402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	}
231402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll}
232402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll}
233