Coverage Report - org.jaudiotagger.tag.id3.framebody.FrameBodyPIC
 
Classes in this File Line Coverage Branch Coverage Complexity
FrameBodyPIC
69%
30/43
16%
1/6
1.125
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *  @author : Eric Farng
 4  
  *
 5  
  *  Version @version:$Id: FrameBodyPIC.java 836 2009-11-12 15:44:07Z paultaylor $
 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  48
     {
 92  48
         setObjectValue(DataTypes.OBJ_TEXT_ENCODING, TextEncoding.ISO_8859_1);
 93  48
     }
 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  
      * @param body
 121  
      */
 122  
     public FrameBodyPIC(FrameBodyAPIC body)
 123  8
     {
 124  8
         this.setObjectValue(DataTypes.OBJ_TEXT_ENCODING, body.getTextEncoding());
 125  8
         this.setObjectValue(DataTypes.OBJ_IMAGE_FORMAT, ImageFormats.getFormatForMimeType((String) body.getObjectValue(DataTypes.OBJ_MIME_TYPE)));
 126  8
         this.setObjectValue(DataTypes.OBJ_PICTURE_DATA, body.getObjectValue(DataTypes.OBJ_PICTURE_DATA));
 127  8
         this.setDescription(body.getDescription());
 128  8
         this.setImageData(body.getImageData());
 129  8
     }
 130  
 
 131  
     /**
 132  
      * Creates a new FrameBodyPIC datatype.
 133  
      *
 134  
      * @param byteBuffer
 135  
      * @param frameSize
 136  
      * @throws InvalidTagException if unable to create framebody from buffer
 137  
      */
 138  
     public FrameBodyPIC(ByteBuffer byteBuffer, int frameSize) throws InvalidTagException
 139  
     {
 140  37
         super(byteBuffer, frameSize);
 141  36
     }
 142  
 
 143  
     /**
 144  
      * Set a description of the image
 145  
      *
 146  
      * @param description of the image
 147  
      */
 148  
     public void setDescription(String description)
 149  
     {
 150  20
         setObjectValue(DataTypes.OBJ_DESCRIPTION, description);
 151  20
     }
 152  
 
 153  
     /**
 154  
      * Get a description of the image
 155  
      *
 156  
      * @return a description of the image
 157  
      */
 158  
     public String getDescription()
 159  
     {
 160  64
         return (String) getObjectValue(DataTypes.OBJ_DESCRIPTION);
 161  
     }
 162  
 
 163  
     /**
 164  
      * Set imageData
 165  
      *
 166  
      * @param imageData
 167  
      */
 168  
     public void setImageData(byte[] imageData)
 169  
     {
 170  8
         setObjectValue(DataTypes.OBJ_PICTURE_DATA, imageData);
 171  8
     }
 172  
 
 173  
     /**
 174  
      * Get Image data
 175  
      *
 176  
      * @return
 177  
      */
 178  
     public byte[] getImageData()
 179  
     {
 180  12
         return (byte[]) getObjectValue(DataTypes.OBJ_PICTURE_DATA);
 181  
     }
 182  
 
 183  
     /**
 184  
      * Set Picture Type
 185  
      *
 186  
      * @param pictureType
 187  
      */
 188  
     public void setPictureType(byte pictureType)
 189  
     {
 190  0
         setObjectValue(DataTypes.OBJ_PICTURE_TYPE, pictureType);
 191  0
     }
 192  
 
 193  
     /**
 194  
      * @return picturetype
 195  
      */
 196  
     public int getPictureType()
 197  
     {
 198  12
         return ((Long) getObjectValue(DataTypes.OBJ_PICTURE_TYPE)).intValue();
 199  
     }
 200  
 
 201  
 
 202  
     /**
 203  
      * The ID3v2 frame identifier
 204  
      *
 205  
      * @return the ID3v2 frame identifier  for this frame type
 206  
      */
 207  
     public String getIdentifier()
 208  
     {
 209  161
         return ID3v22Frames.FRAME_ID_V2_ATTACHED_PICTURE;
 210  
     }
 211  
 
 212  
 
 213  
     /**
 214  
      * If the description cannot be encoded using current encoder, change the encoder
 215  
      */
 216  
     public void write(ByteArrayOutputStream tagBuffer)
 217  
     {
 218  36
         if (!((AbstractString) getObject(DataTypes.OBJ_DESCRIPTION)).canBeEncoded())
 219  
         {
 220  0
             this.setTextEncoding(TextEncoding.UTF_16);
 221  
         }
 222  36
         super.write(tagBuffer);
 223  36
     }
 224  
 
 225  
     /**
 226  
      * Get a description of the image
 227  
      *
 228  
      * @return a description of the image
 229  
      */
 230  
     public String getFormatType()
 231  
     {
 232  12
         return (String) getObjectValue(DataTypes.OBJ_IMAGE_FORMAT);
 233  
     }
 234  
 
 235  
     public boolean isImageUrl()
 236  
     {
 237  0
         return getFormatType() != null && getFormatType().equals(IMAGE_IS_URL);
 238  
     }
 239  
 
 240  
     /**
 241  
      *
 242  
      */
 243  
     protected void setupObjectList()
 244  
     {
 245  93
         objectList.add(new NumberHashMap(DataTypes.OBJ_TEXT_ENCODING, this, TextEncoding.TEXT_ENCODING_FIELD_SIZE));
 246  93
         objectList.add(new StringFixedLength(DataTypes.OBJ_IMAGE_FORMAT, this, 3));
 247  93
         objectList.add(new NumberHashMap(DataTypes.OBJ_PICTURE_TYPE, this, PictureTypes.PICTURE_TYPE_FIELD_SIZE));
 248  93
         objectList.add(new StringNullTerminated(DataTypes.OBJ_DESCRIPTION, this));
 249  93
         objectList.add(new ByteArraySizeTerminated(DataTypes.OBJ_PICTURE_DATA, this));
 250  93
     }
 251  
 
 252  
 }