]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/clipboard/streams/UncompressedDIBInputStream.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/clipboard/streams/UncompressedDIBInputStream.java
new file mode 100644 (file)
index 0000000..c90f45b
--- /dev/null
@@ -0,0 +1,71 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.ui.gfx.clipboard.streams;\r
+\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+\r
+import org.simantics.utils.ui.gfx.clipboard.headers.BitmapInfoHeader;\r
+import org.simantics.utils.ui.gfx.clipboard.headers.Win32Constants;\r
+\r
+\r
+/**\r
+ * <p>\r
+ * Intended to uncompress compressed image streams so that \r
+ * SWT can use images sent to clipboard (native WIN32)\r
+ * </p>\r
+ * <p>\r
+ * Currently no uncompression methods are implemented, so \r
+ * stream throws <code>IOException</code> if its used to \r
+ * stream compressed images.\r
+ * </p>\r
+ * \r
+ * @author Marko Luukkainen\r
+ *\r
+ */\r
+public class UncompressedDIBInputStream extends InputStream {\r
+    private InputStream iStream;\r
+    private int readBytes = 0;\r
+    private byte [] header;\r
+    \r
+    public UncompressedDIBInputStream(InputStream iStream) throws IOException{\r
+        this.iStream = iStream;\r
+        header = new byte[BitmapInfoHeader.sizeof];\r
+        iStream.read(header, 0, BitmapInfoHeader.sizeof);\r
+        \r
+        BitmapInfoHeader infoHeader = new BitmapInfoHeader();\r
+        infoHeader.setBytes(header);\r
+        if (infoHeader.biCompression == Win32Constants.BI_BITFIELDS) {\r
+            throw new IOException("BI_BITFIELDS stream not supported");\r
+        }\r
+        if (infoHeader.biCompression == Win32Constants.BI_JPEG) {\r
+            throw new IOException("BI_JPEG stream not supported");\r
+        }\r
+        if (infoHeader.biCompression == Win32Constants.BI_PNG) {\r
+            throw new IOException("BI_PNG stream not supported");\r
+        }\r
+        if (infoHeader.biCompression == Win32Constants.BI_RLE8) {\r
+            throw new IOException("BI_RLE8 stream not supported");\r
+        }\r
+        if (infoHeader.biCompression == Win32Constants.BI_RLE24) {\r
+            throw new IOException("BI_RLE24 stream not supported");\r
+        }\r
+    }\r
+    \r
+    @Override\r
+    public int read() throws IOException {\r
+        if (readBytes < header.length) {\r
+            return 0xff & header[readBytes++];\r
+        }\r
+        return iStream.read();\r
+    }\r
+}\r