Fields Summary |
---|
private static final boolean | LOGV |
private static final String | TAG |
public static final int | EVENT_START_OF_IFDWhen the parser reaches a new IFD area. Call {@link #getCurrentIfd()} to
know which IFD we are in. |
public static final int | EVENT_NEW_TAGWhen the parser reaches a new tag. Call {@link #getTag()}to get the
corresponding tag. |
public static final int | EVENT_VALUE_OF_REGISTERED_TAGWhen the parser reaches the value area of tag that is registered by
{@link #registerForTagValue(ExifTag)} previously. Call {@link #getTag()}
to get the corresponding tag. |
public static final int | EVENT_COMPRESSED_IMAGEWhen the parser reaches the compressed image area. |
public static final int | EVENT_UNCOMPRESSED_STRIPWhen the parser reaches the uncompressed image strip. Call
{@link #getStripIndex()} to get the index of the strip. |
public static final int | EVENT_ENDWhen there is nothing more to parse. |
public static final int | OPTION_IFD_0Option bit to request to parse IFD0. |
public static final int | OPTION_IFD_1Option bit to request to parse IFD1. |
public static final int | OPTION_IFD_EXIFOption bit to request to parse Exif-IFD. |
public static final int | OPTION_IFD_GPSOption bit to request to parse GPS-IFD. |
public static final int | OPTION_IFD_INTEROPERABILITYOption bit to request to parse Interoperability-IFD. |
public static final int | OPTION_THUMBNAILOption bit to request to parse thumbnail. |
protected static final int | EXIF_HEADER |
protected static final short | EXIF_HEADER_TAIL |
protected static final short | LITTLE_ENDIAN_TAG |
protected static final short | BIG_ENDIAN_TAG |
protected static final short | TIFF_HEADER_TAIL |
protected static final int | TAG_SIZE |
protected static final int | OFFSET_SIZE |
private static final Charset | US_ASCII |
protected static final int | DEFAULT_IFD0_OFFSET |
private final CountedDataInputStream | mTiffStream |
private final int | mOptions |
private int | mIfdStartOffset |
private int | mNumOfTagInIfd |
private int | mIfdType |
private ExifTag | mTag |
private ImageEvent | mImageEvent |
private int | mStripCount |
private ExifTag | mStripSizeTag |
private ExifTag | mJpegSizeTag |
private boolean | mNeedToParseOffsetsInCurrentIfd |
private boolean | mContainExifData |
private int | mApp1End |
private int | mOffsetToApp1EndFromSOF |
private byte[] | mDataAboveIfd0 |
private int | mIfd0Position |
private int | mTiffStartPosition |
private final ExifInterface | mInterface |
private static final short | TAG_EXIF_IFD |
private static final short | TAG_GPS_IFD |
private static final short | TAG_INTEROPERABILITY_IFD |
private static final short | TAG_JPEG_INTERCHANGE_FORMAT |
private static final short | TAG_JPEG_INTERCHANGE_FORMAT_LENGTH |
private static final short | TAG_STRIP_OFFSETS |
private static final short | TAG_STRIP_BYTE_COUNTS |
private final TreeMap | mCorrespondingEvent |
Methods Summary |
---|
private boolean | checkAllowed(int ifd, int tagId)
int info = mInterface.getTagInfo().get(tagId);
if (info == ExifInterface.DEFINITION_NULL) {
return false;
}
return ExifInterface.isIfdAllowed(info, ifd);
|
private void | checkOffsetOrImageTag(ExifTag tag)Check the tag, if the tag is one of the offset tag that points to the IFD
or image the caller is interested in, register the IFD or image.
// Some invalid formattd image contains tag with 0 size.
if (tag.getComponentCount() == 0) {
return;
}
short tid = tag.getTagId();
int ifd = tag.getIfd();
if (tid == TAG_EXIF_IFD && checkAllowed(ifd, ExifInterface.TAG_EXIF_IFD)) {
if (isIfdRequested(IfdId.TYPE_IFD_EXIF)
|| isIfdRequested(IfdId.TYPE_IFD_INTEROPERABILITY)) {
registerIfd(IfdId.TYPE_IFD_EXIF, tag.getValueAt(0));
}
} else if (tid == TAG_GPS_IFD && checkAllowed(ifd, ExifInterface.TAG_GPS_IFD)) {
if (isIfdRequested(IfdId.TYPE_IFD_GPS)) {
registerIfd(IfdId.TYPE_IFD_GPS, tag.getValueAt(0));
}
} else if (tid == TAG_INTEROPERABILITY_IFD
&& checkAllowed(ifd, ExifInterface.TAG_INTEROPERABILITY_IFD)) {
if (isIfdRequested(IfdId.TYPE_IFD_INTEROPERABILITY)) {
registerIfd(IfdId.TYPE_IFD_INTEROPERABILITY, tag.getValueAt(0));
}
} else if (tid == TAG_JPEG_INTERCHANGE_FORMAT
&& checkAllowed(ifd, ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT)) {
if (isThumbnailRequested()) {
registerCompressedImage(tag.getValueAt(0));
}
} else if (tid == TAG_JPEG_INTERCHANGE_FORMAT_LENGTH
&& checkAllowed(ifd, ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH)) {
if (isThumbnailRequested()) {
mJpegSizeTag = tag;
}
} else if (tid == TAG_STRIP_OFFSETS && checkAllowed(ifd, ExifInterface.TAG_STRIP_OFFSETS)) {
if (isThumbnailRequested()) {
if (tag.hasValue()) {
for (int i = 0; i < tag.getComponentCount(); i++) {
if (tag.getDataType() == ExifTag.TYPE_UNSIGNED_SHORT) {
registerUncompressedStrip(i, tag.getValueAt(i));
} else {
registerUncompressedStrip(i, tag.getValueAt(i));
}
}
} else {
mCorrespondingEvent.put(tag.getOffset(), new ExifTagEvent(tag, false));
}
}
} else if (tid == TAG_STRIP_BYTE_COUNTS
&& checkAllowed(ifd, ExifInterface.TAG_STRIP_BYTE_COUNTS)
&&isThumbnailRequested() && tag.hasValue()) {
mStripSizeTag = tag;
}
|
protected java.nio.ByteOrder | getByteOrder()Gets the byte order of the current InputStream.
return mTiffStream.getByteOrder();
|
protected int | getCompressedImageSize()When receiving {@link #EVENT_COMPRESSED_IMAGE}, call this function to get
the image data size.
if (mJpegSizeTag == null) {
return 0;
}
return (int) mJpegSizeTag.getValueAt(0);
|
protected int | getCurrentIfd()Gets the ID of current IFD.
return mIfdType;
|
protected int | getOffsetToExifEndFromSOF()
return mOffsetToApp1EndFromSOF;
|
protected int | getStripCount()When receiving {@link #EVENT_UNCOMPRESSED_STRIP}, call this function to
get the number of strip data.
return mStripCount;
|
protected int | getStripIndex()When receiving {@link #EVENT_UNCOMPRESSED_STRIP}, call this function to
get the index of this strip.
return mImageEvent.stripIndex;
|
protected int | getStripSize()When receiving {@link #EVENT_UNCOMPRESSED_STRIP}, call this function to
get the strip size.
if (mStripSizeTag == null)
return 0;
return (int) mStripSizeTag.getValueAt(0);
|
protected ExifTag | getTag()If {@link #next()} return {@link #EVENT_NEW_TAG} or
{@link #EVENT_VALUE_OF_REGISTERED_TAG}, call this function to get the
corresponding tag.
For {@link #EVENT_NEW_TAG}, the tag may not contain the value if the size
of the value is greater than 4 bytes. One should call
{@link ExifTag#hasValue()} to check if the tag contains value. If there
is no value,call {@link #registerForTagValue(ExifTag)} to have the parser
emit {@link #EVENT_VALUE_OF_REGISTERED_TAG} when it reaches the area
pointed by the offset.
When {@link #EVENT_VALUE_OF_REGISTERED_TAG} is emitted, the value of the
tag will have already been read except for tags of undefined type. For
tags of undefined type, call one of the read methods to get the value.
return mTag;
|
protected int | getTagCountInCurrentIfd()Gets number of tags in the current IFD area.
return mNumOfTagInIfd;
|
protected int | getTiffStartPosition()
return mTiffStartPosition;
|
private boolean | isIfdRequested(int ifdType)
switch (ifdType) {
case IfdId.TYPE_IFD_0:
return (mOptions & OPTION_IFD_0) != 0;
case IfdId.TYPE_IFD_1:
return (mOptions & OPTION_IFD_1) != 0;
case IfdId.TYPE_IFD_EXIF:
return (mOptions & OPTION_IFD_EXIF) != 0;
case IfdId.TYPE_IFD_GPS:
return (mOptions & OPTION_IFD_GPS) != 0;
case IfdId.TYPE_IFD_INTEROPERABILITY:
return (mOptions & OPTION_IFD_INTEROPERABILITY) != 0;
}
return false;
|
private boolean | isThumbnailRequested()
return (mOptions & OPTION_THUMBNAIL) != 0;
|
private boolean | needToParseOffsetsInCurrentIfd()
switch (mIfdType) {
case IfdId.TYPE_IFD_0:
return isIfdRequested(IfdId.TYPE_IFD_EXIF) || isIfdRequested(IfdId.TYPE_IFD_GPS)
|| isIfdRequested(IfdId.TYPE_IFD_INTEROPERABILITY)
|| isIfdRequested(IfdId.TYPE_IFD_1);
case IfdId.TYPE_IFD_1:
return isThumbnailRequested();
case IfdId.TYPE_IFD_EXIF:
// The offset to interoperability IFD is located in Exif IFD
return isIfdRequested(IfdId.TYPE_IFD_INTEROPERABILITY);
default:
return false;
}
|
protected int | next()Moves the parser forward and returns the next parsing event
if (!mContainExifData) {
return EVENT_END;
}
int offset = mTiffStream.getReadByteCount();
int endOfTags = mIfdStartOffset + OFFSET_SIZE + TAG_SIZE * mNumOfTagInIfd;
if (offset < endOfTags) {
mTag = readTag();
if (mTag == null) {
return next();
}
if (mNeedToParseOffsetsInCurrentIfd) {
checkOffsetOrImageTag(mTag);
}
return EVENT_NEW_TAG;
} else if (offset == endOfTags) {
// There is a link to ifd1 at the end of ifd0
if (mIfdType == IfdId.TYPE_IFD_0) {
long ifdOffset = readUnsignedLong();
if (isIfdRequested(IfdId.TYPE_IFD_1) || isThumbnailRequested()) {
if (ifdOffset != 0) {
registerIfd(IfdId.TYPE_IFD_1, ifdOffset);
}
}
} else {
int offsetSize = 4;
// Some camera models use invalid length of the offset
if (mCorrespondingEvent.size() > 0) {
offsetSize = mCorrespondingEvent.firstEntry().getKey() -
mTiffStream.getReadByteCount();
}
if (offsetSize < 4) {
Log.w(TAG, "Invalid size of link to next IFD: " + offsetSize);
} else {
long ifdOffset = readUnsignedLong();
if (ifdOffset != 0) {
Log.w(TAG, "Invalid link to next IFD: " + ifdOffset);
}
}
}
}
while (mCorrespondingEvent.size() != 0) {
Entry<Integer, Object> entry = mCorrespondingEvent.pollFirstEntry();
Object event = entry.getValue();
try {
skipTo(entry.getKey());
} catch (IOException e) {
Log.w(TAG, "Failed to skip to data at: " + entry.getKey() +
" for " + event.getClass().getName() + ", the file may be broken.");
continue;
}
if (event instanceof IfdEvent) {
mIfdType = ((IfdEvent) event).ifd;
mNumOfTagInIfd = mTiffStream.readUnsignedShort();
mIfdStartOffset = entry.getKey();
if (mNumOfTagInIfd * TAG_SIZE + mIfdStartOffset + OFFSET_SIZE > mApp1End) {
Log.w(TAG, "Invalid size of IFD " + mIfdType);
return EVENT_END;
}
mNeedToParseOffsetsInCurrentIfd = needToParseOffsetsInCurrentIfd();
if (((IfdEvent) event).isRequested) {
return EVENT_START_OF_IFD;
} else {
skipRemainingTagsInCurrentIfd();
}
} else if (event instanceof ImageEvent) {
mImageEvent = (ImageEvent) event;
return mImageEvent.type;
} else {
ExifTagEvent tagEvent = (ExifTagEvent) event;
mTag = tagEvent.tag;
if (mTag.getDataType() != ExifTag.TYPE_UNDEFINED) {
readFullTagValue(mTag);
checkOffsetOrImageTag(mTag);
}
if (tagEvent.isRequested) {
return EVENT_VALUE_OF_REGISTERED_TAG;
}
}
}
return EVENT_END;
|
protected static com.android.gallery3d.exif.ExifParser | parse(java.io.InputStream inputStream, int options, ExifInterface iRef)Parses the the given InputStream with the given options
return new ExifParser(inputStream, options, iRef);
|
protected static com.android.gallery3d.exif.ExifParser | parse(java.io.InputStream inputStream, ExifInterface iRef)Parses the the given InputStream with default options; that is, every IFD
and thumbnaill will be parsed.
return new ExifParser(inputStream, OPTION_IFD_0 | OPTION_IFD_1
| OPTION_IFD_EXIF | OPTION_IFD_GPS | OPTION_IFD_INTEROPERABILITY
| OPTION_THUMBNAIL, iRef);
|
private void | parseTiffHeader()
short byteOrder = mTiffStream.readShort();
if (LITTLE_ENDIAN_TAG == byteOrder) {
mTiffStream.setByteOrder(ByteOrder.LITTLE_ENDIAN);
} else if (BIG_ENDIAN_TAG == byteOrder) {
mTiffStream.setByteOrder(ByteOrder.BIG_ENDIAN);
} else {
throw new ExifInvalidFormatException("Invalid TIFF header");
}
if (mTiffStream.readShort() != TIFF_HEADER_TAIL) {
throw new ExifInvalidFormatException("Invalid TIFF header");
}
|
protected int | read(byte[] buffer, int offset, int length)Reads bytes from the InputStream.
return mTiffStream.read(buffer, offset, length);
|
protected int | read(byte[] buffer)Equivalent to read(buffer, 0, buffer.length).
return mTiffStream.read(buffer);
|
protected void | readFullTagValue(ExifTag tag)
// Some invalid images contains tags with wrong size, check it here
short type = tag.getDataType();
if (type == ExifTag.TYPE_ASCII || type == ExifTag.TYPE_UNDEFINED ||
type == ExifTag.TYPE_UNSIGNED_BYTE) {
int size = tag.getComponentCount();
if (mCorrespondingEvent.size() > 0) {
if (mCorrespondingEvent.firstEntry().getKey() < mTiffStream.getReadByteCount()
+ size) {
Object event = mCorrespondingEvent.firstEntry().getValue();
if (event instanceof ImageEvent) {
// Tag value overlaps thumbnail, ignore thumbnail.
Log.w(TAG, "Thumbnail overlaps value for tag: \n" + tag.toString());
Entry<Integer, Object> entry = mCorrespondingEvent.pollFirstEntry();
Log.w(TAG, "Invalid thumbnail offset: " + entry.getKey());
} else {
// Tag value overlaps another tag, shorten count
if (event instanceof IfdEvent) {
Log.w(TAG, "Ifd " + ((IfdEvent) event).ifd
+ " overlaps value for tag: \n" + tag.toString());
} else if (event instanceof ExifTagEvent) {
Log.w(TAG, "Tag value for tag: \n"
+ ((ExifTagEvent) event).tag.toString()
+ " overlaps value for tag: \n" + tag.toString());
}
size = mCorrespondingEvent.firstEntry().getKey()
- mTiffStream.getReadByteCount();
Log.w(TAG, "Invalid size of tag: \n" + tag.toString()
+ " setting count to: " + size);
tag.forceSetComponentCount(size);
}
}
}
}
switch (tag.getDataType()) {
case ExifTag.TYPE_UNSIGNED_BYTE:
case ExifTag.TYPE_UNDEFINED: {
byte buf[] = new byte[tag.getComponentCount()];
read(buf);
tag.setValue(buf);
}
break;
case ExifTag.TYPE_ASCII:
tag.setValue(readString(tag.getComponentCount()));
break;
case ExifTag.TYPE_UNSIGNED_LONG: {
long value[] = new long[tag.getComponentCount()];
for (int i = 0, n = value.length; i < n; i++) {
value[i] = readUnsignedLong();
}
tag.setValue(value);
}
break;
case ExifTag.TYPE_UNSIGNED_RATIONAL: {
Rational value[] = new Rational[tag.getComponentCount()];
for (int i = 0, n = value.length; i < n; i++) {
value[i] = readUnsignedRational();
}
tag.setValue(value);
}
break;
case ExifTag.TYPE_UNSIGNED_SHORT: {
int value[] = new int[tag.getComponentCount()];
for (int i = 0, n = value.length; i < n; i++) {
value[i] = readUnsignedShort();
}
tag.setValue(value);
}
break;
case ExifTag.TYPE_LONG: {
int value[] = new int[tag.getComponentCount()];
for (int i = 0, n = value.length; i < n; i++) {
value[i] = readLong();
}
tag.setValue(value);
}
break;
case ExifTag.TYPE_RATIONAL: {
Rational value[] = new Rational[tag.getComponentCount()];
for (int i = 0, n = value.length; i < n; i++) {
value[i] = readRational();
}
tag.setValue(value);
}
break;
}
if (LOGV) {
Log.v(TAG, "\n" + tag.toString());
}
|
protected int | readLong()Reads value of type {@link ExifTag#TYPE_LONG} from the InputStream.
return mTiffStream.readInt();
|
protected Rational | readRational()Reads value of type {@link ExifTag#TYPE_RATIONAL} from the InputStream.
int nomi = readLong();
int denomi = readLong();
return new Rational(nomi, denomi);
|
protected java.lang.String | readString(int n)Reads a String from the InputStream with US-ASCII charset. The parser
will read n bytes and convert it to ascii string. This is used for
reading values of type {@link ExifTag#TYPE_ASCII}.
return readString(n, US_ASCII);
|
protected java.lang.String | readString(int n, java.nio.charset.Charset charset)Reads a String from the InputStream with the given charset. The parser
will read n bytes and convert it to string. This is used for reading
values of type {@link ExifTag#TYPE_ASCII}.
if (n > 0) {
return mTiffStream.readString(n, charset);
} else {
return "";
}
|
private ExifTag | readTag()
short tagId = mTiffStream.readShort();
short dataFormat = mTiffStream.readShort();
long numOfComp = mTiffStream.readUnsignedInt();
if (numOfComp > Integer.MAX_VALUE) {
throw new ExifInvalidFormatException(
"Number of component is larger then Integer.MAX_VALUE");
}
// Some invalid image file contains invalid data type. Ignore those tags
if (!ExifTag.isValidType(dataFormat)) {
Log.w(TAG, String.format("Tag %04x: Invalid data type %d", tagId, dataFormat));
mTiffStream.skip(4);
return null;
}
// TODO: handle numOfComp overflow
ExifTag tag = new ExifTag(tagId, dataFormat, (int) numOfComp, mIfdType,
((int) numOfComp) != ExifTag.SIZE_UNDEFINED);
int dataSize = tag.getDataSize();
if (dataSize > 4) {
long offset = mTiffStream.readUnsignedInt();
if (offset > Integer.MAX_VALUE) {
throw new ExifInvalidFormatException(
"offset is larger then Integer.MAX_VALUE");
}
// Some invalid images put some undefined data before IFD0.
// Read the data here.
if ((offset < mIfd0Position) && (dataFormat == ExifTag.TYPE_UNDEFINED)) {
byte[] buf = new byte[(int) numOfComp];
System.arraycopy(mDataAboveIfd0, (int) offset - DEFAULT_IFD0_OFFSET,
buf, 0, (int) numOfComp);
tag.setValue(buf);
} else {
tag.setOffset((int) offset);
}
} else {
boolean defCount = tag.hasDefinedCount();
// Set defined count to 0 so we can add \0 to non-terminated strings
tag.setHasDefinedCount(false);
// Read value
readFullTagValue(tag);
tag.setHasDefinedCount(defCount);
mTiffStream.skip(4 - dataSize);
// Set the offset to the position of value.
tag.setOffset(mTiffStream.getReadByteCount() - 4);
}
return tag;
|
protected long | readUnsignedLong()Reads value of type {@link ExifTag#TYPE_UNSIGNED_LONG} from the
InputStream.
return readLong() & 0xffffffffL;
|
protected Rational | readUnsignedRational()Reads value of type {@link ExifTag#TYPE_UNSIGNED_RATIONAL} from the
InputStream.
long nomi = readUnsignedLong();
long denomi = readUnsignedLong();
return new Rational(nomi, denomi);
|
protected int | readUnsignedShort()Reads value of type {@link ExifTag#TYPE_UNSIGNED_SHORT} from the
InputStream.
return mTiffStream.readShort() & 0xffff;
|
private void | registerCompressedImage(long offset)
mCorrespondingEvent.put((int) offset, new ImageEvent(EVENT_COMPRESSED_IMAGE));
|
protected void | registerForTagValue(ExifTag tag)When getting {@link #EVENT_NEW_TAG} in the tag area of IFD, the tag may
not contain the value if the size of the value is greater than 4 bytes.
When the value is not available here, call this method so that the parser
will emit {@link #EVENT_VALUE_OF_REGISTERED_TAG} when it reaches the area
where the value is located.
if (tag.getOffset() >= mTiffStream.getReadByteCount()) {
mCorrespondingEvent.put(tag.getOffset(), new ExifTagEvent(tag, true));
}
|
private void | registerIfd(int ifdType, long offset)
// Cast unsigned int to int since the offset is always smaller
// than the size of APP1 (65536)
mCorrespondingEvent.put((int) offset, new IfdEvent(ifdType, isIfdRequested(ifdType)));
|
private void | registerUncompressedStrip(int stripIndex, long offset)
mCorrespondingEvent.put((int) offset, new ImageEvent(EVENT_UNCOMPRESSED_STRIP
, stripIndex));
|
private boolean | seekTiffData(java.io.InputStream inputStream)
CountedDataInputStream dataStream = new CountedDataInputStream(inputStream);
if (dataStream.readShort() != JpegHeader.SOI) {
throw new ExifInvalidFormatException("Invalid JPEG format");
}
short marker = dataStream.readShort();
while (marker != JpegHeader.EOI
&& !JpegHeader.isSofMarker(marker)) {
int length = dataStream.readUnsignedShort();
// Some invalid formatted image contains multiple APP1,
// try to find the one with Exif data.
if (marker == JpegHeader.APP1) {
int header = 0;
short headerTail = 0;
if (length >= 8) {
header = dataStream.readInt();
headerTail = dataStream.readShort();
length -= 6;
if (header == EXIF_HEADER && headerTail == EXIF_HEADER_TAIL) {
mTiffStartPosition = dataStream.getReadByteCount();
mApp1End = length;
mOffsetToApp1EndFromSOF = mTiffStartPosition + mApp1End;
return true;
}
}
}
if (length < 2 || (length - 2) != dataStream.skip(length - 2)) {
Log.w(TAG, "Invalid JPEG format.");
return false;
}
marker = dataStream.readShort();
}
return false;
|
protected void | skipRemainingTagsInCurrentIfd()Skips the tags area of current IFD, if the parser is not in the tag area,
nothing will happen.
int endOfTags = mIfdStartOffset + OFFSET_SIZE + TAG_SIZE * mNumOfTagInIfd;
int offset = mTiffStream.getReadByteCount();
if (offset > endOfTags) {
return;
}
if (mNeedToParseOffsetsInCurrentIfd) {
while (offset < endOfTags) {
mTag = readTag();
offset += TAG_SIZE;
if (mTag == null) {
continue;
}
checkOffsetOrImageTag(mTag);
}
} else {
skipTo(endOfTags);
}
long ifdOffset = readUnsignedLong();
// For ifd0, there is a link to ifd1 in the end of all tags
if (mIfdType == IfdId.TYPE_IFD_0
&& (isIfdRequested(IfdId.TYPE_IFD_1) || isThumbnailRequested())) {
if (ifdOffset > 0) {
registerIfd(IfdId.TYPE_IFD_1, ifdOffset);
}
}
|
private void | skipTo(int offset)
mTiffStream.skipTo(offset);
while (!mCorrespondingEvent.isEmpty() && mCorrespondingEvent.firstKey() < offset) {
mCorrespondingEvent.pollFirstEntry();
}
|