| 1 | |
package org.jaudiotagger.audio.asf.tag; |
| 2 | |
|
| 3 | |
import org.jaudiotagger.audio.asf.data.ContentDescriptor; |
| 4 | |
import org.jaudiotagger.audio.asf.data.AsfHeader; |
| 5 | |
import org.jaudiotagger.tag.id3.valuepair.ImageFormats; |
| 6 | |
import org.jaudiotagger.logging.ErrorMessage; |
| 7 | |
|
| 8 | |
import javax.imageio.ImageIO; |
| 9 | |
import java.io.UnsupportedEncodingException; |
| 10 | |
import java.io.ByteArrayInputStream; |
| 11 | |
import java.io.IOException; |
| 12 | |
import java.io.ByteArrayOutputStream; |
| 13 | |
import java.awt.image.BufferedImage; |
| 14 | |
import java.util.logging.Logger; |
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
public class AsfTagCoverField extends AsfTagField |
| 33 | |
{ |
| 34 | |
|
| 35 | 5 | public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.asf.tag"); |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
private int pictureType; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
private String mimeType; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
private String description; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
private int imageDataSize; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | 41 | private int endOfName=0; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
public AsfTagCoverField(ContentDescriptor source) |
| 69 | |
{ |
| 70 | 39 | super(source); |
| 71 | |
|
| 72 | 39 | if(!source.getName().equals(AsfFieldKey.COVER_ART.getFieldName())) |
| 73 | |
{ |
| 74 | 0 | throw new IllegalArgumentException("Descriptor description must be WM/Picture"); |
| 75 | |
} |
| 76 | 39 | if (source.getType() != ContentDescriptor.TYPE_BINARY) |
| 77 | |
{ |
| 78 | 0 | throw new IllegalArgumentException("Descriptor type must be binary"); |
| 79 | |
} |
| 80 | |
|
| 81 | |
try |
| 82 | |
{ |
| 83 | 39 | processRawContent(); |
| 84 | |
} |
| 85 | 0 | catch(UnsupportedEncodingException uee) |
| 86 | |
{ |
| 87 | |
|
| 88 | 0 | throw new RuntimeException(uee); |
| 89 | 39 | } |
| 90 | 39 | } |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
public AsfTagCoverField(byte[] imageData,int pictureType,String description) |
| 99 | |
{ |
| 100 | 2 | super(new ContentDescriptor(AsfFieldKey.COVER_ART.getFieldName(), ContentDescriptor.TYPE_BINARY)); |
| 101 | 2 | this.getDescriptor().setBinaryValue(createRawContent(imageData,pictureType,description)); |
| 102 | 2 | } |
| 103 | |
|
| 104 | |
private byte[] createRawContent(byte[] data,int pictureType,String description) |
| 105 | |
{ |
| 106 | |
|
| 107 | 2 | mimeType=ImageFormats.getMimeTypeForBinarySignature(data); |
| 108 | 2 | description=description; |
| 109 | 2 | if(mimeType==null) |
| 110 | |
{ |
| 111 | 0 | throw new RuntimeException(ErrorMessage.GENERAL_UNIDENITIFED_IMAGE_FORMAT.getMsg()); |
| 112 | |
} |
| 113 | |
|
| 114 | 2 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 115 | |
|
| 116 | |
|
| 117 | 2 | baos.write(pictureType); |
| 118 | |
|
| 119 | |
|
| 120 | 2 | baos.write(org.jaudiotagger.audio.generic.Utils.getSizeLEInt32(data.length),0,4); |
| 121 | |
|
| 122 | |
|
| 123 | |
byte[] mimeTypeData; |
| 124 | |
try |
| 125 | |
{ |
| 126 | 2 | mimeTypeData=mimeType.getBytes(AsfHeader.ASF_CHARSET.name()); |
| 127 | |
} |
| 128 | 0 | catch(UnsupportedEncodingException uee) |
| 129 | |
{ |
| 130 | |
|
| 131 | 0 | throw new RuntimeException("Unable to find encoding:"+AsfHeader.ASF_CHARSET.name()); |
| 132 | 2 | } |
| 133 | 2 | baos.write(mimeTypeData,0,mimeTypeData.length); |
| 134 | |
|
| 135 | |
|
| 136 | 2 | baos.write(0x00); |
| 137 | 2 | baos.write(0x00); |
| 138 | |
|
| 139 | |
|
| 140 | 2 | if(description!=null && description.length()>0) |
| 141 | |
{ |
| 142 | |
byte[] descriptionData; |
| 143 | |
try |
| 144 | |
{ |
| 145 | 1 | descriptionData=description.getBytes(AsfHeader.ASF_CHARSET.name()); |
| 146 | |
} |
| 147 | 0 | catch(UnsupportedEncodingException uee) |
| 148 | |
{ |
| 149 | |
|
| 150 | 0 | throw new RuntimeException("Unable to find encoding:"+AsfHeader.ASF_CHARSET.name()); |
| 151 | 1 | } |
| 152 | 1 | baos.write(descriptionData,0,descriptionData.length); |
| 153 | |
} |
| 154 | |
|
| 155 | |
|
| 156 | 2 | baos.write(0x00); |
| 157 | 2 | baos.write(0x00); |
| 158 | |
|
| 159 | |
|
| 160 | 2 | baos.write(data,0,data.length); |
| 161 | |
|
| 162 | 2 | return baos.toByteArray(); |
| 163 | |
} |
| 164 | |
|
| 165 | |
|
| 166 | |
private void processRawContent() throws UnsupportedEncodingException |
| 167 | |
{ |
| 168 | |
|
| 169 | 39 | pictureType=this.getRawContent()[0]; |
| 170 | |
|
| 171 | |
|
| 172 | 39 | imageDataSize=org.jaudiotagger.audio.generic.Utils.getIntLE(this.getRawContent(), 1, 2); |
| 173 | |
|
| 174 | |
|
| 175 | 39 | int count=5; |
| 176 | 39 | mimeType=null; |
| 177 | 39 | description =null; |
| 178 | 39 | int endOfMimeType=0; |
| 179 | |
|
| 180 | 693 | while(count<this.getRawContent().length - 1) |
| 181 | |
{ |
| 182 | 693 | if(getRawContent()[count]==0 && getRawContent()[count+1]==0 ) |
| 183 | |
{ |
| 184 | 78 | if(mimeType==null) |
| 185 | |
{ |
| 186 | 39 | mimeType = new String(getRawContent(),5,(count)-5,"UTF-16LE"); |
| 187 | 39 | endOfMimeType=count+2; |
| 188 | |
} |
| 189 | 39 | else if(description ==null) |
| 190 | |
{ |
| 191 | 39 | description = new String(getRawContent(),endOfMimeType,count - endOfMimeType,"UTF-16LE"); |
| 192 | 39 | endOfName=count+2; |
| 193 | 39 | break; |
| 194 | |
} |
| 195 | |
} |
| 196 | 654 | count+=2; |
| 197 | |
} |
| 198 | 39 | } |
| 199 | |
|
| 200 | |
public String getMimeType() |
| 201 | |
{ |
| 202 | 10 | return mimeType; |
| 203 | |
} |
| 204 | |
|
| 205 | |
public String getDescription() |
| 206 | |
{ |
| 207 | 10 | return description; |
| 208 | |
} |
| 209 | |
|
| 210 | |
public int getPictureType() |
| 211 | |
{ |
| 212 | 11 | return pictureType; |
| 213 | |
} |
| 214 | |
|
| 215 | |
public int getImageDataSize() |
| 216 | |
{ |
| 217 | 3 | return imageDataSize; |
| 218 | |
} |
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
public byte[] getRawImageData() |
| 225 | |
{ |
| 226 | 25 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 227 | 25 | baos.write(getRawContent(),endOfName,getRawContent().length - endOfName); |
| 228 | 25 | return baos.toByteArray(); |
| 229 | |
} |
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
public BufferedImage getImage() throws IOException |
| 237 | |
{ |
| 238 | 15 | BufferedImage bi = ImageIO.read(ImageIO |
| 239 | |
.createImageInputStream(new ByteArrayInputStream(getRawImageData()))); |
| 240 | 15 | return bi; |
| 241 | |
} |
| 242 | |
} |