]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/clipboard/streams/UncompressedDIBInputStream.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / gfx / clipboard / streams / UncompressedDIBInputStream.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.ui.gfx.clipboard.streams;\r
13 \r
14 import java.io.IOException;\r
15 import java.io.InputStream;\r
16 \r
17 import org.simantics.utils.ui.gfx.clipboard.headers.BitmapInfoHeader;\r
18 import org.simantics.utils.ui.gfx.clipboard.headers.Win32Constants;\r
19 \r
20 \r
21 /**\r
22  * <p>\r
23  * Intended to uncompress compressed image streams so that \r
24  * SWT can use images sent to clipboard (native WIN32)\r
25  * </p>\r
26  * <p>\r
27  * Currently no uncompression methods are implemented, so \r
28  * stream throws <code>IOException</code> if its used to \r
29  * stream compressed images.\r
30  * </p>\r
31  * \r
32  * @author Marko Luukkainen\r
33  *\r
34  */\r
35 public class UncompressedDIBInputStream extends InputStream {\r
36     private InputStream iStream;\r
37     private int readBytes = 0;\r
38     private byte [] header;\r
39     \r
40     public UncompressedDIBInputStream(InputStream iStream) throws IOException{\r
41         this.iStream = iStream;\r
42         header = new byte[BitmapInfoHeader.sizeof];\r
43         iStream.read(header, 0, BitmapInfoHeader.sizeof);\r
44         \r
45         BitmapInfoHeader infoHeader = new BitmapInfoHeader();\r
46         infoHeader.setBytes(header);\r
47         if (infoHeader.biCompression == Win32Constants.BI_BITFIELDS) {\r
48             throw new IOException("BI_BITFIELDS stream not supported");\r
49         }\r
50         if (infoHeader.biCompression == Win32Constants.BI_JPEG) {\r
51             throw new IOException("BI_JPEG stream not supported");\r
52         }\r
53         if (infoHeader.biCompression == Win32Constants.BI_PNG) {\r
54             throw new IOException("BI_PNG stream not supported");\r
55         }\r
56         if (infoHeader.biCompression == Win32Constants.BI_RLE8) {\r
57             throw new IOException("BI_RLE8 stream not supported");\r
58         }\r
59         if (infoHeader.biCompression == Win32Constants.BI_RLE24) {\r
60             throw new IOException("BI_RLE24 stream not supported");\r
61         }\r
62     }\r
63     \r
64     @Override\r
65     public int read() throws IOException {\r
66         if (readBytes < header.length) {\r
67             return 0xff & header[readBytes++];\r
68         }\r
69         return iStream.read();\r
70     }\r
71 }\r