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 #include "image_source_native.h"
17 #include "jpeg_decoder_yuv.h"
18 #include "picture_native_impl.h"
19 #include "common_utils.h"
20 #include "image_error_convert.h"
21 #include "image_source.h"
22 #include "image_source_native_impl.h"
23 #include "image_utils.h"
24 #include "pixelmap_native_impl.h"
25 #include "picture_native.h"
26 #include "media_errors.h"
27 #include "image_log.h"
28 #include "native_color_space_manager.h"
29 #include "ndk_color_space.h"
30
31 #ifndef _WIN32
32 #include "securec.h"
33 #else
34 #include "memory.h"
35 #endif
36
37 using namespace OHOS;
38 using namespace Media;
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 const uint32_t DEFAULT_INDEX = 0;
44 constexpr size_t SIZE_ZERO = 0;
45 constexpr uint32_t INVALID_SAMPLE_SIZE = 0;
46 const int32_t INVALID_FD = -1;
47 static constexpr int32_t FORMAT_0 = 0;
48 static constexpr int32_t FORMAT_2 = 2;
49 static constexpr int32_t FORMAT_3 = 3;
50 static constexpr int32_t FORMAT_4 = 4;
51 static constexpr int32_t FORMAT_5 = 5;
52 static constexpr int32_t FORMAT_6 = 6;
53 static constexpr int32_t FORMAT_7 = 7;
54 static constexpr int32_t FORMAT_8 = 8;
55 static constexpr int32_t FORMAT_9 = 9;
56 using JpegYuvDecodeError = OHOS::ImagePlugin::JpegYuvDecodeError;
57 static Image_MimeType *IMAGE_SOURCE_SUPPORTED_FORMATS = nullptr;
58 static size_t SUPPORTED_FORMATS_SIZE = 0;
59
60 struct OH_DecodingOptions {
61 int32_t pixelFormat;
62 uint32_t index;
63 uint32_t sampleSize = INVALID_SAMPLE_SIZE;
64 uint32_t rotate;
65 struct Image_Size desiredSize;
66 struct Image_Region desiredRegion;
67 int32_t desiredDynamicRange = IMAGE_DYNAMIC_RANGE_SDR;
68 int32_t cropAndScaleStrategy;
69 int32_t desiredColorSpace = 0;
70 struct Image_Region cropRegion;
71 bool isCropRegionSet = false;
72 };
73
74 struct OH_ImageSource_Info {
75 /** Image width, in pixels. */
76 int32_t width;
77 /** Image height, in pixels. */
78 int32_t height;
79 /** Image dynamicRange*/
80 bool isHdr;
81 /** Image mime type. */
82 Image_MimeType mimeType;
83 };
84
85 static const std::map<int32_t, Image_ErrorCode> ERROR_CODE_MAP = {
86 {ERR_IMAGE_INVALID_PARAMETER, Image_ErrorCode::IMAGE_BAD_PARAMETER},
87 {COMMON_ERR_INVALID_PARAMETER, Image_ErrorCode::IMAGE_BAD_PARAMETER},
88 {JpegYuvDecodeError::JpegYuvDecodeError_InvalidParameter, Image_ErrorCode::IMAGE_BAD_PARAMETER},
89 {ERR_IMAGE_SOURCE_DATA, Image_ErrorCode::IMAGE_BAD_SOURCE},
90 {ERR_IMAGE_SOURCE_DATA_INCOMPLETE, Image_ErrorCode::IMAGE_BAD_SOURCE},
91 {ERR_IMAGE_GET_DATA_ABNORMAL, Image_ErrorCode::IMAGE_BAD_SOURCE},
92 {ERR_IMAGE_DATA_ABNORMAL, Image_ErrorCode::IMAGE_BAD_SOURCE},
93 {ERROR, Image_ErrorCode::IMAGE_BAD_SOURCE},
94 {JpegYuvDecodeError::JpegYuvDecodeError_BadImage, Image_ErrorCode::IMAGE_BAD_SOURCE},
95 {ERR_IMAGE_MISMATCHED_FORMAT, Image_ErrorCode::IMAGE_SOURCE_UNSUPPORTED_MIMETYPE},
96 {ERR_IMAGE_UNKNOWN_FORMAT, Image_ErrorCode::IMAGE_SOURCE_UNSUPPORTED_MIMETYPE},
97 {ERR_IMAGE_DECODE_HEAD_ABNORMAL, Image_ErrorCode::IMAGE_SOURCE_UNSUPPORTED_MIMETYPE},
98 {ERR_IMAGE_TOO_LARGE, Image_ErrorCode::IMAGE_SOURCE_TOO_LARGE},
99 {ERR_MEDIA_INVALID_OPERATION, Image_ErrorCode::IMAGE_SOURCE_UNSUPPORTED_ALLOCATOR_TYPE},
100 {IMAGE_RESULT_FORMAT_CONVERT_FAILED, Image_ErrorCode::IMAGE_SOURCE_UNSUPPORTED_OPTIONS},
101 {ERR_MEDIA_FORMAT_UNSUPPORT, Image_ErrorCode::IMAGE_SOURCE_UNSUPPORTED_OPTIONS},
102 {ERR_IMAGE_PIXELMAP_CREATE_FAILED, Image_ErrorCode::IMAGE_SOURCE_UNSUPPORTED_OPTIONS},
103 {JpegYuvDecodeError::JpegYuvDecodeError_ConvertError, Image_ErrorCode::IMAGE_SOURCE_UNSUPPORTED_OPTIONS},
104 {ERR_IMAGE_CROP, Image_ErrorCode::IMAGE_SOURCE_UNSUPPORTED_OPTIONS},
105 {ERR_IMAGE_DECODE_FAILED, Image_ErrorCode::IMAGE_DECODE_FAILED},
106 {ERR_IMAGE_DECODE_ABNORMAL, Image_ErrorCode::IMAGE_DECODE_FAILED},
107 {ERR_IMAGE_PLUGIN_CREATE_FAILED, Image_ErrorCode::IMAGE_DECODE_FAILED},
108 {JpegYuvDecodeError::JpegYuvDecodeError_DecodeFailed, Image_ErrorCode::IMAGE_DECODE_FAILED},
109 {JpegYuvDecodeError::JpegYuvDecodeError_MemoryNotEnoughToSaveResult, Image_ErrorCode::IMAGE_DECODE_FAILED},
110 {ERR_IMAGE_MALLOC_ABNORMAL, Image_ErrorCode::IMAGE_SOURCE_ALLOC_FAILED},
111 {ERR_IMAGE_DATA_UNSUPPORT, Image_ErrorCode::IMAGE_SOURCE_ALLOC_FAILED},
112 {ERR_DMA_NOT_EXIST, Image_ErrorCode::IMAGE_SOURCE_ALLOC_FAILED},
113 {ERR_DMA_DATA_ABNORMAL, Image_ErrorCode::IMAGE_SOURCE_ALLOC_FAILED},
114 {ERR_SHAMEM_DATA_ABNORMAL, Image_ErrorCode::IMAGE_SOURCE_ALLOC_FAILED}
115 };
ConvertToErrorCode(int32_t errorCode)116 static Image_ErrorCode ConvertToErrorCode(int32_t errorCode)
117 {
118 Image_ErrorCode apiErrorCode = Image_ErrorCode::IMAGE_DECODE_FAILED;
119 auto iter = ERROR_CODE_MAP.find(errorCode);
120 if (iter != ERROR_CODE_MAP.end()) {
121 apiErrorCode = iter->second;
122 }
123 return apiErrorCode;
124 }
125
AuxTypeInnerToNative(OHOS::Media::AuxiliaryPictureType type)126 static Image_AuxiliaryPictureType AuxTypeInnerToNative(OHOS::Media::AuxiliaryPictureType type)
127 {
128 return static_cast<Image_AuxiliaryPictureType>(static_cast<int>(type));
129 }
130
AuxTypeNativeToInner(Image_AuxiliaryPictureType type)131 static OHOS::Media::AuxiliaryPictureType AuxTypeNativeToInner(Image_AuxiliaryPictureType type)
132 {
133 return static_cast<OHOS::Media::AuxiliaryPictureType>(static_cast<int>(type));
134 }
135
ParseImageDynamicRange(int32_t val)136 static DecodeDynamicRange ParseImageDynamicRange(int32_t val)
137 {
138 if (val <= static_cast<int32_t>(DecodeDynamicRange::HDR)) {
139 return DecodeDynamicRange(val);
140 }
141
142 return DecodeDynamicRange::SDR;
143 }
144
releaseMimeType(Image_MimeType * mimeType)145 static void releaseMimeType(Image_MimeType *mimeType)
146 {
147 if (mimeType->data != nullptr) {
148 free(mimeType->data);
149 mimeType->data = nullptr;
150 }
151 mimeType->size = 0;
152 }
153
154 MIDK_EXPORT
OH_DecodingOptions_Create(OH_DecodingOptions ** options)155 Image_ErrorCode OH_DecodingOptions_Create(OH_DecodingOptions **options)
156 {
157 *options = new OH_DecodingOptions();
158 if (*options == nullptr) {
159 return IMAGE_BAD_PARAMETER;
160 }
161 return IMAGE_SUCCESS;
162 }
163
164 MIDK_EXPORT
OH_DecodingOptions_GetPixelFormat(OH_DecodingOptions * options,int32_t * pixelFormat)165 Image_ErrorCode OH_DecodingOptions_GetPixelFormat(OH_DecodingOptions *options,
166 int32_t *pixelFormat)
167 {
168 if (options == nullptr || pixelFormat == nullptr) {
169 return IMAGE_BAD_PARAMETER;
170 }
171 *pixelFormat = options->pixelFormat;
172 return IMAGE_SUCCESS;
173 }
174
175 MIDK_EXPORT
OH_DecodingOptions_SetPixelFormat(OH_DecodingOptions * options,int32_t pixelFormat)176 Image_ErrorCode OH_DecodingOptions_SetPixelFormat(OH_DecodingOptions *options,
177 int32_t pixelFormat)
178 {
179 if (options == nullptr) {
180 return IMAGE_BAD_PARAMETER;
181 }
182 options->pixelFormat = pixelFormat;
183 return IMAGE_SUCCESS;
184 }
185
IsCropStrategyValid(int32_t strategy)186 static inline bool IsCropStrategyValid(int32_t strategy)
187 {
188 return strategy >= static_cast<int32_t>(CropAndScaleStrategy::SCALE_FIRST) &&
189 strategy <= static_cast<int32_t>(CropAndScaleStrategy::CROP_FIRST);
190 }
191
OH_DecodingOptions_GetCropAndScaleStrategy(OH_DecodingOptions * options,int32_t * cropAndScaleStrategy)192 Image_ErrorCode OH_DecodingOptions_GetCropAndScaleStrategy(OH_DecodingOptions *options,
193 int32_t *cropAndScaleStrategy)
194 {
195 if (options == nullptr || cropAndScaleStrategy == nullptr) {
196 IMAGE_LOGE("options or cropAndScaleStrategy is nullptr");
197 return IMAGE_BAD_PARAMETER;
198 }
199 if (!IsCropStrategyValid(options->cropAndScaleStrategy)) {
200 IMAGE_LOGE("SetCropAndScaleStrategy was not called or the method call failed");
201 return IMAGE_BAD_PARAMETER;
202 }
203 *cropAndScaleStrategy = options->cropAndScaleStrategy;
204 return IMAGE_SUCCESS;
205 }
206
OH_DecodingOptions_SetCropAndScaleStrategy(OH_DecodingOptions * options,int32_t cropAndScaleStrategy)207 Image_ErrorCode OH_DecodingOptions_SetCropAndScaleStrategy(OH_DecodingOptions *options,
208 int32_t cropAndScaleStrategy)
209 {
210 if (options == nullptr) {
211 return IMAGE_BAD_PARAMETER;
212 }
213 if (!IsCropStrategyValid(cropAndScaleStrategy)) {
214 IMAGE_LOGE("cropAndScaleStrategy:%{public}d is invalid", cropAndScaleStrategy);
215 return IMAGE_BAD_PARAMETER;
216 }
217 options->cropAndScaleStrategy = cropAndScaleStrategy;
218 return IMAGE_SUCCESS;
219 }
220
221 MIDK_EXPORT
OH_DecodingOptions_GetIndex(OH_DecodingOptions * options,uint32_t * index)222 Image_ErrorCode OH_DecodingOptions_GetIndex(OH_DecodingOptions *options, uint32_t *index)
223 {
224 if (options == nullptr || index == nullptr) {
225 return IMAGE_BAD_PARAMETER;
226 }
227 *index = options->index;
228 return IMAGE_SUCCESS;
229 }
230
231 MIDK_EXPORT
OH_DecodingOptions_SetIndex(OH_DecodingOptions * options,uint32_t index)232 Image_ErrorCode OH_DecodingOptions_SetIndex(OH_DecodingOptions *options, uint32_t index)
233 {
234 if (options == nullptr) {
235 return IMAGE_BAD_PARAMETER;
236 }
237 options->index = index;
238 return IMAGE_SUCCESS;
239 }
240
241 MIDK_EXPORT
OH_DecodingOptions_GetRotate(OH_DecodingOptions * options,float * rotate)242 Image_ErrorCode OH_DecodingOptions_GetRotate(OH_DecodingOptions *options, float *rotate)
243 {
244 if (options == nullptr || rotate == nullptr) {
245 return IMAGE_BAD_PARAMETER;
246 }
247 *rotate = options->rotate;
248 return IMAGE_SUCCESS;
249 }
250
251 MIDK_EXPORT
OH_DecodingOptions_SetRotate(OH_DecodingOptions * options,float rotate)252 Image_ErrorCode OH_DecodingOptions_SetRotate(OH_DecodingOptions *options, float rotate)
253 {
254 if (options == nullptr) {
255 return IMAGE_BAD_PARAMETER;
256 }
257 options->rotate = rotate;
258 return IMAGE_SUCCESS;
259 }
260
261 MIDK_EXPORT
OH_DecodingOptions_GetDesiredSize(OH_DecodingOptions * options,Image_Size * desiredSize)262 Image_ErrorCode OH_DecodingOptions_GetDesiredSize(OH_DecodingOptions *options,
263 Image_Size *desiredSize)
264 {
265 if (options == nullptr || desiredSize == nullptr) {
266 return IMAGE_BAD_PARAMETER;
267 }
268 desiredSize->width = options->desiredSize.width;
269 desiredSize->height = options->desiredSize.height;
270 return IMAGE_SUCCESS;
271 }
272
273 MIDK_EXPORT
OH_DecodingOptions_SetDesiredSize(OH_DecodingOptions * options,Image_Size * desiredSize)274 Image_ErrorCode OH_DecodingOptions_SetDesiredSize(OH_DecodingOptions *options,
275 Image_Size *desiredSize)
276 {
277 if (options == nullptr || desiredSize == nullptr) {
278 return IMAGE_BAD_PARAMETER;
279 }
280 options->desiredSize.width = desiredSize->width;
281 options->desiredSize.height = desiredSize->height;
282 return IMAGE_SUCCESS;
283 }
284
285 MIDK_EXPORT
OH_DecodingOptions_GetDesiredRegion(OH_DecodingOptions * options,Image_Region * desiredRegion)286 Image_ErrorCode OH_DecodingOptions_GetDesiredRegion(OH_DecodingOptions *options,
287 Image_Region *desiredRegion)
288 {
289 if (options == nullptr || desiredRegion == nullptr) {
290 return IMAGE_BAD_PARAMETER;
291 }
292 desiredRegion->x = options->desiredRegion.x;
293 desiredRegion->y = options->desiredRegion.y;
294 desiredRegion->width = options->desiredRegion.width;
295 desiredRegion->height = options->desiredRegion.height;
296 return IMAGE_SUCCESS;
297 }
298
299 MIDK_EXPORT
OH_DecodingOptions_SetDesiredRegion(OH_DecodingOptions * options,Image_Region * desiredRegion)300 Image_ErrorCode OH_DecodingOptions_SetDesiredRegion(OH_DecodingOptions *options,
301 Image_Region *desiredRegion)
302 {
303 if (options == nullptr || desiredRegion == nullptr) {
304 return IMAGE_BAD_PARAMETER;
305 }
306 options->desiredRegion.x = desiredRegion->x;
307 options->desiredRegion.y = desiredRegion->y;
308 options->desiredRegion.width = desiredRegion->width;
309 options->desiredRegion.height = desiredRegion->height;
310 return IMAGE_SUCCESS;
311 }
312
OH_DecodingOptions_GetCropRegion(OH_DecodingOptions * options,Image_Region * cropRegion)313 Image_ErrorCode OH_DecodingOptions_GetCropRegion(OH_DecodingOptions *options, Image_Region *cropRegion)
314 {
315 if (options == nullptr || cropRegion == nullptr) {
316 return IMAGE_SOURCE_INVALID_PARAMETER;
317 }
318 cropRegion->x = options->cropRegion.x;
319 cropRegion->y = options->cropRegion.y;
320 cropRegion->width = options->cropRegion.width;
321 cropRegion->height = options->cropRegion.height;
322 return IMAGE_SUCCESS;
323 }
324
OH_DecodingOptions_SetCropRegion(OH_DecodingOptions * options,Image_Region * cropRegion)325 Image_ErrorCode OH_DecodingOptions_SetCropRegion(OH_DecodingOptions *options, Image_Region *cropRegion)
326 {
327 if (options == nullptr || cropRegion == nullptr) {
328 return IMAGE_SOURCE_INVALID_PARAMETER;
329 }
330 options->cropRegion.x = cropRegion->x;
331 options->cropRegion.y = cropRegion->y;
332 options->cropRegion.width = cropRegion->width;
333 options->cropRegion.height = cropRegion->height;
334 options->isCropRegionSet = true;
335 return IMAGE_SUCCESS;
336 }
337
338 MIDK_EXPORT
OH_DecodingOptions_GetDesiredDynamicRange(OH_DecodingOptions * options,int32_t * desiredDynamicRange)339 Image_ErrorCode OH_DecodingOptions_GetDesiredDynamicRange(OH_DecodingOptions *options,
340 int32_t *desiredDynamicRange)
341 {
342 if (options == nullptr || desiredDynamicRange == nullptr) {
343 return IMAGE_BAD_PARAMETER;
344 }
345 *desiredDynamicRange = options->desiredDynamicRange;
346 return IMAGE_SUCCESS;
347 }
348
349 MIDK_EXPORT
OH_DecodingOptions_SetDesiredDynamicRange(OH_DecodingOptions * options,int32_t desiredDynamicRange)350 Image_ErrorCode OH_DecodingOptions_SetDesiredDynamicRange(OH_DecodingOptions *options,
351 int32_t desiredDynamicRange)
352 {
353 if (options == nullptr) {
354 return IMAGE_BAD_PARAMETER;
355 }
356 options->desiredDynamicRange = desiredDynamicRange;
357 return IMAGE_SUCCESS;
358 }
359
360 MIDK_EXPORT
OH_DecodingOptions_GetDesiredColorSpace(OH_DecodingOptions * options,int32_t * colorSpace)361 Image_ErrorCode OH_DecodingOptions_GetDesiredColorSpace(OH_DecodingOptions *options, int32_t *colorSpace)
362 {
363 if (options == nullptr || colorSpace == nullptr) {
364 return IMAGE_SOURCE_INVALID_PARAMETER;
365 }
366 *colorSpace = options->desiredColorSpace;
367 return IMAGE_SUCCESS;
368 }
369
IsColorSpaceInvalid(int32_t colorSpace)370 inline static bool IsColorSpaceInvalid(int32_t colorSpace)
371 {
372 return colorSpace <= static_cast<int32_t>(ColorSpaceName::NONE) ||
373 colorSpace > static_cast<int32_t>(ColorSpaceName::LINEAR_BT2020) ||
374 colorSpace == static_cast<int32_t>(ColorSpaceName::CUSTOM);
375 }
376
377 MIDK_EXPORT
OH_DecodingOptions_SetDesiredColorSpace(OH_DecodingOptions * options,int32_t colorSpace)378 Image_ErrorCode OH_DecodingOptions_SetDesiredColorSpace(OH_DecodingOptions *options, int32_t colorSpace)
379 {
380 if (options == nullptr || IsColorSpaceInvalid(colorSpace) ||
381 colorSpace == static_cast<int32_t>(ColorSpaceName::CUSTOM)) {
382 return IMAGE_SOURCE_INVALID_PARAMETER;
383 }
384 options->desiredColorSpace = colorSpace;
385 return IMAGE_SUCCESS;
386 }
387
388 MIDK_EXPORT
OH_DecodingOptions_Release(OH_DecodingOptions * options)389 Image_ErrorCode OH_DecodingOptions_Release(OH_DecodingOptions *options)
390 {
391 if (options == nullptr) {
392 return IMAGE_BAD_PARAMETER;
393 }
394 delete options;
395 return IMAGE_SUCCESS;
396 }
397
398 MIDK_EXPORT
OH_ImageSourceInfo_Create(OH_ImageSource_Info ** info)399 Image_ErrorCode OH_ImageSourceInfo_Create(OH_ImageSource_Info **info)
400 {
401 *info = new OH_ImageSource_Info();
402 if (*info == nullptr) {
403 return IMAGE_BAD_PARAMETER;
404 }
405 return IMAGE_SUCCESS;
406 }
407
408 MIDK_EXPORT
OH_ImageSourceInfo_GetWidth(OH_ImageSource_Info * info,uint32_t * width)409 Image_ErrorCode OH_ImageSourceInfo_GetWidth(OH_ImageSource_Info *info, uint32_t *width)
410 {
411 if (info == nullptr || width == nullptr) {
412 return IMAGE_BAD_PARAMETER;
413 }
414 *width = info->width;
415 return IMAGE_SUCCESS;
416 }
417
418 MIDK_EXPORT
OH_ImageSourceInfo_GetHeight(OH_ImageSource_Info * info,uint32_t * height)419 Image_ErrorCode OH_ImageSourceInfo_GetHeight(OH_ImageSource_Info *info, uint32_t *height)
420 {
421 if (info == nullptr || height == nullptr) {
422 return IMAGE_BAD_PARAMETER;
423 }
424 *height = info->height;
425 return IMAGE_SUCCESS;
426 }
427
428 MIDK_EXPORT
OH_ImageSourceInfo_GetDynamicRange(OH_ImageSource_Info * info,bool * isHdr)429 Image_ErrorCode OH_ImageSourceInfo_GetDynamicRange(OH_ImageSource_Info *info, bool *isHdr)
430 {
431 if (info == nullptr || isHdr == nullptr) {
432 return IMAGE_BAD_PARAMETER;
433 }
434 *isHdr = info->isHdr;
435 return IMAGE_SUCCESS;
436 }
437
438 MIDK_EXPORT
OH_ImageSourceInfo_GetMimeType(OH_ImageSource_Info * info,Image_MimeType * mimeType)439 Image_ErrorCode OH_ImageSourceInfo_GetMimeType(OH_ImageSource_Info *info, Image_MimeType *mimeType)
440 {
441 if (info == nullptr || mimeType == nullptr) {
442 return IMAGE_SOURCE_INVALID_PARAMETER;
443 }
444 *mimeType = info->mimeType;
445 return IMAGE_SUCCESS;
446 }
447
448 MIDK_EXPORT
OH_ImageSourceInfo_Release(OH_ImageSource_Info * info)449 Image_ErrorCode OH_ImageSourceInfo_Release(OH_ImageSource_Info *info)
450 {
451 if (info == nullptr) {
452 return IMAGE_BAD_PARAMETER;
453 }
454 releaseMimeType(&info->mimeType);
455 delete info;
456 info = nullptr;
457 return IMAGE_SUCCESS;
458 }
459
460
UrlToPath(const std::string & path)461 std::string OH_ImageSourceNative::UrlToPath(const std::string &path)
462 {
463 const std::string filePrefix = "file://";
464 if (path.size() > filePrefix.size() &&
465 (path.compare(0, filePrefix.size(), filePrefix) == 0)) {
466 return path.substr(filePrefix.size());
467 }
468 return path;
469 }
470
ParseDecodingOps(DecodeOptions & decOps,struct OH_DecodingOptions * ops)471 static void ParseDecodingOps(DecodeOptions &decOps, struct OH_DecodingOptions *ops)
472 {
473 if (ops->sampleSize != INVALID_SAMPLE_SIZE) {
474 decOps.sampleSize = ops->sampleSize;
475 }
476 decOps.rotateNewDegrees = ops->rotate;
477 decOps.desiredSize.width = static_cast<int32_t>(ops->desiredSize.width);
478 decOps.desiredSize.height = static_cast<int32_t>(ops->desiredSize.height);
479 if (ops->isCropRegionSet) {
480 decOps.CropRect.left = static_cast<int32_t>(ops->cropRegion.x);
481 decOps.CropRect.top = static_cast<int32_t>(ops->cropRegion.y);
482 decOps.CropRect.width = static_cast<int32_t>(ops->cropRegion.width);
483 decOps.CropRect.height = static_cast<int32_t>(ops->cropRegion.height);
484 } else if (IsCropStrategyValid(ops->cropAndScaleStrategy)) {
485 decOps.CropRect.left = static_cast<int32_t>(ops->desiredRegion.x);
486 decOps.CropRect.top = static_cast<int32_t>(ops->desiredRegion.y);
487 decOps.CropRect.width = static_cast<int32_t>(ops->desiredRegion.width);
488 decOps.CropRect.height = static_cast<int32_t>(ops->desiredRegion.height);
489 } else {
490 decOps.desiredRegion.left = static_cast<int32_t>(ops->desiredRegion.x);
491 decOps.desiredRegion.top = static_cast<int32_t>(ops->desiredRegion.y);
492 decOps.desiredRegion.width = static_cast<int32_t>(ops->desiredRegion.width);
493 decOps.desiredRegion.height = static_cast<int32_t>(ops->desiredRegion.height);
494 }
495 decOps.desiredDynamicRange = ParseImageDynamicRange(ops->desiredDynamicRange);
496 switch (static_cast<int32_t>(ops->pixelFormat)) {
497 case FORMAT_0:
498 case FORMAT_2:
499 case FORMAT_3:
500 case FORMAT_4:
501 case FORMAT_5:
502 case FORMAT_6:
503 case FORMAT_7:
504 case FORMAT_8:
505 case FORMAT_9:
506 decOps.desiredPixelFormat = PixelFormat(ops->pixelFormat);
507 break;
508 default:
509 decOps.desiredPixelFormat = PixelFormat::UNKNOWN;
510 }
511 if (IsCropStrategyValid(ops->cropAndScaleStrategy)) {
512 decOps.cropAndScaleStrategy = static_cast<OHOS::Media::CropAndScaleStrategy>(ops->cropAndScaleStrategy);
513 }
514 OH_NativeColorSpaceManager* colorSpaceNative =
515 OH_NativeColorSpaceManager_CreateFromName(ColorSpaceName(ops->desiredColorSpace));
516 if (colorSpaceNative != nullptr) {
517 ColorManager::ColorSpace nativeColorspace =
518 reinterpret_cast<NativeColorSpaceManager*>(colorSpaceNative)->GetInnerColorSpace();
519 decOps.desiredColorSpaceInfo = std::make_shared<OHOS::ColorManager::ColorSpace>(nativeColorspace);
520 OH_NativeColorSpaceManager_Destroy(colorSpaceNative);
521 } else {
522 IMAGE_LOGD("no colorSpace");
523 }
524 }
525
ParseImageSourceInfo(struct OH_ImageSource_Info * source,const ImageInfo & info)526 static void ParseImageSourceInfo(struct OH_ImageSource_Info *source, const ImageInfo &info)
527 {
528 if (source == nullptr) {
529 return;
530 }
531 source->width = info.size.width;
532 source->height = info.size.height;
533 if (source->mimeType.data != nullptr) {
534 return;
535 }
536 if (info.encodedFormat.empty()) {
537 std::string unknownStr = "unknown";
538 source->mimeType.data = strdup(unknownStr.c_str());
539 source->mimeType.size = unknownStr.size();
540 return;
541 }
542 source->mimeType.size = info.encodedFormat.size();
543 source->mimeType.data = static_cast<char *>(malloc(source->mimeType.size));
544 if (source->mimeType.data == nullptr) {
545 return;
546 }
547 if (memcpy_s(source->mimeType.data, source->mimeType.size, info.encodedFormat.c_str(),
548 info.encodedFormat.size()) != 0) {
549 releaseMimeType(&source->mimeType);
550 }
551 }
552
553 MIDK_EXPORT
OH_ImageSourceNative_CreateFromUri(char * uri,size_t uriSize,OH_ImageSourceNative ** res)554 Image_ErrorCode OH_ImageSourceNative_CreateFromUri(char *uri, size_t uriSize, OH_ImageSourceNative **res)
555 {
556 if (uri == nullptr) {
557 return IMAGE_BAD_PARAMETER;
558 }
559 SourceOptions options;
560 auto imageSource = new OH_ImageSourceNative(uri, uriSize, options);
561 if (imageSource == nullptr || imageSource->GetInnerImageSource() == nullptr) {
562 if (imageSource) {
563 delete imageSource;
564 }
565 *res = nullptr;
566 return IMAGE_BAD_PARAMETER;
567 }
568 std::string tmp(uri, uriSize);
569 if (tmp.empty()) {
570 delete imageSource;
571 return IMAGE_BAD_PARAMETER;
572 }
573 imageSource->filePath_ = tmp;
574 *res = imageSource;
575 return IMAGE_SUCCESS;
576 }
577
578 MIDK_EXPORT
OH_ImageSourceNative_CreateFromFd(int32_t fd,OH_ImageSourceNative ** res)579 Image_ErrorCode OH_ImageSourceNative_CreateFromFd(int32_t fd, OH_ImageSourceNative **res)
580 {
581 SourceOptions options;
582 auto imageSource = new OH_ImageSourceNative(fd, options);
583 if (imageSource == nullptr || imageSource->GetInnerImageSource() == nullptr) {
584 if (imageSource) {
585 delete imageSource;
586 }
587 *res = nullptr;
588 return IMAGE_BAD_PARAMETER;
589 }
590 imageSource->fileDescriptor_ = fd;
591 *res = imageSource;
592 return IMAGE_SUCCESS;
593 }
594
CreateFromDataInternal(uint8_t * data,size_t dataSize,OH_ImageSourceNative ** res,bool isUserBuffer)595 Image_ErrorCode CreateFromDataInternal(uint8_t *data, size_t dataSize, OH_ImageSourceNative **res, bool isUserBuffer)
596 {
597 if (data == nullptr) {
598 return isUserBuffer ? IMAGE_SOURCE_INVALID_PARAMETER : IMAGE_BAD_PARAMETER;
599 }
600 SourceOptions options;
601 auto imageSource = new OH_ImageSourceNative(data, dataSize, options, isUserBuffer);
602 if (imageSource == nullptr || imageSource->GetInnerImageSource() == nullptr) {
603 if (imageSource) {
604 delete imageSource;
605 }
606 *res = nullptr;
607 return isUserBuffer ? IMAGE_SOURCE_INVALID_PARAMETER : IMAGE_BAD_PARAMETER;
608 }
609 imageSource->fileBuffer_ = reinterpret_cast<void*>(data);
610 imageSource->fileBufferSize_ = dataSize;
611 *res = imageSource;
612 return IMAGE_SUCCESS;
613 }
614
615 MIDK_EXPORT
OH_ImageSourceNative_CreateFromData(uint8_t * data,size_t dataSize,OH_ImageSourceNative ** res)616 Image_ErrorCode OH_ImageSourceNative_CreateFromData(uint8_t *data, size_t dataSize, OH_ImageSourceNative **res)
617 {
618 return CreateFromDataInternal(data, dataSize, res, false);
619 }
620
621 MIDK_EXPORT
OH_ImageSourceNative_CreateFromDataWithUserBuffer(uint8_t * data,size_t datalength,OH_ImageSourceNative ** imageSource)622 Image_ErrorCode OH_ImageSourceNative_CreateFromDataWithUserBuffer(uint8_t *data, size_t datalength,
623 OH_ImageSourceNative **imageSource)
624 {
625 return CreateFromDataInternal(data, datalength, imageSource, true);
626 }
627
628 MIDK_EXPORT
OH_ImageSourceNative_CreateFromRawFile(RawFileDescriptor * rawFile,OH_ImageSourceNative ** res)629 Image_ErrorCode OH_ImageSourceNative_CreateFromRawFile(RawFileDescriptor *rawFile, OH_ImageSourceNative **res)
630 {
631 if (rawFile == nullptr) {
632 return IMAGE_BAD_PARAMETER;
633 }
634 SourceOptions options;
635 auto imageSource = new OH_ImageSourceNative(*rawFile, options);
636 if (imageSource == nullptr || imageSource->GetInnerImageSource() == nullptr) {
637 if (imageSource) {
638 delete imageSource;
639 }
640 *res = nullptr;
641 return IMAGE_BAD_PARAMETER;
642 }
643 *res = imageSource;
644 return IMAGE_SUCCESS;
645 }
646
647 MIDK_EXPORT
OH_ImageSourceNative_CreatePixelmap(OH_ImageSourceNative * source,OH_DecodingOptions * ops,OH_PixelmapNative ** pixelmap)648 Image_ErrorCode OH_ImageSourceNative_CreatePixelmap(OH_ImageSourceNative *source, OH_DecodingOptions *ops,
649 OH_PixelmapNative **pixelmap)
650 {
651 if (source == nullptr) {
652 return IMAGE_BAD_PARAMETER;
653 }
654 DecodeOptions decOps;
655 uint32_t index = DEFAULT_INDEX;
656 uint32_t errorCode = IMAGE_BAD_PARAMETER;
657 if (ops != nullptr) {
658 ParseDecodingOps(decOps, ops);
659 index = ops->index;
660 } else {
661 OH_DecodingOptions localOps{};
662 ParseDecodingOps(decOps, &localOps);
663 }
664 std::unique_ptr<PixelMap> tmpPixelmap = source->GetInnerImageSource()->CreatePixelMapEx(index, decOps, errorCode);
665 if (tmpPixelmap == nullptr || errorCode != IMAGE_SUCCESS) {
666 return IMAGE_UNSUPPORTED_OPERATION;
667 }
668 std::shared_ptr<PixelMap> nativePixelmap = std::move(tmpPixelmap);
669 OH_PixelmapNative *stPixMap = new OH_PixelmapNative(nativePixelmap);
670 *pixelmap = stPixMap;
671 return IMAGE_SUCCESS;
672 }
673
674 MIDK_EXPORT
OH_ImageSourceNative_CreatePixelmapUsingAllocator(OH_ImageSourceNative * source,OH_DecodingOptions * ops,IMAGE_ALLOCATOR_TYPE allocator,OH_PixelmapNative ** pixelmap)675 Image_ErrorCode OH_ImageSourceNative_CreatePixelmapUsingAllocator(OH_ImageSourceNative *source, OH_DecodingOptions *ops,
676 IMAGE_ALLOCATOR_TYPE allocator, OH_PixelmapNative **pixelmap)
677 {
678 if (source == nullptr || ops == nullptr || pixelmap == nullptr) {
679 return IMAGE_BAD_PARAMETER;
680 }
681 DecodeOptions decOps;
682 uint32_t index = DEFAULT_INDEX;
683 uint32_t errorCode = IMAGE_BAD_PARAMETER;
684 ParseDecodingOps(decOps, ops);
685 index = ops->index;
686 if (source->GetInnerImageSource() == nullptr) {
687 return IMAGE_BAD_SOURCE;
688 }
689 if (!source->GetInnerImageSource()->IsSupportAllocatorType(decOps, static_cast<int32_t>(allocator))) {
690 return IMAGE_SOURCE_UNSUPPORTED_ALLOCATOR_TYPE;
691 }
692 std::unique_ptr<PixelMap> tmpPixelmap = source->GetInnerImageSource()->CreatePixelMapEx(index, decOps, errorCode);
693 if (tmpPixelmap == nullptr || errorCode != IMAGE_SUCCESS) {
694 return ConvertToErrorCode(errorCode);
695 }
696 std::shared_ptr<PixelMap> nativePixelmap = std::move(tmpPixelmap);
697 OH_PixelmapNative *stPixMap = new OH_PixelmapNative(nativePixelmap);
698 *pixelmap = stPixMap;
699 return IMAGE_SUCCESS;
700 }
701
702 MIDK_EXPORT
OH_ImageSourceNative_CreatePixelmapList(OH_ImageSourceNative * source,OH_DecodingOptions * ops,OH_PixelmapNative * resVecPixMap[],size_t outSize)703 Image_ErrorCode OH_ImageSourceNative_CreatePixelmapList(OH_ImageSourceNative *source, OH_DecodingOptions *ops,
704 OH_PixelmapNative *resVecPixMap[], size_t outSize)
705 {
706 if (source == nullptr || ops == nullptr || resVecPixMap == nullptr || outSize == SIZE_ZERO) {
707 return IMAGE_BAD_PARAMETER;
708 }
709 DecodeOptions decOps;
710 uint32_t errorCode = IMAGE_BAD_PARAMETER;
711 ParseDecodingOps(decOps, ops);
712
713 auto pixelmapList = source->GetInnerImageSource()->CreatePixelMapList(decOps, errorCode);
714 if (pixelmapList == nullptr || errorCode != IMAGE_SUCCESS) {
715 return IMAGE_BAD_PARAMETER;
716 }
717 if (outSize < (*pixelmapList).size()) {
718 return IMAGE_BAD_PARAMETER;
719 }
720 size_t index = 0;
721 for (auto &item : *pixelmapList) {
722 std::shared_ptr<PixelMap> tempPixMap = std::move(item);
723 OH_PixelmapNative *stPixMap = new OH_PixelmapNative(tempPixMap);
724 resVecPixMap[index] = stPixMap;
725 index ++;
726 }
727 return IMAGE_SUCCESS;
728 }
729
730 MIDK_EXPORT
OH_ImageSourceNative_CreatePicture(OH_ImageSourceNative * source,OH_DecodingOptionsForPicture * options,OH_PictureNative ** picture)731 Image_ErrorCode OH_ImageSourceNative_CreatePicture(OH_ImageSourceNative *source, OH_DecodingOptionsForPicture *options,
732 OH_PictureNative **picture)
733 {
734 if (source == nullptr || !source->GetInnerImageSource() || options == nullptr
735 || picture == nullptr || !options->GetInnerDecodingOptForPicture()) {
736 return IMAGE_BAD_PARAMETER;
737 }
738
739 auto innerDecodingOptionsForPicture = options->GetInnerDecodingOptForPicture().get();
740 uint32_t errorCode;
741 auto pictureTemp = source->GetInnerImageSource()->CreatePicture(*innerDecodingOptionsForPicture, errorCode);
742 if (errorCode != SUCCESS) {
743 return IMAGE_DECODE_FAILED;
744 }
745
746 auto pictureNative = new OH_PictureNative(std::move(pictureTemp));
747 *picture = pictureNative;
748 return IMAGE_SUCCESS;
749 }
750
751 MIDK_EXPORT
OH_ImageSourceNative_CreatePictureAtIndex(OH_ImageSourceNative * source,uint32_t index,OH_PictureNative ** picture)752 Image_ErrorCode OH_ImageSourceNative_CreatePictureAtIndex(OH_ImageSourceNative *source, uint32_t index,
753 OH_PictureNative **picture)
754 {
755 if (source == nullptr || source->GetInnerImageSource() == nullptr) {
756 return Image_ErrorCode::IMAGE_BAD_SOURCE;
757 }
758 if (picture == nullptr) {
759 return Image_ErrorCode::IMAGE_SOURCE_UNSUPPORTED_OPTIONS;
760 }
761
762 uint32_t errorCode = ERR_MEDIA_INVALID_VALUE;
763 auto pictureTemp = source->GetInnerImageSource()->CreatePictureAtIndex(index, errorCode);
764 if (errorCode != SUCCESS) {
765 auto errMsg = ImageErrorConvert::CreatePictureAtIndexMakeErrMsg(errorCode);
766 return static_cast<Image_ErrorCode>(errMsg.first);
767 }
768
769 auto pictureNative = new OH_PictureNative(std::move(pictureTemp));
770 *picture = pictureNative;
771 return Image_ErrorCode::IMAGE_SUCCESS;
772 }
773
774 MIDK_EXPORT
OH_ImageSourceNative_GetDelayTimeList(OH_ImageSourceNative * source,int32_t * delayTimeList,size_t size)775 Image_ErrorCode OH_ImageSourceNative_GetDelayTimeList(OH_ImageSourceNative *source, int32_t *delayTimeList, size_t size)
776 {
777 if (source == nullptr || delayTimeList == nullptr) {
778 return IMAGE_BAD_PARAMETER;
779 }
780 uint32_t errorCode = IMAGE_SUCCESS;
781 auto delayTimes = source->GetInnerImageSource()->GetDelayTime(errorCode);
782 if (delayTimes == nullptr || errorCode != IMAGE_SUCCESS) {
783 return IMAGE_BAD_PARAMETER;
784 }
785 size_t actCount = (*delayTimes).size();
786 if (size < actCount) {
787 return IMAGE_BAD_PARAMETER;
788 }
789 for (size_t i = SIZE_ZERO; i < actCount; i++) {
790 delayTimeList[i] = (*delayTimes)[i];
791 }
792 return IMAGE_SUCCESS;
793 }
794
795 MIDK_EXPORT
OH_ImageSourceNative_GetImageInfo(OH_ImageSourceNative * source,int32_t index,struct OH_ImageSource_Info * info)796 Image_ErrorCode OH_ImageSourceNative_GetImageInfo(OH_ImageSourceNative *source, int32_t index,
797 struct OH_ImageSource_Info *info)
798 {
799 if (source == nullptr || info == nullptr) {
800 return IMAGE_BAD_PARAMETER;
801 }
802 ImageInfo imageInfo;
803 uint32_t errorCode = source->GetInnerImageSource()->GetImageInfo(index, imageInfo);
804 if (errorCode != IMAGE_SUCCESS) {
805 return IMAGE_BAD_PARAMETER;
806 }
807 ParseImageSourceInfo(info, imageInfo);
808 info->isHdr = source->GetInnerImageSource()->IsHdrImage();
809 return IMAGE_SUCCESS;
810 }
811
812 MIDK_EXPORT
OH_ImageSourceNative_GetImageProperty(OH_ImageSourceNative * source,Image_String * key,Image_String * value)813 Image_ErrorCode OH_ImageSourceNative_GetImageProperty(OH_ImageSourceNative *source, Image_String *key,
814 Image_String *value)
815 {
816 if (source == nullptr) {
817 return IMAGE_BAD_PARAMETER;
818 }
819 if (key == nullptr || key->data == nullptr || key->size == SIZE_ZERO) {
820 return IMAGE_BAD_PARAMETER;
821 }
822 if (value == nullptr) {
823 return IMAGE_BAD_PARAMETER;
824 }
825 std::string keyString(key->data, key->size);
826 if (keyString.empty()) {
827 return IMAGE_BAD_PARAMETER;
828 }
829 std::string val;
830 uint32_t errorCode = source->GetInnerImageSource()->GetImagePropertyString(DEFAULT_INDEX, keyString, val);
831 if (errorCode != IMAGE_SUCCESS || val.empty()) {
832 return IMAGE_BAD_PARAMETER;
833 }
834
835 if (value->size != SIZE_ZERO && value->size < val.size()) {
836 return IMAGE_BAD_PARAMETER;
837 }
838 value->size = (value->size == SIZE_ZERO) ? val.size() : value->size;
839 value->data = static_cast<char *>(malloc(value->size));
840 if (value->data == nullptr) {
841 return IMAGE_ALLOC_FAILED;
842 }
843 if (EOK != memcpy_s(value->data, value->size, val.c_str(), val.size())) {
844 return IMAGE_COPY_FAILED;
845 }
846 return IMAGE_SUCCESS;
847 }
848
849 MIDK_EXPORT
OH_ImageSourceNative_GetImagePropertyWithNull(OH_ImageSourceNative * source,Image_String * key,Image_String * value)850 Image_ErrorCode OH_ImageSourceNative_GetImagePropertyWithNull(OH_ImageSourceNative *source, Image_String *key,
851 Image_String *value)
852 {
853 if (source == nullptr || key == nullptr || key->data == nullptr || key->size == SIZE_ZERO || value == nullptr) {
854 return IMAGE_SOURCE_INVALID_PARAMETER;
855 }
856
857 std::string keyString(key->data, key->size);
858 if (keyString.empty()) {
859 return IMAGE_SOURCE_INVALID_PARAMETER;
860 }
861
862 std::string val;
863 uint32_t errorCode = source->GetInnerImageSource()->GetImagePropertyString(DEFAULT_INDEX, keyString, val);
864 if (errorCode != IMAGE_SUCCESS || val.empty()) {
865 return IMAGE_SOURCE_INVALID_PARAMETER;
866 }
867
868 if (value->size != SIZE_ZERO && value->size < val.size()) {
869 return IMAGE_SOURCE_INVALID_PARAMETER;
870 }
871
872 size_t allocSize = val.size() + 1;
873 char* buffer = static_cast<char*>(malloc(allocSize));
874 if (buffer == nullptr) {
875 return IMAGE_SOURCE_INVALID_PARAMETER;
876 }
877
878 if (EOK != memcpy_s(buffer, allocSize, val.c_str(), val.size())) {
879 free(buffer);
880 return IMAGE_SOURCE_INVALID_PARAMETER;
881 }
882 buffer[val.size()] = '\0';
883
884 value->data = buffer;
885 value->size = val.size();
886 return IMAGE_SUCCESS;
887 }
888
889 MIDK_EXPORT
OH_ImageSourceNative_ModifyImageProperty(OH_ImageSourceNative * source,Image_String * key,Image_String * value)890 Image_ErrorCode OH_ImageSourceNative_ModifyImageProperty(OH_ImageSourceNative *source, Image_String *key,
891 Image_String *value)
892 {
893 if (source == nullptr) {
894 return IMAGE_BAD_PARAMETER;
895 }
896 if (key == nullptr || key->data == nullptr || key->size == SIZE_ZERO) {
897 return IMAGE_BAD_PARAMETER;
898 }
899 if (value == nullptr || value->data == nullptr || value->size == SIZE_ZERO) {
900 return IMAGE_BAD_PARAMETER;
901 }
902
903 std::string keyStr(key->data, key->size);
904 if (keyStr.empty()) {
905 return IMAGE_BAD_PARAMETER;
906 }
907 std::string val(value->data, value->size);
908 if (val.empty()) {
909 return IMAGE_BAD_PARAMETER;
910 }
911 uint32_t errorCode = IMAGE_BAD_PARAMETER;
912 if (!(source->filePath_.empty())) {
913 errorCode = source->GetInnerImageSource()->ModifyImageProperty(DEFAULT_INDEX, keyStr, val, source->filePath_);
914 } else if (source->fileDescriptor_ != INVALID_FD) {
915 errorCode = source->GetInnerImageSource()->ModifyImageProperty(DEFAULT_INDEX, keyStr, val,
916 source->fileDescriptor_);
917 } else if (source->fileBuffer_ != nullptr && source->fileBufferSize_ != 0) {
918 errorCode = source->GetInnerImageSource()->ModifyImageProperty(DEFAULT_INDEX, keyStr, val,
919 static_cast<uint8_t *>(source->fileBuffer_), source->fileBufferSize_);
920 } else {
921 return IMAGE_BAD_PARAMETER;
922 }
923 if (errorCode == IMAGE_SUCCESS) {
924 return IMAGE_SUCCESS;
925 }
926 return IMAGE_BAD_PARAMETER;
927 }
928
929 MIDK_EXPORT
OH_ImageSourceNative_GetFrameCount(OH_ImageSourceNative * source,uint32_t * frameCount)930 Image_ErrorCode OH_ImageSourceNative_GetFrameCount(OH_ImageSourceNative *source, uint32_t *frameCount)
931 {
932 if (source == nullptr || frameCount == nullptr) {
933 return IMAGE_BAD_PARAMETER;
934 }
935 uint32_t errorCode = IMAGE_BAD_PARAMETER;
936 *frameCount = source->GetInnerImageSource()->GetFrameCount(errorCode);
937 if (errorCode != IMAGE_SUCCESS) {
938 return IMAGE_BAD_PARAMETER;
939 }
940 return IMAGE_SUCCESS;
941 }
942
943 MIDK_EXPORT
OH_ImageSourceNative_Release(OH_ImageSourceNative * source)944 Image_ErrorCode OH_ImageSourceNative_Release(OH_ImageSourceNative *source)
945 {
946 if (source == nullptr) {
947 return IMAGE_BAD_PARAMETER;
948 }
949 source->~OH_ImageSourceNative();
950 return IMAGE_SUCCESS;
951 }
952
953 MIDK_EXPORT
OH_DecodingOptionsForPicture_Create(OH_DecodingOptionsForPicture ** options)954 Image_ErrorCode OH_DecodingOptionsForPicture_Create(OH_DecodingOptionsForPicture **options)
955 {
956 if (options == nullptr) {
957 return IMAGE_BAD_PARAMETER;
958 }
959 auto decodingOptionsForPicture = std::make_shared<OHOS::Media::DecodingOptionsForPicture>();
960 *options = new OH_DecodingOptionsForPicture(decodingOptionsForPicture);
961 return IMAGE_SUCCESS;
962 }
963
964 MIDK_EXPORT
OH_DecodingOptionsForPicture_GetDesiredAuxiliaryPictures(OH_DecodingOptionsForPicture * options,Image_AuxiliaryPictureType ** desiredAuxiliaryPictures,size_t * length)965 Image_ErrorCode OH_DecodingOptionsForPicture_GetDesiredAuxiliaryPictures(OH_DecodingOptionsForPicture *options,
966 Image_AuxiliaryPictureType **desiredAuxiliaryPictures, size_t *length)
967 {
968 if (options == nullptr || !options->GetInnerDecodingOptForPicture() ||
969 desiredAuxiliaryPictures == nullptr || length == nullptr) {
970 return IMAGE_BAD_PARAMETER;
971 }
972 auto innerDecodingSet = options->GetInnerDecodingOptForPicture()->desireAuxiliaryPictures;
973 if (innerDecodingSet.size() == 0) {
974 return IMAGE_BAD_PARAMETER;
975 }
976 auto lenTmp = innerDecodingSet.size();
977 auto auxTypeArrayUniptr = std::make_unique<Image_AuxiliaryPictureType[]>(lenTmp);
978 int index = 0;
979 for (auto innerDecoding : innerDecodingSet) {
980 auxTypeArrayUniptr[index++] = AuxTypeInnerToNative(innerDecoding);
981 }
982 *desiredAuxiliaryPictures = auxTypeArrayUniptr.release();
983 *length = lenTmp;
984 return IMAGE_SUCCESS;
985 }
986
987 MIDK_EXPORT
OH_DecodingOptionsForPicture_SetDesiredAuxiliaryPictures(OH_DecodingOptionsForPicture * options,Image_AuxiliaryPictureType * desiredAuxiliaryPictures,size_t length)988 Image_ErrorCode OH_DecodingOptionsForPicture_SetDesiredAuxiliaryPictures(OH_DecodingOptionsForPicture *options,
989 Image_AuxiliaryPictureType *desiredAuxiliaryPictures, size_t length)
990 {
991 if (options == nullptr || !options->GetInnerDecodingOptForPicture() ||
992 desiredAuxiliaryPictures == nullptr || length <= 0) {
993 return IMAGE_BAD_PARAMETER;
994 }
995 std::set<AuxiliaryPictureType> tmpDesireSet;
996 auto innerDecodingOptionsForPicture = options->GetInnerDecodingOptForPicture().get();
997 for (size_t index = 0; index < length; index++) {
998 auto auxTypeTmp = AuxTypeNativeToInner(desiredAuxiliaryPictures[index]);
999 if (!OHOS::Media::ImageUtils::IsAuxiliaryPictureTypeSupported(auxTypeTmp)) {
1000 return IMAGE_BAD_PARAMETER;
1001 }
1002 tmpDesireSet.insert(auxTypeTmp);
1003 }
1004 innerDecodingOptionsForPicture->desireAuxiliaryPictures.insert(tmpDesireSet.begin(), tmpDesireSet.end());
1005 return IMAGE_SUCCESS;
1006 }
1007
1008 MIDK_EXPORT
OH_DecodingOptionsForPicture_Release(OH_DecodingOptionsForPicture * options)1009 Image_ErrorCode OH_DecodingOptionsForPicture_Release(OH_DecodingOptionsForPicture *options)
1010 {
1011 if (options == nullptr) {
1012 return IMAGE_BAD_PARAMETER;
1013 }
1014 delete options;
1015 options = nullptr;
1016 return IMAGE_SUCCESS;
1017 }
1018
1019 MIDK_EXPORT
OH_ImageSourceNative_GetSupportedFormats(Image_MimeType ** supportedFormat,size_t * length)1020 Image_ErrorCode OH_ImageSourceNative_GetSupportedFormats(Image_MimeType** supportedFormat, size_t* length)
1021 {
1022 if (supportedFormat == nullptr || length == nullptr) {
1023 return IMAGE_SOURCE_INVALID_PARAMETER;
1024 }
1025 if (IMAGE_SOURCE_SUPPORTED_FORMATS != nullptr || SUPPORTED_FORMATS_SIZE != 0) {
1026 *supportedFormat = IMAGE_SOURCE_SUPPORTED_FORMATS;
1027 *length = SUPPORTED_FORMATS_SIZE;
1028 return IMAGE_SUCCESS;
1029 }
1030 std::set<std::string> formats;
1031 ImageSource::GetSupportedFormats(formats);
1032 *length = formats.size();
1033 *supportedFormat = new Image_MimeType[*length];
1034 size_t count = 0;
1035 for (const auto& str : formats) {
1036 (*supportedFormat)[count].data = strdup(str.c_str());
1037 if ((*supportedFormat)[count].data == nullptr) {
1038 IMAGE_LOGE("ImageSource strdup failed");
1039 continue;
1040 }
1041 (*supportedFormat)[count].size = str.size();
1042 count++;
1043 }
1044 IMAGE_SOURCE_SUPPORTED_FORMATS = *supportedFormat;
1045 SUPPORTED_FORMATS_SIZE = *length;
1046 return IMAGE_SUCCESS;
1047 }
1048
1049 #ifdef __cplusplus
1050 };
1051 #endif