| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package com.drew.imaging.png; |
| 22 |
|
| 23 |
import com.drew.lang.annotations.NotNull; |
| 24 |
|
| 25 |
import java.io.UnsupportedEncodingException; |
| 26 |
import java.util.Arrays; |
| 27 |
import java.util.HashSet; |
| 28 |
import java.util.Set; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class PngChunkType |
| 34 |
{ |
| 35 |
private static final Set<String> _identifiersAllowingMultiples |
| 36 |
= new HashSet<String>(Arrays.asList("IDAT", "sPLT", "iTXt", "tEXt", "zTXt")); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static final PngChunkType IHDR; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static final PngChunkType PLTE; |
| 72 |
public static final PngChunkType IDAT; |
| 73 |
public static final PngChunkType IEND; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static final PngChunkType cHRM; |
| 79 |
public static final PngChunkType gAMA; |
| 80 |
public static final PngChunkType iCCP; |
| 81 |
public static final PngChunkType sBIT; |
| 82 |
public static final PngChunkType sRGB; |
| 83 |
public static final PngChunkType bKGD; |
| 84 |
public static final PngChunkType hIST; |
| 85 |
public static final PngChunkType tRNS; |
| 86 |
public static final PngChunkType pHYs; |
| 87 |
public static final PngChunkType sPLT; |
| 88 |
public static final PngChunkType tIME; |
| 89 |
public static final PngChunkType iTXt; |
| 90 |
public static final PngChunkType eXIf; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public static final PngChunkType tEXt; |
| 106 |
public static final PngChunkType zTXt; |
| 107 |
|
| 108 |
static { |
| 109 |
try { |
| 110 |
IHDR = new PngChunkType("IHDR"); |
| 111 |
PLTE = new PngChunkType("PLTE"); |
| 112 |
IDAT = new PngChunkType("IDAT", true); |
| 113 |
IEND = new PngChunkType("IEND"); |
| 114 |
cHRM = new PngChunkType("cHRM"); |
| 115 |
gAMA = new PngChunkType("gAMA"); |
| 116 |
iCCP = new PngChunkType("iCCP"); |
| 117 |
sBIT = new PngChunkType("sBIT"); |
| 118 |
sRGB = new PngChunkType("sRGB"); |
| 119 |
bKGD = new PngChunkType("bKGD"); |
| 120 |
hIST = new PngChunkType("hIST"); |
| 121 |
tRNS = new PngChunkType("tRNS"); |
| 122 |
pHYs = new PngChunkType("pHYs"); |
| 123 |
sPLT = new PngChunkType("sPLT", true); |
| 124 |
tIME = new PngChunkType("tIME"); |
| 125 |
iTXt = new PngChunkType("iTXt", true); |
| 126 |
tEXt = new PngChunkType("tEXt", true); |
| 127 |
zTXt = new PngChunkType("zTXt", true); |
| 128 |
eXIf = new PngChunkType("eXIf"); |
| 129 |
} catch (PngProcessingException e) { |
| 130 |
throw new IllegalArgumentException(e); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
private final byte[] _bytes; |
| 135 |
private final boolean _multipleAllowed; |
| 136 |
|
| 137 |
public PngChunkType(@NotNull String identifier) throws PngProcessingException |
| 138 |
{ |
| 139 |
this(identifier, false); |
| 140 |
} |
| 141 |
|
| 142 |
public PngChunkType(@NotNull String identifier, boolean multipleAllowed) throws PngProcessingException |
| 143 |
{ |
| 144 |
_multipleAllowed = multipleAllowed; |
| 145 |
|
| 146 |
try { |
| 147 |
byte[] bytes = identifier.getBytes("ASCII"); |
| 148 |
validateBytes(bytes); |
| 149 |
_bytes = bytes; |
| 150 |
} catch (UnsupportedEncodingException e) { |
| 151 |
throw new IllegalArgumentException("Unable to convert string code to bytes."); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
public PngChunkType(@NotNull byte[] bytes) throws PngProcessingException |
| 156 |
{ |
| 157 |
validateBytes(bytes); |
| 158 |
_bytes = bytes; |
| 159 |
_multipleAllowed = _identifiersAllowingMultiples.contains(getIdentifier()); |
| 160 |
} |
| 161 |
|
| 162 |
private static void validateBytes(byte[] bytes) throws PngProcessingException |
| 163 |
{ |
| 164 |
if (bytes.length != 4) { |
| 165 |
throw new PngProcessingException("PNG chunk type identifier must be four bytes in length"); |
| 166 |
} |
| 167 |
|
| 168 |
for (byte b : bytes) { |
| 169 |
if (!isValidByte(b)) { |
| 170 |
throw new PngProcessingException("PNG chunk type identifier may only contain alphabet characters"); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
public boolean isCritical() |
| 176 |
{ |
| 177 |
return isUpperCase(_bytes[0]); |
| 178 |
} |
| 179 |
|
| 180 |
public boolean isAncillary() |
| 181 |
{ |
| 182 |
return !isCritical(); |
| 183 |
} |
| 184 |
|
| 185 |
public boolean isPrivate() |
| 186 |
{ |
| 187 |
return isUpperCase(_bytes[1]); |
| 188 |
} |
| 189 |
|
| 190 |
public boolean isSafeToCopy() |
| 191 |
{ |
| 192 |
return isLowerCase(_bytes[3]); |
| 193 |
} |
| 194 |
|
| 195 |
public boolean areMultipleAllowed() |
| 196 |
{ |
| 197 |
return _multipleAllowed; |
| 198 |
} |
| 199 |
|
| 200 |
private static boolean isLowerCase(byte b) |
| 201 |
{ |
| 202 |
return (b & (1 << 5)) != 0; |
| 203 |
} |
| 204 |
|
| 205 |
private static boolean isUpperCase(byte b) |
| 206 |
{ |
| 207 |
return (b & (1 << 5)) == 0; |
| 208 |
} |
| 209 |
|
| 210 |
private static boolean isValidByte(byte b) |
| 211 |
{ |
| 212 |
return (b >= 65 && b <= 90) || (b >= 97 && b <= 122); |
| 213 |
} |
| 214 |
|
| 215 |
public String getIdentifier() |
| 216 |
{ |
| 217 |
try { |
| 218 |
return new String(_bytes, "ASCII"); |
| 219 |
} catch (UnsupportedEncodingException e) { |
| 220 |
|
| 221 |
|
| 222 |
assert(false); |
| 223 |
return "Invalid object instance"; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
@Override |
| 228 |
public String toString() |
| 229 |
{ |
| 230 |
return getIdentifier(); |
| 231 |
} |
| 232 |
|
| 233 |
@Override |
| 234 |
public boolean equals(Object o) |
| 235 |
{ |
| 236 |
if (this == o) |
| 237 |
return true; |
| 238 |
|
| 239 |
if (o == null || getClass() != o.getClass()) |
| 240 |
return false; |
| 241 |
|
| 242 |
PngChunkType that = (PngChunkType)o; |
| 243 |
|
| 244 |
return Arrays.equals(_bytes, that._bytes); |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
public int hashCode() |
| 249 |
{ |
| 250 |
return Arrays.hashCode(_bytes); |
| 251 |
} |
| 252 |
} |