| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package com.drew.metadata.webp; |
| 22 |
|
| 23 |
import com.drew.imaging.riff.RiffHandler; |
| 24 |
import com.drew.lang.ByteArrayReader; |
| 25 |
import com.drew.lang.RandomAccessReader; |
| 26 |
import com.drew.lang.annotations.NotNull; |
| 27 |
import com.drew.metadata.Metadata; |
| 28 |
import com.drew.metadata.exif.ExifReader; |
| 29 |
import com.drew.metadata.icc.IccReader; |
| 30 |
import com.drew.metadata.xmp.XmpReader; |
| 31 |
|
| 32 |
import java.io.IOException; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class WebpRiffHandler implements RiffHandler |
| 47 |
{ |
| 48 |
@NotNull |
| 49 |
private final Metadata _metadata; |
| 50 |
|
| 51 |
public WebpRiffHandler(@NotNull Metadata metadata) |
| 52 |
{ |
| 53 |
_metadata = metadata; |
| 54 |
} |
| 55 |
|
| 56 |
public boolean shouldAcceptRiffIdentifier(@NotNull String identifier) |
| 57 |
{ |
| 58 |
return identifier.equals(WebpDirectory.FORMAT); |
| 59 |
} |
| 60 |
|
| 61 |
public boolean shouldAcceptChunk(@NotNull String fourCC) |
| 62 |
{ |
| 63 |
return fourCC.equals(WebpDirectory.CHUNK_VP8X) |
| 64 |
|| fourCC.equals(WebpDirectory.CHUNK_VP8L) |
| 65 |
|| fourCC.equals(WebpDirectory.CHUNK_VP8) |
| 66 |
|| fourCC.equals(WebpDirectory.CHUNK_EXIF) |
| 67 |
|| fourCC.equals(WebpDirectory.CHUNK_ICCP) |
| 68 |
|| fourCC.equals(WebpDirectory.CHUNK_XMP); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public boolean shouldAcceptList(@NotNull String fourCC) |
| 73 |
{ |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
public void processChunk(@NotNull String fourCC, @NotNull byte[] payload) |
| 78 |
{ |
| 79 |
WebpDirectory directory = new WebpDirectory(); |
| 80 |
if (fourCC.equals(WebpDirectory.CHUNK_EXIF)) { |
| 81 |
|
| 82 |
|
| 83 |
ByteArrayReader reader = ExifReader.startsWithJpegExifPreamble(payload) |
| 84 |
? new ByteArrayReader(payload, ExifReader.JPEG_SEGMENT_PREAMBLE.length()) |
| 85 |
: new ByteArrayReader(payload); |
| 86 |
new ExifReader().extract(reader, _metadata); |
| 87 |
} else if (fourCC.equals(WebpDirectory.CHUNK_ICCP)) { |
| 88 |
new IccReader().extract(new ByteArrayReader(payload), _metadata); |
| 89 |
} else if (fourCC.equals(WebpDirectory.CHUNK_XMP)) { |
| 90 |
new XmpReader().extract(payload, _metadata); |
| 91 |
} else if (fourCC.equals(WebpDirectory.CHUNK_VP8X) && payload.length == 10) { |
| 92 |
RandomAccessReader reader = new ByteArrayReader(payload); |
| 93 |
reader.setMotorolaByteOrder(false); |
| 94 |
|
| 95 |
try { |
| 96 |
|
| 97 |
|
| 98 |
boolean isAnimation = reader.getBit(1); |
| 99 |
|
| 100 |
|
| 101 |
boolean hasAlpha = reader.getBit(4); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
int widthMinusOne = reader.getInt24(4); |
| 106 |
int heightMinusOne = reader.getInt24(7); |
| 107 |
|
| 108 |
directory.setInt(WebpDirectory.TAG_IMAGE_WIDTH, widthMinusOne + 1); |
| 109 |
directory.setInt(WebpDirectory.TAG_IMAGE_HEIGHT, heightMinusOne + 1); |
| 110 |
directory.setBoolean(WebpDirectory.TAG_HAS_ALPHA, hasAlpha); |
| 111 |
directory.setBoolean(WebpDirectory.TAG_IS_ANIMATION, isAnimation); |
| 112 |
|
| 113 |
_metadata.addDirectory(directory); |
| 114 |
|
| 115 |
} catch (IOException e) { |
| 116 |
directory.addError(e.getMessage()); |
| 117 |
} |
| 118 |
} else if (fourCC.equals(WebpDirectory.CHUNK_VP8L) && payload.length > 4) { |
| 119 |
RandomAccessReader reader = new ByteArrayReader(payload); |
| 120 |
reader.setMotorolaByteOrder(false); |
| 121 |
|
| 122 |
try { |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
if (reader.getInt8(0) != 0x2F) |
| 127 |
return; |
| 128 |
int b1 = reader.getUInt8(1); |
| 129 |
int b2 = reader.getUInt8(2); |
| 130 |
int b3 = reader.getUInt8(3); |
| 131 |
int b4 = reader.getUInt8(4); |
| 132 |
|
| 133 |
int widthMinusOne = (b2 & 0x3F) << 8 | b1; |
| 134 |
|
| 135 |
int heightMinusOne = (b4 & 0x0F) << 10 | b3 << 2 | (b2 & 0xC0) >> 6; |
| 136 |
|
| 137 |
directory.setInt(WebpDirectory.TAG_IMAGE_WIDTH, widthMinusOne + 1); |
| 138 |
directory.setInt(WebpDirectory.TAG_IMAGE_HEIGHT, heightMinusOne + 1); |
| 139 |
|
| 140 |
_metadata.addDirectory(directory); |
| 141 |
|
| 142 |
} catch (IOException e) { |
| 143 |
directory.addError(e.getMessage()); |
| 144 |
} |
| 145 |
} else if (fourCC.equals(WebpDirectory.CHUNK_VP8) && payload.length > 9) { |
| 146 |
RandomAccessReader reader = new ByteArrayReader(payload); |
| 147 |
reader.setMotorolaByteOrder(false); |
| 148 |
|
| 149 |
try { |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
if (reader.getUInt8(3) != 0x9D || |
| 155 |
reader.getUInt8(4) != 0x01 || |
| 156 |
reader.getUInt8(5) != 0x2A) |
| 157 |
return; |
| 158 |
int width = reader.getUInt16(6); |
| 159 |
int height = reader.getUInt16(8); |
| 160 |
|
| 161 |
directory.setInt(WebpDirectory.TAG_IMAGE_WIDTH, width); |
| 162 |
directory.setInt(WebpDirectory.TAG_IMAGE_HEIGHT, height); |
| 163 |
|
| 164 |
_metadata.addDirectory(directory); |
| 165 |
|
| 166 |
} catch (IOException ex) { |
| 167 |
directory.addError(ex.getMessage()); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public void addError(@NotNull String message) |
| 174 |
{ |
| 175 |
WebpDirectory directory = new WebpDirectory(); |
| 176 |
directory.addError(message); |
| 177 |
_metadata.addDirectory(directory); |
| 178 |
} |
| 179 |
} |