[LIB-9] Add functional for transcoding vector images
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / ImageFormat.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.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22
23 import javax.imageio.ImageIO;
24
25 /**
26  * Supported image formats for reading/writing, indicated by type.
27  *
28  * @author Dmitry Samoshin aka gotty
29  */
30 public enum ImageFormat {
31
32     PNG ( Type.BITMAP, new String[]{"png"}         ),
33     GIF ( Type.BITMAP, new String[]{"gif"}         ),
34     JPG ( Type.BITMAP, new String[]{"jpg", "jpeg"} ),
35     BMP ( Type.BITMAP, new String[]{"bmp", "wbmp"} ),
36     SVG ( Type.VECTOR, new String[]{"svg"}         );
37
38     enum Type {
39
40         BITMAP,
41         VECTOR
42
43     }
44
45     private Type formatType;
46     private String[] formatExts;
47
48     private boolean isRead;
49     private boolean isWrite;
50
51     private static String[] allAvailableExts;
52
53     ImageFormat(Type type, String[] exts) {
54         formatType = type;
55         formatExts = exts;
56         isRead = isExist(
57                 ImageIO.getReaderFormatNames(), formatExts
58         );
59         isWrite = isExist(
60                 ImageIO.getWriterFormatNames(), formatExts
61         );
62     }
63
64     public Type getType() {
65         return formatType;
66     }
67
68     public String getExt() {
69         return formatExts[0];
70     }
71
72     public String[] getExts() {
73         return formatExts;
74     }
75
76     public boolean isRead() {
77         return isRead;
78     }
79
80     public boolean isWrite() {
81         return isWrite;
82     }
83
84     public static ImageFormat findFormat(String formatName) {
85         if (formatName != null) {
86             for (ImageFormat imageFormat : ImageFormat.values()) {
87                 if (isExist(imageFormat.getExts(), formatName))
88                     return imageFormat;
89             }
90         }
91         return null;
92     }
93
94     public static String[] getAllExts() {
95         if (allAvailableExts == null) {
96             List<String> listExts = new ArrayList<>();
97             for (ImageFormat imageFormat : ImageFormat.values()) {
98                 listExts.addAll(Arrays.asList(imageFormat.getExts()));
99             }
100             allAvailableExts = listExts.toArray(new String[listExts.size()]);
101         }
102         return allAvailableExts;
103     }
104
105     private static boolean isExist(String[] names, String... args) {
106         for (String arg : args) {
107             for (String name : names) {
108                 if (arg.equalsIgnoreCase(name))
109                     return true;
110             }
111         }
112         return false;
113     }
114
115 }