| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package com.drew.metadata.bmp; |
| 22 |
|
| 23 |
import com.drew.lang.ByteArrayReader; |
| 24 |
import com.drew.lang.Charsets; |
| 25 |
import com.drew.lang.SequentialReader; |
| 26 |
import com.drew.lang.annotations.NotNull; |
| 27 |
import com.drew.metadata.Directory; |
| 28 |
import com.drew.metadata.ErrorDirectory; |
| 29 |
import com.drew.metadata.Metadata; |
| 30 |
import com.drew.metadata.MetadataException; |
| 31 |
import com.drew.metadata.bmp.BmpHeaderDirectory.ColorSpaceType; |
| 32 |
import com.drew.metadata.icc.IccReader; |
| 33 |
|
| 34 |
import java.io.IOException; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class BmpReader |
| 52 |
{ |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public static final int BITMAP = 0x4D42; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static final int OS2_BITMAP_ARRAY = 0x4142; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public static final int OS2_ICON = 0x4349; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public static final int OS2_COLOR_ICON = 0x4943; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static final int OS2_COLOR_POINTER = 0x5043; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public static final int OS2_POINTER = 0x5450; |
| 78 |
|
| 79 |
public void extract(@NotNull final SequentialReader reader, final @NotNull Metadata metadata) |
| 80 |
{ |
| 81 |
reader.setMotorolaByteOrder(false); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
readFileHeader(reader, metadata, true); |
| 89 |
} |
| 90 |
|
| 91 |
protected void readFileHeader(@NotNull final SequentialReader reader, final @NotNull Metadata metadata, boolean allowArray) { |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
final int magicNumber; |
| 117 |
try { |
| 118 |
magicNumber = reader.getUInt16(); |
| 119 |
} catch (IOException e) { |
| 120 |
metadata.addDirectory(new ErrorDirectory("Couldn't determine bitmap type: " + e.getMessage())); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
Directory directory = null; |
| 125 |
try { |
| 126 |
switch (magicNumber) { |
| 127 |
case OS2_BITMAP_ARRAY: |
| 128 |
if (!allowArray) { |
| 129 |
addError("Invalid bitmap file - nested arrays not allowed", metadata); |
| 130 |
return; |
| 131 |
} |
| 132 |
reader.skip(4); |
| 133 |
long nextHeaderOffset = reader.getUInt32(); |
| 134 |
reader.skip(2 + 2); |
| 135 |
readFileHeader(reader, metadata, false); |
| 136 |
if (nextHeaderOffset == 0) { |
| 137 |
return; |
| 138 |
} |
| 139 |
if (reader.getPosition() > nextHeaderOffset) { |
| 140 |
addError("Invalid next header offset", metadata); |
| 141 |
return; |
| 142 |
} |
| 143 |
reader.skip(nextHeaderOffset - reader.getPosition()); |
| 144 |
readFileHeader(reader, metadata, true); |
| 145 |
break; |
| 146 |
case BITMAP: |
| 147 |
case OS2_ICON: |
| 148 |
case OS2_COLOR_ICON: |
| 149 |
case OS2_COLOR_POINTER: |
| 150 |
case OS2_POINTER: |
| 151 |
directory = new BmpHeaderDirectory(); |
| 152 |
metadata.addDirectory(directory); |
| 153 |
directory.setInt(BmpHeaderDirectory.TAG_BITMAP_TYPE, magicNumber); |
| 154 |
|
| 155 |
reader.skip(4 + 2 + 2 + 4); |
| 156 |
readBitmapHeader(reader, (BmpHeaderDirectory) directory, metadata); |
| 157 |
break; |
| 158 |
default: |
| 159 |
metadata.addDirectory(new ErrorDirectory("Invalid BMP magic number 0x" + Integer.toHexString(magicNumber))); |
| 160 |
return; |
| 161 |
} |
| 162 |
} catch (IOException e) { |
| 163 |
if (directory == null) { |
| 164 |
addError("Unable to read BMP file header", metadata); |
| 165 |
} else { |
| 166 |
directory.addError("Unable to read BMP file header"); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
protected void readBitmapHeader(@NotNull final SequentialReader reader, final @NotNull BmpHeaderDirectory directory, final @NotNull Metadata metadata) { |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
try { |
| 265 |
int bitmapType = directory.getInt(BmpHeaderDirectory.TAG_BITMAP_TYPE); |
| 266 |
long headerOffset = reader.getPosition(); |
| 267 |
int headerSize = reader.getInt32(); |
| 268 |
|
| 269 |
directory.setInt(BmpHeaderDirectory.TAG_HEADER_SIZE, headerSize); |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
if (headerSize == 12 && bitmapType == BITMAP) { |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
directory.setInt(BmpHeaderDirectory.TAG_IMAGE_WIDTH, reader.getInt16()); |
| 294 |
directory.setInt(BmpHeaderDirectory.TAG_IMAGE_HEIGHT, reader.getInt16()); |
| 295 |
directory.setInt(BmpHeaderDirectory.TAG_COLOUR_PLANES, reader.getUInt16()); |
| 296 |
directory.setInt(BmpHeaderDirectory.TAG_BITS_PER_PIXEL, reader.getUInt16()); |
| 297 |
} else if (headerSize == 12) { |
| 298 |
directory.setInt(BmpHeaderDirectory.TAG_IMAGE_WIDTH, reader.getUInt16()); |
| 299 |
directory.setInt(BmpHeaderDirectory.TAG_IMAGE_HEIGHT, reader.getUInt16()); |
| 300 |
directory.setInt(BmpHeaderDirectory.TAG_COLOUR_PLANES, reader.getUInt16()); |
| 301 |
directory.setInt(BmpHeaderDirectory.TAG_BITS_PER_PIXEL, reader.getUInt16()); |
| 302 |
} else if (headerSize == 16 || headerSize == 64) { |
| 303 |
directory.setInt(BmpHeaderDirectory.TAG_IMAGE_WIDTH, reader.getInt32()); |
| 304 |
directory.setInt(BmpHeaderDirectory.TAG_IMAGE_HEIGHT, reader.getInt32()); |
| 305 |
directory.setInt(BmpHeaderDirectory.TAG_COLOUR_PLANES, reader.getUInt16()); |
| 306 |
directory.setInt(BmpHeaderDirectory.TAG_BITS_PER_PIXEL, reader.getUInt16()); |
| 307 |
if (headerSize > 16) { |
| 308 |
directory.setInt(BmpHeaderDirectory.TAG_COMPRESSION, reader.getInt32()); |
| 309 |
reader.skip(4); |
| 310 |
directory.setInt(BmpHeaderDirectory.TAG_X_PIXELS_PER_METER, reader.getInt32()); |
| 311 |
directory.setInt(BmpHeaderDirectory.TAG_Y_PIXELS_PER_METER, reader.getInt32()); |
| 312 |
directory.setInt(BmpHeaderDirectory.TAG_PALETTE_COLOUR_COUNT, reader.getInt32()); |
| 313 |
directory.setInt(BmpHeaderDirectory.TAG_IMPORTANT_COLOUR_COUNT, reader.getInt32()); |
| 314 |
reader.skip( |
| 315 |
2 + |
| 316 |
2 + |
| 317 |
2 |
| 318 |
); |
| 319 |
directory.setInt(BmpHeaderDirectory.TAG_RENDERING, reader.getUInt16()); |
| 320 |
reader.skip(4 + 4); |
| 321 |
directory.setInt(BmpHeaderDirectory.TAG_COLOR_ENCODING, reader.getInt32()); |
| 322 |
reader.skip(4); |
| 323 |
} |
| 324 |
} else if ( |
| 325 |
headerSize == 40 || headerSize == 52 || headerSize == 56 || |
| 326 |
headerSize == 108 || headerSize == 124 |
| 327 |
) { |
| 328 |
directory.setInt(BmpHeaderDirectory.TAG_IMAGE_WIDTH, reader.getInt32()); |
| 329 |
directory.setInt(BmpHeaderDirectory.TAG_IMAGE_HEIGHT, reader.getInt32()); |
| 330 |
directory.setInt(BmpHeaderDirectory.TAG_COLOUR_PLANES, reader.getUInt16()); |
| 331 |
directory.setInt(BmpHeaderDirectory.TAG_BITS_PER_PIXEL, reader.getUInt16()); |
| 332 |
directory.setInt(BmpHeaderDirectory.TAG_COMPRESSION, reader.getInt32()); |
| 333 |
|
| 334 |
reader.skip(4); |
| 335 |
directory.setInt(BmpHeaderDirectory.TAG_X_PIXELS_PER_METER, reader.getInt32()); |
| 336 |
directory.setInt(BmpHeaderDirectory.TAG_Y_PIXELS_PER_METER, reader.getInt32()); |
| 337 |
directory.setInt(BmpHeaderDirectory.TAG_PALETTE_COLOUR_COUNT, reader.getInt32()); |
| 338 |
directory.setInt(BmpHeaderDirectory.TAG_IMPORTANT_COLOUR_COUNT, reader.getInt32()); |
| 339 |
if (headerSize == 40) { |
| 340 |
return; |
| 341 |
} |
| 342 |
directory.setLong(BmpHeaderDirectory.TAG_RED_MASK, reader.getUInt32()); |
| 343 |
directory.setLong(BmpHeaderDirectory.TAG_GREEN_MASK, reader.getUInt32()); |
| 344 |
directory.setLong(BmpHeaderDirectory.TAG_BLUE_MASK, reader.getUInt32()); |
| 345 |
if (headerSize == 52) { |
| 346 |
return; |
| 347 |
} |
| 348 |
directory.setLong(BmpHeaderDirectory.TAG_ALPHA_MASK, reader.getUInt32()); |
| 349 |
if (headerSize == 56) { |
| 350 |
return; |
| 351 |
} |
| 352 |
long csType = reader.getUInt32(); |
| 353 |
directory.setLong(BmpHeaderDirectory.TAG_COLOR_SPACE_TYPE, csType); |
| 354 |
reader.skip(36); |
| 355 |
directory.setLong(BmpHeaderDirectory.TAG_GAMMA_RED, reader.getUInt32()); |
| 356 |
directory.setLong(BmpHeaderDirectory.TAG_GAMMA_GREEN, reader.getUInt32()); |
| 357 |
directory.setLong(BmpHeaderDirectory.TAG_GAMMA_BLUE, reader.getUInt32()); |
| 358 |
if (headerSize == 108) { |
| 359 |
return; |
| 360 |
} |
| 361 |
directory.setInt(BmpHeaderDirectory.TAG_INTENT, reader.getInt32()); |
| 362 |
if (csType == ColorSpaceType.PROFILE_EMBEDDED.getValue() || csType == ColorSpaceType.PROFILE_LINKED.getValue()) { |
| 363 |
long profileOffset = reader.getUInt32(); |
| 364 |
int profileSize = reader.getInt32(); |
| 365 |
if (profileSize < 0) { |
| 366 |
directory.addError("Invalid profile size " + profileSize); |
| 367 |
return; |
| 368 |
} |
| 369 |
if (reader.getPosition() > headerOffset + profileOffset) { |
| 370 |
directory.addError("Invalid profile data offset 0x" + Long.toHexString(headerOffset + profileOffset)); |
| 371 |
return; |
| 372 |
} |
| 373 |
reader.skip(headerOffset + profileOffset - reader.getPosition()); |
| 374 |
if (csType == ColorSpaceType.PROFILE_LINKED.getValue()) { |
| 375 |
directory.setString(BmpHeaderDirectory.TAG_LINKED_PROFILE, reader.getNullTerminatedString(profileSize, Charsets.WINDOWS_1252)); |
| 376 |
} else { |
| 377 |
ByteArrayReader randomAccessReader = new ByteArrayReader(reader.getBytes(profileSize)); |
| 378 |
new IccReader().extract(randomAccessReader, metadata, directory); |
| 379 |
} |
| 380 |
} else { |
| 381 |
reader.skip( |
| 382 |
4 + |
| 383 |
4 + |
| 384 |
4 |
| 385 |
); |
| 386 |
} |
| 387 |
} else { |
| 388 |
directory.addError("Unexpected DIB header size: " + headerSize); |
| 389 |
} |
| 390 |
} catch (IOException e) { |
| 391 |
directory.addError("Unable to read BMP header"); |
| 392 |
} catch (MetadataException e) { |
| 393 |
directory.addError("Internal error"); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
protected void addError(@NotNull String errorMessage, final @NotNull Metadata metadata) { |
| 398 |
ErrorDirectory directory = metadata.getFirstDirectoryOfType(ErrorDirectory.class); |
| 399 |
if (directory == null) { |
| 400 |
metadata.addDirectory(new ErrorDirectory(errorMessage)); |
| 401 |
} else { |
| 402 |
directory.addError(errorMessage); |
| 403 |
} |
| 404 |
} |
| 405 |
} |