001package com.hfg.image; 002 003import java.io.File; 004import java.util.ArrayList; 005import java.util.List; 006 007import com.hfg.util.FileUtil; 008import com.hfg.util.collection.CollectionUtil; 009import com.hfg.util.mime.MimeType; 010 011//------------------------------------------------------------------------------ 012/** 013 * Standard image formats. 014 * 015 * @author J. Alex Taylor, hairyfatguy.com 016 */ 017//------------------------------------------------------------------------------ 018// com.hfg XML/HTML Coding Library 019// 020// This library is free software; you can redistribute it and/or 021// modify it under the terms of the GNU Lesser General Public 022// License as published by the Free Software Foundation; either 023// version 2.1 of the License, or (at your option) any later version. 024// 025// This library is distributed in the hope that it will be useful, 026// but WITHOUT ANY WARRANTY; without even the implied warranty of 027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 028// Lesser General Public License for more details. 029// 030// You should have received a copy of the GNU Lesser General Public 031// License along with this library; if not, write to the Free Software 032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 033// 034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 035// jataylor@hairyfatguy.com 036//------------------------------------------------------------------------------ 037 038 039public enum ImageFormat 040{ 041 PNG("png", MimeType.IMAGE_PNG), 042 JPEG("jpg", MimeType.IMAGE_JPG), 043 TIFF("tiff", MimeType.IMAGE_TIFF), 044 GIF("gif", MimeType.IMAGE_GIF), 045 ICO("ico", MimeType.IMAGE_ICO), 046 SVG("svg", MimeType.IMAGE_SVG_XML); 047 048 private String mFileExtension; 049 private MimeType mMimeType; 050 private List<String> mAlternateExtensions; 051 052 static 053 { 054 JPEG.mAlternateExtensions = new ArrayList<>(1); 055 JPEG.mAlternateExtensions.add("jpeg"); 056 057 TIFF.mAlternateExtensions = new ArrayList<>(1); 058 TIFF.mAlternateExtensions.add("tif"); 059 } 060 061 //-------------------------------------------------------------------------- 062 private ImageFormat(String inFileExtension, MimeType inMimeType) 063 { 064 mFileExtension = inFileExtension; 065 mMimeType = inMimeType; 066 } 067 068 069 //-------------------------------------------------------------------------- 070 public String getFileExtension() 071 { 072 return mFileExtension; 073 } 074 075 //-------------------------------------------------------------------------- 076 public MimeType getMimeType() 077 { 078 return mMimeType; 079 } 080 081 //-------------------------------------------------------------------------- 082 public static boolean isJPEG(File inFile) 083 { 084 boolean result = false; 085 086 String extension = FileUtil.getExtension(inFile); 087 if (extension != null) 088 { 089 if (extension.equalsIgnoreCase(JPEG.getFileExtension()) 090 || extension.equalsIgnoreCase(JPEG.name()) 091 || (CollectionUtil.hasValues(JPEG.mAlternateExtensions) 092 && JPEG.mAlternateExtensions.contains(extension))) 093 { 094 result = true; 095 } 096 } 097 098 return result; 099 } 100 101 //-------------------------------------------------------------------------- 102 /** 103 Tries to guess the image format from the filename extension in a case-insensitive way. 104 Returns null if no match is found. 105 */ 106 public static ImageFormat guessFormatFromName(String inName) 107 { 108 ImageFormat format = null; 109 110 String lowercaseName = inName.toLowerCase(); 111 String extension = FileUtil.getExtension(new File(lowercaseName)); 112 113 for (ImageFormat imageFormat : values()) 114 { 115 if (extension.equals(imageFormat.getFileExtension()) 116 || extension.equals(imageFormat.name()) 117 || (CollectionUtil.hasValues(imageFormat.mAlternateExtensions) 118 && imageFormat.mAlternateExtensions.contains(extension))) 119 { 120 format = imageFormat; 121 break; 122 } 123 } 124 125 return format; 126 } 127}