[LIB-9] Add functional for transcoding vector images
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / vector / VectorImageShaper.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.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.net.URL;
26
27 import org.apache.batik.transcoder.TranscoderException;
28
29 import org.hedgecode.chess.img.ImageException;
30 import org.hedgecode.chess.img.ImageShaper;
31
32 /**
33  * Shaper for vector images.
34  *
35  * @author Dmitry Samoshin aka gotty
36  */
37 public class VectorImageShaper implements ImageShaper {
38
39     private VectorImageShaper() {
40     }
41
42     @Override
43     public BufferedImage shape(InputStream imageStream, int size) throws ImageException {
44         SquareImageTranscoder imageTranscoder =
45                 new SquareImageTranscoder(BufferedImage.TYPE_INT_ARGB, size);
46         try {
47             return imageTranscoder.transcode(imageStream);
48         } catch (TranscoderException e) {
49             throw new ImageException("image.unable.load.vector", e.getMessage());
50         }
51     }
52
53     @Override
54     public BufferedImage shape(URL imageUrl, int size) throws ImageException {
55         try {
56             return shape(
57                     imageUrl.openStream(),
58                     size
59             );
60         } catch (IOException e) {
61             throw new ImageException("image.unable.load.vector", e.getMessage());
62         }
63     }
64
65     @Override
66     public BufferedImage shape(File imageFile, int size) throws ImageException {
67         try {
68             return shape(
69                     new FileInputStream(imageFile),
70                     size
71             );
72         } catch (FileNotFoundException e) {
73             throw new ImageException("image.unable.load.vector", e.getMessage());
74         }
75     }
76
77     public static ImageShaper create() {
78         return new VectorImageShaper();
79     }
80
81 }