• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef IMAGE_PACKER_IMPL_H
16 #define IMAGE_PACKER_IMPL_H
17 
18 #include "cj_ffi/cj_common_ffi.h"
19 #include "ffi_remote_data.h"
20 #include "image_log.h"
21 #include "image_packer.h"
22 #include "inttypes.h"
23 #include "media_errors.h"
24 #include "picture_impl.h"
25 #include "pixel_map_impl.h"
26 
27 namespace OHOS {
28 namespace Media {
29 class ImagePackerImpl : public OHOS::FFI::FFIData {
30     DECL_TYPE(ImagePackerImpl, OHOS::FFI::FFIData)
31 public:
32     ImagePackerImpl();
33     std::tuple<int32_t, uint8_t*, int64_t> Packing(PixelMap& source, const PackOption& option, uint64_t bufferSize);
34     std::tuple<int32_t, uint8_t*, int64_t> Packing(ImageSource& source, const PackOption& option, uint64_t bufferSize);
35     std::tuple<int32_t, uint8_t*, int64_t> PackToData(
36         std::shared_ptr<PixelMap> source, const PackOption& option, uint64_t bufferSize);
37     std::tuple<int32_t, uint8_t*, int64_t> PackToData(
38         std::shared_ptr<ImageSource> source, const PackOption& option, uint64_t bufferSize);
39     std::tuple<int32_t, uint8_t*, int64_t> PackToData(
40         std::shared_ptr<Picture> source, const PackOption& option, uint64_t bufferSize);
41     uint32_t PackToFile(std::shared_ptr<PixelMap> source, int fd, const PackOption& option);
42     uint32_t PackToFile(std::shared_ptr<ImageSource> source, int fd, const PackOption& option);
43     uint32_t PackToFile(std::shared_ptr<Picture> source, int fd, const PackOption& option);
44     std::shared_ptr<ImagePacker> GetImagePacker();
45 
Release()46     void Release()
47     {
48         real_.reset();
49     }
50 
51     template<typename T>
CommonPacking(T & source,const PackOption & option,uint64_t bufferSize)52     std::tuple<int32_t, uint8_t*, int64_t> CommonPacking(T& source, const PackOption& option, uint64_t bufferSize)
53     {
54         if (real_ == nullptr) {
55             IMAGE_LOGE("Packing failed, real_ is nullptr");
56             return std::make_tuple(ERR_IMAGE_INIT_ABNORMAL, nullptr, 0);
57         }
58 
59         if (bufferSize <= 0 || bufferSize > INT32_MAX) {
60             IMAGE_LOGE("Packing failed, bufferSize cannot be less than or equal to 0 or more than INT32_MAX");
61             return std::make_tuple(ERR_IMAGE_INIT_ABNORMAL, nullptr, 0);
62         }
63 
64         uint8_t* resultBuffer = static_cast<uint8_t*>(malloc(sizeof(uint8_t) * bufferSize));
65         if (resultBuffer == nullptr) {
66             IMAGE_LOGE("Packing failed, malloc buffer failed");
67             return std::make_tuple(ERR_IMAGE_INIT_ABNORMAL, nullptr, 0);
68         }
69 
70         uint32_t packingRet = real_->StartPacking(resultBuffer, bufferSize, option);
71         if (packingRet != SUCCESS) {
72             IMAGE_LOGE("Packing failed, StartPacking failed, ret=%{public}u.", packingRet);
73             free(resultBuffer);
74             return std::make_tuple(packingRet, nullptr, 0);
75         }
76 
77         uint32_t addImageRet = real_->AddImage(source);
78         if (addImageRet != SUCCESS) {
79             IMAGE_LOGE("Packing failed, AddImage failed, ret=%{public}u.", addImageRet);
80             free(resultBuffer);
81             return std::make_tuple(addImageRet, nullptr, 0);
82         }
83 
84         int64_t packedSize = 0;
85         uint32_t finalPackRet = real_->FinalizePacking(packedSize);
86         if (finalPackRet != SUCCESS) {
87             IMAGE_LOGE("Packing failed, FinalizePacking failed, ret=%{public}u.", finalPackRet);
88             free(resultBuffer);
89             return std::make_tuple(finalPackRet, nullptr, 0);
90         }
91         IMAGE_LOGD("packedSize=%{public}" PRId64, packedSize);
92 
93         return std::make_tuple(SUCCESS_CODE, resultBuffer, packedSize);
94     }
95 
96 private:
97     std::shared_ptr<ImagePacker> real_ = nullptr;
98 };
99 } // namespace Media
100 } // namespace OHOS
101 #endif // IMAGE_PACKER_IMPL_H
102