25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.internal.image.FileFormat;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 public import org.eclipse.swt.graphics.ImageLoader;
|
|
18 public import org.eclipse.swt.graphics.ImageData;
|
|
19 public import org.eclipse.swt.internal.image.LEDataInputStream;
|
|
20 public import org.eclipse.swt.internal.image.LEDataOutputStream;
|
|
21
|
|
22 import org.eclipse.swt.SWT;
|
|
23
|
|
24 public import java.io.InputStream;
|
|
25 public import java.io.OutputStream;
|
|
26
|
|
27 import org.eclipse.swt.internal.image.GIFFileFormat;
|
|
28 import org.eclipse.swt.internal.image.WinBMPFileFormat;
|
|
29 import org.eclipse.swt.internal.image.WinICOFileFormat;
|
|
30 import org.eclipse.swt.internal.image.TIFFFileFormat;
|
|
31 import org.eclipse.swt.internal.image.OS2BMPFileFormat;
|
|
32 import org.eclipse.swt.internal.image.JPEGFileFormat;
|
|
33 import org.eclipse.swt.internal.image.PNGFileFormat;
|
|
34
|
48
|
35 version(Tango){
|
25
|
36 import tango.core.Tuple;
|
48
|
37 } else { // Phobos
|
|
38 }
|
25
|
39
|
|
40 /**
|
|
41 * Abstract factory class for loading/unloading images from files or streams
|
|
42 * in various image file formats.
|
|
43 *
|
|
44 */
|
|
45 public abstract class FileFormat {
|
|
46 static const String FORMAT_PACKAGE = "org.eclipse.swt.internal.image"; //$NON-NLS-1$
|
|
47 static const String FORMAT_SUFFIX = "FileFormat"; //$NON-NLS-1$
|
|
48 static const 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$
|
|
49 alias Tuple!( WinBMPFileFormat, WinBMPFileFormat, GIFFileFormat, WinICOFileFormat, JPEGFileFormat, PNGFileFormat, TIFFFileFormat, OS2BMPFileFormat ) TFormats;
|
|
50 LEDataInputStream inputStream;
|
|
51 LEDataOutputStream outputStream;
|
|
52 ImageLoader loader;
|
|
53 int compression;
|
|
54
|
|
55 /**
|
|
56 * Return whether or not the specified input stream
|
|
57 * represents a supported file format.
|
|
58 */
|
|
59 abstract bool isFileFormat(LEDataInputStream stream);
|
|
60
|
|
61 abstract ImageData[] loadFromByteStream();
|
|
62
|
|
63 /**
|
|
64 * Read the specified input stream, and return the
|
|
65 * device independent image array represented by the stream.
|
|
66 */
|
|
67 public ImageData[] loadFromStream(LEDataInputStream stream) {
|
|
68 try {
|
|
69 inputStream = stream;
|
|
70 return loadFromByteStream();
|
|
71 } catch (IOException e) {
|
|
72 SWT.error(SWT.ERROR_IO, e);
|
|
73 return null;
|
|
74 } catch (Exception e) {
|
|
75 SWT.error(SWT.ERROR_INVALID_IMAGE, e);
|
|
76 return null;
|
|
77 }
|
|
78 }
|
|
79
|
|
80 /**
|
|
81 * Read the specified input stream using the specified loader, and
|
|
82 * return the device independent image array represented by the stream.
|
|
83 */
|
|
84 public static ImageData[] load(InputStream istr, ImageLoader loader) {
|
|
85 FileFormat fileFormat = null;
|
|
86 LEDataInputStream stream = new LEDataInputStream(istr);
|
|
87 bool isSupported = false;
|
|
88 foreach( TFormat; TFormats ){
|
|
89 try{
|
|
90 fileFormat = new TFormat();
|
|
91 if (fileFormat.isFileFormat(stream)) {
|
|
92 isSupported = true;
|
|
93 break;
|
|
94 }
|
|
95 } catch (Exception e) {
|
|
96 }
|
|
97 }
|
|
98 if (!isSupported) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
|
|
99 fileFormat.loader = loader;
|
|
100 return fileFormat.loadFromStream(stream);
|
|
101 }
|
|
102
|
|
103 /**
|
|
104 * Write the device independent image array stored in the specified loader
|
|
105 * to the specified output stream using the specified file format.
|
|
106 */
|
|
107 public static void save(OutputStream os, int format, ImageLoader loader) {
|
|
108 if (format < 0 || format >= FORMATS.length) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
|
|
109 if (FORMATS[format] is null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
|
|
110 if (loader.data is null || loader.data.length < 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
111
|
|
112 LEDataOutputStream stream = new LEDataOutputStream(os);
|
|
113 FileFormat fileFormat = null;
|
|
114 try {
|
|
115 foreach( idx, TFormat; TFormats ){
|
|
116 if( idx is format ){
|
|
117 fileFormat = new TFormat();
|
|
118 }
|
|
119 }
|
|
120 } catch (Exception e) {
|
|
121 SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
|
|
122 }
|
|
123 if (format is SWT.IMAGE_BMP_RLE) {
|
|
124 switch (loader.data[0].depth) {
|
|
125 case 8: fileFormat.compression = 1; break;
|
|
126 case 4: fileFormat.compression = 2; break;
|
|
127 default:
|
|
128 }
|
|
129 }
|
|
130 fileFormat.unloadIntoStream(loader, stream);
|
|
131 }
|
|
132
|
|
133 abstract void unloadIntoByteStream(ImageLoader loader);
|
|
134
|
|
135 /**
|
|
136 * Write the device independent image array stored in the specified loader
|
|
137 * to the specified output stream.
|
|
138 */
|
|
139 public void unloadIntoStream(ImageLoader loader, LEDataOutputStream stream) {
|
|
140 try {
|
|
141 outputStream = stream;
|
|
142 unloadIntoByteStream(loader);
|
|
143 outputStream.flush();
|
|
144 } catch (Exception e) {
|
|
145 try {outputStream.flush();} catch (Exception f) {}
|
|
146 SWT.error(SWT.ERROR_IO, e);
|
|
147 }
|
|
148 }
|
|
149 }
|