[LIB-9] Add functional for transcoding vector images
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / vector / SquareImageTranscoder.java
diff --git a/chesshog-graphics/src/main/java/org/hedgecode/chess/img/vector/SquareImageTranscoder.java b/chesshog-graphics/src/main/java/org/hedgecode/chess/img/vector/SquareImageTranscoder.java
new file mode 100644 (file)
index 0000000..deeb890
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * 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.IOException;
+import java.io.InputStream;
+
+import javax.imageio.ImageIO;
+
+import org.apache.batik.transcoder.TranscoderException;
+import org.apache.batik.transcoder.TranscoderInput;
+import org.apache.batik.transcoder.TranscoderOutput;
+import org.apache.batik.transcoder.image.ImageTranscoder;
+
+import org.hedgecode.chess.img.ImageFormat;
+
+/**
+ * Transcoder for vector square images to {@link BufferedImage}.
+ *
+ * @author Dmitry Samoshin aka gotty
+ * @see ImageTranscoder
+ */
+public class SquareImageTranscoder extends ImageTranscoder {
+
+    private final int type;
+    private BufferedImage image;
+
+    SquareImageTranscoder(int type) {
+        this.type = type;
+    }
+
+    SquareImageTranscoder(int type, int size) {
+        this.type = type;
+        addTranscodingHint(ImageTranscoder.KEY_WIDTH, (float) size);
+        addTranscodingHint(ImageTranscoder.KEY_HEIGHT, (float) size);
+
+    }
+
+    @Override
+    protected void setImageSize(float width, float height) {
+        if (width > 0 && height > 0) {
+            super.setImageSize(width, height);
+        }
+    }
+
+    @Override
+    public BufferedImage createImage(int width, int height) {
+        return new BufferedImage(width, height, type);
+    }
+
+    @Override
+    public void writeImage(BufferedImage image, TranscoderOutput to) throws TranscoderException {
+        this.image = image;
+    }
+
+    public BufferedImage transcode(InputStream inputStream) throws TranscoderException {
+        transcode(
+                new TranscoderInput(inputStream),
+                null
+        );
+        return getImage();
+    }
+
+    public BufferedImage getImage() {
+        return image;
+    }
+
+
+    public static void main(String[] args) throws TranscoderException, IOException {
+        SquareImageTranscoder imageTranscoder =
+                new SquareImageTranscoder(BufferedImage.TYPE_INT_ARGB); // , 250
+        ImageIO.write(
+                imageTranscoder.transcode(
+                        ClassLoader.getSystemResourceAsStream("images/pieces/spatial/bb.svg")
+                ),
+                ImageFormat.PNG.name(),
+                new File("bishop" + "." + ImageFormat.PNG.getExt())
+        );
+    }
+
+}