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