Debian Codegraph @

src/com/drew/metadata/jfif/JfifReader.java

raw symbols SCIP
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.jfif;
22
23 import com.drew.imaging.jpeg.JpegSegmentMetadataReader;
24 import com.drew.imaging.jpeg.JpegSegmentType;
25 import com.drew.lang.ByteArrayReader;
26 import com.drew.lang.RandomAccessReader;
27 import com.drew.lang.annotations.NotNull;
28 import com.drew.metadata.Metadata;
29 import com.drew.metadata.MetadataReader;
30
31 import java.io.IOException;
32 import java.util.Collections;
33
34 /**
35 * Reader for JFIF data, found in the APP0 JPEG segment.
36 *
37 * <ul>
38 * <li>http://en.wikipedia.org/wiki/JPEG_File_Interchange_Format</li>
39 * <li>http://www.w3.org/Graphics/JPEG/jfif3.pdf</li>
40 * </ul>
41 *
42 * @author Yuri Binev, Drew Noakes, Markus Meyer
43 */
44 public class JfifReader implements JpegSegmentMetadataReader, MetadataReader
45 {
46 public static final String PREAMBLE = "JFIF";
47
48 @NotNull
49 public Iterable<JpegSegmentType> getSegmentTypes()
50 {
51 return Collections.singletonList(JpegSegmentType.APP0);
52 }
53
54 public void readJpegSegments(@NotNull Iterable<byte[]> segments, @NotNull Metadata metadata, @NotNull JpegSegmentType segmentType)
55 {
56 for (byte[] segmentBytes : segments) {
57 // Skip segments not starting with the required header
58 if (segmentBytes.length >= PREAMBLE.length() && PREAMBLE.equals(new String(segmentBytes, 0, PREAMBLE.length())))
59 extract(new ByteArrayReader(segmentBytes), metadata);
60 }
61 }
62
63 /**
64 * Performs the Jfif data extraction, adding found values to the specified
65 * instance of {@link Metadata}.
66 */
67 public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata)
68 {
69 JfifDirectory directory = new JfifDirectory();
70 metadata.addDirectory(directory);
71
72 try {
73 // For JFIF, the tag number is also the offset into the segment
74
75 directory.setInt(JfifDirectory.TAG_VERSION, reader.getUInt16(JfifDirectory.TAG_VERSION));
76 directory.setInt(JfifDirectory.TAG_UNITS, reader.getUInt8(JfifDirectory.TAG_UNITS));
77 directory.setInt(JfifDirectory.TAG_RESX, reader.getUInt16(JfifDirectory.TAG_RESX));
78 directory.setInt(JfifDirectory.TAG_RESY, reader.getUInt16(JfifDirectory.TAG_RESY));
79 directory.setInt(JfifDirectory.TAG_THUMB_WIDTH, reader.getUInt8(JfifDirectory.TAG_THUMB_WIDTH));
80 directory.setInt(JfifDirectory.TAG_THUMB_HEIGHT, reader.getUInt8(JfifDirectory.TAG_THUMB_HEIGHT));
81 } catch (IOException me) {
82 directory.addError(me.getMessage());
83 }
84 }
85 }