1 | /*
|
---|
2 | * Copyright 2002-2019 Drew Noakes and contributors
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | * More information about this project is available at:
|
---|
17 | *
|
---|
18 | * https://drewnoakes.com/code/exif/
|
---|
19 | * https://github.com/drewnoakes/metadata-extractor
|
---|
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 | * Reader for JPEG Huffman tables, found in the DHT JPEG segment.
|
---|
36 | *
|
---|
37 | * @author Nadahar
|
---|
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 | * Performs the DHT tables extraction, adding found tables to the specified
|
---|
56 | * instance of {@link Metadata}.
|
---|
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 | }
|
---|