• 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 #include "image_ffi.h"
16 
17 #include <cstdint>
18 
19 #include "cj_color_manager.h"
20 #include "image_common.h"
21 #include "image_creator_impl.h"
22 #include "image_log.h"
23 #include "image_packer_impl.h"
24 #include "image_receiver_impl.h"
25 #include "image_source_impl.h"
26 #include "image_type.h"
27 #include "media_errors.h"
28 #include "metadata_impl.h"
29 
30 using namespace OHOS::FFI;
31 
32 namespace OHOS {
33 namespace Media {
34 extern "C" {
35 //--------------------- ImageSource ------------------------------------------------------------------------
36 static const std::string FILE_URL_PREFIX = "file://";
FileUrlToRawPath(const std::string & path)37 static std::string FileUrlToRawPath(const std::string& path)
38 {
39     if (path.size() > FILE_URL_PREFIX.size() && (path.compare(0, FILE_URL_PREFIX.size(), FILE_URL_PREFIX) == 0)) {
40         return path.substr(FILE_URL_PREFIX.size());
41     }
42     return path;
43 }
44 
FfiOHOSCreateImageSourceByPath(char * uri,uint32_t * errCode)45 int64_t FfiOHOSCreateImageSourceByPath(char* uri, uint32_t* errCode)
46 {
47     IMAGE_LOGD("[ImageSource] FfiOHOSCreateImageSourceByPath start");
48     std::string path = FileUrlToRawPath(uri);
49     std::unique_ptr<ImageSource> ptr_ = ImageSourceImpl::CreateImageSource(path, errCode);
50     if (*errCode != SUCCESS_CODE) {
51         IMAGE_LOGE("[ImageSource] FfiOHOSCreateImageSourceByPath failed");
52         return INIT_FAILED;
53     }
54     auto nativeImage = FFIData::Create<ImageSourceImpl>(move(ptr_));
55     if (!nativeImage) {
56         IMAGE_LOGE("[ImageSource] FfiOHOSCreateImageSourceByPath failed");
57         *errCode = ERR_IMAGE_INIT_ABNORMAL;
58         return INIT_FAILED;
59     }
60     nativeImage->SetPathName(path);
61     IMAGE_LOGD("[ImageSource] FfiOHOSCreateImageSourceByPath success");
62     return nativeImage->GetID();
63 }
64 
ParseCSourceOptions(CSourceOptions opts)65 static SourceOptions ParseCSourceOptions(CSourceOptions opts)
66 {
67     SourceOptions options;
68     options.baseDensity = opts.baseDensity;
69     options.pixelFormat = PixelFormat(opts.pixelFormat);
70     options.size.height = opts.height;
71     options.size.width = opts.width;
72     IMAGE_LOGD("[ImageSource] SourceOptions height is %{public}d, width is %{public}d", options.size.height,
73         options.size.width);
74     return options;
75 }
76 
FfiOHOSCreateImageSourceByPathWithOption(char * uri,CSourceOptions opts,uint32_t * errCode)77 int64_t FfiOHOSCreateImageSourceByPathWithOption(char* uri, CSourceOptions opts, uint32_t* errCode)
78 {
79     IMAGE_LOGD("[ImageSource] FfiOHOSCreateImageSourceByPathWithOption start");
80     std::string path = FileUrlToRawPath(uri);
81     SourceOptions options = ParseCSourceOptions(opts);
82     std::unique_ptr<ImageSource> ptr_ = ImageSourceImpl::CreateImageSourceWithOption(path, options, errCode);
83     if (*errCode != SUCCESS_CODE) {
84         IMAGE_LOGE("[ImageSource] FfiOHOSCreateImageSourceByPathWithOption failed");
85         return INIT_FAILED;
86     }
87     auto nativeImage = FFIData::Create<ImageSourceImpl>(move(ptr_));
88     if (!nativeImage) {
89         IMAGE_LOGE("[ImageSource] FfiOHOSCreateImageSourceByPathWithOption failed");
90         *errCode = ERR_IMAGE_INIT_ABNORMAL;
91         return INIT_FAILED;
92     }
93     nativeImage->SetPathName(path);
94     IMAGE_LOGD("[ImageSource] FfiOHOSCreateImageSourceByPathWithOption success");
95     return nativeImage->GetID();
96 }
97 
FfiOHOSCreateImageSourceByFd(int fd,uint32_t * errCode)98 int64_t FfiOHOSCreateImageSourceByFd(int fd, uint32_t* errCode)
99 {
100     IMAGE_LOGD("[ImageSource] FfiOHOSCreateImageSourceByFd start");
101     std::unique_ptr<ImageSource> ptr_ = ImageSourceImpl::CreateImageSource(fd, errCode);
102     if (*errCode != SUCCESS_CODE) {
103         IMAGE_LOGE("[ImageSource] FfiOHOSCreateImageSourceByFd failed");
104         return INIT_FAILED;
105     }
106     auto nativeImage = FFIData::Create<ImageSourceImpl>(move(ptr_));
107     if (!nativeImage) {
108         IMAGE_LOGE("[ImageSource] FfiOHOSCreateImageSourceByFd failed");
109         *errCode = ERR_IMAGE_INIT_ABNORMAL;
110         return INIT_FAILED;
111     }
112     nativeImage->SetFd(fd);
113     IMAGE_LOGD("[ImageSource] FfiOHOSCreateImageSourceByFd success");
114     return nativeImage->GetID();
115 }
116 
FfiOHOSCreateImageSourceByFdWithOption(int fd,CSourceOptions opts,uint32_t * errCode)117 int64_t FfiOHOSCreateImageSourceByFdWithOption(int fd, CSourceOptions opts, uint32_t* errCode)
118 {
119     IMAGE_LOGD("[ImageSource] FfiOHOSCreateImageSourceByFdWithOption start");
120     SourceOptions options = ParseCSourceOptions(opts);
121     std::unique_ptr<ImageSource> ptr_ = ImageSourceImpl::CreateImageSourceWithOption(fd, options, errCode);
122     if (*errCode != SUCCESS_CODE) {
123         IMAGE_LOGE("[ImageSource] FfiOHOSCreateImageSourceByFdWithOption failed");
124         return INIT_FAILED;
125     }
126     auto nativeImage = FFIData::Create<ImageSourceImpl>(move(ptr_));
127     if (!nativeImage) {
128         IMAGE_LOGE("[ImageSource] FfiOHOSCreateImageSourceByFdWithOption failed");
129         *errCode = ERR_IMAGE_INIT_ABNORMAL;
130         return INIT_FAILED;
131     }
132     nativeImage->SetFd(fd);
133     IMAGE_LOGD("[ImageSource] FfiOHOSCreateImageSourceByFdWithOption success");
134     return nativeImage->GetID();
135 }
136 
FfiOHOSCreateImageSourceByBuffer(uint8_t * data,uint32_t size,uint32_t * errCode)137 int64_t FfiOHOSCreateImageSourceByBuffer(uint8_t* data, uint32_t size, uint32_t* errCode)
138 {
139     IMAGE_LOGD("[ImageSource] FfiOHOSCreateImageSourceByBuffer start");
140     std::unique_ptr<ImageSource> ptr_ = ImageSourceImpl::CreateImageSource(data, size, errCode);
141     if (*errCode != SUCCESS_CODE) {
142         IMAGE_LOGE("[ImageSource] FfiOHOSCreateImageSourceByBuffer failed");
143         return INIT_FAILED;
144     }
145     auto nativeImage = FFIData::Create<ImageSourceImpl>(move(ptr_));
146     if (!nativeImage) {
147         IMAGE_LOGE("[ImageSource] FfiOHOSCreateImageSourceByBuffer failed");
148         *errCode = ERR_IMAGE_INIT_ABNORMAL;
149         return INIT_FAILED;
150     }
151     nativeImage->SetBuffer(data, size);
152     IMAGE_LOGD("[ImageSource] FfiOHOSCreateImageSourceByBuffer success");
153     return nativeImage->GetID();
154 }
155 
FfiOHOSCreateImageSourceByBufferWithOption(uint8_t * data,uint32_t size,CSourceOptions opts,uint32_t * errCode)156 int64_t FfiOHOSCreateImageSourceByBufferWithOption(uint8_t* data, uint32_t size, CSourceOptions opts, uint32_t* errCode)
157 {
158     IMAGE_LOGD("[ImageSource] FfiOHOSCreateImageSourceByBufferWithOption start");
159     SourceOptions options = ParseCSourceOptions(opts);
160     std::unique_ptr<ImageSource> ptr_ = ImageSourceImpl::CreateImageSourceWithOption(data, size, options, errCode);
161     if (*errCode != SUCCESS_CODE) {
162         IMAGE_LOGE("[ImageSource] FfiOHOSCreateImageSourceByBufferWithOption failed");
163         return INIT_FAILED;
164     }
165     auto nativeImage = FFIData::Create<ImageSourceImpl>(move(ptr_));
166     if (!nativeImage) {
167         IMAGE_LOGE("[ImageSource] FfiOHOSCreateImageSourceByBufferWithOption failed");
168         *errCode = ERR_IMAGE_INIT_ABNORMAL;
169         return INIT_FAILED;
170     }
171     nativeImage->SetBuffer(data, size);
172     IMAGE_LOGD("[ImageSource] FfiOHOSCreateImageSourceByBufferWithOption success");
173     return nativeImage->GetID();
174 }
175 
FfiOHOSCreateImageSourceByRawFile(int fd,int32_t offset,int32_t length,CSourceOptions opts,uint32_t * errCode)176 int64_t FfiOHOSCreateImageSourceByRawFile(
177     int fd, int32_t offset, int32_t length, CSourceOptions opts, uint32_t* errCode)
178 {
179     IMAGE_LOGD("[ImageSource] FfiOHOSCreateImageSourceByRawFile start");
180     SourceOptions options = ParseCSourceOptions(opts);
181     std::unique_ptr<ImageSource> ptr_ = ImageSourceImpl::CreateImageSource(fd, offset, length, options, *errCode);
182     if (*errCode != SUCCESS_CODE) {
183         IMAGE_LOGE("[ImageSource] FfiOHOSCreateImageSourceByRawFile failed");
184         return INIT_FAILED;
185     }
186     auto nativeImage = FFIData::Create<ImageSourceImpl>(move(ptr_));
187     if (!nativeImage) {
188         IMAGE_LOGE("[ImageSource] FfiOHOSCreateImageSourceByRawFile failed");
189         *errCode = ERR_IMAGE_INIT_ABNORMAL;
190         return INIT_FAILED;
191     }
192     IMAGE_LOGD("[ImageSource] FfiOHOSCreateImageSourceByRawFile success");
193     return nativeImage->GetID();
194 }
195 
FfiOHOSCreateIncrementalSource(const uint8_t * data,uint32_t size,CSourceOptions opts,uint32_t * errCode)196 int64_t FfiOHOSCreateIncrementalSource(const uint8_t* data, uint32_t size, CSourceOptions opts, uint32_t* errCode)
197 {
198     IMAGE_LOGD("[ImageSource] FfiOHOSCreateIncrementalSource start");
199     SourceOptions options = ParseCSourceOptions(opts);
200     auto ptr = ImageSourceImpl::CreateIncrementalSource(data, size, options, *errCode);
201     if (*errCode != SUCCESS_CODE) {
202         return INIT_FAILED;
203     }
204     auto nativeImage = FFIData::Create<ImageSourceImpl>(move(std::get<0>(ptr)), move(std::get<1>(ptr)));
205     if (!nativeImage) {
206         IMAGE_LOGE("[ImageSource] FfiOHOSCreateIncrementalSource failed");
207         *errCode = ERR_IMAGE_INIT_ABNORMAL;
208         return INIT_FAILED;
209     }
210     IMAGE_LOGD("[ImageSource] FfiOHOSCreateIncrementalSource success");
211 
212     return nativeImage->GetID();
213 }
214 
FfiOHOSImageSourceGetImageInfo(int64_t id,uint32_t index,uint32_t * errCode)215 CImageInfo FfiOHOSImageSourceGetImageInfo(int64_t id, uint32_t index, uint32_t* errCode)
216 {
217     IMAGE_LOGD("[ImageSource] FfiOHOSImageSourceGetImageInfo start");
218     auto instance = FFIData::GetData<ImageSourceImpl>(id);
219     CImageInfo ret = {};
220     if (!instance) {
221         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
222         *errCode = ERR_IMAGE_INIT_ABNORMAL;
223         return ret;
224     }
225     ImageInfo info;
226     *errCode = instance->GetImageInfo(index, info);
227     if (*errCode != 0) {
228         return ret;
229     }
230     ret.height = info.size.height;
231     ret.width = info.size.width;
232     ret.density = info.baseDensity;
233     IMAGE_LOGD("[ImageSource] FfiOHOSImageSourceGetImageInfo success");
234     return ret;
235 }
236 
ParseImageSourceImageInfo(ImageInfo info,ImageSource * imageSource)237 static CImageInfoV2 ParseImageSourceImageInfo(ImageInfo info, ImageSource* imageSource)
238 {
239     CImageInfoV2 ret = {};
240     ret.height = info.size.height;
241     ret.width = info.size.width;
242     ret.density = info.baseDensity;
243     ret.pixelFormat = static_cast<int32_t>(info.pixelFormat);
244     ret.alphaType = static_cast<int32_t>(info.alphaType);
245     ret.mimeType = Utils::MallocCString(info.encodedFormat);
246     ret.isHdr = imageSource->IsHdrImage();
247     return ret;
248 }
249 
FfiOHOSImageSourceGetImageInfoV2(int64_t id,uint32_t index,uint32_t * errCode)250 CImageInfoV2 FfiOHOSImageSourceGetImageInfoV2(int64_t id, uint32_t index, uint32_t* errCode)
251 {
252     IMAGE_LOGD("[ImageSource] FfiOHOSImageSourceGetImageInfoV2 start");
253     auto instance = FFIData::GetData<ImageSourceImpl>(id);
254     CImageInfoV2 ret = {};
255     if (!instance) {
256         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
257         *errCode = ERR_IMAGE_INIT_ABNORMAL;
258         return ret;
259     }
260     ImageInfo info;
261     *errCode = instance->GetImageInfo(index, info);
262     if (*errCode != 0) {
263         return ret;
264     }
265     ret = ParseImageSourceImageInfo(info, instance->nativeImgSrc.get());
266     IMAGE_LOGD("[ImageSource] FfiOHOSImageSourceGetImageInfoV2 success");
267     return ret;
268 }
269 
FreeArrayPtr(char ** ptr,int count)270 void FreeArrayPtr(char** ptr, int count)
271 {
272     for (int i = 0; i < count; i++) {
273         free(ptr[i]);
274     }
275 }
276 
FfiOHOSGetSupportedFormats(int64_t id,uint32_t * errCode)277 CArrString FfiOHOSGetSupportedFormats(int64_t id, uint32_t* errCode)
278 {
279     IMAGE_LOGD("[ImageSource] FfiOHOSGetSupportedFormats start");
280     CArrString ret = { .head = nullptr, .size = 0 };
281     *errCode = ERR_IMAGE_INIT_ABNORMAL;
282     auto instance = FFIData::GetData<ImageSourceImpl>(id);
283     if (!instance) {
284         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
285         return ret;
286     }
287     std::set<std::string> formats;
288     *errCode = instance->GetSupportedFormats(formats);
289     if (*errCode == SUCCESS_CODE) {
290         size_t size = formats.size();
291         if (size == 0) {
292             IMAGE_LOGE("[ImageSource] FfiOHOSGetSupportedFormats size cannot be equal to 0.");
293             *errCode = ERR_IMAGE_MALLOC_ABNORMAL;
294             return ret;
295         }
296 
297         auto arr = static_cast<char**>(malloc(sizeof(char*) * size));
298         if (!arr) {
299             IMAGE_LOGE("[ImageSource] FfiOHOSGetSupportedFormats failed to malloc arr.");
300             *errCode = ERR_IMAGE_MALLOC_ABNORMAL;
301             return ret;
302         }
303 
304         int32_t i = 0;
305         for (const std::string& str : formats) {
306             auto temp = Utils::MallocCString(str);
307             if (!temp) {
308                 IMAGE_LOGE("[ImageSource] FfiOHOSGetSupportedFormats failed to copy string.");
309                 FreeArrayPtr(arr, i);
310                 free(arr);
311                 *errCode = ERR_IMAGE_MALLOC_ABNORMAL;
312                 return ret;
313             }
314             arr[i] = temp;
315             i++;
316         }
317         ret.head = arr;
318         ret.size = static_cast<int64_t>(size);
319     }
320     IMAGE_LOGD("[ImageSource] FfiOHOSGetSupportedFormats success");
321     return ret;
322 }
323 
FfiOHOSGetImageProperty(int64_t id,char * key,uint32_t index,char * defaultValue,uint32_t * errCode)324 char* FfiOHOSGetImageProperty(int64_t id, char* key, uint32_t index, char* defaultValue, uint32_t* errCode)
325 {
326     IMAGE_LOGD("[ImageSource] FfiOHOSGetImageProperty start");
327     char* ret = nullptr;
328     *errCode = ERR_IMAGE_INIT_ABNORMAL;
329     auto instance = FFIData::GetData<ImageSourceImpl>(id);
330     if (!instance) {
331         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
332         return ret;
333     }
334     std::string skey = key;
335     std::string value = defaultValue;
336     *errCode = instance->GetImageProperty(skey, index, value);
337     if (*errCode != SUCCESS_CODE) {
338         return ret;
339     }
340     ret = Utils::MallocCString(value);
341     IMAGE_LOGD("[ImageSource] FfiOHOSGetImageProperty success");
342     return ret;
343 }
344 
FfiOHOSModifyImageProperty(int64_t id,char * key,char * value)345 uint32_t FfiOHOSModifyImageProperty(int64_t id, char* key, char* value)
346 {
347     IMAGE_LOGD("[ImageSource] FfiOHOSModifyImageProperty start");
348     auto instance = FFIData::GetData<ImageSourceImpl>(id);
349     if (!instance) {
350         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
351         return ERR_IMAGE_INIT_ABNORMAL;
352     }
353     uint32_t ret = instance->ModifyImageProperty(key, value);
354     IMAGE_LOGD("[ImageSource] FfiOHOSModifyImageProperty success");
355     return ret;
356 }
357 
FfiOHOSGetFrameCount(int64_t id)358 RetDataUI32 FfiOHOSGetFrameCount(int64_t id)
359 {
360     IMAGE_LOGD("[ImageSource] FfiOHOSGetFrameCount start");
361     RetDataUI32 ret = { .code = ERR_IMAGE_INIT_ABNORMAL, .data = 0 };
362     auto instance = FFIData::GetData<ImageSourceImpl>(id);
363     if (!instance) {
364         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
365         return ret;
366     }
367     ret.data = instance->GetFrameCount(ret.code);
368     IMAGE_LOGD("[ImageSource] FfiOHOSGetFrameCount success");
369     return ret;
370 }
371 
FfiOHOSUpdateData(int64_t id,UpdateDataInfo info)372 uint32_t FfiOHOSUpdateData(int64_t id, UpdateDataInfo info)
373 {
374     IMAGE_LOGD("[ImageSource] FfiOHOSUpdateData start");
375     auto instance = FFIData::GetData<ImageSourceImpl>(id);
376     if (!instance) {
377         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
378         return ERR_IMAGE_INIT_ABNORMAL;
379     }
380     uint8_t* buffer = info.data;
381     if (info.offset < info.arrSize) {
382         buffer = buffer + info.offset;
383     }
384     uint32_t lastSize = info.arrSize - info.offset;
385     uint32_t size = info.updateLen < lastSize ? info.updateLen : lastSize;
386     uint32_t ret = instance->UpdateData(buffer, size, info.isCompleted);
387     if (ret == 0) {
388         auto incPixelMap = instance->GetIncrementalPixelMap();
389         if (incPixelMap != nullptr) {
390             uint8_t decodeProgress = 0;
391             uint32_t err = incPixelMap->PromoteDecoding(decodeProgress);
392             if (!(err == SUCCESS_CODE || (err == ERR_IMAGE_SOURCE_DATA_INCOMPLETE && !info.isCompleted))) {
393                 IMAGE_LOGE("UpdateData PromoteDecoding error");
394             }
395             if (info.isCompleted) {
396                 incPixelMap->DetachFromDecoding();
397             }
398         }
399     }
400 
401     IMAGE_LOGD("[ImageSource] FfiOHOSUpdateData success");
402     return ret;
403 }
404 
FfiOHOSRelease(int64_t id)405 uint32_t FfiOHOSRelease(int64_t id)
406 {
407     IMAGE_LOGD("[ImageSource] FfiOHOSRelease start");
408     auto instance = FFIData::GetData<ImageSourceImpl>(id);
409     if (!instance) {
410         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
411         return ERR_IMAGE_INIT_ABNORMAL;
412     }
413     instance->Release();
414     IMAGE_LOGD("[ImageSource] FfiOHOSRelease success");
415     return SUCCESS_CODE;
416 }
417 
ParseCDecodingOptions(CDecodingOptions & opts)418 static DecodeOptions ParseCDecodingOptions(CDecodingOptions& opts)
419 {
420     DecodeOptions decodeOpts = {};
421     decodeOpts.fitDensity = opts.fitDensity;
422     decodeOpts.desiredSize.height = opts.desiredSize.height;
423     decodeOpts.desiredSize.width = opts.desiredSize.width;
424     IMAGE_LOGD("[ImageSource] desiredSize height is %{public}d, width is %{public}d", decodeOpts.desiredSize.height,
425         decodeOpts.desiredSize.width);
426     decodeOpts.desiredRegion.height = opts.desiredRegion.size.height;
427     decodeOpts.desiredRegion.width = opts.desiredRegion.size.width;
428     decodeOpts.desiredRegion.left = opts.desiredRegion.x;
429     decodeOpts.desiredRegion.top = opts.desiredRegion.y;
430     IMAGE_LOGD("[ImageSource] desiredRegion height is %{public}d, width is %{public}d,"
431                "left is %{public}d, top is %{public}d",
432         decodeOpts.desiredRegion.height, decodeOpts.desiredRegion.width, decodeOpts.desiredRegion.left,
433         decodeOpts.desiredRegion.top);
434     decodeOpts.rotateDegrees = opts.rotateDegrees;
435     decodeOpts.sampleSize = opts.sampleSize;
436     decodeOpts.desiredPixelFormat = PixelFormat(opts.desiredPixelFormat);
437     decodeOpts.editable = opts.editable;
438     if (opts.desiredColorSpace != 0) {
439         auto colorSpace = FFIData::GetData<ColorManager::CjColorManager>(opts.desiredColorSpace);
440         if (colorSpace != nullptr) {
441             decodeOpts.desiredColorSpaceInfo = colorSpace->GetColorSpaceToken();
442         }
443     }
444     return decodeOpts;
445 }
446 
ParseCDecodingOptionsV2(CDecodingOptionsV2 & opts)447 static DecodeOptions ParseCDecodingOptionsV2(CDecodingOptionsV2& opts)
448 {
449     DecodeOptions decodeOpts = {};
450     decodeOpts.fitDensity = opts.fitDensity;
451     decodeOpts.desiredSize.height = opts.desiredSize.height;
452     decodeOpts.desiredSize.width = opts.desiredSize.width;
453     IMAGE_LOGD("[ImageSource] desiredSize height is %{public}d, width is %{public}d", decodeOpts.desiredSize.height,
454         decodeOpts.desiredSize.width);
455     decodeOpts.desiredRegion.height = opts.desiredRegion.size.height;
456     decodeOpts.desiredRegion.width = opts.desiredRegion.size.width;
457     decodeOpts.desiredRegion.left = opts.desiredRegion.x;
458     decodeOpts.desiredRegion.top = opts.desiredRegion.y;
459     IMAGE_LOGD("[ImageSource] desiredRegion height is %{public}d, width is %{public}d,"
460                "left is %{public}d, top is %{public}d",
461         decodeOpts.desiredRegion.height, decodeOpts.desiredRegion.width, decodeOpts.desiredRegion.left,
462         decodeOpts.desiredRegion.top);
463     decodeOpts.rotateDegrees = opts.rotateDegrees;
464     decodeOpts.sampleSize = opts.sampleSize;
465     decodeOpts.desiredPixelFormat = PixelFormat(opts.desiredPixelFormat);
466     decodeOpts.editable = opts.editable;
467     if (opts.desiredColorSpace != 0) {
468         auto colorSpace = FFIData::GetData<ColorManager::CjColorManager>(opts.desiredColorSpace);
469         if (colorSpace != nullptr) {
470             decodeOpts.desiredColorSpaceInfo = colorSpace->GetColorSpaceToken();
471         }
472     }
473     decodeOpts.desiredDynamicRange = DecodeDynamicRange(opts.desiredDynamicRange);
474     return decodeOpts;
475 }
476 
FfiOHOSImageSourceCreatePixelMapList(int64_t id,uint32_t index,CDecodingOptions opts,uint32_t * errorCode)477 CArrI64 FfiOHOSImageSourceCreatePixelMapList(int64_t id, uint32_t index, CDecodingOptions opts, uint32_t* errorCode)
478 {
479     IMAGE_LOGD("[ImageSource] CreatePixelMapList start");
480     CArrI64 ret = { .head = nullptr, .size = 0 };
481     auto instance = FFIData::GetData<ImageSourceImpl>(id);
482     if (!instance) {
483         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
484         *errorCode = ERR_IMAGE_INIT_ABNORMAL;
485         return ret;
486     }
487     DecodeOptions decodeOpts = ParseCDecodingOptions(opts);
488     std::vector<int64_t> data = instance->CreatePixelMapList(index, decodeOpts, errorCode);
489     if (*errorCode == SUCCESS_CODE) {
490         auto size = data.size();
491         if (size == 0) {
492             *errorCode = ERR_IMAGE_MALLOC_ABNORMAL;
493             IMAGE_LOGE("[ImageSource] FfiOHOSImageSourceCreatePixelMapList size error.");
494             return ret;
495         }
496 
497         auto arr = static_cast<int64_t*>(malloc(sizeof(int64_t) * size));
498         if (!arr) {
499             IMAGE_LOGE("[ImageSource] FfiOHOSImageSourceCreatePixelMapList failed to malloc arr.");
500             *errorCode = ERR_IMAGE_MALLOC_ABNORMAL;
501             return ret;
502         }
503         for (int i = 0; i < static_cast<int>(size); ++i) {
504             arr[i] = data[i];
505         }
506         ret.head = arr;
507         ret.size = static_cast<int64_t>(data.size());
508     }
509     IMAGE_LOGD("[ImageSource] FfiOHOSImageSourceCreatePixelMapList success");
510     return ret;
511 }
512 
FfiOHOSImageSourceCreatePixelMapListV2(int64_t id,uint32_t index,CDecodingOptionsV2 opts,uint32_t * errorCode)513 CArrI64 FfiOHOSImageSourceCreatePixelMapListV2(int64_t id, uint32_t index, CDecodingOptionsV2 opts, uint32_t* errorCode)
514 {
515     IMAGE_LOGD("[ImageSource] FfiOHOSImageSourceCreatePixelMapListV2 start");
516     CArrI64 ret = { .head = nullptr, .size = 0 };
517     auto instance = FFIData::GetData<ImageSourceImpl>(id);
518     if (!instance) {
519         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
520         *errorCode = ERR_IMAGE_INIT_ABNORMAL;
521         return ret;
522     }
523     DecodeOptions decodeOpts = ParseCDecodingOptionsV2(opts);
524     std::vector<int64_t> data = instance->CreatePixelMapList(index, decodeOpts, errorCode);
525     if (*errorCode == SUCCESS_CODE) {
526         auto size = data.size();
527         if (size > 0) {
528             auto arr = static_cast<int64_t*>(malloc(sizeof(int64_t) * size));
529             if (!arr) {
530                 IMAGE_LOGE("[ImageSource] FfiOHOSImageSourceCreatePixelMapListV2 failed to malloc arr.");
531                 *errorCode = ERR_IMAGE_MALLOC_ABNORMAL;
532                 return ret;
533             }
534             for (int i = 0; i < static_cast<int>(size); ++i) {
535                 arr[i] = data[i];
536             }
537             ret.head = arr;
538             ret.size = static_cast<int64_t>(data.size());
539         } else {
540             *errorCode = ERR_IMAGE_MALLOC_ABNORMAL;
541             IMAGE_LOGE("[ImageSource] FfiOHOSImageSourceCreatePixelMapListV2 size error.");
542             return ret;
543         }
544     }
545     IMAGE_LOGD("[ImageSource] FfiOHOSImageSourceCreatePixelMapListV2 success");
546     return ret;
547 }
548 
FfiOHOSImageSourceGetDelayTime(int64_t id,uint32_t * errorCode)549 CArrI32 FfiOHOSImageSourceGetDelayTime(int64_t id, uint32_t* errorCode)
550 {
551     IMAGE_LOGD("[ImageSource] FfiOHOSImageSourceGetDelayTime start");
552     CArrI32 ret = { .head = nullptr, .size = 0 };
553     auto instance = FFIData::GetData<ImageSourceImpl>(id);
554     if (!instance) {
555         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
556         *errorCode = ERR_IMAGE_INIT_ABNORMAL;
557         return ret;
558     }
559     auto data = instance->GetDelayTime(errorCode);
560     if (*errorCode == SUCCESS_CODE) {
561         auto size = data->size();
562         if (size <= 0) {
563             IMAGE_LOGE("[ImageSource] FfiOHOSImageSourceGetDelayTime size cannot be less than or equal to 0.");
564             *errorCode = ERR_IMAGE_MALLOC_ABNORMAL;
565             return ret;
566         }
567         auto arr = static_cast<int32_t*>(malloc(sizeof(int32_t) * size));
568         if (!arr) {
569             IMAGE_LOGE("[ImageSource] FfiOHOSImageSourceGetDelayTime failed to malloc arr.");
570             *errorCode = ERR_IMAGE_MALLOC_ABNORMAL;
571             return ret;
572         }
573         for (int i = 0; i < static_cast<int>(size); ++i) {
574             arr[i] = data->operator[](i);
575         }
576         ret.head = arr;
577         ret.size = static_cast<int64_t>(data->size());
578     }
579     IMAGE_LOGD("[ImageSource] FfiOHOSImageSourceGetDelayTime success");
580     return ret;
581 }
582 
FfiImageImageSourceImplGetDisposalTypeList(int64_t id,uint32_t * errorCode)583 CArrI32 FfiImageImageSourceImplGetDisposalTypeList(int64_t id, uint32_t* errorCode)
584 {
585     IMAGE_LOGD("[ImageSource] FfiImageImageSourceImplGetDisposalTypeList start");
586     CArrI32 ret = { .head = nullptr, .size = 0 };
587     auto instance = FFIData::GetData<ImageSourceImpl>(id);
588     if (!instance) {
589         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
590         *errorCode = ERR_IMAGE_INIT_ABNORMAL;
591         return ret;
592     }
593     std::unique_ptr<std::vector<int32_t>> data = instance->GetDisposalTypeList(errorCode);
594     if (*errorCode == SUCCESS_CODE && data != nullptr) {
595         auto size = data->size();
596         if (size <= 0) {
597             return ret;
598         }
599         int32_t* arr = static_cast<int32_t*>(malloc(sizeof(int32_t) * size));
600         if (!arr) {
601             IMAGE_LOGE("[ImageSource] FfiImageImageSourceImplGetDisposalTypeList failed to malloc arr.");
602             *errorCode = ERR_IMAGE_MALLOC_ABNORMAL;
603             return ret;
604         }
605         for (int i = 0; i < static_cast<int>(size); ++i) {
606             arr[i] = data->operator[](i);
607         }
608         ret.head = arr;
609         ret.size = static_cast<int64_t>(data->size());
610     }
611     IMAGE_LOGD("[ImageSource] FfiImageImageSourceImplGetDisposalTypeList success");
612     return ret;
613 }
614 
FfiImageImageSourceImplGetImageProperties(int64_t id,CArrString key,char ** value)615 uint32_t FfiImageImageSourceImplGetImageProperties(int64_t id, CArrString key, char** value)
616 {
617     IMAGE_LOGD("[ImageSource] FfiImageImageSourceImplGetImageProperties start");
618     auto instance = FFIData::GetData<ImageSourceImpl>(id);
619     if (!instance) {
620         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
621         return ERR_IMAGE_INIT_ABNORMAL;
622     }
623     std::vector<std::string> keyStrArray;
624     for (int64_t i = 0; i < key.size; i++) {
625         keyStrArray.push_back(key.head[i]);
626     }
627     std::vector<std::string> valueStrArray;
628     uint32_t errCode = instance->GetImageProperties(keyStrArray, valueStrArray);
629     if (errCode != SUCCESS) {
630         return errCode;
631     }
632     for (size_t i = 0; i < valueStrArray.size(); i++) {
633         value[i] = Utils::MallocCString(valueStrArray[i]);
634     }
635     IMAGE_LOGD("[ImageSource] FfiImageImageSourceImplGetImageProperties success");
636     return errCode;
637 }
638 
FfiImageImageSourceImplModifyImageProperties(int64_t id,CArrString key,CArrString value)639 uint32_t FfiImageImageSourceImplModifyImageProperties(int64_t id, CArrString key, CArrString value)
640 {
641     IMAGE_LOGD("[ImageSource] FfiImageImageSourceImplModifyImageProperties start");
642     auto instance = FFIData::GetData<ImageSourceImpl>(id);
643     if (!instance) {
644         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
645         return ERR_IMAGE_INIT_ABNORMAL;
646     }
647     IMAGE_LOGD("[ImageSource] FfiImageImageSourceImplModifyImageProperties success");
648     return instance->ModifyImageProperties(key.head, value.head, key.size);
649 }
650 
FfiOHOSImageSourceCreatePixelMap(int64_t id,uint32_t index,CDecodingOptions opts)651 RetDataI64U32 FfiOHOSImageSourceCreatePixelMap(int64_t id, uint32_t index, CDecodingOptions opts)
652 {
653     IMAGE_LOGD("[ImageSource] FfiOHOSImageSourceCreatePixelMap start");
654     RetDataI64U32 ret = { .code = ERR_IMAGE_INIT_ABNORMAL, .data = 0 };
655     auto instance = FFIData::GetData<ImageSourceImpl>(id);
656     if (!instance) {
657         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
658         return ret;
659     }
660     DecodeOptions decodeOpts = ParseCDecodingOptions(opts);
661     ret.data = instance->CreatePixelMap(index, decodeOpts, ret.code);
662     IMAGE_LOGD("[ImageSource] FfiOHOSImageSourceCreatePixelMap success");
663     return ret;
664 }
665 
FfiOHOSImageSourceCreatePixelMapV2(int64_t id,uint32_t index,CDecodingOptionsV2 opts)666 RetDataI64U32 FfiOHOSImageSourceCreatePixelMapV2(int64_t id, uint32_t index, CDecodingOptionsV2 opts)
667 {
668     IMAGE_LOGD("[ImageSource] FfiOHOSImageSourceCreatePixelMapV2 start");
669     RetDataI64U32 ret = { .code = ERR_IMAGE_INIT_ABNORMAL, .data = 0 };
670     auto instance = FFIData::GetData<ImageSourceImpl>(id);
671     if (!instance) {
672         IMAGE_LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
673         return ret;
674     }
675     DecodeOptions decodeOpts = ParseCDecodingOptionsV2(opts);
676     ret.data = instance->CreatePixelMap(index, decodeOpts, ret.code);
677     IMAGE_LOGD("[ImageSource] FfiOHOSImageSourceCreatePixelMapV2 success");
678     return ret;
679 }
680 
681 //--------------------- ImageReceiver ------------------------------------------------------------------------
682 
FfiOHOSReceiverGetSize(int64_t id,CSize * retVal)683 uint32_t FfiOHOSReceiverGetSize(int64_t id, CSize* retVal)
684 {
685     IMAGE_LOGD("FfiOHOSReceiverGetSize start");
686     auto instance = FFIData::GetData<ImageReceiverImpl>(id);
687     if (!instance) {
688         return ERR_IMAGE_INIT_ABNORMAL;
689     }
690     uint32_t retCode = instance->GetSize(retVal);
691     IMAGE_LOGD("FfiOHOSReceiverGetSize success");
692     return retCode;
693 }
694 
FfiOHOSReceiverGetCapacity(int64_t id,int32_t * retVal)695 uint32_t FfiOHOSReceiverGetCapacity(int64_t id, int32_t* retVal)
696 {
697     IMAGE_LOGD("FfiOHOSReceiverGetCapacity start");
698     auto instance = FFIData::GetData<ImageReceiverImpl>(id);
699     if (!instance) {
700         return ERR_IMAGE_INIT_ABNORMAL;
701     }
702     uint32_t retCode = instance->GetCapacity(retVal);
703     IMAGE_LOGD("FfiOHOSReceiverGetCapacity success");
704     return retCode;
705 }
706 
FfiOHOSReceiverGetFormat(int64_t id,int32_t * retVal)707 uint32_t FfiOHOSReceiverGetFormat(int64_t id, int32_t* retVal)
708 {
709     IMAGE_LOGD("FfiOHOSReceiverGetFormat start");
710     auto instance = FFIData::GetData<ImageReceiverImpl>(id);
711     if (!instance) {
712         return ERR_IMAGE_INIT_ABNORMAL;
713     }
714     uint32_t retCode = instance->GetFormat(retVal);
715     IMAGE_LOGD("FfiOHOSReceiverGetFormat success");
716     return retCode;
717 }
718 
FfiOHOSCreateImageReceiver(int32_t width,int32_t height,int32_t format,int32_t capacity)719 int64_t FfiOHOSCreateImageReceiver(int32_t width, int32_t height, int32_t format, int32_t capacity)
720 {
721     IMAGE_LOGD("FfiOHOSCreateImageReceiver start");
722     auto id = ImageReceiverImpl::CreateImageReceiver(width, height, format, capacity);
723     IMAGE_LOGD("FfiOHOSCreateImageReceiver success");
724     return id;
725 }
726 
FfiOHOSGetReceivingSurfaceId(int64_t id)727 char* FfiOHOSGetReceivingSurfaceId(int64_t id)
728 {
729     IMAGE_LOGD("FfiOHOSGetReceivingSurfaceId start");
730     auto instance = FFIData::GetData<ImageReceiverImpl>(id);
731     if (!instance) {
732         IMAGE_LOGE("ImageReceiver instance not exist %{public}" PRId64, id);
733         return nullptr;
734     }
735     char* ret = instance->GetReceivingSurfaceId();
736     IMAGE_LOGD("FfiOHOSGetReceivingSurfaceId success");
737     return ret;
738 }
739 
FfiOHOSReadNextImage(int64_t id)740 int64_t FfiOHOSReadNextImage(int64_t id)
741 {
742     IMAGE_LOGD("FfiOHOSReadNextImage start");
743     auto instance = FFIData::GetData<ImageReceiverImpl>(id);
744     if (!instance) {
745         IMAGE_LOGE("ImageReceiver instance not exist %{public}" PRId64, id);
746         return INIT_FAILED;
747     }
748     auto image = instance->ReadNextImage();
749     if (!image) {
750         IMAGE_LOGE("ImageImpl Create is nullptr.");
751         return INIT_FAILED;
752     }
753     IMAGE_LOGD("FfiOHOSReadNextImage success");
754     return image->GetID();
755 }
756 
FfiOHOSReadLatestImage(int64_t id)757 int64_t FfiOHOSReadLatestImage(int64_t id)
758 {
759     IMAGE_LOGD("FfiOHOSReadLatestImage start.");
760     auto instance = FFIData::GetData<ImageReceiverImpl>(id);
761     if (!instance) {
762         IMAGE_LOGE("ImageReceiver instance not exist %{public}" PRId64, id);
763         return INIT_FAILED;
764     }
765     auto image = instance->ReadLatestImage();
766     if (!image) {
767         IMAGE_LOGE("ImageImpl Create is nullptr.");
768         return INIT_FAILED;
769     }
770 
771     IMAGE_LOGD("FfiOHOSReadLatestImage success.");
772     return image->GetID();
773 }
774 
FfiOHOSReceiverRelease(int64_t id)775 void FfiOHOSReceiverRelease(int64_t id)
776 {
777     IMAGE_LOGD("FfiOHOSReceiverRelease start");
778     auto instance = FFIData::GetData<ImageReceiverImpl>(id);
779     if (!instance) {
780         IMAGE_LOGE("ImageReceiver instance not exist %{public}" PRId64, id);
781         return;
782     }
783     instance->Release();
784     IMAGE_LOGD("FfiOHOSReceiverRelease success");
785 }
786 
FfiOHOSImageGetClipRect(int64_t id,CRegion * retVal)787 uint32_t FfiOHOSImageGetClipRect(int64_t id, CRegion* retVal)
788 {
789     IMAGE_LOGD("FfiOHOSImageGetClipRect start");
790     auto instance = FFIData::GetData<ImageImpl>(id);
791     if (!instance) {
792         return ERR_IMAGE_INIT_ABNORMAL;
793     }
794     int64_t retCode = instance->GetClipRect(retVal);
795     IMAGE_LOGD("FfiOHOSImageGetClipRect success");
796     return retCode;
797 }
798 
FfiOHOSImageGetSize(int64_t id,CSize * retVal)799 uint32_t FfiOHOSImageGetSize(int64_t id, CSize* retVal)
800 {
801     IMAGE_LOGD("FfiOHOSImageGetSize start");
802     auto instance = FFIData::GetData<ImageImpl>(id);
803     if (!instance) {
804         return ERR_IMAGE_INIT_ABNORMAL;
805     }
806     uint32_t retCode = instance->GetSize(retVal);
807     IMAGE_LOGD("FfiOHOSImageGetSize success");
808     return retCode;
809 }
810 
FfiOHOSImageGetFormat(int64_t id,int32_t * retVal)811 uint32_t FfiOHOSImageGetFormat(int64_t id, int32_t* retVal)
812 {
813     IMAGE_LOGD("FfiOHOSImageGetFormat start");
814     auto instance = FFIData::GetData<ImageImpl>(id);
815     if (!instance) {
816         return ERR_IMAGE_INIT_ABNORMAL;
817     }
818     uint32_t retCode = instance->GetFormat(retVal);
819     IMAGE_LOGD("FfiOHOSImageGetFormat success");
820     return retCode;
821 }
822 
FfiOHOSGetComponent(int64_t id,int32_t componentType,CRetComponent * ptr)823 uint32_t FfiOHOSGetComponent(int64_t id, int32_t componentType, CRetComponent* ptr)
824 {
825     IMAGE_LOGD("FfiOHOSGetComponent start");
826     auto instance = FFIData::GetData<ImageImpl>(id);
827     if (!instance) {
828         IMAGE_LOGE("ImageImpl instance not exist %{public}" PRId64, id);
829         return ERR_IMAGE_INIT_ABNORMAL;
830     }
831     uint32_t errCode = instance->GetComponent(componentType, ptr);
832     IMAGE_LOGD("FfiOHOSGetComponent success");
833     return errCode;
834 }
835 
FfiImageImageImplGetTimestamp(int64_t id)836 int64_t FfiImageImageImplGetTimestamp(int64_t id)
837 {
838     IMAGE_LOGD("FfiImageImageImplGetTimestamp start");
839     auto instance = FFIData::GetData<ImageImpl>(id);
840     if (!instance) {
841         IMAGE_LOGE("ImageImpl instance not exist %{public}" PRId64, id);
842         return 0;
843     }
844     IMAGE_LOGD("FfiImageImageImplGetTimestamp success");
845     return instance->GetTimestamp();
846 }
847 
FfiOHOSImageRelease(int64_t id)848 void FfiOHOSImageRelease(int64_t id)
849 {
850     IMAGE_LOGD("FfiOHOSImageRelease start");
851     auto instance = FFIData::GetData<ImageImpl>(id);
852     if (!instance) {
853         IMAGE_LOGE("ImageImpl instance not exist %{public}" PRId64, id);
854         return;
855     }
856     instance->Release();
857     IMAGE_LOGD("FfiOHOSImageRelease success");
858 }
859 
FfiImageReceiverImplOn(int64_t id,char * name,int64_t callbackId)860 uint32_t FfiImageReceiverImplOn(int64_t id, char* name, int64_t callbackId)
861 {
862     IMAGE_LOGD("FfiImageReceiverImplOn start");
863     auto instance = FFIData::GetData<ImageReceiverImpl>(id);
864     if (!instance) {
865         IMAGE_LOGE("[ImageReceiver] instance not exist %{public}" PRId64, id);
866         return ERR_IMAGE_INIT_ABNORMAL;
867     }
868     auto cFunc = reinterpret_cast<void (*)()>(callbackId);
869     std::function<void()> func = CJLambda::Create(cFunc);
870     return instance->CjOn(name, func);
871 }
872 
873 //--------------------- ImagePacker ---------------------------------------------------------------------------
FFiOHOSImagePackerConstructor()874 int64_t FFiOHOSImagePackerConstructor()
875 {
876     auto ret = FFIData::Create<ImagePackerImpl>();
877     if (!ret) {
878         return INIT_FAILED;
879     }
880     return ret->GetID();
881 }
882 
ParseCPackOption(CPackingOptionV2 option)883 static PackOption ParseCPackOption(CPackingOptionV2 option)
884 {
885     PackOption packOption = {
886         .format = option.format,
887         .quality = option.quality,
888         .desiredDynamicRange = EncodeDynamicRange(option.desiredDynamicRange),
889         .needsPackProperties = option.needsPackProperties,
890     };
891     return packOption;
892 }
893 
FfiOHOSImagePackerPackingPixelMap(int64_t id,int64_t source,CPackingOption option)894 RetDataCArrUI8 FfiOHOSImagePackerPackingPixelMap(int64_t id, int64_t source, CPackingOption option)
895 {
896     CArrUI8 data = { .head = nullptr, .size = 0 };
897     RetDataCArrUI8 ret = { .code = ERR_IMAGE_INIT_ABNORMAL, .data = data };
898     auto imagePackerImpl = FFIData::GetData<ImagePackerImpl>(id);
899     if (!imagePackerImpl) {
900         IMAGE_LOGE("Packing failed, invalid id of ImagePackerImpl");
901         return ret;
902     }
903 
904     auto pixelMapImpl = FFIData::GetData<PixelMapImpl>(source);
905     if (pixelMapImpl != nullptr) {
906         auto pixelMap = pixelMapImpl->GetRealPixelMap();
907         if (!pixelMap) {
908             return ret;
909         }
910         PackOption packOption = { .format = option.format, .quality = option.quality };
911         auto [code, head, size] = imagePackerImpl->Packing(*pixelMap, packOption, option.bufferSize);
912         if (code != SUCCESS_CODE) {
913             IMAGE_LOGE("Packing failed, error code is %{public}d", code);
914         }
915         data.head = head;
916         data.size = size;
917         ret.code = code;
918         ret.data = data;
919         return ret;
920     }
921 
922     IMAGE_LOGE("Packing failed, invalid id of PixelMapImpl");
923     return ret;
924 }
925 
FfiOHOSImagePackerPackingPixelMapV2(int64_t id,int64_t source,CPackingOptionV2 option)926 RetDataCArrUI8 FfiOHOSImagePackerPackingPixelMapV2(int64_t id, int64_t source, CPackingOptionV2 option)
927 {
928     CArrUI8 data = { .head = nullptr, .size = 0 };
929     RetDataCArrUI8 ret = { .code = ERR_IMAGE_INIT_ABNORMAL, .data = data };
930     auto imagePackerImpl = FFIData::GetData<ImagePackerImpl>(id);
931     if (!imagePackerImpl) {
932         IMAGE_LOGE("Packing failed, invalid id of ImagePackerImpl");
933         return ret;
934     }
935 
936     auto pixelMapImpl = FFIData::GetData<PixelMapImpl>(source);
937     if (pixelMapImpl != nullptr) {
938         auto pixelMap = pixelMapImpl->GetRealPixelMap();
939         if (!pixelMap) {
940             return ret;
941         }
942         PackOption packOption = { .format = option.format, .quality = option.quality };
943         auto [code, head, size] = imagePackerImpl->Packing(*pixelMap, packOption, option.bufferSize);
944         if (code != SUCCESS_CODE) {
945             IMAGE_LOGE("Packing failed, error code is %{public}d", code);
946         }
947         data.head = head;
948         data.size = size;
949         ret.code = code;
950         ret.data = data;
951         return ret;
952     }
953 
954     IMAGE_LOGE("Packing failed, invalid id of PixelMapImpl");
955     return ret;
956 }
957 
FfiOHOSImagePackerPackingImageSource(int64_t id,int64_t source,CPackingOption option)958 RetDataCArrUI8 FfiOHOSImagePackerPackingImageSource(int64_t id, int64_t source, CPackingOption option)
959 {
960     CArrUI8 data = { .head = nullptr, .size = 0 };
961     RetDataCArrUI8 ret = { .code = ERR_IMAGE_INIT_ABNORMAL, .data = data };
962     auto imagePackerImpl = FFIData::GetData<ImagePackerImpl>(id);
963     if (!imagePackerImpl) {
964         IMAGE_LOGE("Packing failed, invalid id of ImagePackerImpl");
965         return ret;
966     }
967 
968     auto imageSourceImpl = FFIData::GetData<ImageSourceImpl>(source);
969     if (imageSourceImpl != nullptr) {
970         PackOption packOption = { .format = option.format, .quality = option.quality };
971         auto imageSource = imageSourceImpl->nativeImgSrc;
972         if (!imageSource) {
973             return ret;
974         }
975         auto [code, head, size] = imagePackerImpl->Packing(*imageSource, packOption, option.bufferSize);
976         if (code != SUCCESS_CODE) {
977             IMAGE_LOGE("Packing failed, error code is %{public}d", code);
978         }
979         data.head = head;
980         data.size = size;
981         ret.code = code;
982         ret.data = data;
983         return ret;
984     }
985 
986     IMAGE_LOGE("Packing failed, invalid id of ImageSourceImpl");
987     return ret;
988 }
989 
FfiOHOSImagePackerPackingImageSourceV2(int64_t id,int64_t source,CPackingOptionV2 option)990 RetDataCArrUI8 FfiOHOSImagePackerPackingImageSourceV2(int64_t id, int64_t source, CPackingOptionV2 option)
991 {
992     CArrUI8 data = { .head = nullptr, .size = 0 };
993     RetDataCArrUI8 ret = { .code = ERR_IMAGE_INIT_ABNORMAL, .data = data };
994     auto imagePackerImpl = FFIData::GetData<ImagePackerImpl>(id);
995     if (!imagePackerImpl) {
996         IMAGE_LOGE("Packing failed, invalid id of ImagePackerImpl");
997         return ret;
998     }
999 
1000     auto imageSourceImpl = FFIData::GetData<ImageSourceImpl>(source);
1001     if (imageSourceImpl != nullptr) {
1002         PackOption packOption = ParseCPackOption(option);
1003         auto imageSource = imageSourceImpl->nativeImgSrc;
1004         if (!imageSource) {
1005             return ret;
1006         }
1007         auto [code, head, size] = imagePackerImpl->Packing(*imageSource, packOption, option.bufferSize);
1008         if (code != SUCCESS_CODE) {
1009             IMAGE_LOGE("Packing failed, error code is %{public}d", code);
1010         }
1011         data.head = head;
1012         data.size = size;
1013         ret.code = code;
1014         ret.data = data;
1015         return ret;
1016     }
1017 
1018     IMAGE_LOGE("Packing failed, invalid id of ImageSourceImpl");
1019     return ret;
1020 }
1021 
FfiImageImagePackerImplPackToDataPixelMap(int64_t id,int64_t source,CPackingOptionV2 option)1022 RetDataCArrUI8 FfiImageImagePackerImplPackToDataPixelMap(int64_t id, int64_t source, CPackingOptionV2 option)
1023 {
1024     IMAGE_LOGD("[ImagePacker] FfiImageImagePackerImplPackToDataPixelMap in");
1025     CArrUI8 data = { .head = nullptr, .size = 0 };
1026     RetDataCArrUI8 ret = { .code = ERR_IMAGE_INIT_ABNORMAL, .data = data };
1027     auto imagePackerImpl = FFIData::GetData<ImagePackerImpl>(id);
1028     if (!imagePackerImpl) {
1029         IMAGE_LOGE("Packing failed, invalid id of ImagePackerImpl");
1030         return ret;
1031     }
1032 
1033     auto pixelMapImpl = FFIData::GetData<PixelMapImpl>(source);
1034     if (pixelMapImpl != nullptr) {
1035         PackOption packOption = ParseCPackOption(option);
1036         auto pixelMap = pixelMapImpl->GetRealPixelMap();
1037         if (!pixelMap) {
1038             return ret;
1039         }
1040         auto [code, head, size] = imagePackerImpl->PackToData(pixelMap, packOption, option.bufferSize);
1041         if (code != SUCCESS_CODE) {
1042             IMAGE_LOGE("Packing failed, error code is %{public}d", code);
1043         }
1044         data.head = head;
1045         data.size = size;
1046         ret.code = code;
1047         ret.data = data;
1048         IMAGE_LOGD("[ImagePacker] FfiImageImagePackerImplPackToDataPixelMap in");
1049         return ret;
1050     }
1051 
1052     IMAGE_LOGE("Packing failed, invalid id of pixelMapImpl");
1053     return ret;
1054 }
1055 
FfiImageImagePackerImplPackToDataImageSource(int64_t id,int64_t source,CPackingOptionV2 option)1056 RetDataCArrUI8 FfiImageImagePackerImplPackToDataImageSource(int64_t id, int64_t source, CPackingOptionV2 option)
1057 {
1058     {
1059         IMAGE_LOGD("[ImagePacker] FfiImageImagePackerImplPackToDataImageSource in");
1060         CArrUI8 data = { .head = nullptr, .size = 0 };
1061         RetDataCArrUI8 ret = { .code = ERR_IMAGE_INIT_ABNORMAL, .data = data };
1062         auto imagePackerImpl = FFIData::GetData<ImagePackerImpl>(id);
1063         if (!imagePackerImpl) {
1064             IMAGE_LOGE("Packing failed, invalid id of ImagePackerImpl");
1065             return ret;
1066         }
1067 
1068         auto imageSourceImpl = FFIData::GetData<ImageSourceImpl>(source);
1069         if (imageSourceImpl != nullptr) {
1070             PackOption packOption = ParseCPackOption(option);
1071             auto imageSource = imageSourceImpl->nativeImgSrc;
1072             if (!imageSource) {
1073                 return ret;
1074             }
1075             auto [code, head, size] = imagePackerImpl->PackToData(imageSource, packOption, option.bufferSize);
1076             if (code != SUCCESS_CODE) {
1077                 IMAGE_LOGE("Packing failed, error code is %{public}d", code);
1078             }
1079             data.head = head;
1080             data.size = size;
1081             ret.code = code;
1082             ret.data = data;
1083             IMAGE_LOGD("[ImagePacker] FfiImageImagePackerImplPackToDataImageSource in");
1084             return ret;
1085         }
1086 
1087         IMAGE_LOGE("Packing failed, invalid id of imageSourceImpl");
1088         return ret;
1089     }
1090 }
1091 
FfiImageImagePackerImplPackingPicture(int64_t id,int64_t source,CPackingOptionV2 option)1092 RetDataCArrUI8 FfiImageImagePackerImplPackingPicture(int64_t id, int64_t source, CPackingOptionV2 option)
1093 {
1094     IMAGE_LOGD("[ImagePacker] FfiImageImagePackerImplPackingPicture in");
1095     CArrUI8 data = { .head = nullptr, .size = 0 };
1096     RetDataCArrUI8 ret = { .code = ERR_IMAGE_INIT_ABNORMAL, .data = data };
1097     auto imagePackerImpl = FFIData::GetData<ImagePackerImpl>(id);
1098     if (!imagePackerImpl) {
1099         IMAGE_LOGE("Packing failed, invalid id of ImagePackerImpl");
1100         return ret;
1101     }
1102 
1103     auto pictureImpl = FFIData::GetData<PictureImpl>(source);
1104     if (pictureImpl != nullptr) {
1105         PackOption packOption = ParseCPackOption(option);
1106         auto picture = pictureImpl->GetPicture();
1107         if (!picture) {
1108             return ret;
1109         }
1110         auto [code, head, size] = imagePackerImpl->PackToData(picture, packOption, option.bufferSize);
1111         if (code != SUCCESS_CODE) {
1112             IMAGE_LOGE("Packing failed, error code is %{public}d", code);
1113         }
1114         data.head = head;
1115         data.size = size;
1116         ret.code = code;
1117         ret.data = data;
1118         IMAGE_LOGD("[ImagePacker] FfiImageImagePackerImplPackingPicture in");
1119         return ret;
1120     }
1121 
1122     IMAGE_LOGE("Packing failed, invalid id of pictureImpl");
1123     return ret;
1124 }
1125 
FfiOHOSImagePackerGetSupportedFormats(int64_t id)1126 RetDataCArrString FfiOHOSImagePackerGetSupportedFormats(int64_t id)
1127 {
1128     RetDataCArrString ret = { .code = ERR_IMAGE_INIT_ABNORMAL, .data = { .head = nullptr, .size = 0 } };
1129     auto imagePackerImpl = FFIData::GetData<ImagePackerImpl>(id);
1130     if (!imagePackerImpl) {
1131         IMAGE_LOGE("Packing failed, invalid id of ImagePackerImpl");
1132         return ret;
1133     }
1134 
1135     auto imagePacker = imagePackerImpl->GetImagePacker();
1136     if (!imagePacker) {
1137         IMAGE_LOGE("fail to get ImagePacker");
1138         return ret;
1139     }
1140 
1141     std::set<std::string> formats;
1142     uint32_t formatsRet = imagePacker->GetSupportedFormats(formats);
1143     if (formatsRet != SUCCESS_CODE) {
1144         IMAGE_LOGE("fail to get supported formats");
1145         return ret;
1146     }
1147 
1148     CArrString arrInfo { .head = nullptr, .size = 0 };
1149     auto size = formats.size();
1150     if (size == 0) {
1151         IMAGE_LOGE("[ImageSource] FfiOHOSImagePackerGetSupportedFormats size cannot be equal to 0.");
1152         ret.code = ERR_SHAMEM_NOT_EXIST;
1153         return ret;
1154     }
1155     auto arr = static_cast<char**>(malloc(sizeof(char*) * size));
1156     if (!arr) {
1157         IMAGE_LOGE("[ImageSource] FfiOHOSImagePackerGetSupportedFormats failed to malloc arr.");
1158         ret.code = ERR_SHAMEM_NOT_EXIST;
1159         return ret;
1160     }
1161 
1162     uint32_t i = 0;
1163     for (const auto& format : formats) {
1164         auto temp = ::Utils::MallocCString(format);
1165         if (!temp) {
1166             FreeArrayPtr(arr, i);
1167             free(arr);
1168             ret.code = ERR_SHAMEM_NOT_EXIST;
1169             return ret;
1170         }
1171         arr[i++] = temp;
1172     }
1173 
1174     arrInfo.head = arr;
1175     arrInfo.size = static_cast<int64_t>(formats.size());
1176     ret.code = SUCCESS_CODE;
1177     ret.data = arrInfo;
1178 
1179     return ret;
1180 }
1181 
FfiOHOSImagePackerPackPixelMapToFile(int64_t id,int64_t source,int fd,CPackingOption option)1182 uint32_t FfiOHOSImagePackerPackPixelMapToFile(int64_t id, int64_t source, int fd, CPackingOption option)
1183 {
1184     auto imagePackerImpl = FFIData::GetData<ImagePackerImpl>(id);
1185     if (!imagePackerImpl) {
1186         IMAGE_LOGE("Packing failed, invalid id of ImagePackerImpl");
1187         return ERR_IMAGE_INIT_ABNORMAL;
1188     }
1189 
1190     auto pixelMapImpl = FFIData::GetData<PixelMapImpl>(source);
1191     if (pixelMapImpl != nullptr) {
1192         auto pixelMap = pixelMapImpl->GetRealPixelMap();
1193         if (!pixelMap) {
1194             return ERR_IMAGE_INIT_ABNORMAL;
1195         }
1196 
1197         PackOption packOption = { .format = option.format, .quality = option.quality };
1198         uint32_t ret = imagePackerImpl->PackToFile(pixelMap, fd, packOption);
1199         return ret;
1200     }
1201     return ERR_IMAGE_INIT_ABNORMAL;
1202 }
1203 
FfiOHOSImagePackerPackPixelMapToFileV2(int64_t id,int64_t source,int fd,CPackingOptionV2 option)1204 uint32_t FfiOHOSImagePackerPackPixelMapToFileV2(int64_t id, int64_t source, int fd, CPackingOptionV2 option)
1205 {
1206     auto imagePackerImpl = FFIData::GetData<ImagePackerImpl>(id);
1207     if (!imagePackerImpl) {
1208         IMAGE_LOGE("Packing failed, invalid id of ImagePackerImpl");
1209         return ERR_IMAGE_INIT_ABNORMAL;
1210     }
1211 
1212     auto pixelMapImpl = FFIData::GetData<PixelMapImpl>(source);
1213     if (pixelMapImpl != nullptr) {
1214         auto pixelMap = pixelMapImpl->GetRealPixelMap();
1215         if (!pixelMap) {
1216             return ERR_IMAGE_INIT_ABNORMAL;
1217         }
1218 
1219         PackOption packOption = ParseCPackOption(option);
1220         uint32_t ret = imagePackerImpl->PackToFile(pixelMap, fd, packOption);
1221         return ret;
1222     }
1223     return ERR_IMAGE_INIT_ABNORMAL;
1224 }
1225 
FfiOHOSImagePackerImageSourcePackToFile(int64_t id,int64_t source,int fd,CPackingOption option)1226 uint32_t FfiOHOSImagePackerImageSourcePackToFile(int64_t id, int64_t source, int fd, CPackingOption option)
1227 {
1228     auto imagePackerImpl = FFIData::GetData<ImagePackerImpl>(id);
1229     if (!imagePackerImpl) {
1230         IMAGE_LOGE("Packing failed, invalid id of ImagePackerImpl");
1231         return ERR_IMAGE_INIT_ABNORMAL;
1232     }
1233 
1234     auto imageSourceImpl = FFIData::GetData<ImageSourceImpl>(source);
1235     if (imageSourceImpl != nullptr) {
1236         PackOption packOption = { .format = option.format, .quality = option.quality };
1237         auto imageSource = imageSourceImpl->nativeImgSrc;
1238         if (!imageSource) {
1239             return ERR_IMAGE_INIT_ABNORMAL;
1240         }
1241 
1242         uint32_t ret = imagePackerImpl->PackToFile(imageSource, fd, packOption);
1243         return ret;
1244     }
1245     return ERR_IMAGE_INIT_ABNORMAL;
1246 }
1247 
FfiOHOSImagePackerImageSourcePackToFileV2(int64_t id,int64_t source,int fd,CPackingOptionV2 option)1248 uint32_t FfiOHOSImagePackerImageSourcePackToFileV2(int64_t id, int64_t source, int fd, CPackingOptionV2 option)
1249 {
1250     auto imagePackerImpl = FFIData::GetData<ImagePackerImpl>(id);
1251     if (!imagePackerImpl) {
1252         IMAGE_LOGE("Packing failed, invalid id of ImagePackerImpl");
1253         return ERR_IMAGE_INIT_ABNORMAL;
1254     }
1255 
1256     auto imageSourceImpl = FFIData::GetData<ImageSourceImpl>(source);
1257     if (imageSourceImpl != nullptr) {
1258         PackOption packOption = ParseCPackOption(option);
1259         auto imageSource = imageSourceImpl->nativeImgSrc;
1260         if (!imageSource) {
1261             return ERR_IMAGE_INIT_ABNORMAL;
1262         }
1263 
1264         uint32_t ret = imagePackerImpl->PackToFile(imageSource, fd, packOption);
1265         return ret;
1266     }
1267     return ERR_IMAGE_INIT_ABNORMAL;
1268 }
1269 
FfiImageImagePackerImplPackToFilePicture(int64_t id,int64_t source,int fd,CPackingOptionV2 option)1270 uint32_t FfiImageImagePackerImplPackToFilePicture(int64_t id, int64_t source, int fd, CPackingOptionV2 option)
1271 {
1272     IMAGE_LOGD("[ImagePacker] FfiImageImagePackerImplPackToFilePicture in");
1273     auto imagePackerImpl = FFIData::GetData<ImagePackerImpl>(id);
1274     if (!imagePackerImpl) {
1275         IMAGE_LOGE("Packing failed, invalid id of ImagePackerImpl");
1276         return ERR_IMAGE_INIT_ABNORMAL;
1277     }
1278 
1279     auto pictureImpl = FFIData::GetData<PictureImpl>(source);
1280     if (pictureImpl != nullptr) {
1281         PackOption packOption = ParseCPackOption(option);
1282         auto picture = pictureImpl->GetPicture();
1283         if (!picture) {
1284             return ERR_IMAGE_INIT_ABNORMAL;
1285         }
1286 
1287         uint32_t ret = imagePackerImpl->PackToFile(picture, fd, packOption);
1288         IMAGE_LOGD("[ImagePacker] FfiImageImagePackerImplPackToFilePicture in");
1289         return ret;
1290     }
1291     return ERR_IMAGE_INIT_ABNORMAL;
1292 }
1293 
FfiOHOSGetPackOptionSize()1294 uint64_t FfiOHOSGetPackOptionSize()
1295 {
1296     return sizeof(PackOption);
1297 }
1298 
FFiOHOSImagePackerRelease(int64_t id)1299 void FFiOHOSImagePackerRelease(int64_t id)
1300 {
1301     IMAGE_LOGD("FFiOHOSImagePackerRelease start");
1302     auto instance = FFIData::GetData<ImagePackerImpl>(id);
1303     if (!instance) {
1304         return;
1305     }
1306     instance->Release();
1307     IMAGE_LOGD("FFiOHOSImagePackerRelease success");
1308 }
1309 
1310 //--------------------- ImageCreator ---------------------------------------------------------------------------
FFiOHOSImageCreatorConstructor(int32_t width,int32_t height,int32_t format,int32_t capacity)1311 int64_t FFiOHOSImageCreatorConstructor(int32_t width, int32_t height, int32_t format, int32_t capacity)
1312 {
1313     auto ret = FFIData::Create<ImageCreatorImpl>(width, height, format, capacity);
1314     if (!ret) {
1315         return INIT_FAILED;
1316     }
1317     return ret->GetID();
1318 }
1319 
FFiOHOSImageCreatorGetCapacity(int64_t id)1320 RetDataI32 FFiOHOSImageCreatorGetCapacity(int64_t id)
1321 {
1322     IMAGE_LOGD("FFiOHOSImageCreatorGetCapacity start");
1323     RetDataI32 ret = { .code = ERR_IMAGE_INIT_ABNORMAL, .data = 0 };
1324     auto instance = FFIData::GetData<ImageCreatorImpl>(id);
1325     if (!instance) {
1326         IMAGE_LOGE("FFiOHOSImageCreatorGetCapacity instance not exist %{public}" PRId64, id);
1327         return ret;
1328     }
1329 
1330     std::shared_ptr<ImageCreator> imageCreator = instance->GetImageCreator();
1331     if (!imageCreator) {
1332         IMAGE_LOGE("FFiOHOSImageCreatorGetCapacity imageCreator is nullptr.");
1333         return ret;
1334     }
1335 
1336     std::shared_ptr<ImageCreatorContext> context = imageCreator->iraContext_;
1337     if (!context) {
1338         IMAGE_LOGE("FFiOHOSImageCreatorGetCapacity context is nullptr.");
1339         return ret;
1340     }
1341 
1342     ret.data = context->GetCapicity();
1343     ret.code = SUCCESS_CODE;
1344     IMAGE_LOGD("FFiOHOSImageCreatorGetCapacity success");
1345     return ret;
1346 }
1347 
FFiOHOSImageCreatorGetformat(int64_t id)1348 RetDataI32 FFiOHOSImageCreatorGetformat(int64_t id)
1349 {
1350     IMAGE_LOGD("FFiOHOSImageCreatorGetformat start");
1351     RetDataI32 ret = { .code = ERR_IMAGE_INIT_ABNORMAL, .data = 0 };
1352     auto instance = FFIData::GetData<ImageCreatorImpl>(id);
1353     if (!instance) {
1354         IMAGE_LOGE("FFiOHOSImageCreatorGetformat instance not exist %{public}" PRId64, id);
1355         return ret;
1356     }
1357 
1358     std::shared_ptr<ImageCreator> imageCreator = instance->GetImageCreator();
1359     if (!imageCreator) {
1360         IMAGE_LOGE("FFiOHOSImageCreatorGetformat imageCreator is nullptr.");
1361         return ret;
1362     }
1363 
1364     std::shared_ptr<ImageCreatorContext> context = imageCreator->iraContext_;
1365     if (!context) {
1366         IMAGE_LOGE("FFiOHOSImageCreatorGetformat context is nullptr.");
1367         return ret;
1368     }
1369 
1370     ret.data = context->GetFormat();
1371     ret.code = SUCCESS_CODE;
1372     IMAGE_LOGD("FFiOHOSImageCreatorGetformat success");
1373     return ret;
1374 }
1375 
FFiOHOSImageCreatorDequeueImage(int64_t id,uint32_t * errCode)1376 int64_t FFiOHOSImageCreatorDequeueImage(int64_t id, uint32_t* errCode)
1377 {
1378     IMAGE_LOGD("FFiOHOSImageCreatorDequeueImage start");
1379     auto instance = FFIData::GetData<ImageCreatorImpl>(id);
1380     if (!instance) {
1381         *errCode = ERR_IMAGE_INIT_ABNORMAL;
1382         return INIT_FAILED;
1383     }
1384 
1385     std::shared_ptr<ImageCreator> imageCreator = instance->GetImageCreator();
1386     if (!imageCreator) {
1387         *errCode = ERR_IMAGE_INIT_ABNORMAL;
1388         return INIT_FAILED;
1389     }
1390     std::shared_ptr<NativeImage> nativeImageRes = imageCreator->DequeueNativeImage();
1391     if (!nativeImageRes) {
1392         *errCode = ERR_IMAGE_INIT_ABNORMAL;
1393         return INIT_FAILED;
1394     }
1395     auto ret = FFIData::Create<ImageImpl>(nativeImageRes);
1396     if (!ret) {
1397         *errCode = ERR_IMAGE_INIT_ABNORMAL;
1398         return INIT_FAILED;
1399     }
1400     *errCode = SUCCESS_CODE;
1401     return ret->GetID();
1402 }
1403 
FFiOHOSImageCreatorQueueImage(int64_t id,int64_t imageId)1404 void FFiOHOSImageCreatorQueueImage(int64_t id, int64_t imageId)
1405 {
1406     IMAGE_LOGD("FFiOHOSImageCreatorQueueImage start");
1407     auto instance = FFIData::GetData<ImageCreatorImpl>(id);
1408     if (!instance) {
1409         IMAGE_LOGE("[ImageCreator] instance not exist %{public}" PRId64, id);
1410         return;
1411     }
1412     auto imageInstance = FFIData::GetData<ImageImpl>(imageId);
1413     if (!imageInstance) {
1414         IMAGE_LOGE("[ImageCreator] imageInstance not exist %{public}" PRId64, imageId);
1415         return;
1416     }
1417     std::shared_ptr<ImageCreator> imageCreator = instance->GetImageCreator();
1418     if (!imageCreator) {
1419         IMAGE_LOGE("[ImageCreator] imageCreator can not be nullptr");
1420         return;
1421     }
1422     imageCreator->QueueNativeImage(imageInstance->GetNativeImage());
1423     IMAGE_LOGD("FFiOHOSImageCreatorQueueImage success");
1424 }
1425 
FFiOHOSImageCreatorRelease(int64_t id)1426 void FFiOHOSImageCreatorRelease(int64_t id)
1427 {
1428     IMAGE_LOGD("FFiOHOSImageCreatorRelease start");
1429     auto instance = FFIData::GetData<ImageCreatorImpl>(id);
1430     if (!instance) {
1431         IMAGE_LOGE("[ImageCreator] instance not exist %{public}" PRId64, id);
1432         return;
1433     }
1434     instance->Release();
1435     IMAGE_LOGD("FFiOHOSImageCreatorRelease success");
1436 }
1437 
FfiImageImageCreatorImplOn(int64_t id,char * name,int64_t callbackId)1438 uint32_t FfiImageImageCreatorImplOn(int64_t id, char* name, int64_t callbackId)
1439 {
1440     IMAGE_LOGD("FfiImageImageCreatorImplOn start");
1441     auto instance = FFIData::GetData<ImageCreatorImpl>(id);
1442     if (!instance) {
1443         IMAGE_LOGE("[ImageCreator] instance not exist %{public}" PRId64, id);
1444         return ERR_IMAGE_INIT_ABNORMAL;
1445     }
1446     auto cFunc = reinterpret_cast<void (*)()>(callbackId);
1447     std::function<void()> func = CJLambda::Create(cFunc);
1448     return instance->CjOn(name, func);
1449 }
1450 
1451 //  Picture
FfiImagePictureImplCreatePicture(int64_t id,uint32_t * errCode)1452 int64_t FfiImagePictureImplCreatePicture(int64_t id, uint32_t* errCode)
1453 {
1454     IMAGE_LOGD("[Picture] FfiImagePictureImplCreatePicture in");
1455     auto pixelMapImpl = FFIData::GetData<PixelMapImpl>(id);
1456     if (!pixelMapImpl) {
1457         IMAGE_LOGE("[Picture] pixelMapImpl not exist %{public}" PRId64, id);
1458         return IMAGE_BAD_PARAMETER;
1459     }
1460     auto pixelMap = pixelMapImpl->GetRealPixelMap();
1461     if (!pixelMap) {
1462         IMAGE_LOGE("[Picture] pixelMap is nullptr");
1463         return IMAGE_BAD_PARAMETER;
1464     }
1465     auto picture = Picture::Create(pixelMap);
1466     if (!picture) {
1467         IMAGE_LOGE("[Picture] picture is nullptr");
1468         return IMAGE_BAD_PARAMETER;
1469     }
1470     auto native = FFIData::Create<PictureImpl>(move(picture));
1471     if (!native) {
1472         IMAGE_LOGE("[Picture] native is nullptr");
1473         return IMAGE_BAD_PARAMETER;
1474     }
1475     IMAGE_LOGD("[Picture] FfiImagePictureImplCreatePicture out");
1476     return native->GetID();
1477 }
1478 
FfiImagePictureImplSetMetadata(int64_t id,int32_t metadataType,int64_t metadataId)1479 uint32_t FfiImagePictureImplSetMetadata(int64_t id, int32_t metadataType, int64_t metadataId)
1480 {
1481     IMAGE_LOGD("[Picture] FfiImagePictureImplSetMetadata in");
1482     if (metadataType != static_cast<int32_t>(MetadataType::EXIF)) {
1483         IMAGE_LOGE("Unsupport MetadataType");
1484         return IMAGE_UNSUPPORTED_METADATA;
1485     }
1486     auto picture = FFIData::GetData<PictureImpl>(id);
1487     if (!picture) {
1488         IMAGE_LOGE("[Picture] picture not exist %{public}" PRId64, id);
1489         return IMAGE_BAD_PARAMETER;
1490     }
1491     auto metadata = FFIData::GetData<MetadataImpl>(metadataId);
1492     if (!metadata) {
1493         IMAGE_LOGE("[Picture] metadata not exist %{public}" PRId64, metadataId);
1494         return IMAGE_BAD_PARAMETER;
1495     }
1496     IMAGE_LOGD("[Picture] FfiImagePictureImplSetMetadata out");
1497     return picture->SetMetadata(MetadataType(metadataType), metadata->GetNativeMetadata());
1498 }
1499 
FfiImagePictureImplGetMetadata(int64_t id,int32_t metadataType,uint32_t * errCode)1500 int64_t FfiImagePictureImplGetMetadata(int64_t id, int32_t metadataType, uint32_t* errCode)
1501 {
1502     IMAGE_LOGD("[Picture] FfiImagePictureImplGetMetadata in");
1503     if (metadataType != static_cast<int32_t>(MetadataType::EXIF)) {
1504         IMAGE_LOGE("Unsupport MetadataType");
1505         *errCode = IMAGE_UNSUPPORTED_METADATA;
1506         return 0;
1507     }
1508     auto picture = FFIData::GetData<PictureImpl>(id);
1509     if (!picture) {
1510         IMAGE_LOGE("[Picture] picture not exist %{public}" PRId64, id);
1511         *errCode = IMAGE_BAD_PARAMETER;
1512         return 0;
1513     }
1514     auto metadata = picture->GetMetadata(MetadataType(metadataType), errCode);
1515     if (!metadata) {
1516         IMAGE_LOGE("[Picture] metadata is nullptr");
1517         *errCode = IMAGE_BAD_PARAMETER;
1518         return 0;
1519     }
1520     auto native = FFIData::Create<MetadataImpl>(metadata);
1521     if (!native) {
1522         IMAGE_LOGE("[Picture] native is nullptr");
1523         *errCode = IMAGE_BAD_PARAMETER;
1524         return 0;
1525     }
1526     IMAGE_LOGD("[Picture] FfiImagePictureImplGetMetadata out");
1527     return native->GetID();
1528 }
1529 
1530 // Metadata
FfiImageMetadataImplGetAllProperties(int64_t id,uint32_t * errCode)1531 CjProperties FfiImageMetadataImplGetAllProperties(int64_t id, uint32_t* errCode)
1532 {
1533     IMAGE_LOGD("[Metadata] FfiImageMetadataImplGetAllProperties in");
1534     CjProperties res = { 0 };
1535     auto metadata = FFIData::GetData<MetadataImpl>(id);
1536     if (!metadata) {
1537         IMAGE_LOGE("[Metadata] metadata is nullptr");
1538         *errCode = IMAGE_BAD_PARAMETER;
1539         return res;
1540     }
1541     std::vector<std::pair<std::string, std::string>> vec = metadata->GetAllProperties(errCode);
1542     if (*errCode != SUCCESS) {
1543         return res;
1544     }
1545     size_t size = vec.size();
1546     if (size < 0) {
1547         *errCode = IMAGE_BAD_PARAMETER;
1548         return res;
1549     }
1550     if (size == 0) {
1551         return res;
1552     }
1553     char** keyArr = static_cast<char**>(malloc(sizeof(char*) * size));
1554     if (keyArr == nullptr) {
1555         IMAGE_LOGE("[Metadata] keyArr is nullptr");
1556         *errCode = IMAGE_BAD_PARAMETER;
1557         return res;
1558     }
1559     char** valueArr = static_cast<char**>(malloc(sizeof(char*) * size));
1560     if (valueArr == nullptr) {
1561         IMAGE_LOGE("[Metadata] valueArr is nullptr");
1562         free(keyArr);
1563         *errCode = IMAGE_BAD_PARAMETER;
1564         return res;
1565     }
1566     size_t index = 0;
1567     for (const auto& p : vec) {
1568         keyArr[index] = Utils::MallocCString(p.first);
1569         valueArr[index] = Utils::MallocCString(p.second);
1570         index++;
1571     }
1572     res.key = keyArr;
1573     res.value = valueArr;
1574     res.size = static_cast<int64_t>(size);
1575     IMAGE_LOGD("[Metadata] FfiImageMetadataImplGetAllProperties out");
1576     return res;
1577 }
1578 
FfiImageMetadataImplReleaseProperties(CjProperties * properties)1579 void FfiImageMetadataImplReleaseProperties(CjProperties* properties)
1580 {
1581     if (properties != nullptr) {
1582         if (properties->key != nullptr) {
1583             for (int64_t i = 0; i < properties->size; i++) {
1584                 free(properties->key[i]);
1585             }
1586         }
1587         free(properties->key);
1588         properties->key = nullptr;
1589         if (properties->value != nullptr) {
1590             for (int64_t i = 0; i < properties->size; i++) {
1591                 free(properties->value[i]);
1592             }
1593         }
1594         free(properties->value);
1595         properties->value = nullptr;
1596     }
1597 }
1598 }
1599 } // namespace Media
1600 } // namespace OHOS