| 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.heif; |
| 22 |
|
| 23 |
import com.drew.imaging.heif.HeifHandler; |
| 24 |
import com.drew.lang.RandomAccessStreamReader; |
| 25 |
import com.drew.lang.SequentialByteArrayReader; |
| 26 |
import com.drew.lang.SequentialReader; |
| 27 |
import com.drew.lang.annotations.NotNull; |
| 28 |
import com.drew.metadata.Metadata; |
| 29 |
import com.drew.metadata.exif.ExifReader; |
| 30 |
import com.drew.metadata.heif.boxes.*; |
| 31 |
|
| 32 |
import java.io.ByteArrayInputStream; |
| 33 |
import java.io.IOException; |
| 34 |
import java.util.Arrays; |
| 35 |
import java.util.Collections; |
| 36 |
import java.util.HashSet; |
| 37 |
import java.util.Set; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class HeifPictureHandler extends HeifHandler<HeifDirectory> |
| 43 |
{ |
| 44 |
private static final Set<String> boxesCanProcess = new HashSet<String>(Arrays.asList( |
| 45 |
HeifBoxTypes.BOX_ITEM_PROTECTION, |
| 46 |
HeifBoxTypes.BOX_PRIMARY_ITEM, |
| 47 |
HeifBoxTypes.BOX_ITEM_INFO, |
| 48 |
HeifBoxTypes.BOX_ITEM_LOCATION, |
| 49 |
HeifBoxTypes.BOX_IMAGE_SPATIAL_EXTENTS, |
| 50 |
HeifBoxTypes.BOX_AUXILIARY_TYPE_PROPERTY, |
| 51 |
HeifBoxTypes.BOX_IMAGE_ROTATION, |
| 52 |
HeifBoxTypes.BOX_COLOUR_INFO, |
| 53 |
HeifBoxTypes.BOX_PIXEL_INFORMATION |
| 54 |
)); |
| 55 |
|
| 56 |
private static final Set<String> itemsCanProcess = new HashSet<String>(Collections.singletonList( |
| 57 |
HeifItemTypes.ITEM_EXIF |
| 58 |
)); |
| 59 |
|
| 60 |
private static final Set<String> containersCanProcess = new HashSet<String>(Arrays.asList( |
| 61 |
HeifContainerTypes.BOX_IMAGE_PROPERTY, |
| 62 |
HeifContainerTypes.BOX_ITEM_PROPERTY, |
| 63 |
HeifContainerTypes.BOX_MEDIA_DATA |
| 64 |
)); |
| 65 |
|
| 66 |
ItemProtectionBox itemProtectionBox; |
| 67 |
PrimaryItemBox primaryItemBox; |
| 68 |
ItemInfoBox itemInfoBox; |
| 69 |
ItemLocationBox itemLocationBox; |
| 70 |
|
| 71 |
public HeifPictureHandler(Metadata metadata) |
| 72 |
{ |
| 73 |
super(metadata); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
protected boolean shouldAcceptBox(@NotNull Box box) |
| 78 |
{ |
| 79 |
return boxesCanProcess.contains(box.type); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
protected boolean shouldAcceptContainer(@NotNull Box box) |
| 84 |
{ |
| 85 |
return containersCanProcess.contains(box.type); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
protected HeifHandler<?> processBox(@NotNull Box box, @NotNull byte[] payload) throws IOException |
| 90 |
{ |
| 91 |
SequentialReader reader = new SequentialByteArrayReader(payload); |
| 92 |
if (box.type.equals(HeifBoxTypes.BOX_ITEM_PROTECTION)) { |
| 93 |
itemProtectionBox = new ItemProtectionBox(reader, box); |
| 94 |
} else if (box.type.equals(HeifBoxTypes.BOX_PRIMARY_ITEM)) { |
| 95 |
primaryItemBox = new PrimaryItemBox(reader, box); |
| 96 |
} else if (box.type.equals(HeifBoxTypes.BOX_ITEM_INFO)) { |
| 97 |
itemInfoBox = new ItemInfoBox(reader, box); |
| 98 |
itemInfoBox.addMetadata(directory); |
| 99 |
} else if (box.type.equals(HeifBoxTypes.BOX_ITEM_LOCATION)) { |
| 100 |
itemLocationBox = new ItemLocationBox(reader, box); |
| 101 |
} else if (box.type.equals(HeifBoxTypes.BOX_IMAGE_SPATIAL_EXTENTS)) { |
| 102 |
ImageSpatialExtentsProperty imageSpatialExtentsProperty = new ImageSpatialExtentsProperty(reader, box); |
| 103 |
imageSpatialExtentsProperty.addMetadata(directory); |
| 104 |
} else if (box.type.equals(HeifBoxTypes.BOX_AUXILIARY_TYPE_PROPERTY)) { |
| 105 |
AuxiliaryTypeProperty auxiliaryTypeProperty = new AuxiliaryTypeProperty(reader, box); |
| 106 |
} else if (box.type.equals(HeifBoxTypes.BOX_IMAGE_ROTATION)) { |
| 107 |
ImageRotationBox imageRotationBox = new ImageRotationBox(reader, box); |
| 108 |
imageRotationBox.addMetadata(directory); |
| 109 |
} else if (box.type.equals(HeifBoxTypes.BOX_COLOUR_INFO)) { |
| 110 |
ColourInformationBox colourInformationBox = new ColourInformationBox(reader, box, metadata); |
| 111 |
colourInformationBox.addMetadata(directory); |
| 112 |
} else if (box.type.equals(HeifBoxTypes.BOX_PIXEL_INFORMATION)) { |
| 113 |
PixelInformationBox pixelInformationBox = new PixelInformationBox(reader, box); |
| 114 |
pixelInformationBox.addMetadata(directory); |
| 115 |
} |
| 116 |
return this; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
protected void processContainer(@NotNull Box box, @NotNull SequentialReader reader) throws IOException |
| 121 |
{ |
| 122 |
if (box.type.equals(HeifContainerTypes.BOX_MEDIA_DATA) && itemInfoBox != null && itemLocationBox != null) { |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
for (ItemLocationBox.Extent extent : itemLocationBox.getExtents()) { |
| 127 |
ItemInfoBox.ItemInfoEntry infoEntry = itemInfoBox.getEntry(extent.getItemId()); |
| 128 |
long bytesToSkip = extent.getOffset() - reader.getPosition(); |
| 129 |
if (bytesToSkip > 0) { |
| 130 |
reader.skip(bytesToSkip); |
| 131 |
} |
| 132 |
if (shouldHandleItem(infoEntry)) { |
| 133 |
handleItem(infoEntry, new SequentialByteArrayReader(reader.getBytes((int) extent.getLength()))); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
private boolean shouldHandleItem(ItemInfoBox.ItemInfoEntry infoEntry) { |
| 140 |
return itemsCanProcess.contains(infoEntry.getItemType()); |
| 141 |
} |
| 142 |
|
| 143 |
private void handleItem(@NotNull ItemInfoBox.ItemInfoEntry entry, |
| 144 |
@NotNull SequentialByteArrayReader payloadReader) throws IOException { |
| 145 |
if (entry.getItemType().equals(HeifItemTypes.ITEM_EXIF)) { |
| 146 |
|
| 147 |
long tiffHeaderOffset = payloadReader.getUInt32(); |
| 148 |
if (tiffHeaderOffset > payloadReader.available()) { |
| 149 |
|
| 150 |
return; |
| 151 |
} |
| 152 |
payloadReader.skip(tiffHeaderOffset); |
| 153 |
ByteArrayInputStream tiffStream = new ByteArrayInputStream(payloadReader.getBytes(payloadReader.available())); |
| 154 |
new ExifReader().extract(new RandomAccessStreamReader(tiffStream), metadata); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
@Override |
| 159 |
protected HeifDirectory getDirectory() |
| 160 |
{ |
| 161 |
return new HeifDirectory(); |
| 162 |
} |
| 163 |
} |