Debian Codegraph @

src/com/drew/imaging/png/PngChunkReader.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.imaging.png;
22
23 import com.drew.lang.SequentialReader;
24 import com.drew.lang.annotations.NotNull;
25 import com.drew.lang.annotations.Nullable;
26
27 import java.io.IOException;
28 import java.util.*;
29
30 /**
31 * @author Drew Noakes https://drewnoakes.com
32 */
33 public class PngChunkReader
34 {
35 private static final byte[] PNG_SIGNATURE_BYTES = {(byte)0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
36
37 public Iterable<PngChunk> extract(@NotNull final SequentialReader reader, @Nullable final Set<PngChunkType> desiredChunkTypes) throws PngProcessingException, IOException
38 {
39 //
40 // PNG DATA STREAM
41 //
42 // Starts with a PNG SIGNATURE, followed by a sequence of CHUNKS.
43 //
44 // PNG SIGNATURE
45 //
46 // Always composed of these bytes: 89 50 4E 47 0D 0A 1A 0A
47 //
48 // CHUNK
49 //
50 // 4 - length of the data field (unsigned, but always within 31 bytes), may be zero
51 // 4 - chunk type, restricted to [65,90] and [97,122] (A-Za-z)
52 // * - data field
53 // 4 - CRC calculated from chunk type and chunk data, but not length
54 //
55 // CHUNK TYPES
56 //
57 // Critical Chunk Types:
58 //
59 // IHDR - image header, always the first chunk in the data stream
60 // PLTE - palette table, associated with indexed PNG images
61 // IDAT - image data chunk, of which there may be many
62 // IEND - image trailer, always the last chunk in the data stream
63 //
64 // Ancillary Chunk Types:
65 //
66 // Transparency information: tRNS
67 // Colour space information: cHRM, gAMA, iCCP, sBIT, sRGB
68 // Textual information: iTXt, tEXt, zTXt
69 // Miscellaneous information: bKGD, hIST, pHYs, sPLT
70 // Time information: tIME
71 //
72 // CHUNK READING
73 //
74 // Only chunk data for types specified in desiredChunkTypes is extracted.
75 // For empty chunk type list NO data is copied from source stream.
76 // For null chunk type list ALL data is copied from source stream.
77 //
78
79 reader.setMotorolaByteOrder(true); // network byte order
80
81 if (!Arrays.equals(PNG_SIGNATURE_BYTES, reader.getBytes(PNG_SIGNATURE_BYTES.length))) {
82 throw new PngProcessingException("PNG signature mismatch");
83 }
84
85 boolean seenImageHeader = false;
86 boolean seenImageTrailer = false;
87
88 List<PngChunk> chunks = new ArrayList<PngChunk>();
89 Set<PngChunkType> seenChunkTypes = new HashSet<PngChunkType>();
90
91 while (!seenImageTrailer) {
92 // Process the next chunk.
93 int chunkDataLength = reader.getInt32();
94
95 if (chunkDataLength < 0)
96 throw new PngProcessingException("PNG chunk length exceeds maximum");
97
98 PngChunkType chunkType = new PngChunkType(reader.getBytes(4));
99
100 boolean willStoreChunk = desiredChunkTypes == null || desiredChunkTypes.contains(chunkType);
101
102 byte[] chunkData;
103 if (willStoreChunk) {
104 chunkData = reader.getBytes(chunkDataLength);
105 } else {
106 chunkData = null; // To satisfy the compiler
107 reader.skip(chunkDataLength);
108 }
109
110 // Skip the CRC bytes at the end of the chunk
111 // TODO consider verifying the CRC value to determine if we're processing bad data
112 reader.skip(4);
113
114 if (willStoreChunk && seenChunkTypes.contains(chunkType) && !chunkType.areMultipleAllowed()) {
115 throw new PngProcessingException(String.format("Observed multiple instances of PNG chunk '%s', for which multiples are not allowed", chunkType));
116 }
117
118 if (chunkType.equals(PngChunkType.IHDR)) {
119 seenImageHeader = true;
120 } else if (!seenImageHeader) {
121 throw new PngProcessingException(String.format("First chunk should be '%s', but '%s' was observed", PngChunkType.IHDR, chunkType));
122 }
123
124 if (chunkType.equals(PngChunkType.IEND)) {
125 seenImageTrailer = true;
126 }
127
128 if (willStoreChunk) {
129 chunks.add(new PngChunk(chunkType, chunkData));
130 }
131
132 seenChunkTypes.add(chunkType);
133 }
134
135 return chunks;
136 }
137 }