1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_PARSER_ITEM_DATA_BOX_H
17 #define PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_PARSER_ITEM_DATA_BOX_H
18 
19 #include "box/heif_box.h"
20 
21 namespace OHOS {
22 namespace ImagePlugin {
23 class HeifIlocBox : public HeifFullBox {
24 public:
HeifIlocBox()25     HeifIlocBox() : HeifFullBox(BOX_TYPE_ILOC) {}
26 
27     struct Extent {
28         uint64_t index = 0;
29         uint64_t length = 0;
30         uint64_t offset = 0;
31         std::vector<uint8_t> data;
32     };
33 
34     struct Item {
35         heif_item_id itemId = 0;
36         uint8_t constructionMethod = 0;
37         uint16_t dataReferenceIndex = 0;
38         uint64_t baseOffset = 0;
39         std::vector<Extent> extents;
40 
GetExtentsTotalSizeItem41         size_t GetExtentsTotalSize() const
42         {
43             size_t total = 0;
44             for (auto &extent: extents) {
45                 total += extent.data.size();
46             }
47             return total;
48         };
49     };
50 
GetItems()51     const std::vector<Item> &GetItems() const { return items_; }
52 
53     heif_error GetIlocDataLength(const Item &item, size_t &length);
54 
55     heif_error ReadData(const Item &item,
56                     const std::shared_ptr<HeifInputStream> &stream,
57                     const std::shared_ptr<class HeifIdatBox> &idat,
58                     std::vector<uint8_t> *dest) const;
59 
60     heif_error AppendData(heif_item_id itemId,
61                      const std::vector<uint8_t> &data,
62                      uint8_t constructionMethod = 0);
63 
64     heif_error UpdateData(heif_item_id itemID, const std::vector<uint8_t> &data, uint8_t constructionMethod);
65 
66     void InferFullBoxVersion() override;
67 
68     heif_error Write(HeifStreamWriter &writer) const override;
69 
70     heif_error WriteMdatBox(HeifStreamWriter &writer);
71 
72     heif_error ReadToExtentData(Item &item, const std::shared_ptr<HeifInputStream> &stream,
73                                 const std::shared_ptr<HeifIdatBox> &idatBox);
74 
75 protected:
76     heif_error ParseContent(HeifStreamReader &reader) override;
77 
78 private:
79     std::vector<Item> items_;
80 
81     mutable size_t startPos_ = 0;
82     uint8_t offsetSize_ = 0;
83     uint8_t lengthSize_ = 0;
84     uint8_t baseOffsetSize_ = 0;
85     uint8_t indexSize_ = 0;
86     void ParseExtents(Item& item, HeifStreamReader &reader, int indexSize, int offsetSize, int lengthSize);
87     void PackIlocHeader(HeifStreamWriter &writer) const;
88 
89     uint64_t idatOffset_ = 0;
90 };
91 
92 class HeifIdatBox : public HeifBox {
93 public:
94     heif_error ReadData(const std::shared_ptr<HeifInputStream> &stream,
95                     uint64_t start, uint64_t length,
96                     std::vector<uint8_t> &outData) const;
97 
AppendData(const std::vector<uint8_t> & data)98     int AppendData(const std::vector<uint8_t> &data)
99     {
100         auto pos = dataForWriting_.size();
101 
102         dataForWriting_.insert(dataForWriting_.end(),
103                                data.begin(),
104                                data.end());
105 
106         return (int) pos;
107     }
108 
109     heif_error Write(HeifStreamWriter &writer) const override;
110 
111 protected:
112     heif_error ParseContent(HeifStreamReader &reader) override;
113 
114     std::streampos startPos_;
115 
116     std::vector<uint8_t> dataForWriting_;
117 };
118 } // namespace ImagePlugin
119 } // namespace OHOS
120 
121 #endif // PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_PARSER_ITEM_DATA_BOX_H
122