Mercurial > projects > dwt-mac
comparison dwt/internal/image/TIFFFileFormat.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 5123b17c98ef |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
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; | |
12 | |
13 | |
14 import java.io.IOException; | |
15 | |
16 import dwt.DWT; | |
17 import dwt.graphics.ImageData; | |
18 import dwt.graphics.ImageLoader; | |
19 | |
20 /** | |
21 * Baseline TIFF decoder revision 6.0 | |
22 * Extension T4-encoding CCITT T.4 1D | |
23 */ | |
24 public final class TIFFFileFormat : FileFormat { | |
25 | |
26 bool isFileFormat(LEDataInputStream stream) { | |
27 try { | |
28 byte[] header = new byte[4]; | |
29 stream.read(header); | |
30 stream.unread(header); | |
31 if (header[0] !is header[1]) return false; | |
32 if (!(header[0] is 0x49 && header[2] is 42 && header[3] is 0) && | |
33 !(header[0] is 0x4d && header[2] is 0 && header[3] is 42)) { | |
34 return false; | |
35 } | |
36 return true; | |
37 } catch (Exception e) { | |
38 return false; | |
39 } | |
40 } | |
41 | |
42 ImageData[] loadFromByteStream() { | |
43 byte[] header = new byte[8]; | |
44 bool isLittleEndian; | |
45 ImageData[] images = new ImageData[0]; | |
46 TIFFRandomFileAccess file = new TIFFRandomFileAccess(inputStream); | |
47 try { | |
48 file.read(header); | |
49 if (header[0] !is header[1]) DWT.error(DWT.ERROR_INVALID_IMAGE); | |
50 if (!(header[0] is 0x49 && header[2] is 42 && header[3] is 0) && | |
51 !(header[0] is 0x4d && header[2] is 0 && header[3] is 42)) { | |
52 DWT.error(DWT.ERROR_INVALID_IMAGE); | |
53 } | |
54 isLittleEndian = header[0] is 0x49; | |
55 int offset = isLittleEndian ? | |
56 (header[4] & 0xFF) | ((header[5] & 0xFF) << 8) | ((header[6] & 0xFF) << 16) | ((header[7] & 0xFF) << 24) : | |
57 (header[7] & 0xFF) | ((header[6] & 0xFF) << 8) | ((header[5] & 0xFF) << 16) | ((header[4] & 0xFF) << 24); | |
58 file.seek(offset); | |
59 TIFFDirectory directory = new TIFFDirectory(file, isLittleEndian, loader); | |
60 ImageData image = directory.read(); | |
61 /* A baseline reader is only expected to read the first directory */ | |
62 images = new ImageData[] {image}; | |
63 } catch (IOException e) { | |
64 DWT.error(DWT.ERROR_IO, e); | |
65 } | |
66 return images; | |
67 } | |
68 | |
69 void unloadIntoByteStream(ImageLoader loader) { | |
70 /* We do not currently support writing multi-page tiff, | |
71 * so we use the first image data in the loader's array. */ | |
72 ImageData image = loader.data[0]; | |
73 TIFFDirectory directory = new TIFFDirectory(image); | |
74 try { | |
75 directory.writeToStream(outputStream); | |
76 } catch (IOException e) { | |
77 DWT.error(DWT.ERROR_IO, e); | |
78 } | |
79 } | |
80 | |
81 } |