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