| 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.jpeg; |
| 22 |
|
| 23 |
import com.drew.imaging.jpeg.JpegSegmentMetadataReader; |
| 24 |
import com.drew.imaging.jpeg.JpegSegmentType; |
| 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.jpeg.HuffmanTablesDirectory.HuffmanTable; |
| 30 |
import com.drew.metadata.jpeg.HuffmanTablesDirectory.HuffmanTable.HuffmanTableClass; |
| 31 |
import java.io.IOException; |
| 32 |
import java.util.Collections; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class JpegDhtReader implements JpegSegmentMetadataReader |
| 40 |
{ |
| 41 |
@NotNull |
| 42 |
public Iterable<JpegSegmentType> getSegmentTypes() |
| 43 |
{ |
| 44 |
return Collections.singletonList(JpegSegmentType.DHT); |
| 45 |
} |
| 46 |
|
| 47 |
public void readJpegSegments(@NotNull Iterable<byte[]> segments, @NotNull Metadata metadata, @NotNull JpegSegmentType segmentType) |
| 48 |
{ |
| 49 |
for (byte[] segmentBytes : segments) { |
| 50 |
extract(new SequentialByteArrayReader(segmentBytes), metadata); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public void extract(@NotNull final SequentialReader reader, @NotNull final Metadata metadata) |
| 59 |
{ |
| 60 |
HuffmanTablesDirectory directory = metadata.getFirstDirectoryOfType(HuffmanTablesDirectory.class); |
| 61 |
if (directory == null) { |
| 62 |
directory = new HuffmanTablesDirectory(); |
| 63 |
metadata.addDirectory(directory); |
| 64 |
} |
| 65 |
|
| 66 |
try { |
| 67 |
while (reader.available() > 0) { |
| 68 |
byte header = reader.getByte(); |
| 69 |
HuffmanTableClass tableClass = HuffmanTableClass.typeOf((header & 0xF0) >> 4); |
| 70 |
int tableDestinationId = header & 0xF; |
| 71 |
|
| 72 |
byte[] lBytes = getBytes(reader, 16); |
| 73 |
int vCount = 0; |
| 74 |
for (byte b : lBytes) { |
| 75 |
vCount += (b & 0xFF); |
| 76 |
} |
| 77 |
byte[] vBytes = getBytes(reader, vCount); |
| 78 |
directory.getTables().add(new HuffmanTable(tableClass, tableDestinationId, lBytes, vBytes)); |
| 79 |
} |
| 80 |
} catch (IOException me) { |
| 81 |
directory.addError(me.getMessage()); |
| 82 |
} |
| 83 |
|
| 84 |
directory.setInt(HuffmanTablesDirectory.TAG_NUMBER_OF_TABLES, directory.getTables().size()); |
| 85 |
} |
| 86 |
|
| 87 |
private byte[] getBytes(@NotNull final SequentialReader reader, int count) throws IOException { |
| 88 |
byte[] bytes = new byte[count]; |
| 89 |
for (int i = 0; i < count; i++) { |
| 90 |
byte b = reader.getByte(); |
| 91 |
if ((b & 0xFF) == 0xFF) { |
| 92 |
byte stuffing = reader.getByte(); |
| 93 |
if (stuffing != 0x00) { |
| 94 |
throw new IOException("Marker " + JpegSegmentType.fromByte(stuffing) + " found inside DHT segment"); |
| 95 |
} |
| 96 |
} |
| 97 |
bytes[i] = b; |
| 98 |
} |
| 99 |
return bytes; |
| 100 |
} |
| 101 |
} |