/* * Copyright (c) 2018-2019. Developed by Hedgecode. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.hedgecode.chess.img; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.imageio.ImageIO; /** * Supported image formats for reading/writing, indicated by type. * * @author Dmitry Samoshin aka gotty */ public enum ImageFormat { PNG ( Type.BITMAP, new String[]{"png"} ), GIF ( Type.BITMAP, new String[]{"gif"} ), JPG ( Type.BITMAP, new String[]{"jpg", "jpeg"} ), BMP ( Type.BITMAP, new String[]{"bmp", "wbmp"} ), SVG ( Type.VECTOR, new String[]{"svg"} ); enum Type { BITMAP, VECTOR } private Type formatType; private String[] formatExts; private boolean isRead; private boolean isWrite; private static String[] allAvailableExts; ImageFormat(Type type, String[] exts) { formatType = type; formatExts = exts; isRead = isExist( ImageIO.getReaderFormatNames(), formatExts ); isWrite = isExist( ImageIO.getWriterFormatNames(), formatExts ); } public Type getType() { return formatType; } public String getExt() { return formatExts[0]; } public String[] getExts() { return formatExts; } public boolean isRead() { return isRead; } public boolean isWrite() { return isWrite; } public static ImageFormat findFormat(String formatName) { if (formatName != null) { for (ImageFormat imageFormat : ImageFormat.values()) { if (isExist(imageFormat.getExts(), formatName)) return imageFormat; } } return null; } public static String[] getAllExts() { if (allAvailableExts == null) { List listExts = new ArrayList<>(); for (ImageFormat imageFormat : ImageFormat.values()) { listExts.addAll(Arrays.asList(imageFormat.getExts())); } allAvailableExts = listExts.toArray(new String[listExts.size()]); } return allAvailableExts; } private static boolean isExist(String[] names, String... args) { for (String arg : args) { for (String name : names) { if (arg.equalsIgnoreCase(name)) return true; } } return false; } }