1 /*******************************************************************************
2 * Copyright (c) 2000, 2018 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.internal.image;
19 import org.eclipse.swt.*;
20 import org.eclipse.swt.graphics.*;
23 * Abstract factory class for loading/unloading images from files or streams
24 * in various image file formats.
27 public abstract class FileFormat {
28 static final String FORMAT_PACKAGE = "org.eclipse.swt.internal.image"; //$NON-NLS-1$
29 static final String FORMAT_SUFFIX = "FileFormat"; //$NON-NLS-1$
30 static final String[] FORMATS = {"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$
32 LEDataInputStream inputStream;
33 LEDataOutputStream outputStream;
37 static FileFormat getFileFormat (LEDataInputStream stream, String format) throws Exception {
38 Class<?> clazz = Class.forName(FORMAT_PACKAGE + '.' + format + FORMAT_SUFFIX);
39 FileFormat fileFormat = (FileFormat) clazz.getDeclaredConstructor().newInstance();
40 if (fileFormat.isFileFormat(stream)) return fileFormat;
45 * Return whether or not the specified input stream
46 * represents a supported file format.
48 abstract boolean isFileFormat(LEDataInputStream stream);
50 abstract ImageData[] loadFromByteStream();
53 * Read the specified input stream, and return the
54 * device independent image array represented by the stream.
56 public ImageData[] loadFromStream(LEDataInputStream stream) {
59 return loadFromByteStream();
60 } catch (Exception e) {
61 if (e instanceof IOException) {
62 SWT.error(SWT.ERROR_IO, e);
64 SWT.error(SWT.ERROR_INVALID_IMAGE, e);
71 * Read the specified input stream using the specified loader, and
72 * return the device independent image array represented by the stream.
74 public static ImageData[] load(InputStream is, ImageLoader loader) {
75 FileFormat fileFormat = null;
76 LEDataInputStream stream = new LEDataInputStream(is);
77 for (int i = 1; i < FORMATS.length; i++) {
78 if (FORMATS[i] != null) {
80 fileFormat = getFileFormat (stream, FORMATS[i]);
81 if (fileFormat != null) break;
82 } catch (ClassNotFoundException e) {
84 } catch (Exception e) {
88 if (fileFormat == null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
89 fileFormat.loader = loader;
90 return fileFormat.loadFromStream(stream);
94 * Write the device independent image array stored in the specified loader
95 * to the specified output stream using the specified file format.
97 public static void save(OutputStream os, int format, ImageLoader loader) {
98 if (format < 0 || format >= FORMATS.length) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
99 if (FORMATS[format] == null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
100 if (loader.data == null || loader.data.length < 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
102 LEDataOutputStream stream = new LEDataOutputStream(os);
103 FileFormat fileFormat = null;
105 Class<?> clazz = Class.forName(FORMAT_PACKAGE + '.' + FORMATS[format] + FORMAT_SUFFIX);
106 fileFormat = (FileFormat) clazz.getDeclaredConstructor().newInstance();
107 } catch (Exception e) {
108 SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
110 if (format == SWT.IMAGE_BMP_RLE) {
111 switch (loader.data[0].depth) {
112 case 8: fileFormat.compression = 1; break;
113 case 4: fileFormat.compression = 2; break;
116 fileFormat.unloadIntoStream(loader, stream);
119 abstract void unloadIntoByteStream(ImageLoader loader);
122 * Write the device independent image array stored in the specified loader
123 * to the specified output stream.
125 public void unloadIntoStream(ImageLoader loader, LEDataOutputStream stream) {
127 outputStream = stream;
128 unloadIntoByteStream(loader);
129 outputStream.flush();
130 } catch (Exception e) {
131 try {outputStream.flush();} catch (Exception f) {}
132 SWT.error(SWT.ERROR_IO, e);