1 /*
2  * Copyright (C) 2022 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 INTERFACES_INNERKITS_INCLUDE_IMAGE_PACKER_H
17 #define INTERFACES_INNERKITS_INCLUDE_IMAGE_PACKER_H
18 
19 #include <set>
20 #include <vector>
21 #include "image_source.h"
22 #include "image_type.h"
23 #include "nocopyable.h"
24 #include "pixel_map.h"
25 
26 namespace OHOS {
27 namespace MultimediaPlugin {
28 class PluginServer;
29 } // namespace MultimediaPlugin
30 } // namespace OHOS
31 
32 namespace OHOS {
33 namespace ImagePlugin {
34 struct PlEncodeOptions;
35 class AbsImageEncoder;
36 } // namespace ImagePlugin
37 } // namespace OHOS
38 
39 namespace OHOS {
40 namespace Media {
41 struct PackOption {
42     /**
43      * Specify the file format of the output image.
44      */
45     std::string format;
46     /**
47      * Hint to the compression quality, 0-100.
48      * Larger values indicate higher image quality but usually take up larger sizes.
49      */
50     uint8_t quality = 100;
51 
52     /**
53      * Hint to how many images will be packed into the image file.
54      */
55     uint32_t numberHint = 1;
56 
57     /**
58      * desired image dynamic range.
59     */
60     EncodeDynamicRange desiredDynamicRange = EncodeDynamicRange::SDR;
61 
62     /**
63      * Specify the number of times the loop should.
64      * 0 means infinite loop.
65      * Only for gif.
66      */
67     uint16_t loop = 0;
68 
69     /**
70      * Specify the delay time for each frame of the dynamic image.
71      * Only for gif.
72      */
73     std::vector<uint16_t> delayTimes;
74 
75     /**
76      * Specify the the decoder process each frame after displaying it.
77      * Only for gif.
78      */
79     std::vector<uint8_t> disposalTypes;
80 
81     /**
82      * Hint to pack image with properties.
83     */
84     bool needsPackProperties = false;
85 
86     /**
87      * Paking for edit scene.
88      */
89     bool isEditScene = true;
90 };
91 
92 class PackerStream;
93 
94 class ImagePacker {
95 public:
96     ImagePacker();
97     ~ImagePacker();
98     static uint32_t GetSupportedFormats(std::set<std::string> &formats);
99     uint32_t StartPacking(uint8_t *data, uint32_t maxSize, const PackOption &option);
100     uint32_t StartPacking(const std::string &filePath, const PackOption &option);
101     uint32_t StartPacking(const int &fd, const PackOption &option);
102     uint32_t StartPacking(std::ostream &outputStream, const PackOption &option);
103     uint32_t AddImage(PixelMap &pixelMap);
104     uint32_t AddImage(ImageSource &source);
105     uint32_t AddImage(ImageSource &source, uint32_t index);
106 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
107     uint32_t AddPicture(Picture &picture);
108 #endif
109     uint32_t FinalizePacking();
110     uint32_t FinalizePacking(int64_t &packedSize);
111 
112 protected:
113     uint32_t StartPackingAdapter(PackerStream &outputStream, const PackOption &option);
114 
115 private:
116     DISALLOW_COPY_AND_MOVE(ImagePacker);
117     static void CopyOptionsToPlugin(const PackOption &opts, ImagePlugin::PlEncodeOptions &plOpts);
118     uint32_t StartPackingImpl(const PackOption &option);
119     uint32_t DoEncodingFunc(std::function<uint32_t(ImagePlugin::AbsImageEncoder*)> func, bool forAll = true);
120     bool GetEncoderPlugin(const PackOption &option);
121     void FreeOldPackerStream();
122     bool IsPackOptionValid(const PackOption &option);
123     static MultimediaPlugin::PluginServer &pluginServer_;
124     std::unique_ptr<PackerStream> packerStream_;
125     std::vector<std::unique_ptr<ImagePlugin::AbsImageEncoder>> encoders_;
126     std::unique_ptr<ImagePlugin::AbsImageEncoder> encoder_;
127     std::unique_ptr<ImagePlugin::AbsImageEncoder> exEncoder_;
128     std::unique_ptr<PixelMap> pixelMap_;  // inner imagesource create, our manage the lifecycle
129     bool encodeToSdr_ = true;
130 };
131 } // namespace Media
132 } // namespace OHOS
133 
134 #endif // INTERFACES_INNERKITS_INCLUDE_IMAGE_PACKER_H
135