[LIB-9] Add functional for transcoding vector images
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / vector / SquareImageTranscoder.java
1 /*
2  * Copyright (c) 2018-2019. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess.img.vector;
18
19 import java.awt.image.BufferedImage;
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.InputStream;
23
24 import javax.imageio.ImageIO;
25
26 import org.apache.batik.transcoder.TranscoderException;
27 import org.apache.batik.transcoder.TranscoderInput;
28 import org.apache.batik.transcoder.TranscoderOutput;
29 import org.apache.batik.transcoder.image.ImageTranscoder;
30
31 import org.hedgecode.chess.img.ImageFormat;
32
33 /**
34  * Transcoder for vector square images to {@link BufferedImage}.
35  *
36  * @author Dmitry Samoshin aka gotty
37  * @see ImageTranscoder
38  */
39 public class SquareImageTranscoder extends ImageTranscoder {
40
41     private final int type;
42     private BufferedImage image;
43
44     SquareImageTranscoder(int type) {
45         this.type = type;
46     }
47
48     SquareImageTranscoder(int type, int size) {
49         this.type = type;
50         addTranscodingHint(ImageTranscoder.KEY_WIDTH, (float) size);
51         addTranscodingHint(ImageTranscoder.KEY_HEIGHT, (float) size);
52
53     }
54
55     @Override
56     protected void setImageSize(float width, float height) {
57         if (width > 0 && height > 0) {
58             super.setImageSize(width, height);
59         }
60     }
61
62     @Override
63     public BufferedImage createImage(int width, int height) {
64         return new BufferedImage(width, height, type);
65     }
66
67     @Override
68     public void writeImage(BufferedImage image, TranscoderOutput to) throws TranscoderException {
69         this.image = image;
70     }
71
72     public BufferedImage transcode(InputStream inputStream) throws TranscoderException {
73         transcode(
74                 new TranscoderInput(inputStream),
75                 null
76         );
77         return getImage();
78     }
79
80     public BufferedImage getImage() {
81         return image;
82     }
83
84
85     public static void main(String[] args) throws TranscoderException, IOException {
86         SquareImageTranscoder imageTranscoder =
87                 new SquareImageTranscoder(BufferedImage.TYPE_INT_ARGB); // , 250
88         ImageIO.write(
89                 imageTranscoder.transcode(
90                         ClassLoader.getSystemResourceAsStream("images/pieces/spatial/bb.svg")
91                 ),
92                 ImageFormat.PNG.name(),
93                 new File("bishop" + "." + ImageFormat.PNG.getExt())
94         );
95     }
96
97 }