| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
package org.jaudiotagger.tag.id3.valuepair; |
| 25 | |
|
| 26 | |
import java.util.HashMap; |
| 27 | |
import java.util.Map; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | 0 | public class ImageFormats |
| 34 | |
{ |
| 35 | |
public static final String V22_JPG_FORMAT = "JPG"; |
| 36 | |
public static final String V22_PNG_FORMAT = "PNG"; |
| 37 | |
public static final String V22_GIF_FORMAT = "GIF"; |
| 38 | |
public static final String V22_BMP_FORMAT = "COVERART_BMP"; |
| 39 | |
|
| 40 | |
public static final String MIME_TYPE_JPEG = "image/jpeg"; |
| 41 | |
public static final String MIME_TYPE_PNG = "image/png"; |
| 42 | |
public static final String MIME_TYPE_GIF = "image/gif"; |
| 43 | |
public static final String MIME_TYPE_BMP = "image/bmp"; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public static final String MIME_TYPE_JPG = "image/jpg"; |
| 49 | |
|
| 50 | 10 | private static Map<String, String> imageFormatsToMimeType = new HashMap<String, String>(); |
| 51 | 10 | private static Map<String, String> imageMimeTypeToFormat = new HashMap<String, String>(); |
| 52 | |
|
| 53 | |
static |
| 54 | |
{ |
| 55 | 10 | imageFormatsToMimeType.put(V22_JPG_FORMAT, MIME_TYPE_JPEG); |
| 56 | 10 | imageFormatsToMimeType.put(V22_PNG_FORMAT, MIME_TYPE_PNG); |
| 57 | 10 | imageFormatsToMimeType.put(V22_GIF_FORMAT, MIME_TYPE_GIF); |
| 58 | 10 | imageFormatsToMimeType.put(V22_BMP_FORMAT, MIME_TYPE_BMP); |
| 59 | |
String value; |
| 60 | 10 | for (String key : imageFormatsToMimeType.keySet()) |
| 61 | |
{ |
| 62 | 40 | value = imageFormatsToMimeType.get(key); |
| 63 | 40 | imageMimeTypeToFormat.put(value, key); |
| 64 | |
} |
| 65 | |
|
| 66 | |
|
| 67 | 10 | imageMimeTypeToFormat.put(MIME_TYPE_JPG, V22_JPG_FORMAT); |
| 68 | 10 | } |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
public static String getMimeTypeForFormat(String format) |
| 74 | |
{ |
| 75 | 15 | return imageFormatsToMimeType.get(format); |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public static String getFormatForMimeType(String mimeType) |
| 82 | |
{ |
| 83 | 6 | return imageMimeTypeToFormat.get(mimeType); |
| 84 | |
} |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
public static boolean binaryDataIsPngFormat(byte[] data) |
| 91 | |
{ |
| 92 | |
|
| 93 | 22 | if ((0x89 == (data[0] & 0xff)) && (0x50 == (data[1] & 0xff)) && (0x4E == (data[2] & 0xff)) && (0x47 == (data[3] & 0xff))) |
| 94 | |
{ |
| 95 | 22 | return true; |
| 96 | |
} |
| 97 | 0 | return false; |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public static boolean binaryDataIsJpgFormat(byte[] data) |
| 105 | |
{ |
| 106 | |
|
| 107 | 0 | if ((0xff == (data[0] & 0xff)) && (0xd8 == (data[1] & 0xff)) && (0xff == (data[2] & 0xff)) && (0xff == (data[3] & 0xe0))) |
| 108 | |
{ |
| 109 | 0 | return true; |
| 110 | |
} |
| 111 | 0 | return false; |
| 112 | |
} |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public static boolean binaryDataIsGifFormat(byte[] data) |
| 119 | |
{ |
| 120 | |
|
| 121 | 0 | if ((0x47 == (data[0] & 0xff)) && (0x49 == (data[1] & 0xff)) && (0x46 == (data[2] & 0xff))) |
| 122 | |
{ |
| 123 | 0 | return true; |
| 124 | |
} |
| 125 | 0 | return false; |
| 126 | |
} |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
public static boolean binaryDataIsBmpFormat(byte[] data) |
| 133 | |
{ |
| 134 | |
|
| 135 | 0 | if ((0x42 == (data[0] & 0xff)) && (0x4d == (data[1] & 0xff)) && (0x3c == (data[2] & 0xff))) |
| 136 | |
{ |
| 137 | 0 | return true; |
| 138 | |
} |
| 139 | 0 | return false; |
| 140 | |
} |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public static String getMimeTypeForBinarySignature(byte[] data) |
| 148 | |
{ |
| 149 | 11 | if(binaryDataIsPngFormat(data)) |
| 150 | |
{ |
| 151 | 11 | return MIME_TYPE_PNG; |
| 152 | |
} |
| 153 | 0 | else if(binaryDataIsJpgFormat(data)) |
| 154 | |
{ |
| 155 | 0 | return MIME_TYPE_JPEG; |
| 156 | |
} |
| 157 | 0 | else if(binaryDataIsGifFormat(data)) |
| 158 | |
{ |
| 159 | 0 | return MIME_TYPE_GIF; |
| 160 | |
} |
| 161 | 0 | else if(binaryDataIsBmpFormat(data)) |
| 162 | |
{ |
| 163 | 0 | return MIME_TYPE_BMP; |
| 164 | |
} |
| 165 | |
else |
| 166 | |
{ |
| 167 | 0 | return null; |
| 168 | |
} |
| 169 | |
} |
| 170 | |
} |