Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0cac9b6f authored by Jin Seok Park's avatar Jin Seok Park
Browse files

Add support for reading XMP from HEIF

Bug: 159373092
Test: atest CtsMediaTestCases:android.media.cts.ExifInterfaceTest
Change-Id: Ia595ffd3eb4099ba3d0f3edb79008801b205cbee
parent c2df8ea9
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -83,6 +83,10 @@ import java.util.zip.CRC32;
 * <p>
 * Supported for writing: JPEG, PNG, WebP.
 * <p>
 * Note: JPEG and HEIF files may contain XMP data either inside the Exif data chunk or outside of
 * it. This class will search both locations for XMP data, but if XMP data exist both inside and
 * outside Exif, will favor the XMP data inside Exif over the one outside.
 * <p>
 * Note: It is recommended to use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
 * <a href="{@docRoot}reference/androidx/exifinterface/media/ExifInterface.html">ExifInterface
 * Library</a> since it is a superset of this class. In addition to the functionalities of this
@@ -3185,6 +3189,24 @@ public class ExifInterface {
                readExifSegment(bytes, IFD_TYPE_PRIMARY);
            }

            String xmpOffsetStr = retriever.extractMetadata(
                    MediaMetadataRetriever.METADATA_KEY_XMP_OFFSET);
            String xmpLengthStr = retriever.extractMetadata(
                    MediaMetadataRetriever.METADATA_KEY_XMP_LENGTH);
            if (xmpOffsetStr != null && xmpLengthStr != null) {
                int offset = Integer.parseInt(xmpOffsetStr);
                int length = Integer.parseInt(xmpLengthStr);
                in.seek(offset);
                byte[] xmpBytes = new byte[length];
                if (in.read(xmpBytes) != length) {
                    throw new IOException("Failed to read XMP from HEIF");
                }
                if (getAttribute(TAG_XMP) == null) {
                    mAttributes[IFD_TYPE_PRIMARY].put(TAG_XMP, new ExifAttribute(
                            IFD_FORMAT_BYTE, xmpBytes.length, offset, xmpBytes));
                }
            }

            if (DEBUG) {
                Log.d(TAG, "Heif meta: " + width + "x" + height + ", rotation " + rotation);
            }