xref: /aosp_15_r20/external/libultrahdr/lib/src/multipictureformat.cpp (revision 89a0ef05262152531a00a15832a2d3b1e3990773)
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 
17 #include "ultrahdr/multipictureformat.h"
18 
19 namespace 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(size_t primary_image_size,size_t primary_image_offset,size_t secondary_image_size,size_t secondary_image_offset)30 std::shared_ptr<DataStruct> generateMpf(size_t primary_image_size, size_t primary_image_offset,
31                                         size_t secondary_image_size,
32                                         size_t secondary_image_offset) {
33   size_t mpf_size = calculateMpfSize();
34   std::shared_ptr<DataStruct> dataStruct = std::make_shared<DataStruct>(mpf_size);
35 
36   dataStruct->write(static_cast<const void*>(kMpfSig), sizeof(kMpfSig));
37 #if USE_BIG_ENDIAN_IN_MPF
38   dataStruct->write(static_cast<const void*>(kMpBigEndian), kMpEndianSize);
39 #else
40   dataStruct->write(static_cast<const void*>(kMpLittleEndian), kMpEndianSize);
41 #endif
42 
43   // Set the Index IFD offset be the position after the endianness value and this offset.
44   constexpr uint32_t indexIfdOffset = 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(Endian_SwapBE32(kMPEntryAttributeFormatJpeg | kMPEntryAttributeTypePrimary));
78   dataStruct->write32(Endian_SwapBE32(primary_image_size));
79   dataStruct->write32(Endian_SwapBE32(primary_image_offset));
80   dataStruct->write16(0);
81   dataStruct->write16(0);
82 
83   // Write the MP entries for secondary image
84   dataStruct->write32(Endian_SwapBE32(kMPEntryAttributeFormatJpeg));
85   dataStruct->write32(Endian_SwapBE32(secondary_image_size));
86   dataStruct->write32(Endian_SwapBE32(secondary_image_offset));
87   dataStruct->write16(0);
88   dataStruct->write16(0);
89 
90   return dataStruct;
91 }
92 
93 }  // namespace ultrahdr
94