| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 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 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
reader.setMotorolaByteOrder(true); |
| 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 |
|
| 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; |
| 107 |
reader.skip(chunkDataLength); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 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 |
} |