Mercurial > projects > dwt-mac
comparison dwt/internal/image/FileFormat.d @ 34:5123b17c98ef
Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sun, 14 Sep 2008 01:45:57 +0200 |
parents | a9ab4c738ed8 |
children | d8635bb48c7c |
comparison
equal
deleted
inserted
replaced
33:965ac0a77267 | 34:5123b17c98ef |
---|---|
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
10 *******************************************************************************/ | 12 *******************************************************************************/ |
11 module dwt.internal.image; | 13 module dwt.internal.image.FileFormat; |
12 | 14 |
15 import dwt.dwthelper.utils; | |
13 | 16 |
14 import java.io.IOException; | 17 public import dwt.graphics.ImageLoader; |
15 import java.io.InputStream; | 18 public import dwt.graphics.ImageData; |
16 import java.io.OutputStream; | 19 public import dwt.internal.image.LEDataInputStream; |
20 public import dwt.internal.image.LEDataOutputStream; | |
17 | 21 |
18 import dwt.DWT; | 22 import dwt.DWT; |
19 import dwt.graphics.ImageData; | 23 |
20 import dwt.graphics.ImageLoader; | 24 public import dwt.dwthelper.InputStream; |
25 public import dwt.dwthelper.OutputStream; | |
26 | |
27 import dwt.internal.image.GIFFileFormat; | |
28 import dwt.internal.image.WinBMPFileFormat; | |
29 import dwt.internal.image.WinICOFileFormat; | |
30 import dwt.internal.image.TIFFFileFormat; | |
31 import dwt.internal.image.OS2BMPFileFormat; | |
32 import dwt.internal.image.JPEGFileFormat; | |
33 import dwt.internal.image.PNGFileFormat; | |
34 | |
35 import tango.core.Exception; | |
36 import tango.core.Tuple; | |
21 | 37 |
22 /** | 38 /** |
23 * Abstract factory class for loading/unloading images from files or streams | 39 * Abstract factory class for loading/unloading images from files or streams |
24 * in various image file formats. | 40 * in various image file formats. |
25 * | 41 * |
26 */ | 42 */ |
27 public abstract class FileFormat { | 43 public abstract class FileFormat { |
28 static final String FORMAT_PACKAGE = "dwt.internal.image"; //$NON-NLS-1$ | 44 static const String FORMAT_PACKAGE = "dwt.internal.image"; //$NON-NLS-1$ |
29 static final String FORMAT_SUFFIX = "FileFormat"; //$NON-NLS-1$ | 45 static const 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$ | 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$ |
31 | 47 alias Tuple!( WinBMPFileFormat, WinBMPFileFormat, GIFFileFormat, WinICOFileFormat, JPEGFileFormat, PNGFileFormat, TIFFFileFormat, OS2BMPFileFormat ) TFormats; |
32 LEDataInputStream inputStream; | 48 LEDataInputStream inputStream; |
33 LEDataOutputStream outputStream; | 49 LEDataOutputStream outputStream; |
34 ImageLoader loader; | 50 ImageLoader loader; |
35 int compression; | 51 int compression; |
36 | 52 |
43 abstract ImageData[] loadFromByteStream(); | 59 abstract ImageData[] loadFromByteStream(); |
44 | 60 |
45 /** | 61 /** |
46 * Read the specified input stream, and return the | 62 * Read the specified input stream, and return the |
47 * device independent image array represented by the stream. | 63 * device independent image array represented by the stream. |
48 */ | 64 */ |
49 public ImageData[] loadFromStream(LEDataInputStream stream) { | 65 public ImageData[] loadFromStream(LEDataInputStream stream) { |
50 try { | 66 try { |
51 inputStream = stream; | 67 inputStream = stream; |
52 return loadFromByteStream(); | 68 return loadFromByteStream(); |
69 } catch (IOException e) { | |
70 DWT.error(DWT.ERROR_IO, e); | |
71 return null; | |
53 } catch (Exception e) { | 72 } catch (Exception e) { |
54 if ( null !is cast(IOException)e ) { | 73 DWT.error(DWT.ERROR_INVALID_IMAGE, e); |
55 DWT.error(DWT.ERROR_IO, e); | |
56 } else { | |
57 DWT.error(DWT.ERROR_INVALID_IMAGE, e); | |
58 } | |
59 return null; | 74 return null; |
60 } | 75 } |
61 } | 76 } |
62 | 77 |
63 /** | 78 /** |
64 * Read the specified input stream using the specified loader, and | 79 * Read the specified input stream using the specified loader, and |
65 * return the device independent image array represented by the stream. | 80 * return the device independent image array represented by the stream. |
66 */ | 81 */ |
67 public static ImageData[] load(InputStream is, ImageLoader loader) { | 82 public static ImageData[] load(InputStream istr, ImageLoader loader) { |
68 FileFormat fileFormat = null; | 83 FileFormat fileFormat = null; |
69 LEDataInputStream stream = new LEDataInputStream(is); | 84 LEDataInputStream stream = new LEDataInputStream(istr); |
70 bool isSupported = false; | 85 bool isSupported = false; |
71 for (int i = 1; i < FORMATS.length; i++) { | 86 foreach( TFormat; TFormats ){ |
72 if (FORMATS[i] !is null) { | 87 try{ |
73 try { | 88 fileFormat = new TFormat(); |
74 Class clazz = Class.forName(FORMAT_PACKAGE + '.' + FORMATS[i] + FORMAT_SUFFIX); | 89 if (fileFormat.isFileFormat(stream)) { |
75 fileFormat = cast(FileFormat) clazz.newInstance(); | 90 isSupported = true; |
76 if (fileFormat.isFileFormat(stream)) { | 91 break; |
77 isSupported = true; | |
78 break; | |
79 } | |
80 } catch (ClassNotFoundException e) { | |
81 FORMATS[i] = null; | |
82 } catch (Exception e) { | |
83 } | 92 } |
93 } catch (Exception e) { | |
84 } | 94 } |
85 } | 95 } |
86 if (!isSupported) DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT); | 96 if (!isSupported) DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT); |
87 fileFormat.loader = loader; | 97 fileFormat.loader = loader; |
88 return fileFormat.loadFromStream(stream); | 98 return fileFormat.loadFromStream(stream); |
89 } | 99 } |
90 | 100 |
91 /** | 101 /** |
92 * Write the device independent image array stored in the specified loader | 102 * Write the device independent image array stored in the specified loader |
93 * to the specified output stream using the specified file format. | 103 * to the specified output stream using the specified file format. |
94 */ | 104 */ |
95 public static void save(OutputStream os, int format, ImageLoader loader) { | 105 public static void save(OutputStream os, int format, ImageLoader loader) { |
96 if (format < 0 || format >= FORMATS.length) DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT); | 106 if (format < 0 || format >= FORMATS.length) DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT); |
97 if (FORMATS[format] is null) DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT); | 107 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); | 108 if (loader.data is null || loader.data.length < 1) DWT.error(DWT.ERROR_INVALID_ARGUMENT); |
99 | 109 |
100 LEDataOutputStream stream = new LEDataOutputStream(os); | 110 LEDataOutputStream stream = new LEDataOutputStream(os); |
101 FileFormat fileFormat = null; | 111 FileFormat fileFormat = null; |
102 try { | 112 try { |
103 Class clazz = Class.forName(FORMAT_PACKAGE + '.' + FORMATS[format] + FORMAT_SUFFIX); | 113 foreach( idx, TFormat; TFormats ){ |
104 fileFormat = cast(FileFormat) clazz.newInstance(); | 114 if( idx is format ){ |
115 fileFormat = new TFormat(); | |
116 } | |
117 } | |
105 } catch (Exception e) { | 118 } catch (Exception e) { |
106 DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT); | 119 DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT); |
107 } | 120 } |
108 if (format is DWT.IMAGE_BMP_RLE) { | 121 if (format is DWT.IMAGE_BMP_RLE) { |
109 switch (loader.data[0].depth) { | 122 switch (loader.data[0].depth) { |
110 case 8: fileFormat.compression = 1; break; | 123 case 8: fileFormat.compression = 1; break; |
111 case 4: fileFormat.compression = 2; break; | 124 case 4: fileFormat.compression = 2; break; |
125 default: | |
112 } | 126 } |
113 } | 127 } |
114 fileFormat.unloadIntoStream(loader, stream); | 128 fileFormat.unloadIntoStream(loader, stream); |
115 } | 129 } |
116 | 130 |
117 abstract void unloadIntoByteStream(ImageLoader loader); | 131 abstract void unloadIntoByteStream(ImageLoader loader); |
118 | 132 |
119 /** | 133 /** |
120 * Write the device independent image array stored in the specified loader | 134 * Write the device independent image array stored in the specified loader |
121 * to the specified output stream. | 135 * to the specified output stream. |
122 */ | 136 */ |
123 public void unloadIntoStream(ImageLoader loader, LEDataOutputStream stream) { | 137 public void unloadIntoStream(ImageLoader loader, LEDataOutputStream stream) { |
124 try { | 138 try { |
125 outputStream = stream; | 139 outputStream = stream; |
126 unloadIntoByteStream(loader); | 140 unloadIntoByteStream(loader); |
127 outputStream.flush(); | 141 outputStream.flush(); |