annotate dwt/internal/image/TIFFFileFormat.d @ 213:36f5cb12e1a2

Update to SWT 3.4M7
author Frank Benoit <benoit@tionex.de>
date Sat, 17 May 2008 17:34:28 +0200
parents 41dbc4d9faab
children fd9c62a2998e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
213
36f5cb12e1a2 Update to SWT 3.4M7
Frank Benoit <benoit@tionex.de>
parents: 57
diff changeset
1 /*******************************************************************************
2
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
2 * Copyright (c) 2000, 2005 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.TIFFFileFormat;
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 import dwt.internal.image.TIFFRandomFileAccess;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
16 import dwt.internal.image.TIFFDirectory;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
17 import dwt.DWT;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
18 import dwt.graphics.ImageData;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
19 import dwt.graphics.ImageLoader;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
20 import dwt.internal.image.FileFormat;
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 import tango.core.Exception;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
23
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 * Baseline TIFF decoder revision 6.0
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
26 * Extension T4-encoding CCITT T.4 1D
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
27 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
28 final class TIFFFileFormat : FileFormat {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
29
48
9a64a7781bab Added override and alias, first chunk. Thanks torhu for doing this patch.
Frank Benoit <benoit@tionex.de>
parents: 2
diff changeset
30 override bool isFileFormat(LEDataInputStream stream) {
2
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
31 try {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
32 byte[] header = new byte[4];
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
33 stream.read(header);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
34 stream.unread(header);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
35 if (header[0] !is header[1]) return false;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
36 if (!(header[0] is 0x49 && header[2] is 42 && header[3] is 0) &&
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
37 !(header[0] is 0x4d && header[2] is 0 && header[3] is 42)) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
38 return false;
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 return true;
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: 48
diff changeset
41 } catch (Exception e) {
2
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
42 return false;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
43 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
44 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
45
48
9a64a7781bab Added override and alias, first chunk. Thanks torhu for doing this patch.
Frank Benoit <benoit@tionex.de>
parents: 2
diff changeset
46 override ImageData[] loadFromByteStream() {
2
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
47 byte[] header = new byte[8];
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
48 bool isLittleEndian;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
49 ImageData[] images = new ImageData[0];
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
50 TIFFRandomFileAccess file = new TIFFRandomFileAccess(inputStream);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
51 try {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
52 file.read(header);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
53 if (header[0] !is header[1]) DWT.error(DWT.ERROR_INVALID_IMAGE);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
54 if (!(header[0] is 0x49 && header[2] is 42 && header[3] is 0) &&
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
55 !(header[0] is 0x4d && header[2] is 0 && header[3] is 42)) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
56 DWT.error(DWT.ERROR_INVALID_IMAGE);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
57 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
58 isLittleEndian = header[0] is 0x49;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
59 int offset = isLittleEndian ?
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
60 (header[4] & 0xFF) | ((header[5] & 0xFF) << 8) | ((header[6] & 0xFF) << 16) | ((header[7] & 0xFF) << 24) :
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
61 (header[7] & 0xFF) | ((header[6] & 0xFF) << 8) | ((header[5] & 0xFF) << 16) | ((header[4] & 0xFF) << 24);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
62 file.seek(offset);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
63 TIFFDirectory directory = new TIFFDirectory(file, isLittleEndian, loader);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
64 ImageData image = directory.read();
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
65 /* A baseline reader is only expected to read the first directory */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
66 images = [image];
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 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
70 return images;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
71 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
72
48
9a64a7781bab Added override and alias, first chunk. Thanks torhu for doing this patch.
Frank Benoit <benoit@tionex.de>
parents: 2
diff changeset
73 override void unloadIntoByteStream(ImageLoader loader) {
2
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
74 /* We do not currently support writing multi-page tiff,
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
75 * so we use the first image data in the loader's array. */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
76 ImageData image = loader.data[0];
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
77 TIFFDirectory directory = new TIFFDirectory(image);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
78 try {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
79 directory.writeToStream(outputStream);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
80 } catch (IOException e) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
81 DWT.error(DWT.ERROR_IO, e);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
82 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
83 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
84
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
85 }