annotate dwt/internal/image/FileFormat.d @ 57:41dbc4d9faab

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