• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 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 #include <ultrahdr/multipictureformat.h>
17 #include <ultrahdr/jpegrutils.h>
18 
19 namespace android::ultrahdr {
calculateMpfSize()20 size_t calculateMpfSize() {
21     return sizeof(kMpfSig) +                 // Signature
22             kMpEndianSize +                   // Endianness
23             sizeof(uint32_t) +                // Index IFD Offset
24             sizeof(uint16_t) +                // Tag count
25             kTagSerializedCount * kTagSize +  // 3 tags at 12 bytes each
26             sizeof(uint32_t) +                // Attribute IFD offset
27             kNumPictures * kMPEntrySize;      // MP Entries for each image
28 }
29 
generateMpf(int primary_image_size,int primary_image_offset,int secondary_image_size,int secondary_image_offset)30 sp<DataStruct> generateMpf(int primary_image_size, int primary_image_offset,
31         int secondary_image_size, int secondary_image_offset) {
32     size_t mpf_size = calculateMpfSize();
33     sp<DataStruct> dataStruct = sp<DataStruct>::make(mpf_size);
34 
35     dataStruct->write(static_cast<const void*>(kMpfSig), sizeof(kMpfSig));
36 #if USE_BIG_ENDIAN
37     dataStruct->write(static_cast<const void*>(kMpBigEndian), kMpEndianSize);
38 #else
39     dataStruct->write(static_cast<const void*>(kMpLittleEndian), kMpEndianSize);
40 #endif
41 
42     // Set the Index IFD offset be the position after the endianness value and this offset.
43     constexpr uint32_t indexIfdOffset =
44             static_cast<uint16_t>(kMpEndianSize + sizeof(kMpfSig));
45     dataStruct->write32(Endian_SwapBE32(indexIfdOffset));
46 
47     // We will write 3 tags (version, number of images, MP entries).
48     dataStruct->write16(Endian_SwapBE16(kTagSerializedCount));
49 
50     // Write the version tag.
51     dataStruct->write16(Endian_SwapBE16(kVersionTag));
52     dataStruct->write16(Endian_SwapBE16(kVersionType));
53     dataStruct->write32(Endian_SwapBE32(kVersionCount));
54     dataStruct->write(kVersionExpected, kVersionSize);
55 
56     // Write the number of images.
57     dataStruct->write16(Endian_SwapBE16(kNumberOfImagesTag));
58     dataStruct->write16(Endian_SwapBE16(kNumberOfImagesType));
59     dataStruct->write32(Endian_SwapBE32(kNumberOfImagesCount));
60     dataStruct->write32(Endian_SwapBE32(kNumPictures));
61 
62     // Write the MP entries.
63     dataStruct->write16(Endian_SwapBE16(kMPEntryTag));
64     dataStruct->write16(Endian_SwapBE16(kMPEntryType));
65     dataStruct->write32(Endian_SwapBE32(kMPEntrySize * kNumPictures));
66     const uint32_t mpEntryOffset =
67             static_cast<uint32_t>(dataStruct->getBytesWritten() -  // The bytes written so far
68                                   sizeof(kMpfSig) +   // Excluding the MPF signature
69                                   sizeof(uint32_t) +  // The 4 bytes for this offset
70                                   sizeof(uint32_t));  // The 4 bytes for the attribute IFD offset.
71     dataStruct->write32(Endian_SwapBE32(mpEntryOffset));
72 
73     // Write the attribute IFD offset (zero because we don't write it).
74     dataStruct->write32(0);
75 
76     // Write the MP entries for primary image
77     dataStruct->write32(
78             Endian_SwapBE32(kMPEntryAttributeFormatJpeg | kMPEntryAttributeTypePrimary));
79     dataStruct->write32(Endian_SwapBE32(primary_image_size));
80     dataStruct->write32(Endian_SwapBE32(primary_image_offset));
81     dataStruct->write16(0);
82     dataStruct->write16(0);
83 
84     // Write the MP entries for secondary image
85     dataStruct->write32(Endian_SwapBE32(kMPEntryAttributeFormatJpeg));
86     dataStruct->write32(Endian_SwapBE32(secondary_image_size));
87     dataStruct->write32(Endian_SwapBE32(secondary_image_offset));
88     dataStruct->write16(0);
89     dataStruct->write16(0);
90 
91     return dataStruct;
92 }
93 
94 } // namespace android::ultrahdr
95