comparison dwt/internal/image/FileFormat.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children e831403a80a9
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
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 *******************************************************************************/
11 module dwt.internal.image;
12
13
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.OutputStream;
17
18 import dwt.DWT;
19 import dwt.graphics.ImageData;
20 import dwt.graphics.ImageLoader;
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 = "dwt.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 /**
38 * Return whether or not the specified input stream
39 * represents a supported file format.
40 */
41 abstract bool isFileFormat(LEDataInputStream stream);
42
43 abstract ImageData[] loadFromByteStream();
44
45 /**
46 * Read the specified input stream, and return the
47 * device independent image array represented by the stream.
48 */
49 public ImageData[] loadFromStream(LEDataInputStream stream) {
50 try {
51 inputStream = stream;
52 return loadFromByteStream();
53 } catch (Exception e) {
54 if (e instanceof IOException) {
55 DWT.error(DWT.ERROR_IO, e);
56 } else {
57 DWT.error(DWT.ERROR_INVALID_IMAGE, e);
58 }
59 return null;
60 }
61 }
62
63 /**
64 * Read the specified input stream using the specified loader, and
65 * return the device independent image array represented by the stream.
66 */
67 public static ImageData[] load(InputStream is, ImageLoader loader) {
68 FileFormat fileFormat = null;
69 LEDataInputStream stream = new LEDataInputStream(is);
70 bool isSupported = false;
71 for (int i = 1; i < FORMATS.length; i++) {
72 if (FORMATS[i] !is null) {
73 try {
74 Class clazz = Class.forName(FORMAT_PACKAGE + '.' + FORMATS[i] + FORMAT_SUFFIX);
75 fileFormat = (FileFormat) clazz.newInstance();
76 if (fileFormat.isFileFormat(stream)) {
77 isSupported = true;
78 break;
79 }
80 } catch (ClassNotFoundException e) {
81 FORMATS[i] = null;
82 } catch (Exception e) {
83 }
84 }
85 }
86 if (!isSupported) DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT);
87 fileFormat.loader = loader;
88 return fileFormat.loadFromStream(stream);
89 }
90
91 /**
92 * Write the device independent image array stored in the specified loader
93 * to the specified output stream using the specified file format.
94 */
95 public static void save(OutputStream os, int format, ImageLoader loader) {
96 if (format < 0 || format >= FORMATS.length) DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT);
97 if (FORMATS[format] is null) DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT);
98 if (loader.data is null || loader.data.length < 1) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
99
100 LEDataOutputStream stream = new LEDataOutputStream(os);
101 FileFormat fileFormat = null;
102 try {
103 Class clazz = Class.forName(FORMAT_PACKAGE + '.' + FORMATS[format] + FORMAT_SUFFIX);
104 fileFormat = (FileFormat) clazz.newInstance();
105 } catch (Exception e) {
106 DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT);
107 }
108 if (format is DWT.IMAGE_BMP_RLE) {
109 switch (loader.data[0].depth) {
110 case 8: fileFormat.compression = 1; break;
111 case 4: fileFormat.compression = 2; break;
112 }
113 }
114 fileFormat.unloadIntoStream(loader, stream);
115 }
116
117 abstract void unloadIntoByteStream(ImageLoader loader);
118
119 /**
120 * Write the device independent image array stored in the specified loader
121 * to the specified output stream.
122 */
123 public void unloadIntoStream(ImageLoader loader, LEDataOutputStream stream) {
124 try {
125 outputStream = stream;
126 unloadIntoByteStream(loader);
127 outputStream.flush();
128 } catch (Exception e) {
129 try {outputStream.flush();} catch (Exception f) {}
130 DWT.error(DWT.ERROR_IO, e);
131 }
132 }
133 }