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 class PackerStream; 59 60 class ImagePacker { 61 public: 62 ImagePacker(); 63 ~ImagePacker(); 64 static uint32_t GetSupportedFormats(std::set<std::string> &formats); 65 uint32_t StartPacking(uint8_t *data, uint32_t maxSize, const PackOption &option); 66 uint32_t StartPacking(const std::string &filePath, const PackOption &option); 67 uint32_t StartPacking(const int &fd, const PackOption &option); 68 uint32_t StartPacking(std::ostream &outputStream, const PackOption &option); 69 uint32_t AddImage(PixelMap &pixelMap); 70 uint32_t AddImage(ImageSource &source); 71 uint32_t AddImage(ImageSource &source, uint32_t index); 72 uint32_t FinalizePacking(); 73 uint32_t FinalizePacking(int64_t &packedSize); 74 75 protected: 76 uint32_t StartPackingAdapter(PackerStream &outputStream, const PackOption &option); 77 78 private: 79 DISALLOW_COPY_AND_MOVE(ImagePacker); 80 static void CopyOptionsToPlugin(const PackOption &opts, ImagePlugin::PlEncodeOptions &plOpts); 81 uint32_t StartPackingImpl(const PackOption &option); 82 uint32_t DoEncodingFunc(std::function<uint32_t(ImagePlugin::AbsImageEncoder*)> func, bool forAll = true); 83 bool GetEncoderPlugin(const PackOption &option); 84 void FreeOldPackerStream(); 85 bool IsPackOptionValid(const PackOption &option); 86 static MultimediaPlugin::PluginServer &pluginServer_; 87 std::unique_ptr<PackerStream> packerStream_; 88 std::vector<std::unique_ptr<ImagePlugin::AbsImageEncoder>> encoders_; 89 std::unique_ptr<ImagePlugin::AbsImageEncoder> encoder_; 90 std::unique_ptr<ImagePlugin::AbsImageEncoder> exEncoder_; 91 std::unique_ptr<PixelMap> pixelMap_; // inner imagesource create, our manage the lifecycle 92 }; 93 } // namespace Media 94 } // namespace OHOS 95 96 #endif // INTERFACES_INNERKITS_INCLUDE_IMAGE_PACKER_H 97