• 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 
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(int primary_image_size,int primary_image_offset,int secondary_image_size,int secondary_image_offset)30 std::shared_ptr<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   std::shared_ptr<DataStruct> dataStruct = std::make_shared<DataStruct>(mpf_size);
34 
35   dataStruct->write(static_cast<const void*>(kMpfSig), sizeof(kMpfSig));
36 #if USE_BIG_ENDIAN_IN_MPF
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 = static_cast<uint16_t>(kMpEndianSize + sizeof(kMpfSig));
44   dataStruct->write32(Endian_SwapBE32(indexIfdOffset));
45 
46   // We will write 3 tags (version, number of images, MP entries).
47   dataStruct->write16(Endian_SwapBE16(kTagSerializedCount));
48 
49   // Write the version tag.
50   dataStruct->write16(Endian_SwapBE16(kVersionTag));
51   dataStruct->write16(Endian_SwapBE16(kVersionType));
52   dataStruct->write32(Endian_SwapBE32(kVersionCount));
53   dataStruct->write(kVersionExpected, kVersionSize);
54 
55   // Write the number of images.
56   dataStruct->write16(Endian_SwapBE16(kNumberOfImagesTag));
57   dataStruct->write16(Endian_SwapBE16(kNumberOfImagesType));
58   dataStruct->write32(Endian_SwapBE32(kNumberOfImagesCount));
59   dataStruct->write32(Endian_SwapBE32(kNumPictures));
60 
61   // Write the MP entries.
62   dataStruct->write16(Endian_SwapBE16(kMPEntryTag));
63   dataStruct->write16(Endian_SwapBE16(kMPEntryType));
64   dataStruct->write32(Endian_SwapBE32(kMPEntrySize * kNumPictures));
65   const uint32_t mpEntryOffset =
66       static_cast<uint32_t>(dataStruct->getBytesWritten() -  // The bytes written so far
67                             sizeof(kMpfSig) +                // Excluding the MPF signature
68                             sizeof(uint32_t) +               // The 4 bytes for this offset
69                             sizeof(uint32_t));  // The 4 bytes for the attribute IFD offset.
70   dataStruct->write32(Endian_SwapBE32(mpEntryOffset));
71 
72   // Write the attribute IFD offset (zero because we don't write it).
73   dataStruct->write32(0);
74 
75   // Write the MP entries for primary image
76   dataStruct->write32(Endian_SwapBE32(kMPEntryAttributeFormatJpeg | kMPEntryAttributeTypePrimary));
77   dataStruct->write32(Endian_SwapBE32(primary_image_size));
78   dataStruct->write32(Endian_SwapBE32(primary_image_offset));
79   dataStruct->write16(0);
80   dataStruct->write16(0);
81 
82   // Write the MP entries for secondary image
83   dataStruct->write32(Endian_SwapBE32(kMPEntryAttributeFormatJpeg));
84   dataStruct->write32(Endian_SwapBE32(secondary_image_size));
85   dataStruct->write32(Endian_SwapBE32(secondary_image_offset));
86   dataStruct->write16(0);
87   dataStruct->write16(0);
88 
89   return dataStruct;
90 }
91 
92 }  // namespace ultrahdr
93