001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.swt.internal.image;
011:
012: import java.io.*;
013: import org.eclipse.swt.*;
014: import org.eclipse.swt.graphics.*;
015:
016: /**
017: * Abstract factory class for loading/unloading images from files or streams
018: * in various image file formats.
019: *
020: */
021: public abstract class FileFormat {
022: static final String FORMAT_PACKAGE = "org.eclipse.swt.internal.image"; //$NON-NLS-1$
023: static final String FORMAT_SUFFIX = "FileFormat"; //$NON-NLS-1$
024: static final String[] FORMATS = {
025: "WinBMP", "WinBMP", "GIF", "WinICO", "JPEG", "PNG", "TIFF", "OS2BMP" }; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$//$NON-NLS-5$ //$NON-NLS-6$//$NON-NLS-7$//$NON-NLS-8$
026:
027: LEDataInputStream inputStream;
028: LEDataOutputStream outputStream;
029: ImageLoader loader;
030: int compression;
031:
032: /**
033: * Return whether or not the specified input stream
034: * represents a supported file format.
035: */
036: abstract boolean isFileFormat(LEDataInputStream stream);
037:
038: abstract ImageData[] loadFromByteStream();
039:
040: /**
041: * Read the specified input stream, and return the
042: * device independent image array represented by the stream.
043: */
044: public ImageData[] loadFromStream(LEDataInputStream stream) {
045: try {
046: inputStream = stream;
047: return loadFromByteStream();
048: } catch (Exception e) {
049: if (e instanceof IOException) {
050: SWT.error(SWT.ERROR_IO, e);
051: } else {
052: SWT.error(SWT.ERROR_INVALID_IMAGE, e);
053: }
054: return null;
055: }
056: }
057:
058: /**
059: * Read the specified input stream using the specified loader, and
060: * return the device independent image array represented by the stream.
061: */
062: public static ImageData[] load(InputStream is, ImageLoader loader) {
063: FileFormat fileFormat = null;
064: LEDataInputStream stream = new LEDataInputStream(is);
065: boolean isSupported = false;
066: for (int i = 1; i < FORMATS.length; i++) {
067: if (FORMATS[i] != null) {
068: try {
069: Class clazz = Class.forName(FORMAT_PACKAGE + '.'
070: + FORMATS[i] + FORMAT_SUFFIX);
071: fileFormat = (FileFormat) clazz.newInstance();
072: if (fileFormat.isFileFormat(stream)) {
073: isSupported = true;
074: break;
075: }
076: } catch (ClassNotFoundException e) {
077: FORMATS[i] = null;
078: } catch (Exception e) {
079: }
080: }
081: }
082: if (!isSupported)
083: SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
084: fileFormat.loader = loader;
085: return fileFormat.loadFromStream(stream);
086: }
087:
088: /**
089: * Write the device independent image array stored in the specified loader
090: * to the specified output stream using the specified file format.
091: */
092: public static void save(OutputStream os, int format,
093: ImageLoader loader) {
094: if (format < 0 || format >= FORMATS.length)
095: SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
096: if (FORMATS[format] == null)
097: SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
098: if (loader.data == null || loader.data.length < 1)
099: SWT.error(SWT.ERROR_INVALID_ARGUMENT);
100:
101: LEDataOutputStream stream = new LEDataOutputStream(os);
102: FileFormat fileFormat = null;
103: try {
104: Class clazz = Class.forName(FORMAT_PACKAGE + '.'
105: + FORMATS[format] + FORMAT_SUFFIX);
106: fileFormat = (FileFormat) clazz.newInstance();
107: } catch (Exception e) {
108: SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
109: }
110: if (format == SWT.IMAGE_BMP_RLE) {
111: switch (loader.data[0].depth) {
112: case 8:
113: fileFormat.compression = 1;
114: break;
115: case 4:
116: fileFormat.compression = 2;
117: break;
118: }
119: }
120: fileFormat.unloadIntoStream(loader, stream);
121: }
122:
123: abstract void unloadIntoByteStream(ImageLoader loader);
124:
125: /**
126: * Write the device independent image array stored in the specified loader
127: * to the specified output stream.
128: */
129: public void unloadIntoStream(ImageLoader loader,
130: LEDataOutputStream stream) {
131: try {
132: outputStream = stream;
133: unloadIntoByteStream(loader);
134: outputStream.flush();
135: } catch (Exception e) {
136: try {
137: outputStream.flush();
138: } catch (Exception f) {
139: }
140: SWT.error(SWT.ERROR_IO, e);
141: }
142: }
143: }
|