summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/mpo/MpoImageData.java
blob: 84d2d0f6e42796fee001587c3388f12b56462768 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * Copyright (c) 2016, The Linux Foundation. All rights reserved.
 * Not a contribution.
 *
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera.mpo;

import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;

/**
 * This class stores the MPO header in IFDs according to the MPO specification.
 */

public class MpoImageData {
    private static final String TAG = "MpoImageData";
    static final int OFFSET_TO_FIRST_IFD = 8;
    static final int MP_FORMAT_IDENTIFIER = 0x4D504600; // 'M' 'P' 'F' 'NULL'
    static final int MP_HEADER_SIZE = 8;
    static final int APP_HEADER_SIZE = 6;

    private final MpoIfdData mMpIndexIfdData = new MpoIfdData(MpoIfdData.TYPE_MP_INDEX_IFD);
    private final MpoIfdData mMpAttribIfdData = new MpoIfdData(MpoIfdData.TYPE_MP_ATTRIB_IFD);
    private final byte[] mJpegData;
    private final ByteOrder mByteOrder;

    public MpoImageData(byte[] jpegData, ByteOrder byteOrder) {
        mJpegData = jpegData;
        mByteOrder = byteOrder;
    }

    /**
     * Gets the jpeg data.
     */
    protected byte[] getJpegData() {
        return mJpegData;
    }

    /**
     * Gets the byte order.
     */
    protected ByteOrder getByteOrder() {
        return mByteOrder;
    }

    /**
     * Returns the {@link mMpAttribIfdData} object if it exists or null.
     */
    protected MpoIfdData getAttribIfdData() {
        return mMpAttribIfdData;
    }

    /**
     * Returns the {@link mMpIndexIfdData} object if it exists or null.
     */
    protected MpoIfdData getIndexIfdData() {
        return mMpIndexIfdData;
    }

    /**
     * Returns the {@link MpoIfdData} object corresponding to a given IFD.
     */
    protected MpoIfdData getMpIfdData(int ifdId) {
        return (ifdId == MpoIfdData.TYPE_MP_INDEX_IFD) ? mMpIndexIfdData : mMpAttribIfdData;
    }

    /**
     * Returns the tag with a given tag ID in the given IFD if the tag exists.
     * Otherwise returns null.
     */
    protected MpoTag getTag(short tag, int ifd) {
        MpoIfdData mpIfdData = getMpIfdData(ifd);
        return mpIfdData.getTag(tag);
    }

    /**
     * Adds the given MpoTag to its default IFD and returns an existing MpoTag
     * with the same TID or null if none exist.
     */
    protected MpoTag addTag(MpoTag tag) {
        if (tag != null) {
            int ifd = tag.getIfd();
            return addTag(tag, ifd);
        }
        return null;
    }

    /**
     * Adds the given MpoTag to the given IFD and returns an existing MpoTag
     * with the same tag ID or null if none exist.
     */
    protected MpoTag addTag(MpoTag tag, int ifdId) {
        if (tag != null && MpoTag.isValidIfd(ifdId)) {
            return getMpIfdData(ifdId).setTag(tag);
        }
        return null;
    }

    /**
     * Removes the tag with a given tag ID and IFD.
     */
    protected void removeTag(short tagId, int ifdId) {
        getMpIfdData(ifdId).removeTag(tagId);
    }

    /**
     * Returns a list of all {@link MpoTag}s in the ExifData or null if there
     * are none.
     */
    protected List<MpoTag> getAllTags() {
        ArrayList<MpoTag> ret = new ArrayList<MpoTag>();
        MpoTag[] tags = mMpIndexIfdData.getAllTags();
        if (tags != null) {
            for (MpoTag t : tags) {
                ret.add(t);
            }
        }

        tags = mMpAttribIfdData.getAllTags();
        if (tags != null) {
            for (MpoTag t : tags) {
                ret.add(t);
            }
        }

        if (ret.size() == 0) {
            return null;
        }
        return ret;
    }

    /**
     * Returns a list of all {@link MpoTag}s in a given IFD or null if there are
     * none.
     */
    protected List<MpoTag> getAllTagsForIfd(int ifd) {
        MpoTag[] tags = getMpIfdData(ifd).getAllTags();
        if (tags == null) {
            return null;
        }
        ArrayList<MpoTag> ret = new ArrayList<MpoTag>(tags.length);
        for (MpoTag t : tags) {
            ret.add(t);
        }
        if (ret.size() == 0) {
            return null;
        }
        return ret;
    }

    /**
     * Returns a list of all {@link MpoTag}s with a given TID or null if there
     * are none.
     */
    protected List<MpoTag> getAllTagsForTagId(short tag) {
        ArrayList<MpoTag> ret = new ArrayList<MpoTag>();
        MpoTag t = mMpIndexIfdData.getTag(tag);
        if (t != null) {
            ret.add(t);
        }

        t = mMpAttribIfdData.getTag(tag);
        if (t != null) {
            ret.add(t);
        }

        if (ret.size() == 0) {
            return null;
        }
        return ret;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (obj instanceof MpoImageData) {
            MpoImageData data = (MpoImageData) obj;
            if (data.mByteOrder != mByteOrder) {
                return false;
            }

            MpoIfdData indexIfd1 = data.getMpIfdData(MpoIfdData.TYPE_MP_INDEX_IFD);
            MpoIfdData indexIfd2 = getMpIfdData(MpoIfdData.TYPE_MP_INDEX_IFD);
            if (indexIfd1 != indexIfd2 && indexIfd1 != null && !indexIfd1.equals(indexIfd2)) {
                return false;
            }

            MpoIfdData attribIfd1 = data.getMpIfdData(MpoIfdData.TYPE_MP_ATTRIB_IFD);
            MpoIfdData attribIfd2 = getMpIfdData(MpoIfdData.TYPE_MP_ATTRIB_IFD);
            if (attribIfd1 != attribIfd2 && attribIfd1 != null && !attribIfd1.equals(attribIfd2)) {
                return false;
            }
            return true;
        }
        return false;
    }

    private int calculateOffsetOfIfd(MpoIfdData ifd, int offset) {
        offset += 2 + ifd.getTagCount() * MpoTag.TAG_SIZE + 4;
        MpoTag[] tags = ifd.getAllTags();
        for (MpoTag tag : tags) {
            if (tag.getDataSize() > 4) {
                tag.setOffset(offset);
                offset += tag.getDataSize();
            }
        }
        return offset;
    }

    public int calculateAllIfdOffsets() {
        int offset = MP_HEADER_SIZE;
        MpoIfdData indexIfd = getIndexIfdData();
        if (indexIfd.getTagCount() > 0)
            offset = calculateOffsetOfIfd(indexIfd, offset);

        MpoIfdData attribIfd = getAttribIfdData();
        if (attribIfd.getTagCount() > 0) {
            indexIfd.setOffsetToNextIfd(offset);
            offset = calculateOffsetOfIfd(attribIfd, offset);
        }

        return offset;
    }

    public int calculateImageSize() {
        return 2 + APP_HEADER_SIZE + calculateAllIfdOffsets() + mJpegData.length;
    }
}