| 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.ico; |
| 22 |
|
| 23 |
import com.drew.lang.SequentialReader; |
| 24 |
import com.drew.lang.annotations.NotNull; |
| 25 |
import com.drew.metadata.Metadata; |
| 26 |
|
| 27 |
import java.io.IOException; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class IcoReader |
| 38 |
{ |
| 39 |
public void extract(@NotNull final SequentialReader reader, @NotNull final Metadata metadata) |
| 40 |
{ |
| 41 |
reader.setMotorolaByteOrder(false); |
| 42 |
|
| 43 |
int type; |
| 44 |
int imageCount; |
| 45 |
|
| 46 |
|
| 47 |
try { |
| 48 |
int reserved = reader.getUInt16(); |
| 49 |
|
| 50 |
if (reserved != 0) { |
| 51 |
IcoDirectory directory = new IcoDirectory(); |
| 52 |
directory.addError("Invalid header bytes"); |
| 53 |
metadata.addDirectory(directory); |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
type = reader.getUInt16(); |
| 58 |
|
| 59 |
if (type != 1 && type != 2) { |
| 60 |
IcoDirectory directory = new IcoDirectory(); |
| 61 |
directory.addError("Invalid type " + type + " -- expecting 1 or 2"); |
| 62 |
metadata.addDirectory(directory); |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
imageCount = reader.getUInt16(); |
| 67 |
|
| 68 |
if (imageCount == 0) { |
| 69 |
IcoDirectory directory = new IcoDirectory(); |
| 70 |
directory.addError("Image count cannot be zero"); |
| 71 |
metadata.addDirectory(directory); |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
} catch (IOException ex) { |
| 76 |
IcoDirectory directory = new IcoDirectory(); |
| 77 |
directory.addError("Exception reading ICO file metadata: " + ex.getMessage()); |
| 78 |
metadata.addDirectory(directory); |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
for (int imageIndex = 0; imageIndex < imageCount; imageIndex++) { |
| 84 |
IcoDirectory directory = new IcoDirectory(); |
| 85 |
try { |
| 86 |
directory.setInt(IcoDirectory.TAG_IMAGE_TYPE, type); |
| 87 |
|
| 88 |
|
| 89 |
int imageWidth = reader.getUInt8(); |
| 90 |
int imageHeight = reader.getUInt8(); |
| 91 |
directory.setInt(IcoDirectory.TAG_IMAGE_WIDTH, imageWidth == 0 ? 256 : imageWidth); |
| 92 |
directory.setInt(IcoDirectory.TAG_IMAGE_HEIGHT, imageHeight == 0 ? 256 : imageHeight); |
| 93 |
directory.setInt(IcoDirectory.TAG_COLOUR_PALETTE_SIZE, reader.getUInt8()); |
| 94 |
|
| 95 |
reader.getUInt8(); |
| 96 |
if (type == 1) { |
| 97 |
|
| 98 |
directory.setInt(IcoDirectory.TAG_COLOUR_PLANES, reader.getUInt16()); |
| 99 |
directory.setInt(IcoDirectory.TAG_BITS_PER_PIXEL, reader.getUInt16()); |
| 100 |
} else { |
| 101 |
|
| 102 |
directory.setInt(IcoDirectory.TAG_CURSOR_HOTSPOT_X, reader.getUInt16()); |
| 103 |
directory.setInt(IcoDirectory.TAG_CURSOR_HOTSPOT_Y, reader.getUInt16()); |
| 104 |
} |
| 105 |
directory.setLong(IcoDirectory.TAG_IMAGE_SIZE_BYTES, reader.getUInt32()); |
| 106 |
directory.setLong(IcoDirectory.TAG_IMAGE_OFFSET_BYTES, reader.getUInt32()); |
| 107 |
} catch (IOException ex) { |
| 108 |
directory.addError("Exception reading ICO file metadata: " + ex.getMessage()); |
| 109 |
} |
| 110 |
metadata.addDirectory(directory); |
| 111 |
} |
| 112 |
} |
| 113 |
} |