2 * Copyright (c) 2018. 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.
28 * @author Dmitry Samoshin aka gotty
30 public enum ImageFormat {
32 PNG ( new String[]{"png"} ),
33 GIF ( new String[]{"gif"} ),
34 JPG ( new String[]{"jpg", "jpeg"} ),
35 SVG ( new String[]{"svg"} ), // todo
36 BMP ( new String[]{"bmp", "wbmp"} );
38 private String[] fortmatExts;
40 private boolean isRead;
41 private boolean isWrite;
43 private static String[] allAvailableExts;
45 ImageFormat(String[] exts) {
48 ImageIO.getReaderFormatNames(), fortmatExts
51 ImageIO.getWriterFormatNames(), fortmatExts
55 public String getExt() {
56 return fortmatExts[0];
59 public String[] getExts() {
63 public boolean isRead() {
67 public boolean isWrite() {
71 public static ImageFormat findFormat(String formatName) {
72 if (formatName != null) {
73 for (ImageFormat imageFormat : ImageFormat.values()) {
74 if (isExist(imageFormat.getExts(), formatName))
81 public static String[] getAllExts() {
82 if (allAvailableExts == null) {
83 List<String> listExts = new ArrayList<>();
84 for (ImageFormat imageFormat : ImageFormat.values()) {
85 listExts.addAll(Arrays.asList(imageFormat.getExts()));
87 allAvailableExts = listExts.toArray(new String[listExts.size()]);
89 return allAvailableExts;
92 private static boolean isExist(String[] names, String... args) {
93 for (String arg : args) {
94 for (String name : names) {
95 if (arg.equalsIgnoreCase(name))
104 public static void main(String[] args) {
105 ImageFormat imageFormat = JPG;
106 System.out.println("Supported format: " + imageFormat);
107 imageFormat = findFormat("jpeg");
108 imageFormat = findFormat("svg");
109 imageFormat = findFormat("jpeeg");