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