Mercurial > projects > dwt-linux
annotate dwt/internal/image/FileFormat.d @ 259:c0d810de7093
Update SWT 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 29 Jun 2008 14:33:38 +0200 |
parents | ce446666f5a2 |
children |
rev | line source |
---|---|
134
116d23207a86
changed PNG loader to use internal inflating
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
1 /******************************************************************************* |
12 | 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 | |
108 | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | |
12 | 12 *******************************************************************************/ |
13 module dwt.internal.image.FileFormat; | |
14 | |
238 | 15 import dwt.dwthelper.utils; |
16 | |
12 | 17 public import dwt.graphics.ImageLoader; |
18 public import dwt.graphics.ImageData; | |
19 public import dwt.internal.image.LEDataInputStream; | |
20 public import dwt.internal.image.LEDataOutputStream; | |
21 | |
71 | 22 import dwt.DWT; |
12 | 23 |
24 public import dwt.dwthelper.InputStream; | |
25 public import dwt.dwthelper.OutputStream; | |
26 | |
13 | 27 import dwt.internal.image.GIFFileFormat; |
12 | 28 import dwt.internal.image.WinBMPFileFormat; |
29 import dwt.internal.image.WinICOFileFormat; | |
30 import dwt.internal.image.TIFFFileFormat; | |
31 import dwt.internal.image.OS2BMPFileFormat; | |
13 | 32 import dwt.internal.image.JPEGFileFormat; |
33 import dwt.internal.image.PNGFileFormat; | |
12 | 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 { | |
238 | 44 static const String FORMAT_PACKAGE = "dwt.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$ | |
12 | 47 alias Tuple!( WinBMPFileFormat, WinBMPFileFormat, GIFFileFormat, WinICOFileFormat, JPEGFileFormat, PNGFileFormat, TIFFFileFormat, OS2BMPFileFormat ) TFormats; |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
48 LEDataInputStream inputStream; |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
49 LEDataOutputStream outputStream; |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
50 ImageLoader loader; |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
51 int compression; |
12 | 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) { | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
66 try { |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
67 inputStream = stream; |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
68 return loadFromByteStream(); |
12 | 69 } catch (IOException e) { |
71 | 70 DWT.error(DWT.ERROR_IO, e); |
12 | 71 return null; |
158
de2578a843a7
Tango update to rev 3158, TracedException>Exception, fromUtf8z>fromStringz,Fix Bug in MenuItem Thanx to nascent for the report.
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
72 } catch (Exception e) { |
71 | 73 DWT.error(DWT.ERROR_INVALID_IMAGE, e); |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
74 return null; |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
75 } |
12 | 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) { | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
83 FileFormat fileFormat = null; |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
84 LEDataInputStream stream = new LEDataInputStream(istr); |
259 | 85 bool isSupported = false; |
12 | 86 foreach( TFormat; TFormats ){ |
87 try{ | |
88 fileFormat = new TFormat(); | |
89 if (fileFormat.isFileFormat(stream)) { | |
90 isSupported = true; | |
91 break; | |
92 } | |
158
de2578a843a7
Tango update to rev 3158, TracedException>Exception, fromUtf8z>fromStringz,Fix Bug in MenuItem Thanx to nascent for the report.
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
93 } catch (Exception e) { |
12 | 94 } |
95 } | |
71 | 96 if (!isSupported) DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT); |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
97 fileFormat.loader = loader; |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
98 return fileFormat.loadFromStream(stream); |
12 | 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) { | |
71 | 106 if (format < 0 || format >= FORMATS.length) DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT); |
131 | 107 if (FORMATS[format] is null) DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT); |
108 if (loader.data is null || loader.data.length < 1) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
12 | 109 |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
110 LEDataOutputStream stream = new LEDataOutputStream(os); |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
111 FileFormat fileFormat = null; |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
112 try { |
12 | 113 foreach( idx, TFormat; TFormats ){ |
131 | 114 if( idx is format ){ |
12 | 115 fileFormat = new TFormat(); |
116 } | |
117 } | |
158
de2578a843a7
Tango update to rev 3158, TracedException>Exception, fromUtf8z>fromStringz,Fix Bug in MenuItem Thanx to nascent for the report.
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
118 } catch (Exception e) { |
71 | 119 DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT); |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
120 } |
131 | 121 if (format is DWT.IMAGE_BMP_RLE) { |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
122 switch (loader.data[0].depth) { |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
123 case 8: fileFormat.compression = 1; break; |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
124 case 4: fileFormat.compression = 2; break; |
55
93981635e709
Checked and inserted missing default switch cases.
Frank Benoit <benoit@tionex.de>
parents:
14
diff
changeset
|
125 default: |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
126 } |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
127 } |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
128 fileFormat.unloadIntoStream(loader, stream); |
12 | 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) { | |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
138 try { |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
139 outputStream = stream; |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
140 unloadIntoByteStream(loader); |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
141 outputStream.flush(); |
158
de2578a843a7
Tango update to rev 3158, TracedException>Exception, fromUtf8z>fromStringz,Fix Bug in MenuItem Thanx to nascent for the report.
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
142 } catch (Exception e) { |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
143 try {outputStream.flush();} catch (Exception f) {} |
71 | 144 DWT.error(DWT.ERROR_IO, e); |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
145 } |
12 | 146 } |
147 } |