KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > internal > image > TIFFFileFormat


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 package org.eclipse.swt.internal.image;
12
13
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.graphics.*;
16 import java.io.*;
17
18 /**
19  * Baseline TIFF decoder revision 6.0
20  * Extension T4-encoding CCITT T.4 1D
21  */

22 final class TIFFFileFormat extends FileFormat {
23
24 boolean isFileFormat(LEDataInputStream stream) {
25     try {
26         byte[] header = new byte[4];
27         stream.read(header);
28         stream.unread(header);
29         if (header[0] != header[1]) return false;
30         if (!(header[0] == 0x49 && header[2] == 42 && header[3] == 0) &&
31             !(header[0] == 0x4d && header[2] == 0 && header[3] == 42)) {
32             return false;
33         }
34         return true;
35     } catch (Exception JavaDoc e) {
36         return false;
37     }
38 }
39
40 ImageData[] loadFromByteStream() {
41     byte[] header = new byte[8];
42     boolean isLittleEndian;
43     ImageData[] images = new ImageData[0];
44     TIFFRandomFileAccess file = new TIFFRandomFileAccess(inputStream);
45     try {
46         file.read(header);
47         if (header[0] != header[1]) SWT.error(SWT.ERROR_INVALID_IMAGE);
48         if (!(header[0] == 0x49 && header[2] == 42 && header[3] == 0) &&
49             !(header[0] == 0x4d && header[2] == 0 && header[3] == 42)) {
50             SWT.error(SWT.ERROR_INVALID_IMAGE);
51         }
52         isLittleEndian = header[0] == 0x49;
53         int offset = isLittleEndian ?
54             (header[4] & 0xFF) | ((header[5] & 0xFF) << 8) | ((header[6] & 0xFF) << 16) | ((header[7] & 0xFF) << 24) :
55             (header[7] & 0xFF) | ((header[6] & 0xFF) << 8) | ((header[5] & 0xFF) << 16) | ((header[4] & 0xFF) << 24);
56         file.seek(offset);
57         TIFFDirectory directory = new TIFFDirectory(file, isLittleEndian, loader);
58         ImageData image = directory.read();
59         /* A baseline reader is only expected to read the first directory */
60         images = new ImageData[] {image};
61     } catch (IOException e) {
62         SWT.error(SWT.ERROR_IO, e);
63     }
64     return images;
65 }
66
67 void unloadIntoByteStream(ImageLoader loader) {
68     /* We do not currently support writing multi-page tiff,
69      * so we use the first image data in the loader's array. */

70     ImageData image = loader.data[0];
71     TIFFDirectory directory = new TIFFDirectory(image);
72     try {
73         directory.writeToStream(outputStream);
74     } catch (IOException e) {
75         SWT.error(SWT.ERROR_IO, e);
76     }
77 }
78
79 }
80
Popular Tags