Debian Codegraph @

src/com/drew/tools/ExtractJpegSegmentTool.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.tools;
22
23 import com.drew.imaging.jpeg.JpegProcessingException;
24 import com.drew.imaging.jpeg.JpegSegmentData;
25 import com.drew.imaging.jpeg.JpegSegmentReader;
26 import com.drew.imaging.jpeg.JpegSegmentType;
27 import com.drew.lang.Iterables;
28 import com.drew.lang.annotations.NotNull;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Set;
35
36 /**
37 * Extracts JPEG segments and writes them to individual files.
38 * <p>
39 * Extracting only the required segment(s) for use in unit testing has several benefits:
40 * <ul>
41 * <li>Helps reduce the repository size. For example a small JPEG image may still be 20kB+ in size, yet its
42 * APPD (IPTC) segment may be as small as 200 bytes.</li>
43 * <li>Makes unit tests run more rapidly.</li>
44 * <li>Partially anonymises user-contributed data by removing image portions.</li>
45 * </ul>
46 *
47 * @author Drew Noakes https://drewnoakes.com
48 */
49 public class ExtractJpegSegmentTool
50 {
51 public static void main(String[] args) throws IOException, JpegProcessingException
52 {
53 if (args.length < 1) {
54 printUsage();
55 System.exit(1);
56 }
57
58 String filePath = args[0];
59
60 if (!new File(filePath).exists()) {
61 System.err.println("File does not exist");
62 printUsage();
63 System.exit(1);
64 }
65
66 Set<JpegSegmentType> segmentTypes = new HashSet<JpegSegmentType>();
67
68 for (int i = 1; i < args.length; i++) {
69 JpegSegmentType segmentType = JpegSegmentType.valueOf(args[i].toUpperCase());
70 if (!segmentType.canContainMetadata) {
71 System.err.printf("WARNING: Segment type %s cannot contain metadata so it may not be necessary to extract it%n", segmentType);
72 }
73 segmentTypes.add(segmentType);
74 }
75
76 if (segmentTypes.size() == 0) {
77 // If none specified, use all that could reasonably contain metadata
78 segmentTypes.addAll(JpegSegmentType.canContainMetadataTypes);
79 }
80
81 System.out.println("Reading: " + filePath);
82
83 JpegSegmentData segmentData = JpegSegmentReader.readSegments(new File(filePath), segmentTypes);
84
85 saveSegmentFiles(filePath, segmentData);
86 }
87
88 public static void saveSegmentFiles(@NotNull String jpegFilePath, @NotNull JpegSegmentData segmentData) throws IOException
89 {
90 for (JpegSegmentType segmentType : segmentData.getSegmentTypes()) {
91 List<byte[]> segments = Iterables.toList(segmentData.getSegments(segmentType));
92 if (segments.size() == 0) {
93 continue;
94 }
95
96 if (segments.size() > 1) {
97 for (int i = 0; i < segments.size(); i++) {
98 String outputFilePath = String.format("%s.%s.%d", jpegFilePath, segmentType.toString().toLowerCase(), i);
99 System.out.println("Writing: " + outputFilePath);
100 FileUtil.saveBytes(new File(outputFilePath), segments.get(i));
101 }
102 } else {
103 String outputFilePath = String.format("%s.%s", jpegFilePath, segmentType.toString().toLowerCase());
104 System.out.println("Writing: " + outputFilePath);
105 FileUtil.saveBytes(new File(outputFilePath), segments.get(0));
106 }
107 }
108 }
109
110 private static void printUsage()
111 {
112 System.out.println("USAGE:\n");
113 System.out.println("\tjava com.drew.tools.ExtractJpegSegmentTool <filename> [<segment> ...]\n");
114
115 System.out.print("Where <segment> is zero or more of:");
116 for (JpegSegmentType segmentType : JpegSegmentType.class.getEnumConstants()) {
117 if (segmentType.canContainMetadata) {
118 System.out.print(" " + segmentType.toString());
119 }
120 }
121 System.out.println();
122 }
123 }