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