[LIB-9] Add functional for transcoding vector images
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / ImageFormatShaper.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;
18
19 import java.awt.image.BufferedImage;
20 import java.io.InputStream;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.hedgecode.chess.img.bitmap.BitmapImageShaper;
25 import org.hedgecode.chess.img.vector.VectorImageShaper;
26
27 import static org.hedgecode.chess.img.ImageFormat.Type;
28
29 /**
30  * Shaper for images, indicated by type format.
31  *
32  * @author Dmitry Samoshin aka gotty
33  */
34 public final class ImageFormatShaper {
35
36     private static ImageFormatShaper _instance = new ImageFormatShaper();
37
38     private Map<Type, ImageShaper> imageShapers = new HashMap<>();
39
40     private ImageFormatShaper() {
41         imageShapers.put(
42                 Type.BITMAP,
43                 BitmapImageShaper.create()
44         );
45         imageShapers.put(
46                 Type.VECTOR,
47                 VectorImageShaper.create()
48         );
49     }
50
51     public BufferedImage shape(String imageFormat, InputStream imageStream, int size)
52             throws ImageException
53     {
54         ImageFormat format = ImageFormat.findFormat(imageFormat);
55         if (format != null) {
56             ImageShaper imageShaper = imageShapers.get(
57                     format.getType()
58             );
59             return imageShaper.shape(imageStream, size);
60         } else {
61             throw new ImageException("image.unknown.image.format", imageFormat);
62         }
63     }
64
65     public static ImageFormatShaper getInstance() {
66         return _instance;
67     }
68
69 }