1 | /*
|
---|
2 | * Copyright 2002-2017 Drew Noakes
|
---|
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.imaging.jpeg;
|
---|
22 |
|
---|
23 | import com.drew.lang.annotations.NotNull;
|
---|
24 | import com.drew.lang.annotations.Nullable;
|
---|
25 |
|
---|
26 | import java.util.*;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Holds a collection of JPEG data segments. This need not necessarily be all segments
|
---|
30 | * within the JPEG. For example, it may be convenient to store only the non-image
|
---|
31 | * segments when analysing metadata.
|
---|
32 | * <p>
|
---|
33 | * Segments are keyed via their {@link JpegSegmentType}. Where multiple segments use the
|
---|
34 | * same segment type, they will all be stored and available.
|
---|
35 | * <p>
|
---|
36 | * Each segment type may contain multiple entries. Conceptually the model is:
|
---|
37 | * <code>Map<JpegSegmentType, Collection<byte[]>></code>. This class provides
|
---|
38 | * convenience methods around that structure.
|
---|
39 | *
|
---|
40 | * @author Drew Noakes https://drewnoakes.com
|
---|
41 | */
|
---|
42 | public class JpegSegmentData
|
---|
43 | {
|
---|
44 | // TODO key this on JpegSegmentType rather than Byte, and hopefully lose much of the use of 'byte' with this class
|
---|
45 | @NotNull
|
---|
46 | private final HashMap<Byte, List<byte[]>> _segmentDataMap = new HashMap<Byte, List<byte[]>>(10);
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Adds segment bytes to the collection.
|
---|
50 | *
|
---|
51 | * @param segmentType the type of the segment being added
|
---|
52 | * @param segmentBytes the byte array holding data for the segment being added
|
---|
53 | */
|
---|
54 | @SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
|
---|
55 | public void addSegment(byte segmentType, @NotNull byte[] segmentBytes)
|
---|
56 | {
|
---|
57 | getOrCreateSegmentList(segmentType).add(segmentBytes);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Gets the set of JPEG segment type identifiers.
|
---|
62 | */
|
---|
63 | public Iterable<JpegSegmentType> getSegmentTypes()
|
---|
64 | {
|
---|
65 | Set<JpegSegmentType> segmentTypes = new HashSet<JpegSegmentType>();
|
---|
66 |
|
---|
67 | for (Byte segmentTypeByte : _segmentDataMap.keySet())
|
---|
68 | {
|
---|
69 | JpegSegmentType segmentType = JpegSegmentType.fromByte(segmentTypeByte);
|
---|
70 | if (segmentType == null) {
|
---|
71 | throw new IllegalStateException("Should not have a segmentTypeByte that is not in the enum: " + Integer.toHexString(segmentTypeByte));
|
---|
72 | }
|
---|
73 | segmentTypes.add(segmentType);
|
---|
74 | }
|
---|
75 |
|
---|
76 | return segmentTypes;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Gets the first JPEG segment data for the specified type.
|
---|
81 | *
|
---|
82 | * @param segmentType the JpegSegmentType for the desired segment
|
---|
83 | * @return a byte[] containing segment data or null if no data exists for that segment
|
---|
84 | */
|
---|
85 | @Nullable
|
---|
86 | public byte[] getSegment(byte segmentType)
|
---|
87 | {
|
---|
88 | return getSegment(segmentType, 0);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Gets the first JPEG segment data for the specified type.
|
---|
93 | *
|
---|
94 | * @param segmentType the JpegSegmentType for the desired segment
|
---|
95 | * @return a byte[] containing segment data or null if no data exists for that segment
|
---|
96 | */
|
---|
97 | @Nullable
|
---|
98 | public byte[] getSegment(@NotNull JpegSegmentType segmentType)
|
---|
99 | {
|
---|
100 | return getSegment(segmentType.byteValue, 0);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Gets segment data for a specific occurrence and type. Use this method when more than one occurrence
|
---|
105 | * of segment data for a given type exists.
|
---|
106 | *
|
---|
107 | * @param segmentType identifies the required segment
|
---|
108 | * @param occurrence the zero-based index of the occurrence
|
---|
109 | * @return the segment data as a byte[], or null if no segment exists for the type & occurrence
|
---|
110 | */
|
---|
111 | @Nullable
|
---|
112 | public byte[] getSegment(@NotNull JpegSegmentType segmentType, int occurrence)
|
---|
113 | {
|
---|
114 | return getSegment(segmentType.byteValue, occurrence);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Gets segment data for a specific occurrence and type. Use this method when more than one occurrence
|
---|
119 | * of segment data for a given type exists.
|
---|
120 | *
|
---|
121 | * @param segmentType identifies the required segment
|
---|
122 | * @param occurrence the zero-based index of the occurrence
|
---|
123 | * @return the segment data as a byte[], or null if no segment exists for the type & occurrence
|
---|
124 | */
|
---|
125 | @Nullable
|
---|
126 | public byte[] getSegment(byte segmentType, int occurrence)
|
---|
127 | {
|
---|
128 | final List<byte[]> segmentList = getSegmentList(segmentType);
|
---|
129 |
|
---|
130 | return segmentList != null && segmentList.size() > occurrence
|
---|
131 | ? segmentList.get(occurrence)
|
---|
132 | : null;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Returns all instances of a given JPEG segment. If no instances exist, an empty sequence is returned.
|
---|
137 | *
|
---|
138 | * @param segmentType a number which identifies the type of JPEG segment being queried
|
---|
139 | * @return zero or more byte arrays, each holding the data of a JPEG segment
|
---|
140 | */
|
---|
141 | @NotNull
|
---|
142 | public Iterable<byte[]> getSegments(@NotNull JpegSegmentType segmentType)
|
---|
143 | {
|
---|
144 | return getSegments(segmentType.byteValue);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Returns all instances of a given JPEG segment. If no instances exist, an empty sequence is returned.
|
---|
149 | *
|
---|
150 | * @param segmentType a number which identifies the type of JPEG segment being queried
|
---|
151 | * @return zero or more byte arrays, each holding the data of a JPEG segment
|
---|
152 | */
|
---|
153 | @NotNull
|
---|
154 | public Iterable<byte[]> getSegments(byte segmentType)
|
---|
155 | {
|
---|
156 | final List<byte[]> segmentList = getSegmentList(segmentType);
|
---|
157 | return segmentList == null ? new ArrayList<byte[]>() : segmentList;
|
---|
158 | }
|
---|
159 |
|
---|
160 | @Nullable
|
---|
161 | private List<byte[]> getSegmentList(byte segmentType)
|
---|
162 | {
|
---|
163 | return _segmentDataMap.get(segmentType);
|
---|
164 | }
|
---|
165 |
|
---|
166 | @NotNull
|
---|
167 | private List<byte[]> getOrCreateSegmentList(byte segmentType)
|
---|
168 | {
|
---|
169 | List<byte[]> segmentList;
|
---|
170 | if (_segmentDataMap.containsKey(segmentType)) {
|
---|
171 | segmentList = _segmentDataMap.get(segmentType);
|
---|
172 | } else {
|
---|
173 | segmentList = new ArrayList<byte[]>();
|
---|
174 | _segmentDataMap.put(segmentType, segmentList);
|
---|
175 | }
|
---|
176 | return segmentList;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Returns the count of segment data byte arrays stored for a given segment type.
|
---|
181 | *
|
---|
182 | * @param segmentType identifies the required segment
|
---|
183 | * @return the segment count (zero if no segments exist).
|
---|
184 | */
|
---|
185 | public int getSegmentCount(@NotNull JpegSegmentType segmentType)
|
---|
186 | {
|
---|
187 | return getSegmentCount(segmentType.byteValue);
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Returns the count of segment data byte arrays stored for a given segment type.
|
---|
192 | *
|
---|
193 | * @param segmentType identifies the required segment
|
---|
194 | * @return the segment count (zero if no segments exist).
|
---|
195 | */
|
---|
196 | public int getSegmentCount(byte segmentType)
|
---|
197 | {
|
---|
198 | final List<byte[]> segmentList = getSegmentList(segmentType);
|
---|
199 | return segmentList == null ? 0 : segmentList.size();
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Removes a specified instance of a segment's data from the collection. Use this method when more than one
|
---|
204 | * occurrence of segment data exists for a given type exists.
|
---|
205 | *
|
---|
206 | * @param segmentType identifies the required segment
|
---|
207 | * @param occurrence the zero-based index of the segment occurrence to remove.
|
---|
208 | */
|
---|
209 | @SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
|
---|
210 | public void removeSegmentOccurrence(@NotNull JpegSegmentType segmentType, int occurrence)
|
---|
211 | {
|
---|
212 | removeSegmentOccurrence(segmentType.byteValue, occurrence);
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Removes a specified instance of a segment's data from the collection. Use this method when more than one
|
---|
217 | * occurrence of segment data exists for a given type exists.
|
---|
218 | *
|
---|
219 | * @param segmentType identifies the required segment
|
---|
220 | * @param occurrence the zero-based index of the segment occurrence to remove.
|
---|
221 | */
|
---|
222 | @SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
|
---|
223 | public void removeSegmentOccurrence(byte segmentType, int occurrence)
|
---|
224 | {
|
---|
225 | final List<byte[]> segmentList = _segmentDataMap.get(segmentType);
|
---|
226 | segmentList.remove(occurrence);
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Removes all segments from the collection having the specified type.
|
---|
231 | *
|
---|
232 | * @param segmentType identifies the required segment
|
---|
233 | */
|
---|
234 | public void removeSegment(@NotNull JpegSegmentType segmentType)
|
---|
235 | {
|
---|
236 | removeSegment(segmentType.byteValue);
|
---|
237 | }
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Removes all segments from the collection having the specified type.
|
---|
241 | *
|
---|
242 | * @param segmentType identifies the required segment
|
---|
243 | */
|
---|
244 | public void removeSegment(byte segmentType)
|
---|
245 | {
|
---|
246 | _segmentDataMap.remove(segmentType);
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Determines whether data is present for a given segment type.
|
---|
251 | *
|
---|
252 | * @param segmentType identifies the required segment
|
---|
253 | * @return true if data exists, otherwise false
|
---|
254 | */
|
---|
255 | public boolean containsSegment(@NotNull JpegSegmentType segmentType)
|
---|
256 | {
|
---|
257 | return containsSegment(segmentType.byteValue);
|
---|
258 | }
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Determines whether data is present for a given segment type.
|
---|
262 | *
|
---|
263 | * @param segmentType identifies the required segment
|
---|
264 | * @return true if data exists, otherwise false
|
---|
265 | */
|
---|
266 | public boolean containsSegment(byte segmentType)
|
---|
267 | {
|
---|
268 | return _segmentDataMap.containsKey(segmentType);
|
---|
269 | }
|
---|
270 | }
|
---|