Coverage Report - org.jaudiotagger.tag.id3.framebody.FrameBodyAPIC
 
Classes in this File Line Coverage Branch Coverage Complexity
FrameBodyAPIC
70%
35/50
67%
4/6
1.278
 
 1  
 /*
 2  
  *  MusicTag Copyright (C)2003,2004
 3  
  *
 4  
  *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
 5  
  *  General Public  License as published by the Free Software Foundation; either version 2.1 of the License,
 6  
  *  or (at your option) any later version.
 7  
  *
 8  
  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 9  
  *  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 10  
  *  See the GNU Lesser General Public License for more details.
 11  
  *
 12  
  *  You should have received a copy of the GNU Lesser General Public License along with this library; if not,
 13  
  *  you can get a copy from http://www.opensource.org/licenses/lgpl-license.php or write to the Free Software
 14  
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 15  
  */
 16  
 package org.jaudiotagger.tag.id3.framebody;
 17  
 
 18  
 import org.jaudiotagger.audio.generic.Utils;
 19  
 import org.jaudiotagger.tag.InvalidTagException;
 20  
 import org.jaudiotagger.tag.datatype.*;
 21  
 import org.jaudiotagger.tag.id3.ID3v24Frames;
 22  
 import org.jaudiotagger.tag.id3.valuepair.ImageFormats;
 23  
 import org.jaudiotagger.tag.id3.valuepair.TextEncoding;
 24  
 import org.jaudiotagger.tag.reference.PictureTypes;
 25  
 
 26  
 import java.io.ByteArrayOutputStream;
 27  
 import java.nio.ByteBuffer;
 28  
 
 29  
 /**
 30  
  * Attached picture frame.
 31  
  * <p/>
 32  
  * <p/>
 33  
  * This frame contains a picture directly related to the audio file.
 34  
  * Image format is the MIME type and subtype for the image. In
 35  
  * the event that the MIME media type name is omitted, "image/" will be
 36  
  * implied. The "image/png" or "image/jpeg" picture format
 37  
  * should be used when interoperability is wanted. Description is a
 38  
  * short description of the picture, represented as a terminated
 39  
  * textstring. The description has a maximum length of 64 characters,
 40  
  * but may be empty. There may be several pictures attached to one file,
 41  
  * each in their individual "APIC" frame, but only one with the same
 42  
  * content descriptor. There may only be one picture with the picture
 43  
  * type declared as picture type $01 and $02 respectively. There is the
 44  
  * possibility to put only a link to the image file by using the 'MIME
 45  
  * type' "-->" and having a complete URL instead of picture data.
 46  
  * The use of linked files should however be used sparingly since there
 47  
  * is the risk of separation of files.
 48  
  * </p><p><table border=0 width="70%">
 49  
  * <tr><td colspan=2> &lt;Header for 'Attached picture', ID: "APIC"&gt;</td></tr>
 50  
  * <tr><td>Text encoding  </td><td>$xx                            </td></tr>
 51  
  * <tr><td>MIME type      </td><td>&lt;text string&gt; $00        </td></tr>
 52  
  * <tr><td>Picture type   </td><td>$xx                            </td></tr>
 53  
  * <tr><td>Description    </td><td>&lt;text string according to encoding&gt; $00 (00)</td></tr>
 54  
  * <tr><td>Picture data   </td><td>&lt;binary data&gt;            </td></tr>
 55  
  * </table></p>
 56  
  * <p><table border=0 width="70%">
 57  
  * <tr><td rowspan=21 valign=top>Picture type:</td>
 58  
  * <td>$00 </td><td>Other                                </td></tr>
 59  
  * <tr><td>$01 </td><td>32x32 pixels 'file icon' (PNG only)  </td></tr>
 60  
  * <tr><td>$02 </td><td>Other file icon                      </td></tr>
 61  
  * <tr><td>$03 </td><td>Cover (front)                        </td></tr>
 62  
  * <tr><td>$04 </td><td>Cover (back)                         </td></tr>
 63  
  * <tr><td>$05 </td><td>Leaflet page                         </td></tr>
 64  
  * <tr><td>$06 </td><td>Media (e.g. lable side of CD)        </td></tr>
 65  
  * <tr><td>$07 </td><td>Lead artist/lead performer/soloist   </td></tr>
 66  
  * <tr><td>$08 </td><td>Artist/performer                     </td></tr>
 67  
  * <tr><td>$09 </td><td>Conductor                            </td></tr>
 68  
  * <tr><td>$0A </td><td>Band/Orchestra                       </td></tr>
 69  
  * <tr><td>$0B </td><td>Composer                             </td></tr>
 70  
  * <tr><td>$0C </td><td>Lyricist/text writer                 </td></tr>
 71  
  * <tr><td>$0D </td><td>Recording Location                   </td></tr>
 72  
  * <tr><td>$0E </td><td>During recording                     </td></tr>
 73  
  * <tr><td>$0F </td><td>During performance                   </td></tr>
 74  
  * <tr><td>$10 </td><td>Movie/video screen capture           </td></tr>
 75  
  * <tr><td>$11 </td><td>A bright coloured fish               </td></tr>
 76  
  * <tr><td>$12 </td><td>Illustration                         </td></tr>
 77  
  * <tr><td>$13 </td><td>Band/artist logotype                 </td></tr>
 78  
  * <tr><td>$14 </td><td>Publisher/Studio logotype            </td></tr>
 79  
  * </table></p>
 80  
  * <p/>
 81  
  * <p>For more details, please refer to the ID3 specifications:
 82  
  * <ul>
 83  
  * <li><a href="http://www.id3.org/id3v2.3.0.txt">ID3 v2.3.0 Spec</a>
 84  
  * </ul>
 85  
  *
 86  
  * @author : Paul Taylor
 87  
  * @author : Eric Farng
 88  
  * @version $Id: FrameBodyAPIC.java,v 1.21 2008/07/21 10:45:42 paultaylor Exp $
 89  
  */
 90  
 public class FrameBodyAPIC extends AbstractID3v2FrameBody implements ID3v24FrameBody, ID3v23FrameBody
 91  
 {
 92  
     public static final String IMAGE_IS_URL = "-->";
 93  
 
 94  
     /**
 95  
      * Creates a new FrameBodyAPIC datatype.
 96  
      */
 97  
     public FrameBodyAPIC()
 98  21
     {
 99  
         //Initilise default text encoding
 100  21
         setObjectValue(DataTypes.OBJ_TEXT_ENCODING, TextEncoding.ISO_8859_1);
 101  21
     }
 102  
 
 103  
     public FrameBodyAPIC(FrameBodyAPIC body)
 104  
     {
 105  33
         super(body);
 106  33
     }
 107  
 
 108  
     /**
 109  
      * Conversion from v2 PIC to v3/v4 APIC
 110  
      */
 111  
     public FrameBodyAPIC(FrameBodyPIC body)
 112  12
     {
 113  12
         this.setObjectValue(DataTypes.OBJ_TEXT_ENCODING, body.getTextEncoding());
 114  12
         this.setObjectValue(DataTypes.OBJ_MIME_TYPE, ImageFormats.getMimeTypeForFormat((String) body.getObjectValue(DataTypes.OBJ_IMAGE_FORMAT)));
 115  12
         this.setObjectValue(DataTypes.OBJ_PICTURE_TYPE, body.getObjectValue(DataTypes.OBJ_PICTURE_TYPE));
 116  12
         this.setObjectValue(DataTypes.OBJ_DESCRIPTION, body.getDescription());
 117  12
         this.setObjectValue(DataTypes.OBJ_PICTURE_DATA, body.getObjectValue(DataTypes.OBJ_PICTURE_DATA));
 118  
 
 119  12
     }
 120  
 
 121  
     /**
 122  
      * Creates a new FrameBodyAPIC datatype.
 123  
      *
 124  
      * @param textEncoding
 125  
      * @param mimeType
 126  
      * @param pictureType
 127  
      * @param description
 128  
      * @param data
 129  
      */
 130  
     public FrameBodyAPIC(byte textEncoding, String mimeType, byte pictureType, String description, byte[] data)
 131  0
     {
 132  0
         this.setObjectValue(DataTypes.OBJ_TEXT_ENCODING, textEncoding);
 133  0
         this.setMimeType(mimeType);
 134  0
         this.setPictureType(pictureType);
 135  0
         this.setDescription(description);
 136  0
         this.setImageData(data);
 137  0
     }
 138  
 
 139  
     /**
 140  
      * Creates a new FrameBodyAPIC datatype.
 141  
      *
 142  
      * @throws InvalidTagException if unable to create framebody from buffer
 143  
      */
 144  
     public FrameBodyAPIC(ByteBuffer byteBuffer, int frameSize) throws InvalidTagException
 145  
     {
 146  55
         super(byteBuffer, frameSize);
 147  55
     }
 148  
 
 149  
     /**
 150  
      * Set a description of the image
 151  
      *
 152  
      * @param description
 153  
      */
 154  
     public void setDescription(String description)
 155  
     {
 156  7
         setObjectValue(DataTypes.OBJ_DESCRIPTION, description);
 157  7
     }
 158  
 
 159  
     /**
 160  
      * Get a description of the image
 161  
      *
 162  
      * @return a description of the image
 163  
      */
 164  
     public String getDescription()
 165  
     {
 166  12
         return (String) getObjectValue(DataTypes.OBJ_DESCRIPTION);
 167  
     }
 168  
 
 169  
     /**
 170  
      * Set mimeType
 171  
      *
 172  
      * @param mimeType
 173  
      */
 174  
     public void setMimeType(String mimeType)
 175  
     {
 176  0
         setObjectValue(DataTypes.OBJ_MIME_TYPE, mimeType);
 177  0
     }
 178  
 
 179  
     /**
 180  
      * Get mimetype
 181  
      *
 182  
      * @return a description of the image
 183  
      */
 184  
     public String getMimeType()
 185  
     {
 186  31
         return (String) getObjectValue(DataTypes.OBJ_MIME_TYPE);
 187  
     }
 188  
 
 189  
     /**
 190  
      * Set imageData
 191  
      *
 192  
      * @param imageData
 193  
      */
 194  
     public void setImageData(byte[] imageData)
 195  
     {
 196  0
         setObjectValue(DataTypes.OBJ_PICTURE_DATA, imageData);
 197  0
     }
 198  
 
 199  
     /**
 200  
      * Get Image data
 201  
      *
 202  
      * @return
 203  
      */
 204  
     public byte[] getImageData()
 205  
     {
 206  9
         return (byte[]) getObjectValue(DataTypes.OBJ_PICTURE_DATA);
 207  
     }
 208  
 
 209  
     /**
 210  
      * Set Picture Type
 211  
      *
 212  
      * @param pictureType
 213  
      */
 214  
     public void setPictureType(byte pictureType)
 215  
     {
 216  0
         setObjectValue(DataTypes.OBJ_PICTURE_TYPE, pictureType);
 217  0
     }
 218  
 
 219  
     /**
 220  
      * @return picturetype
 221  
      */
 222  
     public int getPictureType()
 223  
     {
 224  7
         return ((Long) getObjectValue(DataTypes.OBJ_PICTURE_TYPE)).intValue();
 225  
     }
 226  
 
 227  
     /**
 228  
      * The ID3v2 frame identifier
 229  
      *
 230  
      * @return the ID3v2 frame identifier  for this frame type
 231  
      */
 232  
     public String getIdentifier()
 233  
     {
 234  182
         return ID3v24Frames.FRAME_ID_ATTACHED_PICTURE;
 235  
     }
 236  
 
 237  
 
 238  
     /**
 239  
      * If the description cannot be encoded using current encoder, change the encoder
 240  
      */
 241  
     public void write(ByteArrayOutputStream tagBuffer)
 242  
     {
 243  29
         if (((AbstractString) getObject(DataTypes.OBJ_DESCRIPTION)).canBeEncoded() == false)
 244  
         {
 245  0
             this.setTextEncoding(TextEncoding.UTF_16);
 246  
         }
 247  29
         super.write(tagBuffer);
 248  29
     }
 249  
 
 250  
     /**
 251  
      *
 252  
      */
 253  
     protected void setupObjectList()
 254  
     {
 255  88
         objectList.add(new NumberHashMap(DataTypes.OBJ_TEXT_ENCODING, this, TextEncoding.TEXT_ENCODING_FIELD_SIZE));
 256  88
         objectList.add(new StringNullTerminated(DataTypes.OBJ_MIME_TYPE, this));
 257  88
         objectList.add(new NumberHashMap(DataTypes.OBJ_PICTURE_TYPE, this, PictureTypes.PICTURE_TYPE_FIELD_SIZE));
 258  88
         objectList.add(new TextEncodedStringNullTerminated(DataTypes.OBJ_DESCRIPTION, this));
 259  88
         objectList.add(new ByteArraySizeTerminated(DataTypes.OBJ_PICTURE_DATA, this));
 260  88
     }
 261  
 
 262  
     /**
 263  
      * @return true if imagedata  is held as a url rather than actually being imagedata
 264  
      */
 265  
     public boolean isImageUrl()
 266  
     {
 267  12
         if (getMimeType() == null)
 268  
         {
 269  0
             return false;
 270  
         }
 271  12
         return getMimeType().equals(IMAGE_IS_URL);
 272  
     }
 273  
 
 274  
     /**
 275  
      * @return the image url if there is otherwise return an empty String
 276  
      */
 277  
     public String getImageUrl()
 278  
     {
 279  2
         if (isImageUrl())
 280  
         {
 281  1
             return Utils.getString(((byte[]) getObjectValue(DataTypes.OBJ_PICTURE_DATA)), 0, ((byte[]) getObjectValue(DataTypes.OBJ_PICTURE_DATA)).length, TextEncoding.CHARSET_ISO_8859_1);
 282  
         }
 283  
         else
 284  
         {
 285  1
             return "";
 286  
         }
 287  
     }
 288  
 
 289  
 }