Coverage Report - org.jaudiotagger.tag.mp4.field.Mp4TagCoverField
 
Classes in this File Line Coverage Branch Coverage Complexity
Mp4TagCoverField
66%
31/47
50%
12/24
2.778
 
 1  
 /*
 2  
  * Entagged Audio Tag library
 3  
  * Copyright (c) 2003-2005 Rapha�l Slinckx <raphael@slinckx.net>
 4  
  * 
 5  
  * This library is free software; you can redistribute it and/or
 6  
  * modify it under the terms of the GNU Lesser General Public
 7  
  * License as published by the Free Software Foundation; either
 8  
  * version 2.1 of the License, or (at your option) any later version.
 9  
  *  
 10  
  * This library is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
  * Lesser General Public License for more details.
 14  
  * 
 15  
  * You should have received a copy of the GNU Lesser General Public
 16  
  * License along with this library; if not, write to the Free Software
 17  
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 18  
  */
 19  
 package org.jaudiotagger.tag.mp4.field;
 20  
 
 21  
 import org.jaudiotagger.audio.mp4.atom.Mp4BoxHeader;
 22  
 import org.jaudiotagger.tag.mp4.Mp4FieldKey;
 23  
 import org.jaudiotagger.tag.mp4.atom.Mp4DataBox;
 24  
 import org.jaudiotagger.tag.mp4.atom.Mp4NameBox;
 25  
 import org.jaudiotagger.tag.reference.PictureTypes;
 26  
 import org.jaudiotagger.tag.id3.valuepair.ImageFormats;
 27  
 import org.jaudiotagger.logging.ErrorMessage;
 28  
 
 29  
 import java.io.UnsupportedEncodingException;
 30  
 import java.nio.ByteBuffer;
 31  
 
 32  
 /**
 33  
  * Represents Cover Art
 34  
  * <p/>
 35  
  * <p>Note:Within this library we have a seperate TagCoverField for every image stored, however this does not map
 36  
  * very directly to how they are physically stored within a file, because all are stored under a single covr atom, so
 37  
  * a more complex conversion has to be done then for other fields when writing multiple images back to file.
 38  
  */
 39  
 public class Mp4TagCoverField extends Mp4TagBinaryField
 40  
 {
 41  
 
 42  
     //Type
 43  
     private Mp4FieldType imageType;
 44  
 
 45  
     //Contains the size of each atom including header, required because may only have data atom or
 46  
     //may have data and name atom
 47  
     private int dataAndHeaderSize;
 48  
 
 49  
     /**
 50  
      * Empty CoverArt Field
 51  
      */
 52  
     public Mp4TagCoverField()
 53  
     {
 54  0
         super(Mp4FieldKey.ARTWORK.getFieldName());
 55  0
     }
 56  
 
 57  
     /**
 58  
      * @return data and header size
 59  
      */
 60  
     public int getDataAndHeaderSize()
 61  
     {
 62  77
         return dataAndHeaderSize;
 63  
     }
 64  
 
 65  
     /**
 66  
      * Construct CoverField by reading data from audio file
 67  
      *
 68  
      * @param raw
 69  
      * @param imageType
 70  
      * @throws UnsupportedEncodingException
 71  
      */
 72  
     public Mp4TagCoverField(ByteBuffer raw,Mp4FieldType imageType) throws UnsupportedEncodingException
 73  
     {
 74  77
         super(Mp4FieldKey.ARTWORK.getFieldName(), raw);
 75  77
         this.imageType=imageType;
 76  77
         if(!Mp4FieldType.isCoverArtType(imageType))
 77  
         {
 78  2
             logger.warning(ErrorMessage.MP4_IMAGE_FORMAT_IS_NOT_TO_EXPECTED_TYPE.getMsg(imageType));
 79  
         }
 80  77
     }
 81  
 
 82  
     /**
 83  
      * Construct new cover art with binarydata provided
 84  
      * <p/>
 85  
      * <p/>
 86  
      * Identifies the imageType by looking at the data
 87  
      *
 88  
      * @param data
 89  
      * @throws UnsupportedEncodingException
 90  
      */
 91  
     public Mp4TagCoverField(byte[] data)
 92  
     {
 93  11
         super(Mp4FieldKey.ARTWORK.getFieldName(), data);
 94  
 
 95  
         //Read signature
 96  11
         if (ImageFormats.binaryDataIsPngFormat(data))
 97  
         {
 98  11
             imageType = Mp4FieldType.COVERART_PNG;
 99  
         }
 100  0
         else if (ImageFormats.binaryDataIsJpgFormat(data))
 101  
         {
 102  0
             imageType = Mp4FieldType.COVERART_JPEG;
 103  
         }
 104  0
         else if (ImageFormats.binaryDataIsGifFormat(data))
 105  
         {
 106  0
             imageType = Mp4FieldType.COVERART_GIF;
 107  
         }
 108  0
         else if (ImageFormats.binaryDataIsBmpFormat(data))
 109  
         {
 110  0
             imageType = Mp4FieldType.COVERART_BMP;
 111  
         }
 112  
         else
 113  
         {
 114  0
             logger.warning(ErrorMessage.GENERAL_UNIDENITIFED_IMAGE_FORMAT.getMsg());
 115  0
             imageType = Mp4FieldType.COVERART_PNG;
 116  
         }
 117  11
     }
 118  
 
 119  
   
 120  
     /**
 121  
      * Return field type, for artwork this also identifies the imagetype
 122  
      *
 123  
      * @return field type
 124  
      */
 125  
     public Mp4FieldType getFieldType()
 126  
     {
 127  71
         return imageType;
 128  
     }
 129  
 
 130  
     public boolean isBinary()
 131  
     {
 132  0
         return true;
 133  
     }
 134  
 
 135  
 
 136  
     public String toString()
 137  
     {
 138  24
         return imageType +":" + dataBytes.length + "bytes";
 139  
     }
 140  
 
 141  
     protected void build(ByteBuffer raw)
 142  
     {
 143  77
         Mp4BoxHeader header = new Mp4BoxHeader(raw);
 144  77
         dataSize = header.getDataLength();
 145  77
         dataAndHeaderSize = header.getLength();
 146  
 
 147  
         //Skip the version and length fields
 148  77
         raw.position(raw.position() + Mp4DataBox.PRE_DATA_LENGTH);
 149  
 
 150  
         //Read the raw data into byte array
 151  77
         this.dataBytes = new byte[dataSize - Mp4DataBox.PRE_DATA_LENGTH];
 152  5250980
         for (int i = 0; i < dataBytes.length; i++)
 153  
         {
 154  5250903
             this.dataBytes[i] = raw.get();
 155  
         }
 156  
 
 157  
         //Is there room for another atom (remember actually passed all the data so unless Covr is last atom
 158  
         //there will be room even though more likely to be for the text top level atom)
 159  77
         int positionAfterDataAtom = raw.position();
 160  77
         if (raw.position() + Mp4BoxHeader.HEADER_LENGTH <= raw.limit())
 161  
         {
 162  
             //Is there a following name field (not the norm)
 163  75
             Mp4BoxHeader nameHeader = new Mp4BoxHeader(raw);
 164  75
             if (nameHeader.getId().equals(Mp4NameBox.IDENTIFIER))
 165  
             {
 166  1
                 dataSize += nameHeader.getDataLength();
 167  1
                 dataAndHeaderSize += nameHeader.getLength();
 168  
             }
 169  
             else
 170  
             {
 171  74
                 raw.position(positionAfterDataAtom);
 172  
             }
 173  
         }
 174  
 
 175  
         //After returning buffers position will be after the end of this atom
 176  77
     }
 177  
 
 178  
     /**
 179  
      *
 180  
      * @param imageType
 181  
      * @return the corresponding mimetype
 182  
      */
 183  
     public static String getMimeTypeForImageType(Mp4FieldType imageType)
 184  
     {
 185  6
         if(imageType==Mp4FieldType.COVERART_PNG)
 186  
         {
 187  3
             return ImageFormats.MIME_TYPE_PNG;
 188  
         }
 189  3
         else if(imageType==Mp4FieldType.COVERART_JPEG)
 190  
         {
 191  3
             return ImageFormats.MIME_TYPE_JPEG;
 192  
         }
 193  0
         else if(imageType==Mp4FieldType.COVERART_GIF)
 194  
         {
 195  0
             return ImageFormats.MIME_TYPE_GIF;
 196  
         }
 197  0
         else if(imageType==Mp4FieldType.COVERART_BMP)
 198  
         {
 199  0
             return ImageFormats.MIME_TYPE_BMP;
 200  
         }
 201  
         else
 202  
         {
 203  0
             return null;
 204  
         }
 205  
     }
 206  
 }