source: josm/trunk/src/com/drew/metadata/jpeg/JpegDhtReader.java@ 15217

Last change on this file since 15217 was 15217, checked in by Don-vip, 5 years ago

see #17848 - update to metadata-extractor 2.12.0

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
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 */
21package com.drew.metadata.jpeg;
22
23import com.drew.imaging.jpeg.JpegSegmentMetadataReader;
24import com.drew.imaging.jpeg.JpegSegmentType;
25import com.drew.lang.SequentialByteArrayReader;
26import com.drew.lang.SequentialReader;
27import com.drew.lang.annotations.NotNull;
28import com.drew.metadata.Metadata;
29import com.drew.metadata.jpeg.HuffmanTablesDirectory.HuffmanTable;
30import com.drew.metadata.jpeg.HuffmanTablesDirectory.HuffmanTable.HuffmanTableClass;
31import java.io.IOException;
32import java.util.Collections;
33
34/**
35 * Reader for JPEG Huffman tables, found in the DHT JPEG segment.
36 *
37 * @author Nadahar
38 */
39public 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}
Note: See TracBrowser for help on using the repository browser.