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