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 "image_source.h" 21 #include "image_type.h" 22 #include "nocopyable.h" 23 #include "pixel_map.h" 24 25 namespace OHOS { 26 namespace MultimediaPlugin { 27 class PluginServer; 28 } // namespace MultimediaPlugin 29 } // namespace OHOS 30 31 namespace OHOS { 32 namespace ImagePlugin { 33 struct PlEncodeOptions; 34 class AbsImageEncoder; 35 } // namespace ImagePlugin 36 } // namespace OHOS 37 38 namespace OHOS { 39 namespace Media { 40 struct PackOption { 41 /** 42 * Specify the file format of the output image. 43 */ 44 std::string format; 45 /** 46 * Hint to the compression quality, 0-100. 47 * Larger values indicate higher image quality but usually take up larger sizes. 48 */ 49 uint8_t quality = 100; 50 51 /** 52 * Hint to how many images will be packed into the image file. 53 */ 54 uint32_t numberHint = 1; 55 }; 56 57 class PackerStream; 58 59 class ImagePacker { 60 public: 61 ImagePacker(); 62 ~ImagePacker(); 63 static uint32_t GetSupportedFormats(std::set<std::string> &formats); 64 uint32_t StartPacking(uint8_t *data, uint32_t maxSize, const PackOption &option); 65 uint32_t StartPacking(const std::string &filePath, const PackOption &option); 66 uint32_t StartPacking(const int &fd, const PackOption &option); 67 uint32_t StartPacking(std::ostream &outputStream, const PackOption &option); 68 uint32_t AddImage(PixelMap &pixelMap); 69 uint32_t AddImage(ImageSource &source); 70 uint32_t AddImage(ImageSource &source, uint32_t index); 71 uint32_t FinalizePacking(); 72 uint32_t FinalizePacking(int64_t &packedSize); 73 74 protected: 75 uint32_t StartPackingAdapter(PackerStream &outputStream, const PackOption &option); 76 77 private: 78 DISALLOW_COPY_AND_MOVE(ImagePacker); 79 static void CopyOptionsToPlugin(const PackOption &opts, ImagePlugin::PlEncodeOptions &plOpts); 80 uint32_t StartPackingImpl(const PackOption &option); 81 bool GetEncoderPlugin(const PackOption &option); 82 void FreeOldPackerStream(); 83 bool IsPackOptionValid(const PackOption &option); 84 static MultimediaPlugin::PluginServer &pluginServer_; 85 std::unique_ptr<PackerStream> packerStream_; 86 std::unique_ptr<ImagePlugin::AbsImageEncoder> encoder_; 87 std::unique_ptr<PixelMap> pixelMap_; // inner imagesource create, our manage the lifecycle 88 }; 89 } // namespace Media 90 } // namespace OHOS 91 92 #endif // INTERFACES_INNERKITS_INCLUDE_IMAGE_PACKER_H_ 93