2 * Copyright (c) 2018-2019. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.chess.img;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
23 import javax.imageio.ImageIO;
26 * Supported image formats for reading/writing, indicated by type.
28 * @author Dmitry Samoshin aka gotty
30 public enum ImageFormat {
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"} );
45 private Type formatType;
46 private String[] formatExts;
48 private boolean isRead;
49 private boolean isWrite;
51 private static String[] allAvailableExts;
53 ImageFormat(Type type, String[] exts) {
57 ImageIO.getReaderFormatNames(), formatExts
60 ImageIO.getWriterFormatNames(), formatExts
64 public Type getType() {
68 public String getExt() {
72 public String[] getExts() {
76 public boolean isRead() {
80 public boolean isWrite() {
84 public static ImageFormat findFormat(String formatName) {
85 if (formatName != null) {
86 for (ImageFormat imageFormat : ImageFormat.values()) {
87 if (isExist(imageFormat.getExts(), formatName))
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()));
100 allAvailableExts = listExts.toArray(new String[listExts.size()]);
102 return allAvailableExts;
105 private static boolean isExist(String[] names, String... args) {
106 for (String arg : args) {
107 for (String name : names) {
108 if (arg.equalsIgnoreCase(name))