• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if defined(ACE_STATIC)
19 #include "pixel_map_ohos.h"
20 #endif
21 
22 #include "media_errors.h"
23 
24 namespace OHOS::Ace {
25 namespace {
InitDecodeOptions(Media::DecodeOptions & options,const std::pair<int32_t,int32_t> & size,AIImageQuality imageQuality,bool isHdrDecoderNeed,PixelFormat photoDecodeFormat)26 void InitDecodeOptions(Media::DecodeOptions& options, const std::pair<int32_t, int32_t>& size,
27     AIImageQuality imageQuality, bool isHdrDecoderNeed, PixelFormat photoDecodeFormat)
28 {
29     options.preferDma = true;
30     // only hdr image need to decoder in hdr mode
31     if (isHdrDecoderNeed) {
32         options.desiredDynamicRange = Media::DecodeDynamicRange::AUTO;
33     }
34     if (photoDecodeFormat == PixelFormat::NV21) {
35         options.photoDesiredPixelFormat  = Media::PixelFormat::NV21;
36     } else if (photoDecodeFormat == PixelFormat::RGBA_8888) {
37         options.photoDesiredPixelFormat  = Media::PixelFormat::RGBA_8888;
38     } else if (photoDecodeFormat == PixelFormat::RGBA_1010102) {
39         options.photoDesiredPixelFormat  = Media::PixelFormat::RGBA_1010102;
40     } else if (photoDecodeFormat == PixelFormat::YCBCR_P010) {
41         options.photoDesiredPixelFormat  = Media::PixelFormat::YCBCR_P010;
42     } else if (photoDecodeFormat == PixelFormat::YCRCB_P010) {
43         options.photoDesiredPixelFormat  = Media::PixelFormat::YCRCB_P010;
44     }
45     // Pass imageQuality to imageFramework
46     options.resolutionQuality = static_cast<Media::ResolutionQuality>(imageQuality);
47     if (size.first > 0 && size.second > 0) {
48         options.desiredSize = { size.first, size.second };
49     }
50 }
51 } // namespace
52 
Create(int32_t fd)53 RefPtr<ImageSource> ImageSource::Create(int32_t fd)
54 {
55     uint32_t errorCode;
56     Media::SourceOptions options;
57     auto src = Media::ImageSource::CreateImageSource(fd, options, errorCode);
58     if (errorCode != Media::SUCCESS) {
59         TAG_LOGW(AceLogTag::ACE_IMAGE, "create image source failed, errorCode = %{public}u", errorCode);
60         return nullptr;
61     }
62     return MakeRefPtr<ImageSourceOhos>(std::move(src));
63 }
64 
Create(const uint8_t * data,uint32_t size,uint32_t & errorCode)65 RefPtr<ImageSource> ImageSource::Create(const uint8_t* data, uint32_t size, uint32_t& errorCode)
66 {
67     Media::SourceOptions options;
68     auto src = Media::ImageSource::CreateImageSource(data, size, options, errorCode);
69     if (errorCode != Media::SUCCESS) {
70         TAG_LOGW(AceLogTag::ACE_IMAGE, "create image source failed, errorCode = %{public}u", errorCode);
71         return nullptr;
72     }
73     return MakeRefPtr<ImageSourceOhos>(std::move(src));
74 }
75 
Create(const std::string & filePath)76 RefPtr<ImageSource> ImageSource::Create(const std::string& filePath)
77 {
78     Media::SourceOptions opts;
79     uint32_t errorCode = 0;
80     auto src = Media::ImageSource::CreateImageSource(filePath, opts, errorCode);
81     if (errorCode != Media::SUCCESS) {
82         TAG_LOGW(AceLogTag::ACE_IMAGE, "create image source failed, errorCode = %{public}u", errorCode);
83         return nullptr;
84     }
85     return MakeRefPtr<ImageSourceOhos>(std::move(src));
86 }
87 
IsAstc(const uint8_t * data,size_t size)88 bool ImageSource::IsAstc(const uint8_t* data, size_t size)
89 {
90     return Media::ImageSource::IsASTC(data, size);
91 }
92 
GetASTCInfo(const uint8_t * data,size_t size)93 ImageSource::Size ImageSource::GetASTCInfo(const uint8_t* data, size_t size)
94 {
95     Media::ASTCInfo astcInfo;
96     Media::ImageSource::GetASTCInfo(data, size, astcInfo);
97     return { astcInfo.size.width, astcInfo.size.height };
98 }
99 
GetProperty(const std::string & key)100 std::string ImageSourceOhos::GetProperty(const std::string& key)
101 {
102     std::string value;
103     uint32_t res = imageSource_->GetImagePropertyString(0, key, value);
104     if (res != Media::SUCCESS) {
105         TAG_LOGW(AceLogTag::ACE_IMAGE, "Get ImageSource property %{public}s failed, errorCode = %{public}u",
106             key.c_str(), res);
107     }
108     return value;
109 }
110 
CreatePixelMap(const Size & size,uint32_t & errorCode,const PixelMapConfig & pixelMapConfig)111 RefPtr<PixelMap> ImageSourceOhos::CreatePixelMap(
112     const Size& size, uint32_t& errorCode, const PixelMapConfig& pixelMapConfig)
113 {
114     return CreatePixelMap(0, size, errorCode, pixelMapConfig);
115 }
116 
CreatePixelMap(uint32_t index,const Size & size,uint32_t & errorCode,const PixelMapConfig & pixelMapConfig)117 RefPtr<PixelMap> ImageSourceOhos::CreatePixelMap(
118     uint32_t index, const Size& size, uint32_t& errorCode, const PixelMapConfig& pixelMapConfig)
119 {
120     Media::DecodeOptions options;
121     InitDecodeOptions(
122         options, size, pixelMapConfig.imageQuality, pixelMapConfig.isHdrDecoderNeed, pixelMapConfig.photoDecodeFormat);
123     auto pixmap = imageSource_->CreatePixelMapEx(index, options, errorCode);
124     if (errorCode != Media::SUCCESS) {
125         TAG_LOGW(AceLogTag::ACE_IMAGE,
126             "create PixelMap from ImageSource failed, index = %{public}u, errorCode = %{public}u", index, errorCode);
127         return nullptr;
128     }
129     return PixelMap::Create(std::move(pixmap));
130 }
131 
CreatePixelMap()132 RefPtr<PixelMap> ImageSourceOhos::CreatePixelMap()
133 {
134     uint32_t errorCode;
135     Media::DecodeOptions decodeOpts;
136     auto pixelMap = imageSource_->CreatePixelMap(decodeOpts, errorCode);
137     if (errorCode != Media::SUCCESS) {
138         TAG_LOGW(AceLogTag::ACE_IMAGE,
139             "create PixelMap from ImageSource failed, errorCode = %{public}u", errorCode);
140         return nullptr;
141     }
142     return PixelMap::Create(std::move(pixelMap));
143 }
144 
GetImageSize()145 ImageSource::Size ImageSourceOhos::GetImageSize()
146 {
147     Media::ImageInfo info;
148     auto errorCode = imageSource_->GetImageInfo(info);
149     if (errorCode != Media::SUCCESS) {
150         TAG_LOGW(AceLogTag::ACE_IMAGE, "Get ImageSource info failed, errorCode = %{public}u", errorCode);
151         return { 0, 0 };
152     }
153     return { info.size.width, info.size.height };
154 }
155 
156 #if defined(ACE_STATIC)
CreatePixelMap(const DecodeOptions & options)157 RefPtr<PixelMap> ImageSourceOhos::CreatePixelMap(const DecodeOptions& options)
158 {
159     uint32_t errorCode;
160     Media::DecodeOptions decodeOpts;
161     decodeOpts.desiredPixelFormat = PixelMapOhos::ConvertToMediaPixelFormat(options.desiredFormat);
162     auto pixelmap = imageSource_->CreatePixelMap(decodeOpts, errorCode);
163     if (errorCode != Media::SUCCESS) {
164         TAG_LOGW(AceLogTag::ACE_IMAGE, "create pixelmap failed, errorCode = %{public}u", errorCode);
165         return nullptr;
166     }
167     return PixelMap::Create(std::move(pixelmap));
168 }
169 #endif
170 
GetFrameCount()171 uint32_t ImageSourceOhos::GetFrameCount()
172 {
173     uint32_t errorCode;
174     auto frameCount = imageSource_->GetFrameCount(errorCode);
175     if (errorCode != Media::SUCCESS) {
176         TAG_LOGW(AceLogTag::ACE_IMAGE, "Get image frame count failed, errorCode = %{public}u", errorCode);
177         return 0;
178     }
179     return frameCount;
180 }
181 
GetEncodedFormat()182 std::string ImageSourceOhos::GetEncodedFormat()
183 {
184     uint32_t errorCode;
185     auto sourceInfo = imageSource_->GetSourceInfo(errorCode);
186     if (errorCode != Media::SUCCESS) {
187         TAG_LOGW(AceLogTag::ACE_IMAGE, "Get image source info failed, errorCode = %{public}u", errorCode);
188         return "";
189     }
190     return sourceInfo.encodedFormat;
191 }
192 } // namespace OHOS::Ace
193