/*******************************************************************************
* Copyright (c) 2000, 2007 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.examples.imageanalyzer;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.printing.*;
import org.eclipse.swt.custom.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.text.MessageFormat;
public class ImageAnalyzer {
static ResourceBundle bundle = ResourceBundle.getBundle("examples_images");
Display display;
Shell shell;
Canvas imageCanvas, paletteCanvas;
Label typeLabel, sizeLabel, depthLabel, transparentPixelLabel,
timeToLoadLabel, screenSizeLabel, backgroundPixelLabel,
locationLabel, disposalMethodLabel, delayTimeLabel,
repeatCountLabel, paletteLabel, dataLabel, statusLabel;
Combo backgroundCombo, scaleXCombo, scaleYCombo, alphaCombo;
Button incrementalCheck, transparentCheck, maskCheck, backgroundCheck;
Button previousButton, nextButton, animateButton;
StyledText dataText;
Sash sash;
Color whiteColor, blackColor, redColor, greenColor, blueColor, canvasBackground;
Font fixedWidthFont;
Cursor crossCursor;
GC imageCanvasGC;
int paletteWidth = 140; // recalculated and used as a width hint
int ix = 0, iy = 0, py = 0; // used to scroll the image and palette
float xscale = 1, yscale = 1; // used to scale the image
int alpha = 255; // used to modify the alpha value of the image
boolean incremental = false; // used to incrementally display an image
boolean transparent = true; // used to display an image with transparency
boolean showMask = false; // used to display an icon mask or transparent image mask
boolean showBackground = false; // used to display the background of an animated image
boolean animate = false; // used to animate a multi-image file
Thread animateThread; // draws animated images
Thread incrementalThread; // draws incremental images
String lastPath; // used to seed the file dialog
String currentName; // the current image file or URL name
String fileName; // the current image file
ImageLoader loader; // the loader for the current image file
ImageData[] imageDataArray; // all image data read from the current file
int imageDataIndex; // the index of the current image data
ImageData imageData; // the currently-displayed image data
Image image; // the currently-displayed image
Vector incrementalEvents; // incremental image events
long loadTime = 0; // the time it took to load the current image
static final int INDEX_DIGITS = 4;
static final int ALPHA_CHARS = 5;
static final int ALPHA_CONSTANT = 0;
static final int ALPHA_X = 1;
static final int ALPHA_Y = 2;
static final String[] OPEN_FILTER_EXTENSIONS = new String[] {
"*.bmp; *.gif; *.ico; *.jfif; *.jpeg; *.jpg; *.png; *.tif; *.tiff",
"*.bmp", "*.gif", "*.ico", "*.jpg; *.jpeg; *.jfif", "*.png", "*.tif; *.tiff" };
static final String[] OPEN_FILTER_NAMES = new String[] {
bundle.getString("All_images") + " (bmp, gif, ico, jfif, jpeg, jpg, png, tif, tiff)",
"BMP (*.bmp)", "GIF (*.gif)", "ICO (*.ico)", "JPEG (*.jpg, *.jpeg, *.jfif)",
"PNG (*.png)", "TIFF (*.tif, *.tiff)" };
static final String[] SAVE_FILTER_EXTENSIONS = new String[] {
"*.bmp", "*.bmp", "*.gif", "*.ico", "*.jpg", "*.png", "*.tif", "*.bmp" };
static final String[] SAVE_FILTER_NAMES = new String[] {
"Uncompressed BMP (*.bmp)", "RLE Compressed BMP (*.bmp)", "GIF (*.gif)",
"ICO (*.ico)", "JPEG (*.jpg)", "PNG (*.png)",
"TIFF (*.tif)", "OS/2 BMP (*.bmp)" };
class TextPrompter extends Dialog {
String message = "";
String result = null;
Shell dialog;
Text text;
public TextPrompter (Shell parent, int style) {
super (parent, style);
}
public TextPrompter (Shell parent) {
this (parent, SWT.APPLICATION_MODAL);
}
public String getMessage () {
return message;
}
public void setMessage (String string) {
message = string;
}
public String open () {
dialog = new Shell(getParent(), getStyle());
dialog.setText(getText());
dialog.setLayout(new GridLayout());
Label label = new Label(dialog, SWT.NONE);
label.setText(message);
label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
text = new Text(dialog, SWT.SINGLE | SWT.BORDER);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.widthHint = 300;
text.setLayoutData(data);
Composite buttons = new Composite(dialog, SWT.NONE);
GridLayout grid = new GridLayout();
grid.numColumns = 2;
buttons.setLayout(grid);
buttons.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
Button ok = new Button(buttons, SWT.PUSH);
ok.setText(bundle.getString("OK"));
data = new GridData();
data.widthHint = 75;
ok.setLayoutData(data);
ok.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
result = text.getText();
dialog.dispose();
}
});
Button cancel = new Button(buttons, SWT.PUSH);
cancel.setText(bundle.getString("Cancel"));
data = new GridData();
data.widthHint = 75;
cancel.setLayoutData(data);
cancel.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
dialog.dispose();
}
});
dialog.setDefaultButton(ok);
dialog.pack();
dialog.open();
while (!dialog.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
return result;
}
}
public static void main(String [] args) {
Display display = new Display();
ImageAnalyzer imageAnalyzer = new ImageAnalyzer();
Shell shell = imageAnalyzer.open(display);
while (!shell.isDisposed())
if (!display.readAndDispatch()) display.sleep();
display.dispose();
}
public Shell open(Display dpy) {
// Create a window and set its title.
this.display = dpy;
shell = new Shell(display);
shell.setText(bundle.getString("Image_analyzer"));
// Hook resize and dispose listeners.
shell.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent event) {
resizeShell(event);
}
});
shell.addShellListener(new ShellAdapter() {
public void shellClosed(ShellEvent e) {
animate = false; // stop any animation in progress
if (animateThread != null) {
// wait for the thread to die before disposing the shell.
while (animateThread.isAlive()) {
if (!display.readAndDispatch()) display.sleep();
}
}
e.doit = true;
}
});
shell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
// Clean up.
if (image != null)
image.dispose();
whiteColor.dispose();
blackColor.dispose();
redColor.dispose();
greenColor.dispose();
blueColor.dispose();
fixedWidthFont.dispose();
crossCursor.dispose();
}
});
// Create colors and fonts.
whiteColor = new Color(display, 255, 255, 255);
blackColor = new Color(display, 0, 0, 0);
redColor = new Color(display, 255, 0, 0);
greenColor = new Color(display, 0, 255, 0);
blueColor = new Color(display, 0, 0, 255);
fixedWidthFont = new Font(display, "courier", 10, 0);
crossCursor = new Cursor(display, SWT.CURSOR_CROSS);
// Add a menu bar and widgets.
createMenuBar();
createWidgets();
shell.pack();
// Create a GC for drawing, and hook the listener to dispose it.
imageCanvasGC = new GC(imageCanvas);
imageCanvas.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
imageCanvas