/* * Copyright (c) 2018. 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. * * @author Dmitry Samoshin aka gotty */ public enum ImageFormat { PNG ( new String[]{"png"} ), GIF ( new String[]{"gif"} ), JPG ( new String[]{"jpg", "jpeg"} ), SVG ( new String[]{"svg"} ), // todo BMP ( new String[]{"bmp", "wbmp"} ); private String[] fortmatExts; private boolean isRead; private boolean isWrite; private static String[] allAvailableExts; ImageFormat(String[] exts) { fortmatExts = exts; isRead = isExist( ImageIO.getReaderFormatNames(), fortmatExts ); isWrite = isExist( ImageIO.getWriterFormatNames(), fortmatExts ); } public String getExt() { return fortmatExts[0]; } public String[] getExts() { return fortmatExts; } 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; } /* public static void main(String[] args) { ImageFormat imageFormat = JPG; System.out.println("Supported format: " + imageFormat); imageFormat = findFormat("jpeg"); imageFormat = findFormat("svg"); imageFormat = findFormat("jpeeg"); } */ }