/packages/WallpaperCropper/src/com/android/gallery3d/exif/IfdData.java

https://github.com/aizuzi/platform_frameworks_base · Java · 152 lines · 72 code · 15 blank · 65 comment · 13 complexity · 4f6729d7e5d59b524868518824cde2eb MD5 · raw file

  1. /*
  2. * Copyright (C) 2012 The Android Open Source Project
  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. package com.android.gallery3d.exif;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. /**
  20. * This class stores all the tags in an IFD.
  21. *
  22. * @see ExifData
  23. * @see ExifTag
  24. */
  25. class IfdData {
  26. private final int mIfdId;
  27. private final Map<Short, ExifTag> mExifTags = new HashMap<Short, ExifTag>();
  28. private int mOffsetToNextIfd = 0;
  29. private static final int[] sIfds = {
  30. IfdId.TYPE_IFD_0, IfdId.TYPE_IFD_1, IfdId.TYPE_IFD_EXIF,
  31. IfdId.TYPE_IFD_INTEROPERABILITY, IfdId.TYPE_IFD_GPS
  32. };
  33. /**
  34. * Creates an IfdData with given IFD ID.
  35. *
  36. * @see IfdId#TYPE_IFD_0
  37. * @see IfdId#TYPE_IFD_1
  38. * @see IfdId#TYPE_IFD_EXIF
  39. * @see IfdId#TYPE_IFD_GPS
  40. * @see IfdId#TYPE_IFD_INTEROPERABILITY
  41. */
  42. IfdData(int ifdId) {
  43. mIfdId = ifdId;
  44. }
  45. static protected int[] getIfds() {
  46. return sIfds;
  47. }
  48. /**
  49. * Get a array the contains all {@link ExifTag} in this IFD.
  50. */
  51. protected ExifTag[] getAllTags() {
  52. return mExifTags.values().toArray(new ExifTag[mExifTags.size()]);
  53. }
  54. /**
  55. * Gets the ID of this IFD.
  56. *
  57. * @see IfdId#TYPE_IFD_0
  58. * @see IfdId#TYPE_IFD_1
  59. * @see IfdId#TYPE_IFD_EXIF
  60. * @see IfdId#TYPE_IFD_GPS
  61. * @see IfdId#TYPE_IFD_INTEROPERABILITY
  62. */
  63. protected int getId() {
  64. return mIfdId;
  65. }
  66. /**
  67. * Gets the {@link ExifTag} with given tag id. Return null if there is no
  68. * such tag.
  69. */
  70. protected ExifTag getTag(short tagId) {
  71. return mExifTags.get(tagId);
  72. }
  73. /**
  74. * Adds or replaces a {@link ExifTag}.
  75. */
  76. protected ExifTag setTag(ExifTag tag) {
  77. tag.setIfd(mIfdId);
  78. return mExifTags.put(tag.getTagId(), tag);
  79. }
  80. protected boolean checkCollision(short tagId) {
  81. return mExifTags.get(tagId) != null;
  82. }
  83. /**
  84. * Removes the tag of the given ID
  85. */
  86. protected void removeTag(short tagId) {
  87. mExifTags.remove(tagId);
  88. }
  89. /**
  90. * Gets the tags count in the IFD.
  91. */
  92. protected int getTagCount() {
  93. return mExifTags.size();
  94. }
  95. /**
  96. * Sets the offset of next IFD.
  97. */
  98. protected void setOffsetToNextIfd(int offset) {
  99. mOffsetToNextIfd = offset;
  100. }
  101. /**
  102. * Gets the offset of next IFD.
  103. */
  104. protected int getOffsetToNextIfd() {
  105. return mOffsetToNextIfd;
  106. }
  107. /**
  108. * Returns true if all tags in this two IFDs are equal. Note that tags of
  109. * IFDs offset or thumbnail offset will be ignored.
  110. */
  111. @Override
  112. public boolean equals(Object obj) {
  113. if (this == obj) {
  114. return true;
  115. }
  116. if (obj == null) {
  117. return false;
  118. }
  119. if (obj instanceof IfdData) {
  120. IfdData data = (IfdData) obj;
  121. if (data.getId() == mIfdId && data.getTagCount() == getTagCount()) {
  122. ExifTag[] tags = data.getAllTags();
  123. for (ExifTag tag : tags) {
  124. if (ExifInterface.isOffsetTag(tag.getTagId())) {
  125. continue;
  126. }
  127. ExifTag tag2 = mExifTags.get(tag.getTagId());
  128. if (!tag.equals(tag2)) {
  129. return false;
  130. }
  131. }
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. }