1 /*
2 * Copyright (c) 2023 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_ohos.h"
17
18 #include "image_source.h"
19 #include "image_type.h"
20 #include "media_errors.h"
21
22 #include "base/image/pixel_map.h"
23
24 namespace OHOS::Ace {
Create(int32_t fd)25 RefPtr<ImageSource> ImageSource::Create(int32_t fd)
26 {
27 uint32_t errorCode;
28 Media::SourceOptions options;
29 auto src = Media::ImageSource::CreateImageSource(fd, options, errorCode);
30 if (errorCode != Media::SUCCESS) {
31 TAG_LOGW(AceLogTag::ACE_IMAGE, "create image source failed, errorCode = %{public}u", errorCode);
32 return nullptr;
33 }
34 return MakeRefPtr<ImageSourceOhos>(std::move(src));
35 }
36
Create(const uint8_t * data,uint32_t size)37 RefPtr<ImageSource> ImageSource::Create(const uint8_t* data, uint32_t size)
38 {
39 uint32_t errorCode;
40 Media::SourceOptions options;
41 auto src = Media::ImageSource::CreateImageSource(data, size, options, errorCode);
42 if (errorCode != Media::SUCCESS) {
43 TAG_LOGW(AceLogTag::ACE_IMAGE, "create image source failed, errorCode = %{public}u", errorCode);
44 return nullptr;
45 }
46 return MakeRefPtr<ImageSourceOhos>(std::move(src));
47 }
48
Create(const std::string & filePath)49 RefPtr<ImageSource> ImageSource::Create(const std::string& filePath)
50 {
51 Media::SourceOptions opts;
52 uint32_t errorCode = 0;
53 auto src = Media::ImageSource::CreateImageSource(filePath, opts, errorCode);
54 if (errorCode != Media::SUCCESS) {
55 TAG_LOGW(AceLogTag::ACE_IMAGE, "create image source failed, errorCode = %{public}u", errorCode);
56 return nullptr;
57 }
58 return MakeRefPtr<ImageSourceOhos>(std::move(src));
59 }
60
IsAstc(const uint8_t * data,size_t size)61 bool ImageSource::IsAstc(const uint8_t* data, size_t size)
62 {
63 return Media::ImageSource::IsASTC(data, size);
64 }
65
GetASTCInfo(const uint8_t * data,size_t size)66 ImageSource::Size ImageSource::GetASTCInfo(const uint8_t* data, size_t size)
67 {
68 Media::ASTCInfo astcInfo;
69 Media::ImageSource::GetASTCInfo(data, size, astcInfo);
70 return { astcInfo.size.width, astcInfo.size.height };
71 }
72
GetProperty(const std::string & key)73 std::string ImageSourceOhos::GetProperty(const std::string& key)
74 {
75 std::string value;
76 uint32_t res = imageSource_->GetImagePropertyString(0, key, value);
77 if (res != Media::SUCCESS) {
78 TAG_LOGW(AceLogTag::ACE_IMAGE, "Get ImageSource property %{public}s failed, errorCode = %{public}u",
79 key.c_str(), res);
80 }
81 return value;
82 }
83
CreatePixelMap(const Size & size,AIImageQuality imageQuality,bool isHdrDecoderNeed)84 RefPtr<PixelMap> ImageSourceOhos::CreatePixelMap(const Size& size, AIImageQuality imageQuality, bool isHdrDecoderNeed)
85 {
86 return CreatePixelMap(0, size, imageQuality, isHdrDecoderNeed);
87 }
88
CreatePixelMap(uint32_t index,const Size & size,AIImageQuality imageQuality,bool isHdrDecoderNeed)89 RefPtr<PixelMap> ImageSourceOhos::CreatePixelMap(
90 uint32_t index, const Size& size, AIImageQuality imageQuality, bool isHdrDecoderNeed)
91 {
92 Media::DecodeOptions options;
93 options.preferDma = true;
94 // only hdr image need to decoder in hdr mode
95 if (isHdrDecoderNeed) {
96 options.desiredDynamicRange = Media::DecodeDynamicRange::AUTO;
97 }
98 options.resolutionQuality = static_cast<Media::ResolutionQuality>(imageQuality);
99 // Pass imageQuality to imageFramework
100 if (size.first > 0 && size.second > 0) {
101 options.desiredSize = { size.first, size.second };
102 }
103 uint32_t errorCode;
104 auto pixmap = imageSource_->CreatePixelMapEx(index, options, errorCode);
105 if (errorCode != Media::SUCCESS) {
106 TAG_LOGW(AceLogTag::ACE_IMAGE,
107 "create PixelMap from ImageSource failed, index = %{public}u, errorCode = %{public}u", index, errorCode);
108 return nullptr;
109 }
110 return PixelMap::Create(std::move(pixmap));
111 }
112
CreatePixelMap()113 RefPtr<PixelMap> ImageSourceOhos::CreatePixelMap()
114 {
115 uint32_t errorCode;
116 Media::DecodeOptions decodeOpts;
117 auto pixelMap = imageSource_->CreatePixelMap(decodeOpts, errorCode);
118 if (errorCode != Media::SUCCESS) {
119 TAG_LOGW(AceLogTag::ACE_IMAGE,
120 "create PixelMap from ImageSource failed, errorCode = %{public}u", errorCode);
121 return nullptr;
122 }
123 return PixelMap::Create(std::move(pixelMap));
124 }
125
GetImageSize()126 ImageSource::Size ImageSourceOhos::GetImageSize()
127 {
128 Media::ImageInfo info;
129 auto errorCode = imageSource_->GetImageInfo(info);
130 if (errorCode != Media::SUCCESS) {
131 TAG_LOGW(AceLogTag::ACE_IMAGE, "Get ImageSource info failed, errorCode = %{public}u", errorCode);
132 return { 0, 0 };
133 }
134 return { info.size.width, info.size.height };
135 }
136
GetFrameCount()137 uint32_t ImageSourceOhos::GetFrameCount()
138 {
139 uint32_t errorCode;
140 auto frameCount = imageSource_->GetFrameCount(errorCode);
141 if (errorCode != Media::SUCCESS) {
142 TAG_LOGW(AceLogTag::ACE_IMAGE, "Get image frame count failed, errorCode = %{public}u", errorCode);
143 return 0;
144 }
145 return frameCount;
146 }
147
GetEncodedFormat()148 std::string ImageSourceOhos::GetEncodedFormat()
149 {
150 uint32_t errorCode;
151 auto sourceInfo = imageSource_->GetSourceInfo(errorCode);
152 if (errorCode != Media::SUCCESS) {
153 TAG_LOGW(AceLogTag::ACE_IMAGE, "Get image source info failed, errorCode = %{public}u", errorCode);
154 return "";
155 }
156 return sourceInfo.encodedFormat;
157 }
158 } // namespace OHOS::Ace
159