]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/internal/image/FileFormat.java
Work around SWT 4.13 - 4.18 Win32 DnD bug 567422
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / internal / image / FileFormat.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2018 IBM Corporation and others.
3  *
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/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.internal.image;
15
16
17 import java.io.*;
18
19 import org.eclipse.swt.*;
20 import org.eclipse.swt.graphics.*;
21
22 /**
23  * Abstract factory class for loading/unloading images from files or streams
24  * in various image file formats.
25  *
26  */
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$
31
32         LEDataInputStream inputStream;
33         LEDataOutputStream outputStream;
34         ImageLoader loader;
35         int compression;
36
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;
41         return null;
42 }
43
44 /**
45  * Return whether or not the specified input stream
46  * represents a supported file format.
47  */
48 abstract boolean isFileFormat(LEDataInputStream stream);
49
50 abstract ImageData[] loadFromByteStream();
51
52 /**
53  * Read the specified input stream, and return the
54  * device independent image array represented by the stream.
55  */
56 public ImageData[] loadFromStream(LEDataInputStream stream) {
57         try {
58                 inputStream = stream;
59                 return loadFromByteStream();
60         } catch (Exception e) {
61                 if (e instanceof IOException) {
62                         SWT.error(SWT.ERROR_IO, e);
63                 } else {
64                         SWT.error(SWT.ERROR_INVALID_IMAGE, e);
65                 }
66                 return null;
67         }
68 }
69
70 /**
71  * Read the specified input stream using the specified loader, and
72  * return the device independent image array represented by the stream.
73  */
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) {
79                         try {
80                                 fileFormat = getFileFormat (stream, FORMATS[i]);
81                                 if (fileFormat != null) break;
82                         } catch (ClassNotFoundException e) {
83                                 FORMATS[i] = null;
84                         } catch (Exception e) {
85                         }
86                 }
87         }
88         if (fileFormat == null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
89         fileFormat.loader = loader;
90         return fileFormat.loadFromStream(stream);
91 }
92
93 /**
94  * Write the device independent image array stored in the specified loader
95  * to the specified output stream using the specified file format.
96  */
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);
101
102         LEDataOutputStream stream = new LEDataOutputStream(os);
103         FileFormat fileFormat = null;
104         try {
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);
109         }
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;
114                 }
115         }
116         fileFormat.unloadIntoStream(loader, stream);
117 }
118
119 abstract void unloadIntoByteStream(ImageLoader loader);
120
121 /**
122  * Write the device independent image array stored in the specified loader
123  * to the specified output stream.
124  */
125 public void unloadIntoStream(ImageLoader loader, LEDataOutputStream stream) {
126         try {
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);
133         }
134 }
135 }