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