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