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 LOGW("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 LOGW("create image source failed, errorCode = %{public}u", errorCode);
44 return nullptr;
45 }
46 return MakeRefPtr<ImageSourceOhos>(std::move(src));
47 }
48
GetProperty(const std::string & key)49 std::string ImageSourceOhos::GetProperty(const std::string& key)
50 {
51 std::string value;
52 uint32_t res = imageSource_->GetImagePropertyString(0, key, value);
53 if (res != Media::SUCCESS) {
54 LOGW("Get ImageSource property %{public}s failed, errorCode = %{public}u", key.c_str(), res);
55 }
56 return value;
57 }
58
CreatePixelMap(const Size & size)59 RefPtr<PixelMap> ImageSourceOhos::CreatePixelMap(const Size& size)
60 {
61 return CreatePixelMap(0, size);
62 }
63
CreatePixelMap(uint32_t index,const Size & size)64 RefPtr<PixelMap> ImageSourceOhos::CreatePixelMap(uint32_t index, const Size& size)
65 {
66 Media::DecodeOptions options;
67 if (size.first > 0 && size.second > 0) {
68 options.desiredSize = { size.first, size.second };
69 }
70 uint32_t errorCode;
71 auto pixmap = imageSource_->CreatePixelMapEx(index, options, errorCode);
72 if (errorCode != Media::SUCCESS) {
73 LOGW("create PixelMap from ImageSource failed, index = %{public}u, errorCode = %{public}u", index, errorCode);
74 return nullptr;
75 }
76 return PixelMap::Create(std::move(pixmap));
77 }
78
GetImageSize()79 ImageSource::Size ImageSourceOhos::GetImageSize()
80 {
81 Media::ImageInfo info;
82 auto errorCode = imageSource_->GetImageInfo(info);
83 if (errorCode != Media::SUCCESS) {
84 LOGW("Get ImageSource info failed, errorCode = %{public}u", errorCode);
85 return { 0, 0 };
86 }
87 return { info.size.width, info.size.height };
88 }
89 } // namespace OHOS::Ace
90