comparison org.eclipse.swt.win32.win32.x86/src/org/eclipse/swt/internal/image/FileFormat.d @ 0:6dd524f61e62

add dwt win and basic java stuff
author Frank Benoit <benoit@tionex.de>
date Mon, 02 Mar 2009 14:44:16 +0100
parents
children f36c67707cb3
comparison
equal deleted inserted replaced
-1:000000000000 0:6dd524f61e62
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
35 import tango.core.Exception;
36 import tango.core.Tuple;
37
38 /**
39 * Abstract factory class for loading/unloading images from files or streams
40 * in various image file formats.
41 *
42 */
43 public abstract class FileFormat {
44 static const String FORMAT_PACKAGE = "org.eclipse.swt.internal.image"; //$NON-NLS-1$
45 static const String FORMAT_SUFFIX = "FileFormat"; //$NON-NLS-1$
46 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$
47 alias Tuple!( WinBMPFileFormat, WinBMPFileFormat, GIFFileFormat, WinICOFileFormat, JPEGFileFormat, PNGFileFormat, TIFFFileFormat, OS2BMPFileFormat ) TFormats;
48 LEDataInputStream inputStream;
49 LEDataOutputStream outputStream;
50 ImageLoader loader;
51 int compression;
52
53 /**
54 * Return whether or not the specified input stream
55 * represents a supported file format.
56 */
57 abstract bool isFileFormat(LEDataInputStream stream);
58
59 abstract ImageData[] loadFromByteStream();
60
61 /**
62 * Read the specified input stream, and return the
63 * device independent image array represented by the stream.
64 */
65 public ImageData[] loadFromStream(LEDataInputStream stream) {
66 try {
67 inputStream = stream;
68 return loadFromByteStream();
69 } catch (IOException e) {
70 SWT.error(SWT.ERROR_IO, e);
71 return null;
72 } catch (Exception e) {
73 SWT.error(SWT.ERROR_INVALID_IMAGE, e);
74 return null;
75 }
76 }
77
78 /**
79 * Read the specified input stream using the specified loader, and
80 * return the device independent image array represented by the stream.
81 */
82 public static ImageData[] load(InputStream istr, ImageLoader loader) {
83 FileFormat fileFormat = null;
84 LEDataInputStream stream = new LEDataInputStream(istr);
85 bool isSupported = false;
86 foreach( TFormat; TFormats ){
87 try{
88 fileFormat = new TFormat();
89 if (fileFormat.isFileFormat(stream)) {
90 isSupported = true;
91 break;
92 }
93 } catch (Exception e) {
94 }
95 }
96 if (!isSupported) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
97 fileFormat.loader = loader;
98 return fileFormat.loadFromStream(stream);
99 }
100
101 /**
102 * Write the device independent image array stored in the specified loader
103 * to the specified output stream using the specified file format.
104 */
105 public static void save(OutputStream os, int format, ImageLoader loader) {
106 if (format < 0 || format >= FORMATS.length) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
107 if (FORMATS[format] is null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
108 if (loader.data is null || loader.data.length < 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
109
110 LEDataOutputStream stream = new LEDataOutputStream(os);
111 FileFormat fileFormat = null;
112 try {
113 foreach( idx, TFormat; TFormats ){
114 if( idx is format ){
115 fileFormat = new TFormat();
116 }
117 }
118 } catch (Exception e) {
119 SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
120 }
121 if (format is SWT.IMAGE_BMP_RLE) {
122 switch (loader.data[0].depth) {
123 case 8: fileFormat.compression = 1; break;
124 case 4: fileFormat.compression = 2; break;
125 default:
126 }
127 }
128 fileFormat.unloadIntoStream(loader, stream);
129 }
130
131 abstract void unloadIntoByteStream(ImageLoader loader);
132
133 /**
134 * Write the device independent image array stored in the specified loader
135 * to the specified output stream.
136 */
137 public void unloadIntoStream(ImageLoader loader, LEDataOutputStream stream) {
138 try {
139 outputStream = stream;
140 unloadIntoByteStream(loader);
141 outputStream.flush();
142 } catch (Exception e) {
143 try {outputStream.flush();} catch (Exception f) {}
144 SWT.error(SWT.ERROR_IO, e);
145 }
146 }
147 }