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