[LIB-9] Add functional for transcoding vector images
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / vector / VectorImageShaper.java
diff --git a/chesshog-graphics/src/main/java/org/hedgecode/chess/img/vector/VectorImageShaper.java b/chesshog-graphics/src/main/java/org/hedgecode/chess/img/vector/VectorImageShaper.java
new file mode 100644 (file)
index 0000000..bd11dcf
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2018-2019. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.img.vector;
+
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import org.apache.batik.transcoder.TranscoderException;
+
+import org.hedgecode.chess.img.ImageException;
+import org.hedgecode.chess.img.ImageShaper;
+
+/**
+ * Shaper for vector images.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class VectorImageShaper implements ImageShaper {
+
+    private VectorImageShaper() {
+    }
+
+    @Override
+    public BufferedImage shape(InputStream imageStream, int size) throws ImageException {
+        SquareImageTranscoder imageTranscoder =
+                new SquareImageTranscoder(BufferedImage.TYPE_INT_ARGB, size);
+        try {
+            return imageTranscoder.transcode(imageStream);
+        } catch (TranscoderException e) {
+            throw new ImageException("image.unable.load.vector", e.getMessage());
+        }
+    }
+
+    @Override
+    public BufferedImage shape(URL imageUrl, int size) throws ImageException {
+        try {
+            return shape(
+                    imageUrl.openStream(),
+                    size
+            );
+        } catch (IOException e) {
+            throw new ImageException("image.unable.load.vector", e.getMessage());
+        }
+    }
+
+    @Override
+    public BufferedImage shape(File imageFile, int size) throws ImageException {
+        try {
+            return shape(
+                    new FileInputStream(imageFile),
+                    size
+            );
+        } catch (FileNotFoundException e) {
+            throw new ImageException("image.unable.load.vector", e.getMessage());
+        }
+    }
+
+    public static ImageShaper create() {
+        return new VectorImageShaper();
+    }
+
+}