comparison dwt/internal/image/TIFFFileFormat.d @ 13:3d9bbe0a83a0

FileFormats
author Frank Benoit <benoit@tionex.de>
date Sun, 06 Jan 2008 22:54:14 +0100
parents
children 8cec8f536af3
comparison
equal deleted inserted replaced
12:0c78fa47d476 13:3d9bbe0a83a0
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 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.TIFFFileFormat;
12
13 import dwt.internal.image.TIFFRandomFileAccess;
14 import dwt.internal.image.TIFFDirectory;
15 import dwt.SWT;
16 import dwt.graphics.ImageData;
17 import dwt.graphics.ImageLoader;
18 import dwt.internal.image.FileFormat;
19
20 import tango.core.Exception;
21
22 /**
23 * Baseline TIFF decoder revision 6.0
24 * Extension T4-encoding CCITT T.4 1D
25 */
26 final class TIFFFileFormat : FileFormat {
27
28 bool isFileFormat(LEDataInputStream stream) {
29 try {
30 byte[] header = new byte[4];
31 stream.read(header);
32 stream.unread(header);
33 if (header[0] != header[1]) return false;
34 if (!(header[0] == 0x49 && header[2] == 42 && header[3] == 0) &&
35 !(header[0] == 0x4d && header[2] == 0 && header[3] == 42)) {
36 return false;
37 }
38 return true;
39 } catch (TracedException e) {
40 return false;
41 }
42 }
43
44 ImageData[] loadFromByteStream() {
45 byte[] header = new byte[8];
46 bool isLittleEndian;
47 ImageData[] images = new ImageData[0];
48 TIFFRandomFileAccess file = new TIFFRandomFileAccess(inputStream);
49 try {
50 file.read(header);
51 if (header[0] != header[1]) SWT.error(SWT.ERROR_INVALID_IMAGE);
52 if (!(header[0] == 0x49 && header[2] == 42 && header[3] == 0) &&
53 !(header[0] == 0x4d && header[2] == 0 && header[3] == 42)) {
54 SWT.error(SWT.ERROR_INVALID_IMAGE);
55 }
56 isLittleEndian = header[0] == 0x49;
57 int offset = isLittleEndian ?
58 (header[4] & 0xFF) | ((header[5] & 0xFF) << 8) | ((header[6] & 0xFF) << 16) | ((header[7] & 0xFF) << 24) :
59 (header[7] & 0xFF) | ((header[6] & 0xFF) << 8) | ((header[5] & 0xFF) << 16) | ((header[4] & 0xFF) << 24);
60 file.seek(offset);
61 TIFFDirectory directory = new TIFFDirectory(file, isLittleEndian, loader);
62 ImageData image = directory.read();
63 /* A baseline reader is only expected to read the first directory */
64 images = [image];
65 } catch (IOException e) {
66 SWT.error(SWT.ERROR_IO, e);
67 }
68 return images;
69 }
70
71 void unloadIntoByteStream(ImageLoader loader) {
72 /* We do not currently support writing multi-page tiff,
73 * so we use the first image data in the loader's array. */
74 ImageData image = loader.data[0];
75 TIFFDirectory directory = new TIFFDirectory(image);
76 try {
77 directory.writeToStream(outputStream);
78 } catch (IOException e) {
79 SWT.error(SWT.ERROR_IO, e);
80 }
81 }
82
83 }