Coverage Report - org.jaudiotagger.tag.id3.valuepair.ImageFormats
 
Classes in this File Line Coverage Branch Coverage Complexity
ImageFormats
49%
17/35
14%
5/36
0
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *
 4  
  *  Version @version:$Id: ImageFormats.java,v 1.9 2008/12/01 15:30:55 paultaylor Exp $
 5  
  *
 6  
  *  MusicTag Copyright (C)2003,2004
 7  
  *
 8  
  *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
 9  
  *  General Public  License as published by the Free Software Foundation; either version 2.1 of the License,
 10  
  *  or (at your option) any later version.
 11  
  *
 12  
  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 13  
  *  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 14  
  *  See the GNU Lesser General Public License for more details.
 15  
  *
 16  
  *  You should have received a copy of the GNU Lesser General Public License along with this library; if not,
 17  
  *  you can get a copy from http://www.opensource.org/licenses/lgpl-license.php or write to the Free Software
 18  
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 19  
  *
 20  
  * Description:
 21  
  * This class maps from v2.2 Image formats (PIC) to v2.3/v2.4 Mimetypes (APIC) and
 22  
  *  vice versa.
 23  
  */
 24  
 package org.jaudiotagger.tag.id3.valuepair;
 25  
 
 26  
 import java.util.HashMap;
 27  
 import java.util.Map;
 28  
 
 29  
 /**
 30  
  * Represents the image formats support by ID3, provides a mapping between the format field supported in ID3v22 and the
 31  
  * mimetype field supported by ID3v23/ID3v24.
 32  
  */
 33  0
 public class ImageFormats
 34  
 {
 35  
     public static final String V22_JPG_FORMAT = "JPG";
 36  
     public static final String V22_PNG_FORMAT = "PNG";
 37  
     public static final String V22_GIF_FORMAT = "GIF";
 38  
     public static final String V22_BMP_FORMAT = "COVERART_BMP";
 39  
 
 40  
     public static final String MIME_TYPE_JPEG = "image/jpeg";
 41  
     public static final String MIME_TYPE_PNG = "image/png";
 42  
     public static final String MIME_TYPE_GIF = "image/gif";
 43  
     public static final String MIME_TYPE_BMP = "image/bmp";
 44  
 
 45  
     /**
 46  
      * Sometimes this is used for jpg instead :or have I made this up
 47  
      */
 48  
     public static final String MIME_TYPE_JPG = "image/jpg";
 49  
 
 50  10
     private static Map<String, String> imageFormatsToMimeType = new HashMap<String, String>();
 51  10
     private static Map<String, String> imageMimeTypeToFormat = new HashMap<String, String>();
 52  
 
 53  
     static
 54  
     {
 55  10
         imageFormatsToMimeType.put(V22_JPG_FORMAT, MIME_TYPE_JPEG);
 56  10
         imageFormatsToMimeType.put(V22_PNG_FORMAT, MIME_TYPE_PNG);
 57  10
         imageFormatsToMimeType.put(V22_GIF_FORMAT, MIME_TYPE_GIF);
 58  10
         imageFormatsToMimeType.put(V22_BMP_FORMAT, MIME_TYPE_BMP);
 59  
         String value;
 60  10
         for (String key : imageFormatsToMimeType.keySet())
 61  
         {
 62  40
             value = imageFormatsToMimeType.get(key);
 63  40
             imageMimeTypeToFormat.put(value, key);
 64  
         }
 65  
 
 66  
         //The mapping isnt one-one lets add other mimetypes
 67  10
         imageMimeTypeToFormat.put(MIME_TYPE_JPG, V22_JPG_FORMAT);
 68  10
     }
 69  
 
 70  
     /**
 71  
      * Get v2.3 mimetype from v2.2 format
 72  
      */
 73  
     public static String getMimeTypeForFormat(String format)
 74  
     {
 75  15
         return imageFormatsToMimeType.get(format);
 76  
     }
 77  
 
 78  
     /**
 79  
      * Get v2.2 format from v2.3 mimetype
 80  
      */
 81  
     public static String getFormatForMimeType(String mimeType)
 82  
     {
 83  6
         return imageMimeTypeToFormat.get(mimeType);
 84  
     }
 85  
 
 86  
     /**
 87  
      * @param data
 88  
      * @return true if binary data matches expected header for a png
 89  
      */
 90  
     public static boolean binaryDataIsPngFormat(byte[] data)
 91  
     {
 92  
         //Read signature
 93  22
         if ((0x89 == (data[0] & 0xff)) && (0x50 == (data[1] & 0xff)) && (0x4E == (data[2] & 0xff)) && (0x47 == (data[3] & 0xff)))
 94  
         {
 95  22
             return true;
 96  
         }
 97  0
         return false;
 98  
     }
 99  
 
 100  
     /**
 101  
      * @param data
 102  
      * @return true if binary data matches expected header for a jpg
 103  
      */
 104  
     public static boolean binaryDataIsJpgFormat(byte[] data)
 105  
     {
 106  
         //Read signature
 107  0
         if ((0xff == (data[0] & 0xff)) && (0xd8 == (data[1] & 0xff)) && (0xff == (data[2] & 0xff)) && (0xff == (data[3] & 0xe0)))
 108  
         {
 109  0
             return true;
 110  
         }
 111  0
         return false;
 112  
     }
 113  
 
 114  
     /**
 115  
      * @param data
 116  
      * @return true if binary data matches expected header for a gif
 117  
      */
 118  
     public static boolean binaryDataIsGifFormat(byte[] data)
 119  
     {
 120  
         //Read signature
 121  0
         if ((0x47 == (data[0] & 0xff)) && (0x49 == (data[1] & 0xff)) && (0x46 == (data[2] & 0xff)))
 122  
         {
 123  0
             return true;
 124  
         }
 125  0
         return false;
 126  
     }
 127  
 
 128  
     /**
 129  
      * @param data
 130  
      * @return true if binary data matches expected header for a bmp
 131  
      */
 132  
     public static boolean binaryDataIsBmpFormat(byte[] data)
 133  
     {
 134  
         //Read signature
 135  0
         if ((0x42 == (data[0] & 0xff)) && (0x4d == (data[1] & 0xff)) && (0x3c == (data[2] & 0xff)))
 136  
         {
 137  0
             return true;
 138  
         }
 139  0
         return false;
 140  
     }
 141  
 
 142  
     /**
 143  
      *
 144  
      * @param data
 145  
      * @return correct mimetype for the image data represented by this byte data
 146  
      */
 147  
     public static String getMimeTypeForBinarySignature(byte[] data)
 148  
     {
 149  11
         if(binaryDataIsPngFormat(data))
 150  
         {
 151  11
             return MIME_TYPE_PNG;
 152  
         }
 153  0
         else if(binaryDataIsJpgFormat(data))
 154  
         {
 155  0
             return MIME_TYPE_JPEG;
 156  
         }
 157  0
         else if(binaryDataIsGifFormat(data))
 158  
         {
 159  0
             return MIME_TYPE_GIF;
 160  
         }
 161  0
         else if(binaryDataIsBmpFormat(data))
 162  
         {
 163  0
             return MIME_TYPE_BMP;
 164  
         }
 165  
         else
 166  
         {
 167  0
             return null;
 168  
         }
 169  
     }
 170  
 }