| 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.ErrorDirectory; |
| 29 |
import com.drew.metadata.Metadata; |
| 30 |
|
| 31 |
import java.io.IOException; |
| 32 |
import java.util.Collections; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class JpegDnlReader implements JpegSegmentMetadataReader |
| 40 |
{ |
| 41 |
@NotNull |
| 42 |
public Iterable<JpegSegmentType> getSegmentTypes() |
| 43 |
{ |
| 44 |
return Collections.singletonList(JpegSegmentType.DNL); |
| 45 |
} |
| 46 |
|
| 47 |
public void readJpegSegments(@NotNull Iterable<byte[]> segments, @NotNull Metadata metadata, @NotNull JpegSegmentType segmentType) |
| 48 |
{ |
| 49 |
for (byte[] segmentBytes : segments) { |
| 50 |
extract(segmentBytes, metadata, segmentType); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
public void extract(byte[] segmentBytes, Metadata metadata, JpegSegmentType segmentType) |
| 55 |
{ |
| 56 |
JpegDirectory directory = metadata.getFirstDirectoryOfType(JpegDirectory.class); |
| 57 |
if (directory == null) { |
| 58 |
ErrorDirectory errorDirectory = new ErrorDirectory(); |
| 59 |
metadata.addDirectory(errorDirectory); |
| 60 |
errorDirectory.addError("DNL segment found without SOFx - illegal JPEG format"); |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
SequentialReader reader = new SequentialByteArrayReader(segmentBytes); |
| 65 |
|
| 66 |
try { |
| 67 |
|
| 68 |
Integer i = directory.getInteger(JpegDirectory.TAG_IMAGE_HEIGHT); |
| 69 |
if (i == null || i == 0) { |
| 70 |
directory.setInt(JpegDirectory.TAG_IMAGE_HEIGHT, reader.getUInt16()); |
| 71 |
} |
| 72 |
} catch (IOException ex) { |
| 73 |
directory.addError(ex.getMessage()); |
| 74 |
} |
| 75 |
} |
| 76 |
} |