All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.image.ImageFormat Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.image;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.hfg.util.FileUtil;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.util.mime.MimeType;

//------------------------------------------------------------------------------
/**
 * Standard image formats.
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------


public enum ImageFormat
{
   PNG("png",   MimeType.IMAGE_PNG),
   JPEG("jpg",  MimeType.IMAGE_JPG),
   TIFF("tiff", MimeType.IMAGE_TIFF),
   GIF("gif",   MimeType.IMAGE_GIF),
   ICO("ico",   MimeType.IMAGE_ICO),
   SVG("svg",   MimeType.IMAGE_SVG_XML);
   
   private String   mFileExtension;
   private MimeType mMimeType;
   private List mAlternateExtensions;

   static
   {
      JPEG.mAlternateExtensions = new ArrayList<>(1);
      JPEG.mAlternateExtensions.add("jpeg");

      TIFF.mAlternateExtensions = new ArrayList<>(1);
      TIFF.mAlternateExtensions.add("tif");
   }

   //--------------------------------------------------------------------------
   private ImageFormat(String inFileExtension, MimeType inMimeType)
   {
      mFileExtension = inFileExtension;
      mMimeType = inMimeType;
   }


   //--------------------------------------------------------------------------
   public String getFileExtension() 
   {
      return mFileExtension;
   }

   //--------------------------------------------------------------------------
   public MimeType getMimeType()
   {
      return mMimeType;
   }

   //--------------------------------------------------------------------------
   public static boolean isJPEG(File inFile)
   {
      boolean result = false;

      String extension = FileUtil.getExtension(inFile);
      if (extension != null)
      {
         if (extension.equalsIgnoreCase(JPEG.getFileExtension())
             || extension.equalsIgnoreCase(JPEG.name())
             || (CollectionUtil.hasValues(JPEG.mAlternateExtensions)
                 && JPEG.mAlternateExtensions.contains(extension)))
         {
            result = true;
         }
      }

      return result;
   }

   //--------------------------------------------------------------------------
   /**
    Tries to guess the image format from the filename extension in a case-insensitive way.
    Returns null if no match is found.
    */
   public static ImageFormat guessFormatFromName(String inName)
   {
      ImageFormat format = null;

      String lowercaseName = inName.toLowerCase();
      String extension = FileUtil.getExtension(new File(lowercaseName));

      for (ImageFormat imageFormat : values())
      {
         if (extension.equals(imageFormat.getFileExtension())
             || extension.equals(imageFormat.name())
             || (CollectionUtil.hasValues(imageFormat.mAlternateExtensions)
                 && imageFormat.mAlternateExtensions.contains(extension)))
         {
            format = imageFormat;
            break;
         }
      }

      return format;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy